Text Input
Form inputs with sizes and style variations
TextInput with an icon and clear button
TextInput is a controlled component. This means that the visible text will always match the contents of the value prop.
In this example, notice how value is stored in a useState variable. The onChange function will set the new value when the user enters or removes a character in the input.
function ClearableTextInput() { const [value, setValue] = React.useState('1355 Market St.'); return ( <div> <Label for="example-1">Street address</Label> <TextInput id="example-1" value={value} placeholder="Enter an address" innerLeft={ <TextInputIcon> <ContentModifierMapPinMedium /> </TextInputIcon> } innerRight={<TextInputClearButton onClick={() => setValue('')} />} onChange={setValue} /> </div> ); }
Text Input sizes
Text Inputs are available in two sizes: small and large. large is the default size.
Small input
function TextInputExample() { const [value, setValue] = React.useState(undefined); return ( <TextInput size="small" value={value} placeholder="example@example.com" onChange={setValue} /> ); }
Large input
function TextInputExample() { const [value, setValue] = React.useState(undefined); return ( <TextInput size="large" value={value} placeholder="example@example.com" onChange={setValue} /> ); }
Disabled inputs
The isDisabled prop disables the TextInput visually and functionally.
function TextInputExample() { const [value, setValue] = React.useState(undefined); return ( <TextInput isDisabled value={value} placeholder="example@example.com" onChange={setValue} /> ); }
Text Input with an error
The hasError prop only changes the input’s color. It should be used alongside an error message that helps users advance through the form.
function TextInputExample() { const [value, setValue] = React.useState(undefined); return ( <TextInput hasError value={value} placeholder="example@example.com" onChange={setValue} /> ); }
Button attached to an input
You can use the InputRow component to attach a Button to a Text Input.
function TextInputExample() { const [value, setValue] = React.useState(undefined); return ( <InputRow widthRatios={[1, null]}> <TextInput placeholder="Enter a zip code" onChange={setValue} value={value} /> <Button>Find a pro</Button> </InputRow> ); }
Date inputs
Because of browser UI inconsistencies we do not use type='date' and instead suggest using a separate text input or select elements to gather this information from users.
Props
TextInput
idAdds a HTML
idattribute to the input. This is used for linking the HTML with a Label.TypestringisDisabledVisually and functionally disable the input.
TypebooleanDefaultfalseisReadOnlyAdds
readonlyHTML attribute, allowing users to select (but not modify) the input.TypebooleanDefaultfalseisRequiredAdds the
requiredHTML attribute.TypebooleanDefaultfalsepatternA regular expression that the
<input>element's value is checked against when submitting a form.TypestringmaxLengthThe maximum number of characters that a user can enter.
onChangewill not fire if a user enters a character that exceedsmaxLength.TypenumbermaxThe maximum value that can be entered. Valid when
type=number.TypenumberminThe minimum value that can be entered. Valid when
type=number.TypenumberstepThe granularity of values that can be entered. Valid when
type=number.TypenumberhasErrorMakes the text and border color red.
TypebooleanDefaultfalseplaceholderText that appears within the input when there is no
value.TypestringsizeControls the height and padding of the input.
Type'small' | 'large'Default'large'typeSets the
typeattribute on the input element.Type'email' | 'password' | 'text' | 'search' | 'tel' | 'number'Default'text'inputModeA proposed specification that enables specification of virtual keyboard type in Chrome. Currently only supported in Chrome and Android.
Type'numeric'nameThe HTML
nameattribute that will be pased to the input. It is required if working with a form that uses<form action="" method="">to submit data to a server.TypestringvalueThe current value of the input.
Typestring | numberDefault''innerLeftContent that appears within the input on the left.
TypeReact.ReactNodeinnerRightContent that appears within the input on the right.
TypeReact.ReactNodeonChangeThe function that is called when the input value changes.
It receives two arguments:
onChange(newValue, event).The consumer of this component should use that data to update the
valueprop passed in to this component.Type(value: string, event: React.ChangeEvent<HTMLInputElement>) => voidDefault(): void => {}onClickFunction that fires when you click into the input.
Type(event: React.MouseEvent<HTMLInputElement, MouseEvent>) => voidDefault(): void => {}onFocusFires when the input gains focus.
Type(event: React.FocusEvent<HTMLInputElement>) => voidDefault(): void => {}onBlurFires when the input loses focus, regardless of whether the value has changed.
Type(event: React.FocusEvent<HTMLInputElement>) => voidDefault(): void => {}onKeyDownFires when a key is pressed down with the input focused.
Type(event: React.KeyboardEvent<HTMLInputElement>) => voidDefault(): void => {}onKeyUpFires when a key press is released with the input focused.
Type(event: React.KeyboardEvent<HTMLInputElement>) => voidDefault(): void => {}onKeyPressdeprecatedFires when a valid key input is made.
Note:This event is deprecated in the DOM APIs. Use
onKeyDownoronKeyUpinstead.Type(event: React.KeyboardEvent<HTMLInputElement>) => voidDefault(): void => {}shouldFocusOnPageLoadThis tells the browser to give the input focus when the page is loaded. This can only be used once on a page.
TypebooleanDefaultfalsedataTestA selector hook into the React component for use in automated testing environments. It is applied internally to the
<input />element.TypestringautoCompleteThis tells the browser whether to attempt autocompletion of the input. Supports all values.
TypeReact.InputHTMLAttributes<HTMLInputElement>['autoComplete']enterKeyHintThis tells the browser what action label (or icon) to present for the enter key on virtual keyboards. See MDN for more information.
TypeReact.InputHTMLAttributes<HTMLInputElement>['enterKeyHint']
TextInputIcon
childrenrequiredAn icon component from Thumbprint Icons. You should pair "Medium" icons with
largeinputs and "Small" icons withsmallinputs.TypeReact.ReactNodecolorSet the icon color with a color from Thumbprint Tokens.
TypestringDefault'inherit'
TextInputClearButton
onClickrequiredType() => void