1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-22 19:26:13 +02:00
metamask-extension/ui/components/component-library/text-field
George Marshall 5ee7da6afe
Adding FormTextField component (#16497)
* Adding FormTextField component

* Adding to index.js

* Adding id, label and helptext stories

* Removing unneeded htmlFor and fixing accessibility on helpText story

* Fixing issues with review suggestions

* Fixing lint issue

* Adding snapshot test
2022-11-22 13:04:27 -08:00
..
index.js Adding TextField component (#16105) 2022-10-25 15:23:18 -07:00
README.mdx Component library adding global index and other housekeeping (#16441) 2022-11-16 14:15:08 -08:00
text-field.constants.js Adding TextField component (#16105) 2022-10-25 15:23:18 -07:00
text-field.js Adding TextFieldSearch component (#16296) 2022-11-15 08:49:02 -08:00
text-field.scss Adding TextField component (#16105) 2022-10-25 15:23:18 -07:00
text-field.stories.js TextField component updates (#16424) 2022-11-10 11:13:15 -08:00
text-field.test.js Adding FormTextField component (#16497) 2022-11-22 13:04:27 -08:00

import { Story, Canvas, ArgsTable } from '@storybook/addon-docs';

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

The `TextField` accepts all props below as well as all [Box](/docs/ui-components-ui-box-box-stories-js--default-story#props) and [TextFieldBase](/docs/ui-components-component-library-text-field-base-text-field-base-stories-js--default-story#props) component props

<ArgsTable of={TextField} />

### Show Clear Button

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.

The clear button uses [ButtonIcon](/docs/ui-components-component-library-button-icon-button-icon-stories-js--default-story) and accepts all props from that component.

**NOTE: The `showClearButton` only works with a controlled input.**

<Canvas>
  <Story id="ui-components-component-library-text-field-text-field-stories-js--show-clear-button" />
</Canvas>

```jsx
import { TextField } from '../../ui/components/component-library';

const [value, setValue] = useState('show clear');

const handleOnChange = (e) => {
  setValue(e.target.value);
};

const handleOnClear = () => {
  setValue('');
};

<TextField
  placeholder="Enter text to show clear"
  value={value}
  onChange={handleOnChange}
  showClearButton
  clearButtonOnClick={handleOnClear}
/>;
```

### Clear Button Props

Use the `clearButtonProps` to access other props of the clear button.

<Canvas>
  <Story id="ui-components-component-library-text-field-text-field-stories-js--clear-button-props" />
</Canvas>

```jsx
import React, { useState } from 'react';
import {
  SIZES,
  COLORS,
  BORDER_RADIUS,
} from '../../../helpers/constants/design-system';

import { TextField } from '../../ui/components/component-library';

const [value, setValue] = useState('show clear');

const handleOnChange = (e) => {
  setValue(e.target.value);
};

const handleOnClear = () => {
  setValue('');
};

<TextField
  placeholder="Enter text to show clear"
  value={value}
  onChange={handleOnChange}
  showClearButton
  clearButtonOnClick={handleOnClear}
  clearButtonProps={{
    backgroundColor: COLORS.BACKGROUND_ALTERNATIVE,
    borderRadius: BORDER_RADIUS.XS,
    'data-testid': 'clear-button',
  }}
/>;
```