mui / material-ui

Material UI: Ready-to-use foundational React components, free forever. It includes Material UI, which implements Google's Material Design.
https://mui.com/material-ui/
MIT License
91.86k stars 31.57k forks source link

[material-ui] Add Number Input component #19154

Open m4theushw opened 4 years ago

m4theushw commented 4 years ago

I noticed in the Material-UI's roadmap a Numeric Input component to be built. If nobody has took the lead then I can help. Recently I had to develop a component like that for a personal project (first screenshot below).

Summary 💡

I didn't look deeply but I think about a <NumericField /> wrapping a <TextField />. This text field component would render the step buttons.

Examples 🌈

Capture d’écran 2020-09-04 à 13 45 42 Capture d’écran 2020-09-04 à 13 46 23

Motivation 🔦

Sometimes we want to force an input to only accept numbers, but the support for type="number" has limitations:

TODO

Benchmarks

https://mui-org.notion.site/Input-Number-component-364825a7bec94381809ac11ff05b4cc0

dcworldwide commented 4 years ago

It would need to support floating point numbers in addition to integers. With the ability to set precision ranges.

My project's current solution was to wrap TextField.

Is adding generic mask property for TextField sufficient? That is more generic....as it ensures userland can define the content for their needs...phone numbers, bank account numbers and of course floats, integers. As these are common...const regex patterns provide by mui would be nice.

m4theushw commented 4 years ago

It would need to support floating point numbers in addition to integers.

@dcworldwide I don't know, step buttons are not very useful with continuous quantities.

Is adding generic mask property for TextField sufficient?

For phone numbers, bank account numbers or currency, masks solve the problem. But to enforce a minimum/maximum value or a custom step (0, 2, 4, 6) regex patterns become huge.

Maybe I need redefine what kind of numeric input I am talking. I see an opportunity for an integer field and a floating-point field.

mbrookes commented 4 years ago

https://elemefe.github.io/element-react/#/en-US/input-number

m4theushw commented 4 years ago

I'm thinking about starting a draft for NumericField. To support currency values should we (1) use Intl.NumberFormat, (2) extend the Material-UI's localization API or (3) delegate all formating to user?

yehonadav commented 4 years ago

i would vote to start with (3)

msalahz commented 4 years ago

https://stackoverflow.com/questions/47798104/set-min-max-on-textfield-type-number

GaddMaster commented 2 years ago

Hey Why when I copy and paste exactly this from the TextField doc it does not work ? <TextField inputProps={{ inputMode: 'numeric', pattern: '[0-9]*' }} /> Daniel

GaddMaster commented 2 years ago

@sscotth Your code sandbox also does not work Chrome Firefox

gearcoded commented 2 years ago

This is a little bit offtopic, but the documentation leads to this page. So, this information might be helpful for someone.

From the examples with the slider, I found the following way to add the Input element with the Number type:

import MuiInput from '@mui/material/Input';

function NumberComponent(props){
  // if you want to use state, uncomment this:
  // let { numberValue } = props || 1;

  // if you want to use state, uncomment this:
  // const [numberValue, handleDurationChange] = useState(numberProperty);

  return (
    <MuiInput
      value={numberValue}
      size="small"
      inputProps={{
        step: 1,
        min: 0,
        max: 99999,
        type: 'number',
      }}
    />
  )
}
aurorapete commented 2 years ago

I wanted to chime and say that, from a product designer's perspective, an incrementer would be very useful to have in Material UI. The software I use on a daily basis (Figma, Adobe products, Cinema 4D) uses this kind of field all the time, and because I work on apps at my company which involve similar visualization and manipulation of values, I want to be able to provide this kind of a control to our users. Pasting a small mockup of the sorts of things we would want it do in our case.

image

So, I'm mainly just chiming in here to say this would be a really valuable to provide in Material UI, and it could help product designers like me advocate more strongly for Material UI in our various spheres of influence. Cheers.

violabg commented 2 years ago

any update on this component?

Benzer1406 commented 2 years ago

Any update here?

AndyFang36 commented 2 years ago

Great, thank you!

mbrookes commented 1 year ago

@siriwatknp Could you link to the source for yours?

siriwatknp commented 1 year ago

Demo: https://next.mui-treasury.com/?path=/story/component-numberinput--default Component src: https://github.com/siriwatknp/mui-treasury/blob/next/packages/component-numberinput/src/NumberInput.tsx useNumberInput hook: https://github.com/siriwatknp/mui-treasury/blob/next/packages/use-number-input/src/useNumberInput.ts

mbrookes commented 1 year ago

Ah, that's why I couldn't find it :)

Thanks!

TheRealCuran commented 1 year ago

Just to make sure: please don't overlook a step parameter in the implementation. And please make it possible to define a set of valid fractions (static and by callback). I have number fields implemented in a tool we use internally, where only a defined set of fractions are OK. Say you press the "up" button and you would only want to got from x.33 to x.5, then there should be some kind of option to define those valid steps.

In case it helps, my Order ID is: :credit_card: 47016

mbrookes commented 1 year ago

Not sure why Sam is asking for interviews to ask what components users would like in v6 when we already have a concrete list based on issue upvotes; and even better, an existing implementation of the most popular request. Nor does it need to wait for v6 - an addition isn't a breaking change. Lab/refine/publish...

afilp commented 1 year ago

If possible, I would "urge" this to happen soon. Why?

Well, I have found myself several times accidentally changing the focused number input while scrolling through the touchpad towards the end of the page to hit the SAVE button. Due to the stepper...

See the result here, this was entered as 24500, I then scrolled down to hit Save and I saved the form, see what happened to the input (I re-entered to the form afterwards):

image

So, this can be done by many users too and they will not even notice! (I noticed this accidentally too)

Thanks!

MVR5991 commented 1 year ago

If possible, I would "urge" this to happen soon. Why?

Well, I have found myself several times accidentally changing the focused number input while scrolling through the touchpad towards the end of the page to hit the SAVE button. Due to the stepper...

We recently had the same bug in our application which took a while to get noticed since it is very subtle. Our workaround for now is something like this:

export const blurTarget = (event: SyntheticEvent) => {
  event.target instanceof HTMLElement && event.target.blur()
}

 <TextField
      size="small"
      type="number"
     onWheel={blurTarget}

My company would also like to see this implemented as soon as possible :)

Thanks

jayarjo commented 1 year ago

Any progress on this?

mnajdova commented 1 year ago

Any progress on this?

We plan to start working on this in this quarter.

oliviertassinari commented 1 year ago

@mj12albert I think that it could be great to spend your first couple of weeks on smaller changes than this issue. There are probably two different NumberInput components to build here. My idea is that with smaller changes, you could maybe learn the codebase, workflows, and build confidence faster.

thiagorochatr commented 1 year ago

Hey guys!

I don't know if it has much to do with the topic of discussion, but I would like to ask a question! If I can't send it here, let me know and I'll remove it! And if possible, point me to the correct place to take this doubt.

In a system I'm building, I ask the user for the value in Brazilian Reais (R$ 00,00 format). This week, I was told that the input is not working on Samsung and OnePlus phones (I suspect it is on any phone with a Chromium-based browser). On my MacBook and Iphone, the input works perfectly.

Can anyone help me, please? Links: https://github.com/thiagorochatr/teste-input-value/blob/main/components/original/InputPriceOG.tsx https://teste-input-value.vercel.app/original

thiagorochatr commented 1 year ago

Hey guys!

I don't know if it has much to do with the topic of discussion, but I would like to ask a question! If I can't send it here, let me know and I'll remove it! And if possible, point me to the correct place to take this doubt.

In a system I'm building, I ask the user for the value in Brazilian Reais (R$ 00,00 format). This week, I was told that the input is not working on Samsung and OnePlus phones (I suspect it is on any phone with a Chromium-based browser). On my MacBook and Iphone, the input works perfectly.

Can anyone help me, please? Links: https://github.com/thiagorochatr/teste-input-value/blob/main/components/original/InputPriceOG.tsx https://teste-input-value.vercel.app/original

Update: fixed!

JahnoelRondon commented 11 months ago

Demo: https://next.mui-treasury.com/?path=/story/component-numberinput--default Component src: https://github.com/siriwatknp/mui-treasury/blob/next/packages/component-numberinput/src/NumberInput.tsx useNumberInput hook: https://github.com/siriwatknp/mui-treasury/blob/next/packages/use-number-input/src/useNumberInput.ts

You can still use + and other math operators

JahnoelRondon commented 11 months ago

Current example solution With Keydown preventing changes based on criteria.

// Solution with keydown
    <TextField
      value={state.value}
      type="number"
      onKeyDown={(e) => {
        if (e.key === "e" || e.key === "E" || e.key === "-" || e.key === "+") {
          e.preventDefault()
        }
      }}
      onChange={(e) => {
        handleChange();
      }}
    />

found this stack over flow helpful https://stackoverflow.com/questions/31706611/why-does-the-html-input-with-type-number-allow-the-letter-e-to-be-entered-in

mj12albert commented 9 months ago

✨ We've released a first iteration on the Number Input for Base UI in v5.14.4! The docs: https://mui.com/base-ui/react-number-input/.

jo-chemla commented 8 months ago

✨ We've released a first iteration on the Number Input for Base UI in v5.14.4!

Great to hear! Will this be ported to Material UI core itself at some point? Thanks!

mbrookes commented 7 months ago

Adding a reminder to update: https://mui.com/material-ui/react-text-field/#type-quot-number-quot when this is released.

nameer commented 7 months ago

Here is my implementation of NumberInput with support for decimal numbers: https://gist.github.com/nameer/664abb9d4e261a210de1f1ba814a0ad2

To use it for integers, simply set decimalScale to 0.

secretwpn commented 7 months ago

One thing about existing number inputs I've always found annoying is the half-sized up/down arrows - they are often too small to be usable and I personally would much prefer something like 123 + - - normal text field followed by icon buttons.

This way the buttons have enough vertical space to get to usable size

nameer commented 7 months ago

Here is my implementation of NumberInput with support for decimal numbers: https://gist.github.com/nameer/664abb9d4e261a210de1f1ba814a0ad2

To use it for integers, simply set decimalScale to 0.

Sample

A live implementation is here: https://codesandbox.io/p/sandbox/numberinput-7fxlym

This component supports setting minimum, maximum, and step values, and also handles decimal numbers. It allows you to provide an initial number, and you can monitor changes via the onChange prop. onChange event will only be triggered if the value is a valid number (typing the dot of "5." will not trigger it).

MiLandry commented 4 months ago

Is there going to be any support for adding labels to number input fields?

Catlike14 commented 4 months ago

It seems to not work with react-hook-form. https://codesandbox.io/p/sandbox/epic-monad-jrtnc2 Am I missing something?

simonetavoletta commented 1 month ago

Hello, is there any news about this new component?

Thank you 😄

xav1639 commented 1 day ago

Super duper excited for this component by the way can't wait 😉