SearchField
Search allows users to quickly find content. The Search field is made up of the Text field component and an optional Button component.
Basic Usage
With an initial value
With a placeholder
With callbacks
With a custom label
With custom screenreader text
With the submit button outside the input
Advanced Usage
For needs that deviate from the basic usage above, use <SearchField.Advanced />. The children elements must contain the SearchField.Label and SearchField.Input components at a minimum.
With a custom label
With an initial value
With a placeholder
With no clear button
With no submit or clear buttons
Advanced usage with the submit button outside the input
Use class pgn__searchfield_wrapper to group input elements apart from the submit button.
Theme Variables (SCSS)#
$search-btn-variants: ("light": $primary-500,"dark": $brand-500,);$search-border-radius: 0 !default;$search-line-height: 1.5rem !default;$search-border-color: $gray-500 !default;$search-border-color-interaction: $black !default;$search-border-width: .0625rem !default;$search-border-width-interaction: .125rem !default;$search-disabled-opacity: .3 !default;$search-button-margin: .5rem !default;$input-height-search: calc(#{$input-line-height * 1em} + #{$input-padding-y * 2}) !default;$search-border-focus-color: $black !default;$search-border-focus-width: .3125rem !default;
- onSubmitfuncRequiredSpecifies a callback function for when the SearchFieldis submitted by the user. For example:<SearchField onSubmit={(value) => { console.log(value); } />
- labelstring|elementSpecifies the label to use for the input field (e.g., for i18n translations). 
- classNamestringSpecifies a custom class name. 
- onBlurfuncDefault() => {}Specifies a callback function for when the user loses focus in the SearchFieldcomponent. The default is an empty function. For example:<SearchField onBlur={event => console.log(event)} />
- onChangefuncDefault() => {}Specifies a callback function for when the value in SearchFieldis changed by the user. The default is an empty function. For example:<SearchField onChange={value => console.log(value)} />
- onClearfuncDefault() => {}Specifies a callback function for when the value in SearchFieldis cleared by the user. The default is an empty function. For example:<SearchField onClear={() => console.log('search cleared')} />
- onFocusfuncDefault() => {}Specifies a callback function for when the user focuses in the SearchFieldcomponent. The default is an empty function. For example:<SearchField onFocus={event => console.log(event)} />
- placeholderstringSpecifies the placeholder text for the input. 
- screenReaderTextshape{label:string|elementRequired,submitButton:string|elementRequired,clearButton:}string|element,Default{ label: SEARCH_FIELD_SCREEN_READER_TEXT_LABEL, submitButton: SEARCH_FIELD_SCREEN_READER_TEXT_SUBMIT_BUTTON, clearButton: SEARCH_FIELD_SCREEN_READER_TEXT_CLEAR_BUTTON, }Specifies the screenreader text for both the clear and submit buttons (e.g., for i18n translations). The default is { label: 'search', clearButton: 'clear search', searchButton: 'submit search' }.
- valuestringDefault''Specifies the initial value for the input. The default is an empty string. 
- iconsshape{submit:elementRequired,clear:}element,Default{ clear: <Icon src={Close} />, submit: <Icon src={Search} />, }Specifies the icon element(s) to use for the clear and submit buttons. The default is: { submit: import {Search} from '@edx/paragon/icons';, clear: import {Close} from '@edx/paragon/icons'. }
- formAriaLabelstringSpecifies the aria-label attribute on the form element. This is useful if you use the SearchFieldcomponent more than once on a page.
- inputPropsshape{}Default{}Props to be passed to the form input 
- variantenum'light' | 'dark'Default'light'The button style variant to use. 
- disabledboolDefaultfalseSpecifies whether the SearchFieldis disabled.
- submitButtonLocationenum'internal' | 'external'Default'internal'Controls whether the search button is internal as an icon or external as a button. 
- buttonTextstringDefault'search'Specifies a text that is displayed on the button. The submitButtonLocationprop should be set toexternal.
- childrennodeRequiredspecifies the nested child elements. At a minimum, SearchField.LabelandSearchField.Inputare required.
- onSubmitfuncRequiredspecifies a callback function for when the SearchFieldis submitted by the user. For example:<SearchField onSubmit={(value) => { console.log(value); } />
- classNamestringspecifies a custom class name. 
- onBlurfuncDefault() => {}specifies a callback function for when the user loses focus in the SearchFieldcomponent. The default is an empty function. For example:<SearchField onBlur={event => console.log(event)} />
- onChangefuncDefault() => {}specifies a callback function for when the value in SearchFieldis changed by the user. The default is an empty function. For example:<SearchField onChange={value => console.log(value)} />
- onClearfuncDefault() => {}specifies a callback function for when the value in SearchFieldis cleared by the user. The default is an empty function. For example:<SearchField onClear={() => console.log('search cleared')} />
- onFocusfuncDefault() => {}specifies a callback function for when the user focuses in the SearchFieldcomponent. The default is an empty function. For example:<SearchField onFocus={event => console.log(event)} />
- screenReaderTextshape{label:string|elementRequired,submitButton:string|elementRequired,clearButton:}string|element,Default{ label: 'search', submitButton: 'submit search', clearButton: 'clear search', }specifies the screenreader text for both the clear and submit buttons (e.g., for i18n translations). The default is { label: 'search', clearButton: 'clear search', searchButton: 'submit search' }.
- valuestringDefault''specifies the initial value for the input. The default is an empty string. 
- iconsshape{submit:elementRequired,clear:}element,Default{ clear: <Icon src={Close} />, submit: <Icon src={Search} />, }specifies the icon element(s) to use for the clear and submit buttons. 
- formAriaLabelstringspecifies the aria-label attribute on the form element. This is useful if you use the SearchFieldcomponent more than once on a page.
- disabledboolDefaultfalseSpecifies whether the SearchFieldis disabled.
- submitButtonLocationenum'internal' | 'external'Default'internal'Controls whether the search button is internal as an icon or external as a button. 
- classNamestringspecifies a custom class name. 
- placeholderstringspecifies the placeholder text for the input. 
- childrenstring|elementspecifies the label to use for the input field (e.g., for i18n translations). Note: if childrenis not provided, a screenreader-only label will be used in its placed based on thescreenReaderText.labelprop forSearchField.Advanced.
- variantenum'light' | 'dark'Default'light'The button style variant to use. 
- submitButtonLocationenum'internal' | 'external'Default'internal'Controls whether the search button is internal as an icon or external as a button. 
- buttonTextstringDefault'Search'Specifies a text that is displayed on the button. The submitButtonLocationprop should be set toexternal.