The visual color appearance of the component
Input
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:
<Input placeholder='Basic usage' />
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
)
<Stack spacing={3}><Input placeholder='extra small size' size='xs' /><Input placeholder='small size' size='sm' /><Input placeholder='medium size' size='md' /><Input placeholder='large size' size='lg' /></Stack>
If you want to use the native DOM size
attribute you can use the htmlSize
prop. For it to work as expected you will also need to provide the width
prop
set to auto
.
<Input htmlSize={4} width='auto' />
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.
<Stack spacing={3}><Input variant='outline' placeholder='Outline' /><Input variant='filled' placeholder='Filled' /><Input variant='flushed' placeholder='Flushed' /><Input variant='unstyled' placeholder='Unstyled' /></Stack>
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.
<Stack spacing={4}><InputGroup><InputLeftAddon>+234</InputLeftAddon><Input type='tel' placeholder='phone number' /></InputGroup>{/* If you add the size prop to `InputGroup`, it'll pass it to all its children. */}<InputGroup size='sm'><InputLeftAddon>https://</InputLeftAddon><Input placeholder='mysite' /><InputRightAddon>.com</InputRightAddon></InputGroup></Stack>
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.
<Stack spacing={4}><InputGroup><InputLeftElement pointerEvents='none'><PhoneIcon color='gray.300' /></InputLeftElement><Input type='tel' placeholder='Phone number' /></InputGroup><InputGroup><InputLeftElement pointerEvents='none' color='gray.300' fontSize='1.2em'>$</InputLeftElement><Input placeholder='Enter amount' /><InputRightElement><CheckIcon color='green.500' /></InputRightElement></InputGroup></Stack>
Password Input Example#
Let's use these components to create a password input with a show/hide password functionality:
function PasswordInput() {const [show, setShow] = React.useState(false)const handleClick = () => setShow(!show)return (<InputGroup size='md'><Inputpr='4.5rem'type={show ? 'text' : 'password'}placeholder='Enter password'/><InputRightElement width='4.5rem'><Button h='1.75rem' size='sm' onClick={handleClick}>{show ? 'Hide' : 'Show'}</Button></InputRightElement></InputGroup>)}
Controlled Input#
function Example() {const [value, setValue] = React.useState('')const handleChange = (event) => setValue(event.target.value)return (<><Text mb='8px'>Value: {value}</Text><Inputvalue={value}onChange={handleChange}placeholder='Here is a sample placeholder'size='sm'/></>)}
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.
<Stack spacing={3}><Input focusBorderColor='lime' placeholder='Here is a sample placeholder' /><InputfocusBorderColor='pink.400'placeholder='Here is a sample placeholder'/><InputisInvaliderrorBorderColor='red.300'placeholder='Here is a sample placeholder'/><InputisInvaliderrorBorderColor='crimson'placeholder='Here is a sample placeholder'/></Stack>
Styling the placeholder#
The placeholder of an input can be styled by using the _placeholder
prop. Per
default the placeholder has an opacity of 0.6, so it can be necessary to set the
opacity to 1 if you want the placeholder to have a specific color.
<Stack spacing={3}><Input placeholder='default placeholder' /><Inputplaceholder='custom placeholder'_placeholder={{ opacity: 1, color: 'gray.500' }}/><Inputcolor='teal'placeholder='custom placeholder'_placeholder={{ color: 'inherit' }}/><Inputcolor='tomato'placeholder='custom placeholder'_placeholder={{ opacity: 0.4, color: 'inherit' }}/></Stack>
Input Methods other than Text#
You can use different types with Input
such as dateTime, color, search, file
etc.
Date and Time Picker
<Input placeholder='Select Date and Time' size='md' type='datetime-local' />
Check for different input types available : https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input#input_types
Props#
The Input
component composes Box so you can
pass all Box
props, and React.InputHTMLAttributes
.
colorScheme
colorScheme
"whiteAlpha" | "blackAlpha" | "gray" | "red" | "orange" | "yellow" | "green" | "teal" | "blue" | "cyan" | "purple" | "pink"
isDisabled
isDisabled
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
boolean
false
isInvalid
isInvalid
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
boolean
false
isReadOnly
isReadOnly
If true
, the form control will be readonly
boolean
false
isRequired
isRequired
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
boolean
false
size
size
The size of the Input
"lg" | "md" | "sm" | "xs"
md
variant
variant
The variant of the Input
"outline" | "filled" | "flushed" | "unstyled"
outline
The Input
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:
addon
- B:
field
- C:
element
Theming properties#
The properties that affect the theming of the Input
component are:
variant
: The visual variant of the input. Defaults tooutline
.size
: The size of the input. 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 { inputAnatomy } from '@chakra-ui/anatomy'import { createMultiStyleConfigHelpers } from '@chakra-ui/react'const { definePartsStyle, defineMultiStyleConfig } =createMultiStyleConfigHelpers(inputAnatomy.keys)
Customizing the default theme#
import { inputAnatomy } 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: {fontFamily: 'mono', // change the font familycolor: 'teal.500', // change the input text color},})export const inputTheme = defineMultiStyleConfig({ baseStyle })
After customizing the input theme, we can import it in our theme file and add it
in the components
property:
import { extendTheme } from '@chakra-ui/react'import { inputTheme } from './components/input.ts'export const theme = extendTheme({components: { Input: inputTheme },})
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 { inputAnatomy } from '@chakra-ui/anatomy'import { createMultiStyleConfigHelpers, defineStyle } from '@chakra-ui/react'const { definePartsStyle, defineMultiStyleConfig } =createMultiStyleConfigHelpers(inputAnatomy.keys)const xl = defineStyle({fontSize: 'lg',px: '4',h: '12',})const sizes = {xl: definePartsStyle({ field: xl, addon: xl }),}export const inputTheme = defineMultiStyleConfig({ sizes })// Now we can use the new `xl` size<Input 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 pill variant. Here's how we can do that:
import { inputAnatomy } from '@chakra-ui/anatomy'import { createMultiStyleConfigHelpers, defineStyle } from '@chakra-ui/react'const { definePartsStyle, defineMultiStyleConfig } =createMultiStyleConfigHelpers(inputAnatomy.keys)const pill = definePartsStyle({field: {border: '1px solid',borderColor: 'gray.200',background: 'gray.50',borderRadius: 'full',// Let's also provide dark mode alternatives_dark: {borderColor: 'gray.600',background: 'gray.800',},},addon: {border: '1px solid',borderColor: 'gray.200',background: 'gray.200',borderRadius: 'full',color: 'gray.500',_dark: {borderColor: 'gray.600',background: 'gray.600',color: 'gray.400',},},})export const inputTheme = defineMultiStyleConfig({variants: { pill },})// Now we can use the new `pill` variant<Input variant="pill" ... />
Changing the default properties#
Let's assume we want to change the default size and variant of every input in our app. Here's how we can do that:
import { inputAnatomy } from '@chakra-ui/anatomy'import { createMultiStyleConfigHelpers, defineStyle } from '@chakra-ui/react'const { defineMultiStyleConfig } = createMultiStyleConfigHelpers(inputAnatomy.keys,)export const inputTheme = defineMultiStyleConfig({defaultProps: {size: 'xl',variant: 'pill',},})// This saves you time, instead of manually setting the size and variant every time you use an input:<Input size="xl" variant="pill" ... />
Showcase#
import { Box, SimpleGrid, GridItem, Icon, IconButton, Input, InputGroup, InputLeftAddon, InputRightElement, useColorMode } from "@chakra-ui/react"; import { FaMoon, FaSun, FaPhone } 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}> <Input placeholder="Themed Outline Input" /> <Input placeholder="Themed Filled Input" variant="filled" /> <GridItem colSpan={2}> <InputGroup variant="custom" colorScheme="purple"> <InputLeftAddon>Phone:</InputLeftAddon> <Input placeholder="Themed Custom Input" /> <InputRightElement pointerEvents="none"> <Icon as={FaPhone} color="green.400" /> </InputRightElement> </InputGroup> </GridItem> </SimpleGrid> <IconButton aria-label="toggle theme" rounded="full" size="xs" position="absolute" bottom={4} left={4} onClick={toggleColorMode} icon={colorMode === "dark" ? <FaSun /> : <FaMoon />} /> </Box> ); }