Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@samhammer/vue-autonumeric

SamhammerAG147MIT1.2.9TypeScript support: included

An AutoNumeric wrapper for Vue.js

autonumeric, currency, money, monetary, Euro, Dollar, Pound, number, numeric, format, form, input, mask, as-you-type, live, vue, vue.js, vuejs, ui, components

readme

vue-autoNumeric

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

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>

...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,
    },
}

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! :)

changelog

Changelog for vue-autoNumeric

1.2.6

  • Fixes #24 VueAutonumeric value prop validator should accept strings

1.2.5

  • Fixes #14 The readOnly option is not respected for non-input tags (contenteditable is always set to true)

1.2.4

  • Fixes #18 Require of AutoNumeric is ES6 module and not the distribution version
  • Fix how using vue-autonumeric with a CDN did not work since the external library it searched was in lowercase.
  • Update the documentation about using vue-autonumeric in an ES6 module setup with Webpack

Important note:

  • We need to use <npmPath>: 'AutoNumeric' in the vue-autonumeric's webpack configuration since we want the user to be able to just use a CDN link to the AutoNumeric library and make sure vue-autonumeric will correctly use this name (since it's exported as AutoNumeric, with this case).
  • However if you are using vue-autonumeric in an ES6 module setup with a bundling tool (ie. Webpack), then you'll need to declare in your project an alias so that Webpack will know how to recognize the correct library name case.
    • The alias configuration example for Webpack:
      resolve: {
      extensions: ['.js', '.vue', '.json'],
      alias     : {
          '~'          : resolve('node_modules'),
          '@'          : resolve('src'),
          'AutoNumeric': resolve('node_modules/autonumeric/dist/autoNumeric.min'),
      },
      },

1.2.3

  • Fixes #15 Prop Validation Returns Error if Empty String
  • Fixes #16 build fails on linux

1.2.2

  • Fix the npm bundle size
  • It included a superfluous .tag.gz of the component generated by npm pack, for testing purpose

1.2.1

  • Fixes #13 Not working on mobile devices / userInteraction is never set to true on touch devices (iOS / Android)
  • Replace the user interaction tracking system with a simple one where the component AutoNumeric rawValue is compared to the watched new value.

1.2.0

  • Simplify how the rawValue decimal places are set
    • In v1.1.*, whenever the options were changed, the number of decimal places for the rawValue was calculated based of the previous options, and not only the new given options.
    • This was useful when two vue-autonumeric components shared the same v-model, but different options that have different values for the rawValue decimal places count.
    • For instance if one of the vue-autonumeric component had a decimalPlaces option set to 2, and another set to 5, then when the user modified the value of one of those components, vue-autonumeric would detect a programmatic value change in the other component (since it did not come from a user interaction), and it would then use set() to set the new value.
    • The first component would drop the additional decimal places when using set()...which in turn would make the other components aware of that new value change, and the second component would then use that new cropped value as well.
    • However, this special decimal places management is complexifying the code quite a bit, and is not 100% effective since some use cases were difficult, if even possible, to manage.
    • This rawValueDecimalPlaces hack is now reverted to a more sane and legible code.
    • From now on, the user will have to explicitly declare the rawValueDecimalPlaces value when updating the options, if he does not want to lose the precision when switching options.
  • Fix the case of the AutoNumeric import name in the webpack configuration

1.1.0

  • Fix issue #10 When using an object to modify both the :value and :options props, the options is not always modified before the value
  • Fix the issue where updating the options to new ones with a higher decimalPlacesRawValue loses the additional decimal places
  • Now allows to update the options by passing an array of options (with objects and predefined option names)
  • When the options and value are both set at the same time, vue-autonumeric now always update the options first, before setting the value.
    • This allows to use objects with the value and a predefined option name to modify the vue-autonumeric component.
    • Note: There is a known bug; if you use multiple vue-autonumeric component sharing the same v-model but different options, changing the value/option may not work correctly.
      • This is due to the fact that when vue-autonumeric detects a value change, it set() it since it does not come from a user interaction.
      • However if one of the vue-autonumeric component has a decimalPlaces option set to 2, and another set to 5, then the first component will drop the additional decimal places when using set()...which in turn will make the other component aware of that new value change, and the second component will then use that new cropped value as well.

1.0.7

  • Fix issue #9 Modifying the options will always lose the decimal places precision after 2 places
    • This happens since resetOnOptions temporarily resets the configuration to the default one, which set the decimalPlaces option to 2.

1.0.6

  • Fix issue #8 Add a new resetOnOptions props so that updating the options one first reset to the default options
  • Add a resetOnOptions props 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 was not set from 0 to 2 and you had to first update to the default configuration.
    • Now by default all options update will reset to the default options first. This can be avoided by setting resetOnOptions to false before changing the options value.

1.0.5

  • Fix issue #2 Allow the component to generate any supported Html element instead of only <input>

1.0.4

  • Fix the component size issue which bundled the whole AutoNumeric library
  • Update the documentation with how to install the component, depending if the AutoNumeric library should be bundled or not

1.0.3

  • Fix importing issues with the AutoNumeric library
  • Complete the documentation on how to install and use the component
  • Add a link to the examples page and its source in the Readme
  • Fix the example script imports by removing the unneeded AutoNumeric link
  • Fix issue #4 Error when building with webpack 2 on linux

1.0.2

  • Fix issue #3 Update the value when the :options prop is modified

1.0.1

  • Rename the component source from Autonumeric to VueAutonumeric to prevent confusion with the AutoNumeric library
  • Update the examples source accordingly
  • Update the readme with badges
  • Update the readme with more detailed installation instructions
  • Fix the examples in the readme and update the Codepen link

1.0.0

  • Release the version v1.0.0 of the vue-autoNumeric component