If true
, the input's value will change based on mouse wheel
Number Input
The NumberInput component is similar to the Input component, but it has controls for incrementing or decrementing numeric values.
The NumberInput
component is a multipart component. The styling needs to be
applied to each part specifically.
To learn more about styling multipart components, visit the Component Style page.
Anatomy#
- A:
root
- B:
field
- C:
stepperGroup
- D:
stepper
Theming properties#
The properties that affect the theming of the NumberInput
component are:
variant
: The visual variant of the button. Defaults tooutline
.size
: The size of the button. Defaults tomd
.
Theming utilities#
createMultiStyleConfigHelpers
: a function that returns a set of utilities for creating style configs for a multipart component (definePartsStyle
anddefineMultiStyleConfig
).definePartsStyle
: a function used to create multipart style objects.defineMultiStyleConfig
: a function used to define the style configuration for a multipart component.
import { numberInputAnatomy } from '@chakra-ui/anatomy'import { createMultiStyleConfigHelpers } from '@chakra-ui/react'const { definePartsStyle, defineMultiStyleConfig } =createMultiStyleConfigHelpers(numberInputAnatomy.keys)
Customizing the default theme#
import { numberInputAnatomy } from '@chakra-ui/anatomy'import { createMultiStyleConfigHelpers } from '@chakra-ui/react'const { definePartsStyle, defineMultiStyleConfig } =createMultiStyleConfigHelpers(inputAnatomy.keys)const baseStyle = definePartsStyle({// define the part you're going to stylefield: {fontWeight: 'semibold',color: 'red.500',},})export const numberInputTheme = defineMultiStyleConfig({ baseStyle })
After customizing the number input theme, we can import it in our theme file and
add it in the components
property:
import { extendTheme } from '@chakra-ui/react'import { numberInputTheme } from './components/number-input'export const theme = extendTheme({components: { NumberInput: numberInputTheme },})
This is a crucial step to make sure that any changes that we make to the input theme are applied.
Adding a custom size#
Let's assume we want to include an extra large input size. Here's how we can do that:
import { numberInputAnatomy } from "@chakra-ui/anatomy";import { createMultiStyleConfigHelpers, defineStyle } from "@chakra-ui/react";const {definePartsStyle,defineMultiStyleConfig} = createMultiStyleConfigHelpers(numberInputAnatomy.keys);const xl = defineStyle({fontSize: "lg",h: "20",px: "2"});const sizes = {xl: definePartsStyle({ field: xl, stepper: xl, addon: xl })};export const numberInputTheme = defineMultiStyleConfig({ sizes });// Now we can use the new `xl` size<NumberInput size="xl" ... />
Every time you're adding anything new to the theme, you'd need to run the CLI command to get proper autocomplete in your IDE. You can learn more about the CLI tool here.
Adding a custom variant#
Let's assume we want to include a custom primary variant. Here's how we can do that:
import { numberInputAnatomy } from "@chakra-ui/anatomy";import { createMultiStyleConfigHelpers } from "@chakra-ui/react";const {definePartsStyle,defineMultiStyleConfig} = createMultiStyleConfigHelpers(numberInputAnatomy.keys);const primary = definePartsStyle({field: {border: "1px solid",borderColor: "gray.200",background: "gray.50",fontWeight: "bold",// Let's also provide dark mode alternatives_dark: {borderColor: "gray.600",background: "gray.800"}},stepper: {color: "purple.500",background: "gray.200"}});export const numberInputTheme = defineMultiStyleConfig({variants: { primary }});// Now we can use the new `pill` variant<NumberInput variant="primary" ... />
Changing the default properties#
Let's assume we want to change the default size and variant of every number input in our app. Here's how we can do that:
import { numberInputAnatomy } from '@chakra-ui/anatomy'import { createMultiStyleConfigHelpers, defineStyle } from '@chakra-ui/react'const { defineMultiStyleConfig } = createMultiStyleConfigHelpers(numberInputAnatomy.keys,)export const numberInputTheme = defineMultiStyleConfig({defaultProps: {size: 'xl',variant: 'primary',},})// This saves you time, instead of manually setting the size and variant every time you use an input:<NumberInput size="xl" variant="primary" ... />
Showcase#
import { Box, IconButton, NumberDecrementStepper, NumberIncrementStepper, NumberInput, NumberInputField, NumberInputStepper, SimpleGrid, useColorMode, Text } from "@chakra-ui/react"; import { FaMoon, FaSun } from "react-icons/fa"; export default function App() { const { toggleColorMode, colorMode } = useColorMode(); return ( <Box position="relative" h="100vh"> <SimpleGrid gap={12} p={12} columns={2}> <Box> <Text fontSize="sm">Themed filled number input:</Text> <NumberInput variant="filled"> <NumberInputField /> <NumberInputStepper> <NumberIncrementStepper /> <NumberDecrementStepper /> </NumberInputStepper> </NumberInput> </Box> <Box> <Text fontSize="sm">Themed outline number input:</Text> <NumberInput variant="outline"> <NumberInputField /> <NumberInputStepper> <NumberIncrementStepper /> <NumberDecrementStepper /> </NumberInputStepper> </NumberInput> </Box> <Box> <Text fontSize="sm">Themed flushed number input:</Text> <NumberInput variant="flushed"> <NumberInputField /> <NumberInputStepper> <NumberIncrementStepper /> <NumberDecrementStepper /> </NumberInputStepper> </NumberInput> </Box> <Box> <Text fontSize="sm">Custom variant number input:</Text> <NumberInput variant="primary"> <NumberInputField /> <NumberInputStepper> <NumberIncrementStepper /> <NumberDecrementStepper /> </NumberInputStepper> </NumberInput> </Box> </SimpleGrid> <IconButton aria-label="toggle theme" rounded="full" size="xs" position="absolute" bottom={4} left={4} onClick={toggleColorMode} icon={colorMode === "dark" ? <FaSun /> : <FaMoon />} /> </Box> ); }
Props#
NumberInput Props#
NumberInput composes Flex
with some additional props listed below.
allowMouseWheel
allowMouseWheel
boolean
aria-describedby
aria-describedby
string
aria-label
aria-label
string
aria-labelledby
aria-labelledby
string
clampValueOnBlur
clampValueOnBlur
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.
boolean
true
colorScheme
colorScheme
Color Schemes for NumberInput
are not implemented in the default theme. You can extend the theme to implement them.
string
defaultValue
defaultValue
The initial value of the counter. Should be less than max
and greater than min
string | number
errorBorderColor
errorBorderColor
The border color when the input is invalid. Use color keys in `theme.colors` @example errorBorderColor = "red.500"
string
focusBorderColor
focusBorderColor
The border color when the input is focused. Use color keys in `theme.colors` @example focusBorderColor = "blue.500"
string
focusInputOnChange
focusInputOnChange
If true
, the input will be focused as you increment
or decrement the value with the stepper
boolean
true
format
format
If using a custom display format, this converts the default format to the custom format.
((value: string | number) => string | number)
getAriaValueText
getAriaValueText
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
((value: string | number) => string)
id
id
The id
to use for the number input field.
string
inputMode
inputMode
Hints at the type of data that might be entered by the user. It also determines the type of keyboard shown to the user on mobile devices
"text" | "search" | "none" | "tel" | "url" | "email" | "numeric" | "decimal"
"decimal"
isDisabled
isDisabled
Whether the input should be disabled
boolean
isInvalid
isInvalid
If true
, the input will have `aria-invalid` set to true
boolean
isReadOnly
isReadOnly
If true
, the input will be in readonly mode
boolean
isRequired
isRequired
Whether the input is required
boolean
isValidCharacter
isValidCharacter
Whether the pressed key should be allowed in the input. The default behavior is to allow DOM floating point characters defined by /^[Ee0-9+\-.]$/
((value: string) => boolean)
keepWithinRange
keepWithinRange
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.
boolean
true
max
max
The maximum value of the counter
number
Number.MAX_SAFE_INTEGER
min
min
The minimum value of the counter
number
Number.MIN_SAFE_INTEGER
name
name
The HTML name
attribute used for forms
string
onBlur
onBlur
FocusEventHandler<HTMLInputElement>
onChange
onChange
The callback fired when the value changes
((valueAsString: string, valueAsNumber: number) => void)
onFocus
onFocus
FocusEventHandler<HTMLInputElement>
onInvalid
onInvalid
((message: ValidityState, value: string, valueAsNumber: number) => void)
parse
parse
If using a custom display format, this converts the custom format to a format parseFloat
understands.
((value: string) => string)
pattern
pattern
The pattern used to check the <input> element's value against on form submission.
string
"[0-9]*(.[0-9]+)?"
precision
precision
The number of decimal points used to round the value
number
size
size
"xs" | "sm" | "md" | "lg"
"md"
step
step
The step used to increment or decrement the value
number
1
value
value
The value of the counter. Should be less than max
and greater than min
string | number
variant
variant
"outline" | "filled" | "flushed" | "unstyled"
"outline"
NumberInputField Props#
NumberInputField
composes Input
so you can pass all Input
props. If you
want to change the size, pass the size
prop to the NumberInput
component
instead, as demonstrated above.
NumberInputStepper Props#
NumberInputStepper
composes Flex
so you can pass all Flex
props.
NumberDecrementStepper and NumberIncrementStepper Props#
NumberDecrementStepper
and NumberIncrementStepper
composes the Box
props
so you can pass all Box
props.
The NumberInput
component is a multipart component. The styling needs to be
applied to each part specifically.
To learn more about styling multipart components, visit the Component Style page.
Anatomy#
- A:
root
- B:
field
- C:
stepperGroup
- D:
stepper
Theming properties#
The properties that affect the theming of the NumberInput
component are:
variant
: The visual variant of the button. Defaults tooutline
.size
: The size of the button. Defaults tomd
.
Theming utilities#
createMultiStyleConfigHelpers
: a function that returns a set of utilities for creating style configs for a multipart component (definePartsStyle
anddefineMultiStyleConfig
).definePartsStyle
: a function used to create multipart style objects.defineMultiStyleConfig
: a function used to define the style configuration for a multipart component.
import { numberInputAnatomy } from '@chakra-ui/anatomy'import { createMultiStyleConfigHelpers } from '@chakra-ui/react'const { definePartsStyle, defineMultiStyleConfig } =createMultiStyleConfigHelpers(numberInputAnatomy.keys)
Customizing the default theme#
import { numberInputAnatomy } from '@chakra-ui/anatomy'import { createMultiStyleConfigHelpers } from '@chakra-ui/react'const { definePartsStyle, defineMultiStyleConfig } =createMultiStyleConfigHelpers(inputAnatomy.keys)const baseStyle = definePartsStyle({// define the part you're going to stylefield: {fontWeight: 'semibold',color: 'red.500',},})export const numberInputTheme = defineMultiStyleConfig({ baseStyle })
After customizing the number input theme, we can import it in our theme file and
add it in the components
property:
import { extendTheme } from '@chakra-ui/react'import { numberInputTheme } from './components/number-input'export const theme = extendTheme({components: { NumberInput: numberInputTheme },})
This is a crucial step to make sure that any changes that we make to the input theme are applied.
Adding a custom size#
Let's assume we want to include an extra large input size. Here's how we can do that:
import { numberInputAnatomy } from "@chakra-ui/anatomy";import { createMultiStyleConfigHelpers, defineStyle } from "@chakra-ui/react";const {definePartsStyle,defineMultiStyleConfig} = createMultiStyleConfigHelpers(numberInputAnatomy.keys);const xl = defineStyle({fontSize: "lg",h: "20",px: "2"});const sizes = {xl: definePartsStyle({ field: xl, stepper: xl, addon: xl })};export const numberInputTheme = defineMultiStyleConfig({ sizes });// Now we can use the new `xl` size<NumberInput size="xl" ... />
Every time you're adding anything new to the theme, you'd need to run the CLI command to get proper autocomplete in your IDE. You can learn more about the CLI tool here.
Adding a custom variant#
Let's assume we want to include a custom primary variant. Here's how we can do that:
import { numberInputAnatomy } from "@chakra-ui/anatomy";import { createMultiStyleConfigHelpers } from "@chakra-ui/react";const {definePartsStyle,defineMultiStyleConfig} = createMultiStyleConfigHelpers(numberInputAnatomy.keys);const primary = definePartsStyle({field: {border: "1px solid",borderColor: "gray.200",background: "gray.50",fontWeight: "bold",// Let's also provide dark mode alternatives_dark: {borderColor: "gray.600",background: "gray.800"}},stepper: {color: "purple.500",background: "gray.200"}});export const numberInputTheme = defineMultiStyleConfig({variants: { primary }});// Now we can use the new `pill` variant<NumberInput variant="primary" ... />
Changing the default properties#
Let's assume we want to change the default size and variant of every number input in our app. Here's how we can do that:
import { numberInputAnatomy } from '@chakra-ui/anatomy'import { createMultiStyleConfigHelpers, defineStyle } from '@chakra-ui/react'const { defineMultiStyleConfig } = createMultiStyleConfigHelpers(numberInputAnatomy.keys,)export const numberInputTheme = defineMultiStyleConfig({defaultProps: {size: 'xl',variant: 'primary',},})// This saves you time, instead of manually setting the size and variant every time you use an input:<NumberInput size="xl" variant="primary" ... />
Showcase#
import { Box, IconButton, NumberDecrementStepper, NumberIncrementStepper, NumberInput, NumberInputField, NumberInputStepper, SimpleGrid, useColorMode, Text } from "@chakra-ui/react"; import { FaMoon, FaSun } from "react-icons/fa"; export default function App() { const { toggleColorMode, colorMode } = useColorMode(); return ( <Box position="relative" h="100vh"> <SimpleGrid gap={12} p={12} columns={2}> <Box> <Text fontSize="sm">Themed filled number input:</Text> <NumberInput variant="filled"> <NumberInputField /> <NumberInputStepper> <NumberIncrementStepper /> <NumberDecrementStepper /> </NumberInputStepper> </NumberInput> </Box> <Box> <Text fontSize="sm">Themed outline number input:</Text> <NumberInput variant="outline"> <NumberInputField /> <NumberInputStepper> <NumberIncrementStepper /> <NumberDecrementStepper /> </NumberInputStepper> </NumberInput> </Box> <Box> <Text fontSize="sm">Themed flushed number input:</Text> <NumberInput variant="flushed"> <NumberInputField /> <NumberInputStepper> <NumberIncrementStepper /> <NumberDecrementStepper /> </NumberInputStepper> </NumberInput> </Box> <Box> <Text fontSize="sm">Custom variant number input:</Text> <NumberInput variant="primary"> <NumberInputField /> <NumberInputStepper> <NumberIncrementStepper /> <NumberDecrementStepper /> </NumberInputStepper> </NumberInput> </Box> </SimpleGrid> <IconButton aria-label="toggle theme" rounded="full" size="xs" position="absolute" bottom={4} left={4} onClick={toggleColorMode} icon={colorMode === "dark" ? <FaSun /> : <FaMoon />} /> </Box> ); }