Number Input

The NumberInput component is similar to the Input component, but it has controls for incrementing or decrementing numeric values.

    View SourceView Theme Source@chakra-ui/number-input

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 to outline.
  • size: The size of the button. Defaults to md.

Theming utilities#

  • createMultiStyleConfigHelpers: a function that returns a set of utilities for creating style configs for a multipart component (definePartsStyle and defineMultiStyleConfig).
  • 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 '@fluidtruck/core'
const { definePartsStyle, defineMultiStyleConfig } =
createMultiStyleConfigHelpers(numberInputAnatomy.keys)

Customizing the default theme#

import { numberInputAnatomy } from '@chakra-ui/anatomy'
import { createMultiStyleConfigHelpers } from '@fluidtruck/core'
const { definePartsStyle, defineMultiStyleConfig } =
createMultiStyleConfigHelpers(numberInputAnatomy.keys)
const baseStyle = definePartsStyle({
// define the part you're going to style
field: {
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 '@fluidtruck/core'
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 "@fluidtruck/core";
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 "@fluidtruck/core";
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 '@fluidtruck/core'
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#