Code Monkey home page Code Monkey logo

material-ui-currency-textfield's Introduction

Material-ui currency textfield

npm version

CurrencyTextField is a Material-ui react component. It provides a user friendly experience while inputing currency numbers.

CurrencyTextField wraps the functionality of autonumeric and it is a port of react-numeric in Material-ui.

Example of material

Main features:

  • Adds thousands separator automatically.
  • Adds automatically the decimals on blur.
  • Smart input. User can only type the accepted characters depending on the current value.
  • Lots of config options...

Install

npm install @unicef/material-ui-currency-textfield --save

Usage

Documentation and live demo is available here

import React from 'react'
import CurrencyTextField from '@unicef/material-ui-currency-textfield'

export default function MyComponent() {

  const [value, setValue] = React.useState();

  return (
    <CurrencyTextField
		label="Amount"
		variant="standard"
		value={value}
		currencySymbol="$"
		//minimumValue="0"
		outputFormat="string"
		decimalCharacter="."
		digitGroupSeparator=","
		onChange={(event, value)=> setValue(value)}
    />
  );
}

Development

In order to extend the component, clone the project and install the dependencies.

$ git clone https://github.com/unicef/material-ui-currency-textfield.git
$ npm install

The following commands are available:

npm start

Builds the component outputing it in the dist folder. It is refreshed everytime you make changes in the code.

npm start

To see the output in the browser run the example app (/example)

 cd example 
 npm install (only first time)
 npm start

Runs the app in the development mode. Open http://localhost:3000 to view the app in the browser.

It will reload automatically upon edits. Lint errors are also displayed on the console.

npm run build

Outputs the build for production to the dist folder.

npm run styleguide

Generates the documentation available on.

Open http://localhost:6060 to view it in the browser.

It watches for changes and automatically reloads the browser.

We use styleguidelist for documenting our custom components.

npm run styleguide:build

Builds the styleguide documentation for production. The output targets the styleguide folder.

About UNICEF

UNICEF works in over 190 countries and territories to protect the rights of every child. UNICEF has spent more than 70 years working to improve the lives of children and their families. In UNICEF, we believe all children have a right to survive, thrive and fulfill their potential โ€“ to the benefit of a better world.

Donate

Collaborations and support

Just fork the project and make a pull request. You may also consider donating.

License

Copyright (c) 2019 UNICEF.org

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Acknowledgements

The majority of the source code of this repo was developed by @sureshsevarthi.

Also, this source code is based on react-numeric.

material-ui-currency-textfield's People

Contributors

merlos avatar sureshsevarthi 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

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

material-ui-currency-textfield's Issues

Set the value through the state

Doing something like this:
resetValue = () => { this.setState({ value: 0, }); };
will not work as it work in any other text field.

Option to ALLOW typing decimals

This was almost perfect for what I needed...except my client needs to type in the cents and needs that mask to show the separator and cents while users type.

If the user types 12345678 it would (in pt-BR) show while typing: 123.456,78.

Is there any feature or option to make that possible?
Thanks.

Max input length

Component' input appears to have max value of 9,999,999,999,999.99 - can it be greater that this?

Disable decimals

Hey there! Can you please tell me how to disable the auto fill on decimals?

Thanks!

Can't enter 1.99 directly

After you enter 1, it becomes 1.00 immediately and the cursor moves to the end, and it doesn't accept input 99 anymore. You have to select the two 0s and enter the digit again. Did I miss some options?

How would I create a dynamic maximum value?

I'm trying to create a dynamic maximum value when an on change function happens.

For instance:

  • I have a maximum value of 100.
  • I have three currency input fields.
  • I place 50 into the first input field.
  • I place 40 into the second field.

I've gone ahead and implemented state that would cover this. But even if the maximumValue is 0. I can still enter numbers

Multiple @material-ui/styles instances

I have an existing project with @material-ui/core version 4.4.2 which uses @material-ui/[email protected]

This component has mui as a dependency rather than a peer dep. Which causes a duplicate instance of the styles package

It looks like there are several instances of `@material-ui/styles` initialized in this application.
This may cause theme propagation issues, broken class names, specificity issues, and makes your application bigger without a good reason.

See https://material-ui.com/r/styles-instance-warning for more info.

image

You should consider making it a peer dep

floating label support

Hi

Is it possible to have label floating style?

I guess it's not floating because it considers empty value as 0, is that correct? can I override it?

Currency symbol is taking up space

If I dont want a currency symbol displayed, there is a gap between the beginning of the textbox and where the text starts. Can this be removed

When installing this dependency using react 17

It would be great if this could work with versions later than 16.

npm install @unicef/material-ui-currency-textfield --save

npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR! 
npm ERR! While resolving: MY_REPO
npm ERR! Found: [email protected]
npm ERR! node_modules/react
npm ERR!   react@"^17.0.2" from the root project
npm ERR!   peer react@"^16.8.0 || ^17.0.0" from @material-ui/[email protected]
npm ERR!   node_modules/@material-ui/core
npm ERR!     peer @material-ui/core@">= 4.3.0" from @unicef/[email protected]
npm ERR!     node_modules/@unicef/material-ui-currency-textfield
npm ERR!       @unicef/material-ui-currency-textfield@"*" from the root project
npm ERR!   1 more (react-dom)
npm ERR! 
npm ERR! Could not resolve dependency:
npm ERR! peer react@"^16.8.6" from @unicef/[email protected]
npm ERR! node_modules/@unicef/material-ui-currency-textfield
npm ERR!   @unicef/material-ui-currency-textfield@"*" from the root project
npm ERR! 
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force, or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.

CurrencyTextField-textField class changes on different environments. (Question)

(Wish I could tag this as a question, or that there would be a discussion tab. I can't really ask this on Stackoverflow because it will never be answered).

I'm getting this CurrencyTextField-textField-1 class, which I can see on my snapshot tests. And the test works fine on my local. The class is always called that. But I have the feeling that's dynamically generated and the number at the end is not guaranteed to be the same every time? Because on our build environment it's different and my snapshot test fails.

Is there a way of avoiding this or should I give up on snapshot testing on that screen?

Thanks.

Issue working with react-hook-form validations

The error validations do not work very well with react-hook-form
On submit, the error is not displayed when validation fails

Relevant CodeSandbox

Using @material-ui input with input adornments however works:

<FormControl variant="outlined" className={classes.formControl}>
 <InputLabel htmlFor="amount">Amount</InputLabel>
   <OutlinedInput
                 inputRef={register({
                   validate: (value) =>
                     value && typeof value === "number" && value > 0,
                 })}
                 name="amount"
                 value={amount}
                 onChange={(e) => setAmount(e.target.value)}
                 startAdornment={
                   <InputAdornment position="start">$</InputAdornment>
                 }
                 labelWidth={60}
               />
               {errors.amount && (
                 <FormHelperText error variant="outlined">
                   Please enter an amount
                 </FormHelperText>
               )}
             </FormControl>

Could not find a declaration file for module '@unicef/material-ui-currency-textfield'

Hi,

After installing the module I cannot import it because of this message.
I've tried installing '@types/unicef__material-ui-currency-textfield' too but it gives me 404.

"@unicef/material-ui-currency-textfield": "^0.8.4"
"react": "^16.4.2"

Could not find a declaration file for module '@unicef/material-ui-currency-textfield'. '/home/nilton/Projects/Horus_Web/frontend/node_modules/@unicef/material-ui-currency-textfield/dist/index.js' implicitly has an 'any' type.
Try npm install @types/unicef__material-ui-currency-textfield if it exists or add a new declaration (.d.ts) file containing `declare module '@unicef/material-ui-currency-textfield'

Any tip?

Thank you

value is not being passed down to TextField component

Hey there,

I have noticed, that the value prop is not being passed down from CurrencyTextField in otherProps to TextField, thus not available in the components below.

In my case I overwrite OutlinedInput style based on existence of this prop, so my style does not show up as intended, because the value is always undefined...

It's not that of a big problem, but it would help me a lot and maybe other people in the future as well, if you would add it to otherProps, and also bring the behaviour of CurrencyTextField closer to MUI TextField.

ps. thanks for all your work!

Problem with error indicator

I'm encountering a problem with setting error state to indicate a problematic value. Everything seems to work fine if I just set error={true} but if I set error to true based on some state the currency field ends up copying the currency indicator in to the value. The field ends up reading EUR EUR10.00 and editing the value doesn't work correctly because EUR is included as part of the value of the native input element.

Warning: componentWillReceiveProps has been renamed...

I got this warning in browser:

Warning: componentWillReceiveProps has been renamed, and is not recommended for use. See https://fb.me/react-unsafe-component-lifecycles for details.

  • Move data fetching code or side effects to componentDidUpdate.
  • If you're updating state whenever props change, refactor your code to use memoization techniques or move it to static getDerivedStateFromProps. Learn more at: https://fb.me/react-derived-state
  • Rename componentWillReceiveProps to UNSAFE_componentWillReceiveProps to suppress this warning in non-strict mode. In React 17.x, only the UNSAFE_ name will work. To rename all deprecated lifecycles to their new names, you can run npx react-codemod rename-unsafe-lifecycles in your project source folder.

Please update the following components: CurrencyTextField

Any chance to update the component?

Add TypeScript definitions

There was already an issue for this: #6

However it was closed without actually being resolved. The currently published npm version is still missing Typescript definitions.

Support for InputLabelProps

As one can see, currently only a limited set of prop names are passed into underlying TextField component.

It is quite often required to manage Input label props, e.g. provide custom styles via classes, or add custom shrinking behavior

Is it possible to include InputLabelProps prop to a list of allowed props so that above could be achieved?

Thanks

Enable this to work with react v 17+

When installing this dependency using react 17 an error is produced (as below). It would be great if this could work with versions later than 16.

npm install @unicef/material-ui-currency-textfield --save

npm ERR! code ERESOLVE
npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR! 
npm ERR! While resolving: MY_REPO
npm ERR! Found: [email protected]
npm ERR! node_modules/react
npm ERR!   react@"^17.0.2" from the root project
npm ERR!   peer react@"^16.8.0 || ^17.0.0" from @material-ui/[email protected]
npm ERR!   node_modules/@material-ui/core
npm ERR!     peer @material-ui/core@">= 4.3.0" from @unicef/[email protected]
npm ERR!     node_modules/@unicef/material-ui-currency-textfield
npm ERR!       @unicef/material-ui-currency-textfield@"*" from the root project
npm ERR!   1 more (react-dom)
npm ERR! 
npm ERR! Could not resolve dependency:
npm ERR! peer react@"^16.8.6" from @unicef/[email protected]
npm ERR! node_modules/@unicef/material-ui-currency-textfield
npm ERR!   @unicef/material-ui-currency-textfield@"*" from the root project
npm ERR! 
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force, or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.

Support `input` event in addition to `change` event

The onChange callback used by the currency text field is a bit problematic because of the vague guarantees it provides of being called. I'm experiencing a problem with Chrome on Android where the the callback is ~always called with outdated data because according to specification the event doesn't need to fire every time something changes. It would be nice if CurrencyTextField would also support the onInput callback that is guaranteed to be called every time the user modifies the value.

The AutoNumeric object count on the form is incoherent.

We are getting the following on our production environment.
It is very weird, when we Empty Cache and Hard reload the issue resolves.
Any idea why it is happening for first time visiting users?

image (11)

This is how we using it:

import CurrencyTextField from '@unicef/material-ui-currency-textfield';
import React from 'react';

const Price = ({
  name,
  value,
  setFieldValue,
  minimumValue = '0',
  decimalPlaces = 0,
  decimalCharacter = '.',
  digitGroupSeparator = ',',
}) => {
  return (
    <CurrencyTextField
      id={name}
      value={value}
      label={'label'}
      variant={'outlined'}
      outputFormat={'string'}
      minimumValue={minimumValue}
      style={{
        minWidth: '240px',
      }}
      textAlign="left"
      helperText={'asdf'}
      decimalPlaces={decimalPlaces}
      currencySymbol={'$'}
      decimalCharacter={decimalCharacter}
      digitGroupSeparator={digitGroupSeparator}
      onChange={(event, value) => setFieldValue(name, value)}
    />
  );
};

export default Price;

Input field on the left, currency icon on the right

Hi, I couldn't find any good solution to reverse order of Input field and currency icon.
I need that possibility, due to other fields of material ui looks different, and their Input field on the left.

How can I change Input field on the left, currency icon on the right?
Example:
image

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.