--- title: NumberInput description: "`NumberInput`は、ユーザーからの数値入力を取得するために使用されるコンポーネントです。" links: - style: https://github.com/yamada-ui/yamada-ui/tree/main/packages/react/src/components/number-input/number-input.style.ts - source: https://github.com/yamada-ui/yamada-ui/tree/main/packages/react/src/components/number-input - storybook: https://yamada-ui.github.io/yamada-ui?path=/story/components-numberinput--basic --- ```tsx ``` ## 使い方 ```tsx import { NumberInput } from "@yamada-ui/react" ``` ```tsx import { NumberInput } from "@/components/ui" ``` ```tsx import { NumberInput } from "@workspaces/ui" ``` ```tsx ``` ### バリアントを変更する ```tsx {(variant) => ( )} ``` ### サイズを変更する ```tsx preview {(size) => ( )} ``` ### デフォルトの値を設定する デフォルトの値を設定する場合は、`defaultValue`に文字列または数値を設定します。 ```tsx ``` ### 最小・最大値を設定する 最小値と最大値を設定する場合は、`min`または`max`に数値を設定します。 ```tsx ``` ### ステップ値を設定する ステップ値を設定する場合は、`step`に数値を設定します。 ```tsx ``` ### 小数点を設定する 小数点を設定する場合は、`precision`に数値を設定します。 ```tsx ``` ### ブラー時の値のクランプを無効にする デフォルトでは、ユーザーの自由な数値入力を受け付けますが、`min`または`max`を超える数値は、ブラー時に自動的に調整されます。この自動的に調整される動作を無効にする場合は、`clampValueOnBlur`を`false`に設定します。 ```tsx ``` ### 範囲外の値を許可する 範囲外の値を許可する場合は、`keepWithinRange`を`false`に設定します。 ```tsx ``` ### 無効にする 無効にする場合は、`disabled`を`true`に設定します。 ```tsx {(variant) => ( )} ``` ### 読み取り専用にする 読み取り専用にする場合は、`readOnly`を`true`に設定します。 ```tsx {(variant) => ( )} ``` ### 無効な入力にする 無効な入力にする場合は、`invalid`を`true`に設定します。 ```tsx {(variant) => ( )} ``` ### ボーダーの色を変更する ボーダーの色を変更する場合は、`focusBorderColor`または`errorBorderColor`に値を設定します。 ```tsx ``` ### プレースホルダーのカラーを変更する プレースホルダーのカラーを変更する場合は、`_placeholder`に値を設定します。 ```tsx ``` ### ステッパーをカスタマイズする ステッパーをカスタマイズする場合は、`incrementProps`または`decrementProps`にpropsを設定します。 ```tsx }} decrementProps={{ children: }} /> ``` ### コンポーネントをカスタマイズする コンポーネントをカスタマイズする場合は、`useNumberInput`を使用します。 ```tsx const { getInputProps, getIncrementProps, getDecrementProps } = useNumberInput({ step: 0.01, defaultValue: 3.14, min: 3, max: 4, precision: 2, }) return ( } {...getIncrementProps()} aria-label="Increment" /> } {...getDecrementProps()} aria-label="Decrement" /> ) ``` ### 制御する ```tsx const [value, setValue] = useState(18) return ( setValue(valueAsNumber)} placeholder="Order Quantity" /> ) ``` ## Props | Prop | Default | Type | Description | | -------------------- | ------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `as` | - | `As` | The HTML element to render. | | `asChild` | - | `boolean` | Merges its props onto its immediate child. | | `css` | - | `CSSObject \| CSSObject[]` | The CSS object. | | `colorScheme` | - | `"amber" \| "black" \| "blackAlpha" \| "blue" \| "cyan" \| "danger" \| "emerald" \| "error" \| "flashy" \| "fuchsia" ...` | Set color scheme variables. | | `size` | `"md"` | `"2xl" \| "lg" \| "md" \| "sm" \| "xl" \| "xs"` | The size of the component. | | `variant` | `"outline"` | `"filled" \| "flushed" \| "outline" \| "plain"` | The variant of the component. | | `allowMouseWheel` | `false` | `boolean` | If `true`, the input's value will change based on mouse wheel. | | `clampValueOnBlur` | `true` | `boolean` | This controls the value update when you blur out of the input. - If `true` and the value is greater than `max`, the value will be reset to `max`. - Else, the value remains the same. | | `controlProps` | - | `NumberInputControlProps` | The props for the control element. | | `decrementProps` | - | `NumberInputDecrementButtonProps` | The props for the decrement button element. | | `defaultValue` | - | `number \| string` | The initial value of the counter. Should be less than `max` and greater than `min`. | | `elementProps` | - | `InputGroup.ElementProps` | The props for the end element. | | `errorBorderColor` | - | `"-moz-initial" \| "ActiveBorder" \| "ActiveCaption" \| "aliceblue" \| "amber.100" \| "amber.200" \| "amber.300" \| "amber.400" \| "amber.50" \| "amber.500" ...` | The border color when the input is invalid. | | `focusBorderColor` | - | `"-moz-initial" \| "ActiveBorder" \| "ActiveCaption" \| "aliceblue" \| "amber.100" \| "amber.200" \| "amber.300" \| "amber.400" \| "amber.50" \| "amber.500" ...` | The border color when the input is focused. | | `focusInputOnChange` | `true` | `boolean` | If `true`, the input will be focused as you increment or decrement the value with the button. | | `format` | - | `(value: number \| string) => string` | If using a custom display format, this converts the default format to the custom format. | | `getAriaValueText` | - | `(value: number \| string) => string \| undefined` | This is used to format the value so that screen readers can speak out a more human-friendly value. It is used to set the `aria-valuetext` property of the input. | | `htmlSize` | - | `number` | The native HTML `size` attribute to be passed to the `input`. | | `incrementProps` | - | `NumberInputIncrementButtonProps` | The props for the increment button element. | | `invalid` | `false` | `boolean` | If `true`, the field will be invalid. | | `isValidCharacter` | - | `(value: string) => boolean` | Whether the pressed key should be allowed in the input. The default behavior is to allow DOM floating point characters defined by /^[Ee0-9+\-.]$/. | | `keepWithinRange` | `true` | `boolean` | This controls the value update behavior in general. - If `true` and you use the stepper or up/down arrow keys, the value will not exceed the `max` or go lower than `min`. - If `false`, the value will be allowed to go out of range. | | `max` | `Number.MAX_SAFE_INTEGER` | `number` | The maximum value of the counter | | `min` | `Number.MIN_SAFE_INTEGER` | `number` | The minimum value of the counter | | `onChange` | - | `(valueAsString: string, valueAsNumber: number) => void` | The callback fired when the value changes. | | `parse` | - | `(value: string) => string` | If using a custom display format, this converts the custom format to a format `parseFloat` understands. | | `precision` | - | `number` | The number of decimal points used to round the value. | | `rootProps` | - | `InputGroup.RootProps` | The props for the root element. | | `step` | `1` | `number` | The step used to increment or decrement the value. | | `value` | - | `number \| string` | The value of the counter. Should be less than `max` and greater than `min`. | ## アクセシビリティ 現在、v2の移行に伴い、このセクションは更新中です。