2022-11-15 17:49:02 +01:00
import { Story, Canvas, ArgsTable } from '@storybook/addon-docs';
2023-02-16 00:43:51 +01:00
import { TextField } from '..';
2022-12-01 22:48:53 +01:00
2022-11-15 17:49:02 +01:00
import { TextFieldSearch } from './text-field-search';
# TextFieldSearch
2022-12-01 22:48:53 +01:00
The `TextFieldSearch` allows users to enter text to search
2022-11-15 17:49:02 +01:00
<Canvas>
2023-01-20 20:27:46 +01:00
<Story id="components-componentlibrary-textfieldsearch--default-story" />
2022-11-15 17:49:02 +01:00
</Canvas>
## Props
2023-01-20 20:27:46 +01:00
The `TextFieldSearch` accepts all props below as well as all [Box](/docs/components-ui-box--default-story#props) component props
2022-11-15 17:49:02 +01:00
<ArgsTable of={TextFieldSearch} />
2023-01-20 20:27:46 +01:00
`TextFieldSearch` accepts all [TextField](/docs/components-componentlibrary-textfield--default-story#props)
2022-12-01 22:48:53 +01:00
component props
<ArgsTable of={TextField} />
### Clear Button On Click
2022-11-15 17:49:02 +01:00
2022-12-01 22:48:53 +01:00
`TextFieldSearch` displays a clear button when text is entered into the input. Use the `clearButtonOnClick` prop to pass an `onClick` event handler to clear the value of the input. To hide the clear button, pass `false` to the `showClearButton` prop.
2022-11-15 17:49:02 +01:00
2023-01-20 20:27:46 +01:00
The clear button uses [ButtonIcon](/docs/components-componentlibrary-buttonicon--default-story) and accepts all props from that component.
2022-11-15 17:49:02 +01:00
**NOTE: The `showClearButton` only works with a controlled input.**
<Canvas>
2023-01-20 20:27:46 +01:00
<Story id="components-componentlibrary-textfieldsearch--clear-button-on-click" />
2022-11-15 17:49:02 +01:00
</Canvas>
```jsx
2022-12-01 22:48:53 +01:00
import { TextFieldSearch } from '../../component-library';
2022-11-15 17:49:02 +01:00
const [value, setValue] = useState('show clear');
const handleOnChange = (e) => {
setValue(e.target.value);
};
const handleOnClear = () => {
setValue('');
};
<TextFieldSearch
placeholder="Enter text to show clear"
value={value}
onChange={handleOnChange}
clearButtonOnClick={handleOnClear}
/>;
```
### Clear Button Props
Use the `clearButtonProps` to access other props of the clear button.
<Canvas>
2023-01-20 20:27:46 +01:00
<Story id="components-componentlibrary-textfieldsearch--clear-button-props" />
2022-11-15 17:49:02 +01:00
</Canvas>
```jsx
import React, { useState } from 'react';
2023-02-14 18:33:04 +01:00
import { Color, BorderRadius } from '../../../helpers/constants/design-system';
2022-11-15 17:49:02 +01:00
2022-12-01 22:48:53 +01:00
import { TextFieldSearch } from '../../component-library';
2022-11-15 17:49:02 +01:00
const [value, setValue] = useState('show clear');
const handleOnChange = (e) => {
setValue(e.target.value);
};
const handleOnClear = () => {
setValue('');
};
<TextFieldSearch
value={value}
onChange={handleOnChange}
clearButtonOnClick={handleOnClear}
clearButtonProps={{
2023-02-14 18:33:04 +01:00
backgroundColor: Color.backgroundAlternative,
borderRadius: BorderRadius.XS,
2022-11-15 17:49:02 +01:00
'data-testid': 'clear-button',
}}
/>;
```