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>
2023-01-20 20:27:46 +01:00
<Story id="components-componentlibrary-textfield--default-story" />
2022-10-26 00:23:18 +02:00
</Canvas>
## Props
2023-01-20 20:27:46 +01:00
The `TextField` accepts all props below as well as all [Box](/docs/components-ui-box--default-story#props) component props
2022-10-26 00:23:18 +02:00
<ArgsTable of={TextField} />
2023-01-20 20:27:46 +01:00
`TextField` accepts all [TextFieldBase](/docs/components-componentlibrary-textfieldbase--default-story#props)
2022-12-06 20:52:03 +01:00
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
2023-01-20 20:27:46 +01:00
The clear button uses [ButtonIcon](/docs/components-componentlibrary-buttonicon--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>
2023-01-20 20:27:46 +01:00
<Story id="components-componentlibrary-textfield--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>
2023-01-20 20:27:46 +01:00
<Story id="components-componentlibrary-textfield--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';
2023-02-14 18:33:04 +01:00
import { Color, BorderRadius } 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={{
2023-02-14 18:33:04 +01:00
backgroundColor: Color.backgroundAlternative,
borderRadius: BorderRadius.XS,
2022-10-26 00:23:18 +02:00
'data-testid': 'clear-button',
}}
/>;
```