Coder Social home page Coder Social logo

autonumeric / vue-autonumeric Goto Github PK

View Code? Open in Web Editor NEW
90.0 3.0 23.0 1.33 MB

A Vue.js component that wraps the awesome autoNumeric input formatter library

Home Page: http://autonumeric.org

License: MIT License

JavaScript 33.78% Vue 66.22%
vuejs autonumeric component awesome

vue-autonumeric's Introduction

vue-autoNumeric

A Vue.js component that wraps the awesome AutoNumeric input formatter library

NPM
NPM version Downloads Known Vulnerabilities

Get in touch on Gitter chat


vue-autoNumeric wraps the awesome AutoNumeric library and generate an <input> element managed by AutoNumeric.

Checkout the demo!

Alternatively you can check the examples directly in your browser to see how to integrate the component with Vue and AutoNumeric.

Installation

yarn add vue-autonumeric
# or
npm install vue-autonumeric --save

Note: In order to minimize the size of the vue-autoNumeric component, the AutoNumeric library dependency is not bundled with it.

This means you need to link the AutoNumeric library with either ways:

...in your html <head> tag directly

<!-- locally... -->
<script src="../node_modules/autonumeric/dist/autonumeric.min.js"></script>
<!-- ...or by using a CDN -->
<script src="https://unpkg.com/autonumeric"></script>

Then you need to tell Webpack to treat the AutoNumeric dependency as external so that it does not try to bundle it.
Here is a really simple webpack.config.js example that does that:

module.exports = {
    entry  : './src/vueAutonumericTest.js',
    output : {
        filename: './dist/bundle.js'
    },
    externals: {
        autonumeric: 'AutoNumeric',
    },
};

...or by importing it directly as an ES6 module

You can choose to directly import the AutoNumeric library in your source code.
First, install the autonumeric dependency so that Webpack can find it using:

yarn add autonumeric
# or
npm install autonumeric --save

You will as usual be able to use the vue-autonumeric component in your Vue components using:

import VueAutonumeric from '../src/components/VueAutonumeric.vue';

export default {
    name      : 'myComponent',

    components: {
        VueAutonumeric,
    },
}

However, when doing that if you want to be able to bundle all the scripts together with Webpack, you'll need to define an alias for the AutoNumeric library in your Webpack config, otherwise Webpack will complain about the npm package autonumeric case.

The alias that you need to declare in your Webpack configuration:

module.exports = {
    entry  : './src/vueAutonumericTest.js',
    output : {
        filename: './dist/bundle.js'
    },
    resolve: {
        alias: {
            AutoNumeric: 'node_modules/autonumeric/dist/autoNumeric.min',
        },
    },
};

How to use?

The AutoNumeric component can be instantiated the same way AutoNumeric can.

With an option object:

<vue-autonumeric
     v-model="myValue"
     :options="{
         digitGroupSeparator: '.',
         decimalCharacter: ',',
         decimalCharacterAlternative: '.',
         currencySymbol: '\u00a0€',
         currencySymbolPlacement: 's',
         roundingMethod: 'U',
         minimumValue: '0'
     }"
></vue-autonumeric>

With a predefined option name:

<vue-autonumeric
    v-model="myValue"
    :options="'French'"
></vue-autonumeric>

With multiple option objects/predefined options:

<vue-autonumeric
    v-model="myValue"
    :options="['euro', { minimumValue: '0' }]"
></vue-autonumeric>

Other props

Placeholder

You can define the input placeholder using:

<vue-autonumeric
    v-model="myValue"
    :options="'euro'"
    :placeholder="'Enter your value here'"
></vue-autonumeric>
Tag

You can also specify the type of html tag (within the AutoNumeric supported list) this component should generate using the tag prop. By default, an <input> element is generated, but if you want a <div> element, you can use:

<vue-autonumeric
    v-model="myValue"
    options="euro"
    tag="div"
></vue-autonumeric>

Note: this will automatically set the contenteditable attribute to true for that generated element.

Integration with other scripts & events support

This wrapper supports setting the AutoNumeric options via an :options prop.
It also supports external value changes (via aNElement.set(42) for instance) and update the formatting and the v-model accordingly.

The paste, drop and wheel events are supported as well.

Moreover, if you modify the :options prop, the AutoNumeric settings will be automatically updated with the new options.

Caveats

Please note that directly setting a :value='42' on the <vue-autonumeric> component will break it (really!).
Do NOT do that:

<vue-autonumeric
    v-model="myValue"
    :options="{ minimumValue: '0' }"
    :value="42042.69" <!-- This fails -->
></vue-autonumeric>

Demo

The official AutoNumeric documentation is using this component extensively :)

An editable live example is available on Codepen.

Examples

You can also check the shipped examples in your browser, and study their source here.
To do so, first compile the example using:

# this will build the component *and* the examples
yarn build 

Then check the resulting html file in your browser using:

firefox ./examples/index.html # or `chrome`

Requirements

Browser support

This supports the same browsers than AutoNumeric supports:

  • Firefox and
  • Chrome

(latest 2 versions)

If you use IE/Edge/Safari/Opera, this might work ;)

Contributing

Whenever you change the source code, you can check how it affects the default examples by first building those in examples/index.html with:

yarn build:examples

The contribution guidelines for vue-autoNumeric are the same than for the parent AutoNumeric project.

Support

As always, if you find this useful, please consider supporting its development!
Huge Thanks :)

License

vue-autoNumeric is open-source and released under the MIT License.


Copyright © 2016-2018 Alexandre Bonneau

PS:
I would love to know how you're using vue-autonumeric.
Contact and tell me! :)

vue-autonumeric's People

Contributors

agentschmitt avatar alexandrebonneau avatar samhwang1990 avatar shrpne avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

vue-autonumeric's Issues

Add a new `resetOnOptions` props so that updating the `options` one first reset to the default options

Add a resetOnOptions prop set the true by default so that updating the options prop first call .options.reset().

This is useful when using predefined option names that do not declare all the options. For instance when switching from 'integer' to 'euro', the decimalPlaces is not set from 0 to 2 and you have to first update to the default configuration if you want to obtain the same format than when using 'euro' directly.

This new resetOnOptions prop will by default make sure all options update will first get reset to the default options, before applying the new ones.

This can be avoided by setting resetOnOptions to false before changing the options value.

Prop Validation Returns Error if Empty String

Hey guys,

Loving the functionality of this library so far, but my only real issue so far is that if I try to pass an empty string (""), I get the following error:

image

As far as I can tell, it's necessary to pass an empty string into the component for it to show the Placeholder value. The component still works as intended after I enter a number, but I'm not too fond of seeing a giant red warning on my console, when it's really not an issue that I can see. Can we add prop validation for a String as well, so an empty value can be passed, or is there a better way to do this?

Thanks!

Inherit events from component wrapper

It would be helpful if I could attach events on component and it would respond to them. Right now component emits only "input" event. I would like to attach "blur" and "keyup.enter" events like this:
<vue-autonumeric @blur="blurHandeler" @keyup.enter="enterHandeler" />

Vue js can do it with this.$listeners

Android cursor issue

Android: A cursor is moved behind the next digit if remove a digit inside a number, e.g. enter 1234, remove a digit 2. A cursor becomes behind 3.
Reproduced on the android 8.1.0, (also on 9). chrome 71.0.3578.99.
before
after

unformatOnSubmit is not triggered when using submit.prevent

<form id="poForm" method="post" v-on:submit.prevent="validateBeforeSubmit">
<vue-autonumeric type="text" name="item_quantity" v-model="quantity" v-bind:options="{unformatOnSubmit: true}"></vue-autonumeric>
</form>

in vue

validateBeforeSubmit: function () {
var test = new FormData($('#poForm')[0]);
console.log(test.get('item_quantity'));
}

the console will print the formatted value not the unformatted value
why i need this because i need to send the name attribute as axios params to be sent to php controller

Create the tests

Create the tests for the vue-autonumeric component.

No test is bad.
Test that stuff.

Support for numeric keyboard via input[type=number]?

Hi!

Thanks for the great component, it worked right out of the box!
One question:
The HTML <input type=number> will make for example Android browsers open a keyboard with only numbers as available input. No other characters. This is a great thing for the end user.

Is there any way to achieve this via your component? I tried adding type=number but that didn't work.

Thanks again!
/Pär

References to autonumeric need to be updated

Hello,

This component has great potential but using it is very inconvenient as it uses the outdated AutoNumeric case instead of the current lower case autonumeric naming convention. This forces complicated webpack configs as a workaround. Would it be possible to fix this issue?

Just doing a simple replace all "AutoNumeric" to "autonumeric" in the source JS file seems to fix the problem.

Best,
Kal

build fails on linux

The recent change made in build/webpack.base.js creates a issue when compiling with webpack on case sensitive OS like linux.

This dependency was not found:

* AutoNumeric in ./node_modules/vue-autonumeric/dist/vue-autonumeric.min.js

THIS

externals: {
    autonumeric: 'AutoNumeric',

SHOULD BE THIS

externals: {
    AutoNumeric: 'autonumeric',

Set value to zero after null

Hello.
I have a problem displaying a zero value after null.
null is not displayed in the input, but if I change the value to 0, it also does not appear...
Maybe I'm doing something wrong?
Or is there a way to avoid this problem?

Example https://codepen.io/Zeroox/pen/mddyerR

This has been fixed with a workaround by setting the value to '0'... But I think this is not good.

option for output-only

i think its a great features to implement some kind output-only
not only for the input text by using autoNumeric.format()

Add TypeScript file definition

Hi guys!

Can someone add typescript file definition ? :) It will be helpful for typescript users :)
something like that:

	class VueAutonumeric extends Vue {
		value: number | null | string
		options: object | string | Array<any>
		resetOnOptions: boolean
		placeholder: string
		tag: string
	}

thanks !)

When using an object to modify both the `:value` and `:options` props, the options is not always modified before the value

Let's say you have the following template:

<vue-autonumeric
	:options="obj.options"
	v-model="obj.val"
/>

If you want to modify both :value and :options props at the same time, you would proceed by doing:

this.obj = {
    options : 'percentageEU3dec',
    val     : 0.00123456,
};

However since the initial default options here sets the decimalPlaces option to 2, then if the value is modified first, then AutoNumeric will (rightly) drop any additional decimal places and keep only 0.00, instead of 0.00123456.

For this to work, we need to find a reliable way for the VueAutonumeric component to first update the options before the value.

That way 'percentageEU3dec' will correctly update decimalPlaces to 5, and then 0.00123 will correctly be saved as the value.

You can see the problem on this codepen.

The event passed as a parameter is not a valid wheel event, 'wheel' given.

Description

Using the wheel of a regular mouse or the up/down swipe gesture of a magic mouse throws an error in the dev tools.

Implementation

<VueAutonumeric
  :options="{
    allowDecimalPadding: 'floats',
    digitGroupSeparator: '.',
    decimalCharacter: ',',
    decimalPlaces: 2,
    decimalPlacesRawValue: 2,
    decimalCharacterAlternative: '.',
    currencySymbol: '\u00a0%',
    percentageEU2dec: true,
    currencySymbolPlacement: 's',
    roundingMethod: 'U',
    emptyInputBehavior: 'press',
  }"
/>

How to reproduce

  1. Implement <VueAutonumeric> component
  2. Click into the input
  3. Scroll

Error

Bildschirmfoto 2020-05-15 um 09 43 07

Add an ability to globally set the default options

I didn't find a way to set the default options globally, so they are merged with the options passed to the component from template. It would be great to have such ability, something like this (using Vue Plugins):

import VueAutonumeric from 'vue-autonumeric';
Vue.use(VueAutonumeric, {
    onInvalidPaste: 'ignore'
});

Ignoring the .lazy modifier

Hello.
Thanks for the great component!

I have identified one issue, though.

When using the .lazy modifier on v-model, the AutoNumeric component updates the model on the input event, rather than on the change event.

I have created a fiddle to illustrate the issue in which there is a standard input next to a Vue AutoNumeric component: https://jsfiddle.net/p462eLa9/1/

Please let me know your thoughts on this.

Problem when importing `vue-autonumeric`, but not when importing `AutoNumeric`

I'm trying to use vue-autonumeric on an Electron app (on Linux).

I can import autonumeric alone

import AutoNumeric from 'AutoNumeric'

using an alias:

resolve: {
  alias: {
    'AutoNumeric': 'numeric'
  }
}

but if I try to import vue-autonumeric I see the error Cannot find module 'AutoNumeric'

import VueAutonumeric from 'vue-autonumeric'

using an alias:

resolve: {
  alias: {
     'AutoNumeric': 'numeric',
    // Or these other aliases
    // 'AutoNumeric': 'node_modules/autonumeric/dist/autoNumeric.min',
    // 'AutoNumeric': path.join(__dirname, '../node_modules/autonumeric/dist/autoNumeric.min'),
  }
}

Require of AutoNumeric is ES6 module and not the distribution version

AutoNumeric is required as ES6 module and not as the distribution version.
When compiling a project using webpack, node_modules is usually not transpiled with babel-loader, resulting in AutoNumeric not being transpiled.

Instead of the ES6 module:

require('autonumeric')

Use the distribution version:

require('autonumeric/dist/autonumeric.min')

It also results in faster compiling and smaller file-size.

Not working on mobile devices / userInteraction is never set to true on touch devices (iOS / Android)

Since userInteraction is set on keydown/paste/wheel/drop and none of those are triggered on touch devices, the autonumeric behavior is strange.
It results in empty value not being set and decimals being inserted on every key, which makes it almost impossible to enter values.

The solution is actually quite simple.
userInteraction is only used in one check, that could be replaced.

current code:

if (!this.userInteraction) {
    // Make sure this is only called when the value is set by an external script, and not from a user input
    // The modification comes from a script, so we need to reformat the new value `newValue`
    if (newValue.value !== oldValue.value) {
        // Compare the 'newValue' with the current 'oldValue' so we do not `set` it again if it's not needed
        //XXX If multiple components share the same v-model, this means 'set' here will be called as many times as there is an input that is not being used by a human interaction
        this.anElement.set(newValue.value);
    }
}

solution:

if (this.anElement.getNumber() !== newValue.value) {
    // Make sure this is only called when the value is set by an external script, and not from a user input
    // The modification comes from a script, so we need to reformat the new value `newValue`
    if (newValue.value !== oldValue.value) {
        // Compare the 'newValue' with the current 'oldValue' so we do not `set` it again if it's not needed
        //XXX If multiple components share the same v-model, this means 'set' here will be called as many times as there is an input that is not being used by a human interaction
        this.anElement.set(newValue.value);
    }
}

userInteraction can therefor be removed completely.

V-model not readable by the autonumeric

Thereis only 1 input on my component which i set that a variable called price = ref(''") as the v-model but when i tried console.log the value, the value is not updated. Anyway to fix this ?

Trying to edit a value sets the caret to the end of the input

It seems the vue package has an issue that the vanilla js package does not: whenever a field's value is updated, the caret is automatically moved to the end of the input.

This makes it cumbersome to make changes like changing $52,000 to $52,500.

Directive usage

Thanks for this great project! I want to suggest a feature.
Would be great if Autonumeric could be used as a directive.
As I can see this Vue integration is pretty simple and component wrapper looks like an overhead to me.

Error when building with webpack 2 on linux

Building with webpack hrows an error:
Module not found: Error: Can't resolve 'AutoNumeric' in /VERY_LONG_PATH_TO_MY_PROJECT_DIR/node_modules/vue-autonumeric/dist'

and problem is, your package tries to import AutoNumeric, but actual directory name is autonumeric, in lowercase.
ln -s from AutoNumeric to autonumeric in my node_modules resolves the issue.

event on change

does v-on:change working in this component?

any docs for events available?

Error while npm run development

Hello,

i'm running on centos 7 and execute command
npm run production

and i got this message :

ERROR in ./node_modules/vue-autonumeric/dist/vue-autonumeric.min.js
Module not found: Error: Can't resolve 'AutoNumeric' in '/home/simonev3/simonev/node_modules/vue-autonumeric/dist'

how to fix this ?

thanks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.