Props
Best practices
Place SearchField above the content the user will be searching.
Hide SearchField behind an IconButton if there is enough space for the full component.
Make the placeholder
specific. Give the user a hint about the content they're searching and/or what parameters they can use to search.
Add critical information to the placeholder
. The placeholder
text disappears once the user begins entering data and will therefore be unavailable.
Make sure SearchField is displayed wide enough to completely display common search terms.
Truncate or wrap text within SearchField.
Accessibility
Labels
SearchField should ideally have a visible label above the input using the label
prop. However, if need be, accessibilityLabel
can be used to provide screen readers with context about the SearchField.
Be sure to also specify (and localize) a string for the accessibilityClearButtonLabel
.
import { useState } from 'react'; import { Box, SearchField } from 'gestalt'; export default function SearchFieldExample() { const [searchValue, setSearchValue] = useState(''); return ( <Box height="100%" padding={4} width="100%"> <SearchField accessibilityClearButtonLabel="Clear search field" accessibilityLabel="Search all of Pinterest" id="searchFieldA11yExample" onChange={({ value }) => setSearchValue(value)} placeholder="Search and explore" value={searchValue} /> </Box> ); }
Localization
Be sure to localize all text strings. Note that localization can lengthen text by 20 to 30 percent.
Also localize value
for those cases when it can be translated.
import { useState } from 'react'; import { Box, SearchField } from 'gestalt'; export default function SearchFieldExample() { const [searchValue, setSearchValue] = useState(''); return ( <Box height="100%" padding={4} width="100%"> <SearchField accessibilityClearButtonLabel="खोज फ़ील्ड साफ़ करें" accessibilityLabel="सभी Pinterest खोजें" id="searchFieldLocalizationExample" onChange={({ value }) => setSearchValue(value)} placeholder="खोजें और एक्सप्लोर करें" value={searchValue} /> </Box> ); }
Variants
Visible label
When specified, label
adds a label above the SearchField. If label
is specified, accessibilityLabel
can be an empty string.
import { useState } from 'react'; import { Box, SearchField } from 'gestalt'; export default function SearchFieldExample() { const [searchValue, setSearchValue] = useState(''); return ( <Box height="100%" padding={4} width="100%"> <SearchField accessibilityClearButtonLabel="Clear search field" accessibilityLabel="" id="searchMessagesLabelExample" label="Search Messages" onChange={({ value }) => setSearchValue(value)} placeholder="Search by name" value={searchValue} /> </Box> ); }
Sizes
There are 2 sizes available: md
(default) and lg
.
import { useState } from 'react'; import { Box, Flex, SearchField } from 'gestalt'; export default function SearchFieldExample() { const [searchValue, setSearchValue] = useState(''); const [lgValue, setLgValue] = useState(''); return ( <Box padding={8} width="100%"> <Flex direction="column" gap={6} width="100%"> <SearchField accessibilityClearButtonLabel="Clear search field" accessibilityLabel="" id="searchfield_size_md_label" onChange={({ value }) => setSearchValue(value)} placeholder="md size placeholder" size="md" value={searchValue} /> <SearchField accessibilityClearButtonLabel="Clear search field" accessibilityLabel="" id="searchfield_size_lg_label" onChange={({ value }) => setLgValue(value)} placeholder="lg size placeholder" size="lg" value={lgValue} /> <SearchField accessibilityClearButtonLabel="Clear search field" accessibilityLabel="" id="searchfield_size_md_nolabel" label="md size label" onChange={({ value }) => setSearchValue(value)} placeholder="Size md" size="md" value={searchValue} /> <SearchField accessibilityClearButtonLabel="Clear search field" accessibilityLabel="" id="searchfield_size_lg_nolabel" label="lg size label" onChange={({ value }) => setLgValue(value)} placeholder="Size lg" size="lg" value={lgValue} /> </Flex> </Box> ); }
Error
An errorMessage
can be specified if needed.
import { useState } from 'react'; import { Box, Flex, SearchField } from 'gestalt'; export default function SearchFieldExample() { const [searchValue, setSearchValue] = useState('pepper#$%'); return ( <Box padding={8} width="100%"> <Flex direction="column" gap={6} width="100%"> <SearchField accessibilityClearButtonLabel="Clear search field" accessibilityLabel="" errorMessage="Invalid search term, please avoid special characters." id="searchMessagesError" label="Search Messages" onChange={({ value }) => setSearchValue(value)} placeholder="Search by name" size="md" value={searchValue} /> <SearchField accessibilityClearButtonLabel="Clear search field" accessibilityLabel="" errorMessage="Invalid search term, please avoid special characters." id="searchMessagesError" label="Search Messages" onChange={({ value }) => setSearchValue(value)} placeholder="Search by name" size="lg" value={searchValue} /> </Flex> </Box> ); }
Component quality checklist
Quality item | Status | Status description |
---|---|---|
Figma Library | Ready | Component is available in Figma for web and mobile web. |
Responsive Web | Ready | Component responds to changing viewport sizes in web and mobile web. |
Related
ComboBox
ComboBox allows users to filter a list when selecting an option. Choose ComboBox when the user is selecting from a finite list of options.
TextField
TextField provides an affordance to input small to medium length text. Unless the text is used to search for or filter through content, choose TextField for shorter text input.
TextArea
TextArea allows for multiline text input, suitable for longer length text. Unless the text is used to search for or filter through content, choose TextArea for longer text input.