1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-26 12:29:06 +01:00
metamask-extension/ui/components/component-library/text-field
2023-02-14 11:33:04 -06:00
..
__snapshots__ TextField house keeping 🧹 (#16668) 2022-12-06 11:52:03 -08:00
index.js Adding TextField component (#16105) 2022-10-25 15:23:18 -07:00
README.mdx fix broken stories and readme from TS update (#17703) 2023-02-14 11:33:04 -06:00
text-field.constants.js Adding TextField component (#16105) 2022-10-25 15:23:18 -07:00
text-field.js feature: migrate design-system.ts (#17518) 2023-02-02 20:15:26 +00:00
text-field.stories.js feature: migrate design-system.ts (#17518) 2023-02-02 20:15:26 +00:00
text-field.test.js TextField house keeping 🧹 (#16668) 2022-12-06 11:52:03 -08:00

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

import { TextFieldBase } from '../text-field-base';

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="components-componentlibrary-textfield--default-story" />
</Canvas>

## Props

The `TextField` accepts all props below as well as all [Box](/docs/components-ui-box--default-story#props) component props

<ArgsTable of={TextField} />

`TextField` accepts all [TextFieldBase](/docs/components-componentlibrary-textfieldbase--default-story#props)
component props

<ArgsTable of={TextFieldBase} />

### 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. Otherwise the clear button will not do anything.

The clear button uses [ButtonIcon](/docs/components-componentlibrary-buttonicon--default-story) and accepts all `ButtonIcon` props.

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

<Canvas>
  <Story id="components-componentlibrary-textfield--show-clear-button" />
</Canvas>

```jsx
import { TextField } from '../../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="components-componentlibrary-textfield--clear-button-props" />
</Canvas>

```jsx
import React, { useState } from 'react';
import { Color, BorderRadius } from '../../../helpers/constants/design-system';

import { TextField } from '../../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: Color.backgroundAlternative,
    borderRadius: BorderRadius.XS,
    'data-testid': 'clear-button',
  }}
/>;
```