Skip to content

Commit

Permalink
feat(textfield-after): add is clearable prop
Browse files Browse the repository at this point in the history
  • Loading branch information
juanigalan91 committed Oct 23, 2019
1 parent c0c5f51 commit 89b6097
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
10 changes: 10 additions & 0 deletions demo/react/doc/product/components/text-field.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@ There are four options: _helper text_, _placeholder_, _icon_ and _with buttons_.
};
```

### Clearable

```javascript jsx withThemeSwitcher disableGrid
(theme) => {
const [value, setValue] = useState('');

return <TextField isClearable value={value} onChange={setValue} theme={theme} />;
};
```

### With Buttons

#### Before button
Expand Down
27 changes: 25 additions & 2 deletions src/components/text-field/react/TextField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import classNames from 'classnames';
import get from 'lodash/get';
import uuid from 'uuid/v4';

import { Icon, Size, Theme } from 'LumX';
import { Emphasis, Icon, IconButton, Size, Theme } from 'LumX';
import { CSS_PREFIX } from 'LumX/core/constants';
import { COMPONENT_PREFIX } from 'LumX/core/react/constants';
import { IGenericProps, getRootClassName } from 'LumX/core/react/utils';
import { handleBasicClasses } from 'LumX/core/utils';
import { mdiAlertCircle, mdiCheckCircle } from 'LumX/icons';
import { mdiAlertCircle, mdiCheckCircle, mdiClose } from 'LumX/icons';

/////////////////////////////

Expand Down Expand Up @@ -46,6 +46,9 @@ interface ITextFieldProps extends IGenericProps {
/** Whether the text field is displayed with valid style or not. */
isValid?: boolean;

/** Whether the text field shows a cross to clear its content or not. */
isClearable?: boolean;

/** Text field label displayed in a label tag. */
label?: string;

Expand Down Expand Up @@ -104,6 +107,7 @@ const CLASSNAME: string = getRootClassName(COMPONENT_NAME);
*/
const DEFAULT_PROPS: Partial<TextFieldProps> = {
hasError: false,
isClearable: false,
isDisabled: false,
isValid: false,
theme: Theme.light,
Expand Down Expand Up @@ -205,6 +209,7 @@ const TextField: React.FC<TextFieldProps> = (props: TextFieldProps): ReactElemen
icon,
id = uuid(),
isDisabled,
isClearable = DEFAULT_PROPS.isClearable,
isValid,
label,
onChange,
Expand All @@ -221,6 +226,20 @@ const TextField: React.FC<TextFieldProps> = (props: TextFieldProps): ReactElemen
} = props;
const [isFocus, setFocus] = useState(false);

/**
* Function triggered when the Clear Button is clicked.
* The idea is to execute the `onChange` callback with an empty string
* and remove focus from the clear button.
* @param evt On clear event.
*/
const onClear = (evt: React.ChangeEvent): void => {
evt.nativeEvent.preventDefault();
evt.nativeEvent.stopPropagation();
(evt.currentTarget as HTMLElement).blur();

onChange('');
};

return (
<div
className={classNames(
Expand Down Expand Up @@ -279,6 +298,10 @@ const TextField: React.FC<TextFieldProps> = (props: TextFieldProps): ReactElemen
})}
</div>

{isClearable && (
<IconButton icon={mdiClose} emphasis={Emphasis.low} size={Size.s} theme={theme} onClick={onClear} />
)}

{(isValid || hasError) && (
<Icon
className={`${CLASSNAME}__input-validity`}
Expand Down

0 comments on commit 89b6097

Please sign in to comment.