TextInput is a form component to add default styling to the native text input.
Note: Don't forget to set aria-label
to make the TextInput accessible to screen reader users.
<TextInput aria-label="Zipcode" name="zipcode" placeholder="Zipcode" autoComplete="postal-code" />
<><TextInputleadingVisual={SearchIcon}aria-label="Zipcode"name="zipcode"placeholder="Zip"autoComplete="postal-code"/><TextInputtrailingVisual={SearchIcon}aria-label="Zipcode"name="zipcode"placeholder="Zip"autoComplete="postal-code"/></>
<><TextInput leadingVisual="$" aria-label="Cost of the thing" name="cost" placeholder="23.45" /><TextInput sx={{width: '150px'}} trailingVisual="minutes" aria-label="Time in minutes" name="time" placeholder="25" /></>
const WithIconAndLoadingIndicator = () => {const [loading, setLoading] = React.useState(true)const toggleLoadingState = () => {setLoading(!loading)}return (<><Box mb={6}><button type="button" onClick={toggleLoadingState}>Toggle loading state {loading ? 'off' : 'on'}</button></Box><Text fontSize={3} mb={1} display="block">No visual</Text><Box mb={3}><TextInput loading={loading} /></Box><Text fontSize={3} mb={1} display="block">Leading visual</Text><Box mb={3}><TextInput leadingVisual={SearchIcon} loading={loading} /></Box><Text fontSize={3} mb={1} display="block">Trailing visual</Text><Box mb={3}><TextInput trailingVisual={SearchIcon} loading={loading} /></Box><Text fontSize={3} mb={1} display="block">Both visuals</Text><Box mb={3}><TextInput leadingVisual={SearchIcon} trailingVisual={SearchIcon} loading={loading} /></Box><Text fontSize={3} mb={2} display="block">Both visuals, position overriden</Text><Box mb={3}><TextInput loaderPosition="trailing" leadingVisual={SearchIcon} trailingVisual={SearchIcon} loading={loading} /></Box></>)}render(<WithIconAndLoadingIndicator />)
<FormControl><FormControl.Label>Icon action</FormControl.Label><TextInputtrailingAction={<TextInput.ActiononClick={() => {alert('clear input')}}icon={XIcon}aria-label="Clear input"sx={{color: 'fg.subtle'}}/>}/></FormControl>
<><TextInputvalidationStatus="error"aria-label="Zipcode"name="zipcode"placeholder="Error"autoComplete="postal-code"/><TextInputvalidationStatus="warning"aria-label="Zipcode"name="zipcode"placeholder="Warning"autoComplete="postal-code"/></>
<TextInput block aria-label="Zipcode" name="zipcode" placeholder="560076" autoComplete="postal-code" />
<TextInput contrast aria-label="Zipcode" name="zipcode" placeholder="Find user" autoComplete="postal-code" />
<TextInputmonospacearia-label="personal access token"name="pat"leadingVisual={CheckIcon}placeholder="github_pat_abcdefg123456789"/>
Name | Type | Default | Description |
---|---|---|---|
aria-label | string | Allows input to be accessible. | |
block | boolean | false | Creates a full-width input element |
contrast | boolean | false | Changes background color to a higher contrast color |
size | 'small' | 'medium' | 'large' | Creates a smaller or larger input than the default. | |
loading | boolean | Whether to show a loading indicator in the input | |
loaderPosition | 'auto' | 'leading' | 'trailing' | Which position to render the loading indicator
| |
leadingVisual | string | React.ComponentType | Visual positioned on the left edge inside the input | |
monospace | boolean | false | Shows input in monospace font |
trailingVisual | string | React.ComponentType | Visual positioned on the right edge inside the input | |
trailingAction | React.ReactElement<HTMLProps<HTMLButtonElement>> | A visual that renders inside the input after the typing area | |
validationStatus | 'error' | 'success' | 'warning' | Style the input to match the status | |
variant Deprecated | 'small' | 'medium' | 'large' | (Use size) Creates a smaller or larger input than the default. | |
width Deprecated | string | number | Array<string | number> | (Use sx prop) Set the width of the input | |
maxWidth Deprecated | string | number | Array<string | number> | (Use sx prop) Set the maximum width of the input | |
minWidth Deprecated | string | number | Array<string | number> | (Use sx prop) Set the minimum width of the input | |
icon Deprecated | React.ComponentType | (Use leadingVisual or trailingVisual) An Octicon React component. To be used inside of input. Positioned on the left edge. | |
sx | SystemStyleObject | Style overrides to apply to the component. See also overriding styles. | |
ref | React.RefObject<HTMLInputElement> | A ref to the element rendered by this component. | |
Additional props are passed to the <input> element. See MDN for a list of props accepted by the <input> element. |
Name | Type | Default | Description |
---|---|---|---|
aria-label | string | Text that appears in a tooltip. If an icon is passed, this is also used as the label used by assistive technologies. | |
icon | React.FunctionComponent | The icon to render inside the button | |
variant Deprecated | 'default' | 'primary' | 'invisible' | 'danger' | (Deprecated so that only the 'invisible' variant is used) Determine's the styles on a button | |
sx | SystemStyleObject | Style overrides to apply to the component. See also overriding styles. | |
ref | React.RefObject<HTMLButtonElement> | A ref to the element rendered by this component. | |
Additional props are passed to the <button> element. See MDN for a list of props accepted by the <button> element. |