2022-10-26 00:23:18 +02:00
import { Story, Canvas, ArgsTable } from '@storybook/addon-docs';
import { TextField } from './text-field';
# TextField
2023-02-16 00:43:51 +01:00
`TextField` lets user enter a text data into a boxed field. It can sometimes contain related information or controls.
2022-10-26 00:23:18 +02:00
<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-02-16 00:43:51 +01:00
### Size
Use the `size` prop to set the height of the `TextField`.
Possible sizes include:
- `sm` 32px
- `md` 40px
- `lg` 48px
Defaults to `md`
<Canvas>
<Story id="components-componentlibrary-textfield--size-story" />
</Canvas>
```jsx
import { Size } from '../../../helpers/constants/design-system';
import { TextField } from '../../component-library';
<TextField size={Size.SM} />
<TextField size={Size.MD} />
<TextField size={Size.LG} />
```
### Type
Use the `type` prop to change the type of input.
Possible types include:
- `text`
- `number`
- `password`
2022-12-06 20:52:03 +01:00
2023-02-16 00:43:51 +01:00
Defaults to `text`.
2022-12-06 20:52:03 +01:00
2023-02-16 00:43:51 +01:00
<Canvas>
<Story id="components-componentlibrary-textfield--type" />
</Canvas>
2022-10-26 00:23:18 +02:00
2023-02-16 00:43:51 +01:00
```jsx
import { TextField } from '../../component-library';
2022-11-10 20:13:15 +01:00
2023-02-16 00:43:51 +01:00
<TextField type="text" /> // (Default)
<TextField type="number" />
<TextField type="password" />
```
2022-11-10 20:13:15 +01:00
2023-02-16 00:43:51 +01:00
### Truncate
Use the `truncate` prop to truncate the text of the the `TextField`. Defaults to `true`.
2022-10-26 00:23:18 +02:00
<Canvas>
2023-02-16 00:43:51 +01:00
<Story id="components-componentlibrary-textfield--truncate" />
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
2023-02-16 00:43:51 +01:00
<TextField truncate />; // truncate is set to `true` by default
<TextField truncate={false} />;
```
### Start accessory End Accessory
Use the `startAccessory` and `endAccessory` props to add components such as icons or buttons to either side of the `TextField`.
2022-10-26 00:23:18 +02:00
2023-02-16 00:43:51 +01:00
<Canvas>
<Story id="components-componentlibrary-textfield--start-accessory-end-accessory" />
</Canvas>
```jsx
2023-03-06 20:30:43 +01:00
import { Color, IconColor, SIZES, DISPLAY } from '../../../helpers/constants/design-system';
2023-04-05 18:11:10 +02:00
import { Icon, IconName } from '../../component-library/deprecated'
2023-04-04 18:48:04 +02:00
import { ButtonIcon, TextField } from '../../component-library';
2023-02-16 00:43:51 +01:00
<TextField
placeholder="Search"
startAccessory={
<Icon
2023-03-06 20:30:43 +01:00
color={IconColor.iconAlternative}
2023-04-05 18:11:10 +02:00
name={IconName.Search}
2023-02-16 00:43:51 +01:00
/>
}
/>
<TextField
placeholder="Public address (0x), or ENS"
endAccessory={
<ButtonIcon
2023-04-05 18:11:10 +02:00
iconName={IconName.ScanBarcode}
2023-02-16 00:43:51 +01:00
ariaLabel="Scan QR code"
2023-03-06 20:30:43 +01:00
iconProps={{ color: IconColor.primaryDefault }}
2023-02-16 00:43:51 +01:00
/>
}
/>
<TextField
placeholder="Enter amount"
type="number"
truncate
startAccessory={<SelectTokenComponent />}
endAccessory={<TokenValueInUSDComponent />}
/>
<TextField
placeholder="Public address (0x), or ENS"
truncate
startAccessory={<AvatarAccount />}
endAccessory={
isAddressValid && (
<Icon
2023-04-05 18:11:10 +02:00
name={IconName.Check}
2023-03-06 20:30:43 +01:00
color={IconColor.successDefault}
2023-02-16 00:43:51 +01:00
/>
)
}
/>
```
### Input Ref
Use the `inputRef` prop to access the ref of the `<input />` html element of `TextField`. This is useful for focusing the input from a button or other component.
<Canvas>
<Story id="components-componentlibrary-textfield--input-ref" />
</Canvas>
```jsx
import { Button, TextField } from '../../component-library';
const inputRef = useRef(null);
const [value, setValue] = useState('');
const handleOnClick = () => {
inputRef.current.focus();
};
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
<TextField
2023-02-16 00:43:51 +01:00
inputRef={inputRef}
2022-11-10 20:13:15 +01:00
value={value}
onChange={handleOnChange}
2023-02-16 00:43:51 +01:00
/>
<Button marginLeft={1} onClick={handleOnClick}>
Edit
</Button>
2022-10-26 00:23:18 +02:00
```
2023-02-16 00:43:51 +01:00
### Input Component
Use the `InputComponent` prop change the component used for the input element. This is useful for replacing the base input with a custom input while retaining the functionality of the `TextField`.
2022-10-26 00:23:18 +02:00
2023-02-16 00:43:51 +01:00
Defaults to the [Text](/docs/components-componentlibrary-text--default-story) component
To function fully the custom component should accept the following props:
- `aria-invalid`
- `as`
- `autoComplete`
- `autoFocus`
- `backgroundColor`
- `defaultValue`
- `disabled`
- `focused`
- `id`
- `margin`
- `maxLength`
- `name`
- `onBlur`
- `onChange`
- `onFocus`
- `padding`
- `paddingLeft`
- `paddingRight`
- `placeholder`
- `readOnly`
- `ref`
- `required`
- `value`
- `variant`
- `type`
2022-10-26 00:23:18 +02:00
<Canvas>
2023-02-16 00:43:51 +01:00
<Story id="components-componentlibrary-textfield--input-component" />
2022-10-26 00:23:18 +02:00
</Canvas>
```jsx
2023-04-05 18:11:10 +02:00
import { TextField, Icon, IconName } from '../../component-library';
2023-02-16 00:43:51 +01:00
// should map the props to the custom input component
const CustomInputComponent = () => <div>{/* Custom input component */}</div>;
const TextFieldCustomInput = (args) => (
<TextField
size={SIZES.LG}
InputComponent={CustomInputComponent}
startAccessory={
2023-04-05 18:11:10 +02:00
<Icon color={IconColor.iconAlternative} name={IconName.Wallet} />
2023-02-16 00:43:51 +01:00
}
/>
);
```
### Auto Complete
Use the `autoComplete` prop to set the autocomplete html attribute. It allows the browser to predict the value based on earlier typed values.
<Canvas>
<Story id="components-componentlibrary-textfield--auto-complete" />
</Canvas>
2022-11-10 20:13:15 +01:00
2023-02-16 00:43:51 +01:00
```jsx
2022-12-06 20:52:03 +01:00
import { TextField } from '../../component-library';
2022-10-26 00:23:18 +02:00
2023-02-16 00:43:51 +01:00
<TextField type="password" autoComplete />;
```
2022-11-10 20:13:15 +01:00
2023-02-16 00:43:51 +01:00
### Auto Focus
2022-11-10 20:13:15 +01:00
2023-02-16 00:43:51 +01:00
Use the `autoFocus` prop to focus the `TextField` during the first mount
2022-11-10 20:13:15 +01:00
2023-02-16 00:43:51 +01:00
To view story see [Canvas tab](/story/components-componentlibrary-textfield--auto-complete). Removing it from docs because created annoying reading experience 😁
```jsx
import { TextField } from '../../component-library';
<TextField autoFocus />;
```
### Default Value
Use the `defaultValue` prop to set the default value of the `TextField`
<Canvas>
<Story id="components-componentlibrary-textfield--default-value" />
</Canvas>
```jsx
import { TextField } from '../../component-library';
<TextField defaultValue="default value" />;
```
### Disabled
Use the `disabled` prop to set the disabled state of the `TextField`
<Canvas>
<Story id="components-componentlibrary-textfield--disabled" />
</Canvas>
```jsx
import { TextField } from '../../component-library';
<TextField disabled />;
```
### Error
Use the `error` prop to set the error state of the `TextField`
<Canvas>
<Story id="components-componentlibrary-textfield--error-story" />
</Canvas>
```jsx
import { TextField } from '../../component-library';
<TextField error />;
```
### Max Length
Use the `maxLength` prop to set the maximum allowed input characters for the `TextField`
<Canvas>
<Story id="components-componentlibrary-textfield--max-length" />
</Canvas>
```jsx
import { TextField } from '../../component-library';
<TextField maxLength={10} />;
```
### Read Only
Use the `readOnly` prop to set the `TextField` to read only. When `readOnly` is true `TextField` will not have a focus state.
<Canvas>
<Story id="components-componentlibrary-textfield--read-only" />
</Canvas>
```jsx
import { TextField } from '../../component-library';
<TextField readOnly />;
```
### Required
Use the `required` prop to set the `TextField` to required. Currently there is no visual difference to the `TextField` when required.
<Canvas>
<Story id="components-componentlibrary-textfield--required" />
</Canvas>
```jsx
import { TextField } from '../../component-library';
// Currently no visual difference
<TextField required />;
2022-10-26 00:23:18 +02:00
```