Text field
A text field is an input field that merchants can type into. It has a range of options and supports several text formats including numbers.
Use to allow merchants to provide text input when the expected input is short. For longer input, use the auto grow or multiline options.
import{TextField}from'@shopify/polaris';import{useState,useCallback}from'react';functionTextFieldExample(){const[value,setValue]=useState('Jaded Pixel');consthandleChange=useCallback((newValue:string)=>setValue(newValue),[],);return(<TextFieldlabel="Store name"value={value}onChange={handleChange}autoComplete="off"/>);}
Props
Want to help make this feature better? Pleaseshare your feedback.
This component defines its props in a way that our website can't automatically parse. The type definition is shown below, but it might be hard to read.
Best practices
Text fields should:
- Be clearly labeled so it’s obvious to merchants what they should enter into the field
- 被贴上“可选的”当你需要请求input that’s not required
- Only ask for information that’s really needed
- Validate input as soon as merchants have finished interacting with a field (but not before)
Autocomplete
The autocomplete attribute in aninput
field controls two types of browser behavior:
- Browser autofill: a feature that automatically populates form fields with previously-saved information, such as passwords, addresses, and credit card data.
- Autofill is an important feature for our users. Google has found that"users complete forms up to 30% faster"when using autofill.
- The WHATWG has a list of supported autofill values for the
autocomplete
attribute.Review the section "4.10.18.7 Autofill"for all the input types and their corresponding autocomplete attribute values.
- Browser autocomplete- a feature that displays previously submitted values for that field.
- When this is on for a field, a user is presented a list with previously submitted values for the input
Recommendation
Always add an autocomplete attribute and value to inputs if the type is: color, date, datetime-local, email, month, number, password, range, search, tel, text, time, url, or week.
Turning autofill/autocomplete off
即使你not want the browser to autofill a user's information, it is recommended you still have an autocomplete attribute with the value off or nope.
Unfortunately,not all browsers supportor respect autocomplete="off". This makes things challenging. Chrome, for example,has a long outstanding bugand won't add support for off for now.
Browser | Support forautocomplete="off" |
Details |
---|---|---|
Chrome | Partial | Intentionally ignoresoff value when the user uses the browser's autofill functionality.See bug. |
Safari | Partial | Ignoresoff value forusername ,email andpassword fields. |
Firefox | Partial | Ignoresoff value for login forms.See bug. |
Edge | Partial | Intentionally ignoresoff value when the user uses the browser's autofill functionality. |
Chrome does seem to turn autocomplete off when using the value nope (or any non valid string). However, we have seen some inconsistencies even with that support.
Recommendation (Chrome only)
- Turning off bothautofillandbrowser autocomplete(previously submitted values) in Chrome
- Use
autocomplete=nope
and alsomust have aname
attribute.
- Use
- Turning offbrowser autocomplete(previously submitted values) in Chrome
- If you don't have
name
attribute and the field is not a typical autofill input (address, email, etc), useautocomplete=off
.
- If you don't have
Virtual keyboard
TheinputMode
property should be set to select the appropriate virtual keyboard for the type of data expected to be entered by the user. Above are examples of different number keyboards set withinputMode
.
Content guidelines
For text field content guidelines, reference thetext fields experience页面。
Related components
- To lay out the elements in a responsive form,use the form layout component
- To describe an invalid form input with a separate validation error,use the inline error component
- It’s common touse a select componentconnected to the left or right of a text field.
Accessibility
Structure
Screen readers convey information about text fields automatically through native HTML.
- Use the
disabled
prop to add the HTMLdisabled
attribute to the text field. - Use the
readOnly
prop to add the HTMLreadonly
attribute to the text field. - If you use the
type
prop, then some assistive technologies adapt the software keyboard to the current task. This helps merchants with mobility, vision, and cognitive issues to enter information more easily.
Use theid
prop to provide a uniqueid
attribute value for the text field. If you don't provide anid
, then the component generates one automatically. All text fields need to have uniqueid
values.
Labeling
Thelabel
prop is required to convey the purpose of the checkbox to all merchants.
If there are separate visual cues that convey the purpose of the text field to sighted merchants, then the label can be visually hidden with thelabelHidden
prop.
When you provide help text via thehelpText
prop or an inline error message via theerror
prop, the help or error content is conveyed to screen reader users with thearia-describedby
attribute. This attribute causes the content to be read along with the label, either immediately or after a short delay.
Use theplaceholder
prop to provide additional instructions. However, don’t rely on placeholders alone since the content isn’t always conveyed to all merchants.
Do
- Use the label to provide instructions critical to using the text field
- Use help text and placeholder text to provide additional, non-critical instructions
Don’t
Use the placeholder to provide information that’s required to use the text field.
Keyboard support
Text fields have standard keyboard support.
- Merchants who rely on the keyboard expect to move focus to each text field using thetabkey (orshift+tabwhen tabbing backwards)
- If the
type
is set tonumber
, then merchants can use the up and down arrow keys to adjust the value typed into the field when hovering over or focusing the field to make the arrows appear - Using the
disabled
prop will prevent the text field from receive keyboard focus or inputs - The
readOnly
prop allows focus on the text field but prevents input or editing - The
inputMode
prop can be used to bring up a relevant keyboard for merchants on mobile; it’s passed down to the input as aninputmode
attribute
Automatically focusing
Although you can use theautoFocus
prop to automatically move focus to the text field, it’s generally best to avoid focusing on fields automatically. TheautoFocus
prop is set tofalse
by default and should only be used in cases where it won’t force focus to skip other controls or content of equal or greater importance.