Number Input
The NumberInput component is similar to the Input component, but it has controls for incrementing or decrementing numeric values.
Props#
NumberInput Props#
NumberInput composes Flex
with some additional props listed below.
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.
Props#
NumberInput Props#
NumberInput composes Flex
with some additional props listed below.
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(numberInputAnatomy.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> ); }