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-search
2023-02-02 20:15:26 +00:00
..
__snapshots__ icon audit, remove xxs icon size (#17089) 2023-01-24 09:39:46 -08:00
index.js Adding TextFieldSearch component (#16296) 2022-11-15 08:49:02 -08:00
README.mdx Added storybook check to CI (#17092) 2023-01-21 00:57:46 +05:30
text-field-search.js feature: migrate design-system.ts (#17518) 2023-02-02 20:15:26 +00:00
text-field-search.scss Adding TextFieldSearch component (#16296) 2022-11-15 08:49:02 -08:00
text-field-search.stories.js feature: migrate design-system.ts (#17518) 2023-02-02 20:15:26 +00:00
text-field-search.test.js TextFieldSearch house keeping updates (#16669) 2022-12-01 13:48:53 -08:00

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

import { TextFieldBase, TextField } from '..';

import { TextFieldSearch } from './text-field-search';

# TextFieldSearch

The `TextFieldSearch` allows users to enter text to search

<Canvas>
  <Story id="components-componentlibrary-textfieldsearch--default-story" />
</Canvas>

## Props

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

<ArgsTable of={TextFieldSearch} />

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

<ArgsTable of={TextField} />

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

<ArgsTable of={TextFieldBase} />

### Clear Button On Click

`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.

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

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

<Canvas>
  <Story id="components-componentlibrary-textfieldsearch--clear-button-on-click" />
</Canvas>

```jsx
import { TextFieldSearch } from '../../component-library';

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>
  <Story id="components-componentlibrary-textfieldsearch--clear-button-props" />
</Canvas>

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

import { TextFieldSearch } from '../../component-library';

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',
  }}
/>;
```