You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The Number Input component and hook provide two props–`onChange` and `onInputChange`–that accept event handlers for when the value of the component changes.
175
+
176
+
#### onChange
177
+
178
+
`onChange` accepts a custom event handler that is called with two arguments: the underlying event, and the latest "clamped" value.
It's called when the `<input>` element is blurred, or when the stepper buttons are clicked, after the value has been clamped based on the [`min`, `max`](#minimum-and-maximum), or [`step`](#incremental-steps) props.
188
+
189
+
:::info
190
+
When using the component, `onChange` can only be passed as a prop on the component—not through `slotProps`.
191
+
:::
192
+
193
+
```jsx
194
+
// ✅ Works
195
+
<NumberInput
196
+
onChange={(event, newValue) =>console.log(`${event.type} event: the new value is ${newValue}`)}
197
+
/>
198
+
199
+
// ❌ Doesn't work
200
+
<NumberInput
201
+
slotProps={{
202
+
input: {
203
+
// expects a native input change event handler, newValue is always undefined
204
+
onChange: (event, newValue) => { ... },
205
+
},
206
+
}}
207
+
/>
208
+
```
209
+
210
+
#### onInputChange
211
+
212
+
`onInputChange` accepts a native input change handler that's passed to the `<input>` element:
It's called whenever the value of the textbox changes–for example, on every keystroke typed into it, before clamping is applied.
219
+
220
+
In other words, it's possible for `event.target.value` to contain out-of-range values or non-numerical characters.
221
+
222
+
:::info
223
+
When using the component, `onInputChange` can only be passed as a prop on the component. If you prefer to use `slotProps`, pass it as `slotProps.input.onChange` instead. Both ways–`onInputChange` and `slotProps.input.onChange`–work the same.
224
+
:::
225
+
226
+
```jsx
227
+
// ✅ Works
228
+
<NumberInput
229
+
onInputChange={(event) =>console.log(`the input value is: ${event.target.value}`)}
230
+
/>
231
+
232
+
// ✅ Works
233
+
<NumberInput
234
+
slotProps={{
235
+
input: {
236
+
// this exactly the same as onInputChange above
237
+
onChange: (event) =>console.log(`the input value is: ${event.target.value}`),
238
+
},
239
+
}}
240
+
/>
241
+
242
+
// ❌ Doesn't work
243
+
<NumberInput
244
+
slotProps={{
245
+
input: {
246
+
// This will throw "unknown event handler"
247
+
onInputChange: () => {},
248
+
},
249
+
}}
250
+
/>
251
+
```
252
+
172
253
### Adornments
173
254
174
255
You can use the `startAdornment` and `endAdornment` props to add a prefix or suffix, respectively, to a Number Input.
0 commit comments