Skip to content

Commit

Permalink
text area clearable
Browse files Browse the repository at this point in the history
  • Loading branch information
shahzad31 committed Jan 5, 2024
1 parent e310d71 commit 2ea9da3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src-docs/src/views/form_controls/text_area.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default () => {

return (
/* DisplayToggles wrapper for Docs only */
<DisplayToggles>
<DisplayToggles canClear>
<EuiTextArea
placeholder="Placeholder text"
aria-label="Use aria labels when no actual label is in use"
Expand Down
22 changes: 22 additions & 0 deletions src/components/form/text_area/text_area.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import classNames from 'classnames';
import { EuiFormControlLayout } from '../form_control_layout';
import { EuiValidatableControl } from '../validatable_control';
import { useFormContext } from '../eui_form_context';
import { EuiFormControlLayoutIconsProps } from '../form_control_layout/form_control_layout_icons';

export type EuiTextAreaProps = TextareaHTMLAttributes<HTMLTextAreaElement> &
CommonProps & {
Expand All @@ -25,6 +26,11 @@ export type EuiTextAreaProps = TextareaHTMLAttributes<HTMLTextAreaElement> &
*/
fullWidth?: boolean;
compressed?: boolean;
/**
* Shows a button that quickly clears any input
*/
isClearable?: boolean;
icon?: EuiFormControlLayoutIconsProps['icon'];

/**
* Which direction, if at all, should the textarea resize
Expand Down Expand Up @@ -59,6 +65,8 @@ export const EuiTextArea: FunctionComponent<EuiTextAreaProps> = (props) => {
placeholder,
resize = 'vertical',
rows,
isClearable,
icon,
...rest
} = props;

Expand All @@ -82,11 +90,25 @@ export const EuiTextArea: FunctionComponent<EuiTextAreaProps> = (props) => {
definedRows = 6;
}

const onClear = () => {
if (rest.onChange) {
rest.onChange({
target: { value: '' },
} as React.ChangeEvent<HTMLTextAreaElement>);
}
};

return (
<EuiFormControlLayout
fullWidth={fullWidth}
isLoading={isLoading}
isInvalid={isInvalid}
clear={
isClearable
? { onClick: onClear, 'data-test-subj': 'clearTextAreaButton' }
: undefined
}
icon={icon}
className="euiFormControlLayout--euiTextArea"
>
<EuiValidatableControl isInvalid={isInvalid}>
Expand Down

0 comments on commit 2ea9da3

Please sign in to comment.