2022-11-15 17:49:02 +01:00
import { Story, Canvas, ArgsTable } from '@storybook/addon-docs';
2022-12-01 22:48:53 +01:00
import { TextFieldBase, TextField } from '..';
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} />
2023-01-20 20:27:46 +01:00
`TextFieldSearch` accepts all [TextFieldBase](/docs/components-componentlibrary-textfieldbase--default-story#props)
2022-12-01 22:48:53 +01:00
component props
<ArgsTable of={TextFieldBase} />
2022-11-15 17:49:02 +01:00
2022-12-01 22:48:53 +01:00
### 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';
import {
SIZES,
COLORS,
BORDER_RADIUS,
} from '../../../helpers/constants/design-system';
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={{
backgroundColor: COLORS.BACKGROUND_ALTERNATIVE,
borderRadius: BORDER_RADIUS.XS,
'data-testid': 'clear-button',
}}
/>;
```