2022-10-26 00:23:18 +02:00
import { Story, Canvas, ArgsTable } from '@storybook/addon-docs';
2022-12-06 20:52:03 +01:00
import { TextFieldBase } from '../text-field-base';
2022-10-26 00:23:18 +02:00
import { TextField } from './text-field';
# TextField
The `TextField` component lets users enter and edit text as well as adding a show clear button option. It wraps `TextFieldBase` and functions only as a controlled input.
<Canvas>
<Story id="ui-components-component-library-text-field-text-field-stories-js--default-story" />
</Canvas>
## Props
2022-12-06 20:52:03 +01:00
The `TextField` accepts all props below as well as all [Box](/docs/ui-components-ui-box-box-stories-js--default-story#props) component props
2022-10-26 00:23:18 +02:00
<ArgsTable of={TextField} />
2022-12-06 20:52:03 +01:00
`TextField` accepts all [TextFieldBase](/docs/ui-components-component-library-text-field-base-text-field-base-stories-js--default-story#props)
component props
<ArgsTable of={TextFieldBase} />
2022-11-10 20:13:15 +01:00
### Show Clear Button
2022-10-26 00:23:18 +02:00
2022-12-06 20:52:03 +01:00
Use the `showClearButton` prop to display a clear button when `TextField` has a value. Use the `clearButtonOnClick` prop to pass an `onClick` event handler to clear the value of the input. Otherwise the clear button will not do anything.
2022-11-10 20:13:15 +01:00
2022-12-06 20:52:03 +01:00
The clear button uses [ButtonIcon](/docs/ui-components-component-library-button-icon-button-icon-stories-js--default-story) and accepts all `ButtonIcon` props.
2022-11-10 20:13:15 +01:00
**NOTE: The `showClearButton` only works with a controlled input.**
2022-10-26 00:23:18 +02:00
<Canvas>
2022-11-10 20:13:15 +01:00
<Story id="ui-components-component-library-text-field-text-field-stories-js--show-clear-button" />
2022-10-26 00:23:18 +02:00
</Canvas>
```jsx
2022-12-06 20:52:03 +01:00
import { TextField } from '../../component-library';
2022-10-26 00:23:18 +02:00
2022-11-10 20:13:15 +01:00
const [value, setValue] = useState('show clear');
2022-10-26 00:23:18 +02:00
2022-11-10 20:13:15 +01:00
const handleOnChange = (e) => {
setValue(e.target.value);
};
2022-10-26 00:23:18 +02:00
2022-11-10 20:13:15 +01:00
const handleOnClear = () => {
setValue('');
};
2022-10-26 00:23:18 +02:00
2022-11-10 20:13:15 +01:00
<TextField
placeholder="Enter text to show clear"
value={value}
onChange={handleOnChange}
showClearButton
clearButtonOnClick={handleOnClear}
/>;
2022-10-26 00:23:18 +02:00
```
2022-11-10 20:13:15 +01:00
### Clear Button Props
2022-10-26 00:23:18 +02:00
2022-11-10 20:13:15 +01:00
Use the `clearButtonProps` to access other props of the clear button.
2022-10-26 00:23:18 +02:00
<Canvas>
2022-11-10 20:13:15 +01:00
<Story id="ui-components-component-library-text-field-text-field-stories-js--clear-button-props" />
2022-10-26 00:23:18 +02:00
</Canvas>
```jsx
2022-11-10 20:13:15 +01:00
import React, { useState } from 'react';
2022-10-26 00:23:18 +02:00
import {
SIZES,
COLORS,
BORDER_RADIUS,
} from '../../../helpers/constants/design-system';
2022-11-10 20:13:15 +01:00
2022-12-06 20:52:03 +01:00
import { TextField } from '../../component-library';
2022-10-26 00:23:18 +02:00
2022-11-10 20:13:15 +01:00
const [value, setValue] = useState('show clear');
const handleOnChange = (e) => {
setValue(e.target.value);
};
const handleOnClear = () => {
setValue('');
};
2022-10-26 00:23:18 +02:00
<TextField
2022-11-10 20:13:15 +01:00
placeholder="Enter text to show clear"
value={value}
onChange={handleOnChange}
showClearButton
clearButtonOnClick={handleOnClear}
2022-10-26 00:23:18 +02:00
clearButtonProps={{
backgroundColor: COLORS.BACKGROUND_ALTERNATIVE,
borderRadius: BORDER_RADIUS.XS,
'data-testid': 'clear-button',
}}
/>;
```