Input
The Input
component is a component that is used to get user input in a text
field.
Import#
import { Input } from "@chakra-ui/react"
Usage#
Here's a basic usage example of the Input
component:
Changing the size of the Input#
The Input
component comes in four sizes. The default is md
.
xs
(24px
)sm
(32px
)md
(40px
)lg
(48px
)
Changing the appearance of the input#
The input component comes in 4 variants: outline
, unstyled
, flushed
, and
filled
. Pass the variant
prop and set it to one of these values.
Left and Right Addons#
Like bootstrap, you can add addons to the left and right of the Input
component. Chakra UI exports InputGroup
, InputLeftAddon
, and
InputRightAddon
to help with this use case.
Add elements inside Input#
In some scenarios, you might need to add an icon or button inside the input
component. Chakra UI exports InputLeftElement
and InputRightElement
to help
with this use case.
If the left or right is an icon or text, you can pass pointerEvents="none"
to
InputLeftElement
or InputRightElement
to ensure that clicking on them
focused the input.
Password Input Example#
Let's use these components to create a password input with a show/hide password functionality:
Controlled Input#
Value:
Changing the focus and error border colors#
You can change the border color that shows when the input receives focus
(focusBorderColor
) and when isInvalid
is set to true
(errorBorderColor
).
The value can be set to a color in the theme object, like teal.400
, or a raw
CSS value.
Props#
The Input
component composes Box so you can pass all Box
props, and React.InputHTMLAttributes
.
Name | Type | Description | Default |
---|---|---|---|
colorScheme | "blue" | "cyan" | "gray" | "green" | "orange" | "pink" | "purple" | "red" | "teal" | "yellow" | "whiteAlpha" | "blackAlpha" | "linkedin" | "facebook" | "messenger" | "whatsapp" | "twitter" | "telegram" | Color Schemes for Input are not implemented in the default theme. You can extend the theme to implement them. | - |
errorBorderColor | string | The border color when the input is invalid. Use color keys in `theme.colors` @example errorBorderColor = "red.500" | - |
focusBorderColor | string | The border color when the input is focused. Use color keys in `theme.colors` @example focusBorderColor = "blue.500" | - |
isDisabled | boolean | If `true`, the form control will be disabled. This has 2 side effects: - The `FormLabel` will have `data-disabled` attribute - The form element (e.g, Input) will be disabled | - |
isFullWidth | boolean | If `true`, the input element will span the full width of its parent @deprecated This component defaults to 100% width, please use the props `maxWidth` or `width` to configure | - |
isInvalid | boolean | If `true`, the form control will be invalid. This has 2 side effects: - The `FormLabel` and `FormErrorIcon` will have `data-invalid` set to `true` - The form element (e.g, Input) will have `aria-invalid` set to `true` | - |
isReadOnly | boolean | If `true`, the form control will be readonly | - |
isRequired | boolean | If `true`, the form control will be required. This has 2 side effects: - The `FormLabel` will show a required indicator - The form element (e.g, Input) will have `aria-required` set to `true` | - |
size | "sm" | "md" | "lg" | "xs" | "md" | |
variant | "outline" | "unstyled" | "filled" | "flushed" | "outline" |