Skip to content

Commit

Permalink
feat(Input): clear button
Browse files Browse the repository at this point in the history
  • Loading branch information
zouxuoz committed Dec 28, 2018
1 parent 9233991 commit 8fc589a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
20 changes: 19 additions & 1 deletion src/components/Input/Input.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import React, { PureComponent } from 'react';
import fp from 'lodash/fp';
import InputMask from 'react-input-mask';
import { InputWrapperTag, InputTag, InputIndicatorTag, InputRightIconTag, InputLeftIconTag } from './Input.theme';

import { Icon } from '../Icon';
import { InputWrapperTag, InputTag, InputIndicatorTag, InputRightIconTag, InputLeftIconTag, InputClearButtonTag } from './Input.theme';

type InputCommonProps = {
/** field placeholder */
Expand All @@ -30,6 +32,8 @@ type InputCommonProps = {
kind?: 'bordered' | 'underline',
/** readonly */
readOnly?: boolean,
/** clearable */
clearable?: boolean,
};

type InputProps = {
Expand Down Expand Up @@ -79,6 +83,14 @@ class Input extends PureComponent<InputProps> {
}
}

onClear = () => {
const { onChange } = this.props;

if (typeof onChange === 'function') {
onChange('');
}
};

render() {
const {
align,
Expand All @@ -101,6 +113,7 @@ class Input extends PureComponent<InputProps> {
kind,
disabled,
readOnly,
clearable,
...rest
} = this.props;
const hasLeftIcon = !!leftIcon;
Expand Down Expand Up @@ -152,6 +165,11 @@ class Input extends PureComponent<InputProps> {
<If condition={ hasRightIcon }>
<InputRightIconTag tagName="div">{ rightIcon }</InputRightIconTag>
</If>
<If condition={ !!clearable && !!value }>
<InputClearButtonTag onClick={ this.onClear } tagName="div">
<Icon name="Delete" size="sm" />
</InputClearButtonTag>
</If>
</InputWrapperTag>
);
}
Expand Down
7 changes: 6 additions & 1 deletion src/components/Input/Input.stories.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';

export default (asStory) => {
asStory('Components/Input', module, (story, { Input }) => {
asStory('Components/Input', module, (story, { Input, StateContainer }) => {
story
.add('without value', () => (
<Input name="input" onChange={ () => null } />
Expand Down Expand Up @@ -35,6 +35,11 @@ export default (asStory) => {
))
.add('with center align', () => (
<Input name="input" align="center" />
))
.add('with clear button', () => (
<StateContainer value="Text">
<Input name="input" clearable />
</StateContainer>
));
});
};
12 changes: 12 additions & 0 deletions src/components/Input/Input.theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@ const InputRightIconTag = createStyledTag(`${name}RightIcon`, {
right: '8px',
});

const InputClearButtonTag = createStyledTag(`${name}ClearButton`, (props) => ({
position: 'absolute',
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
height: '100%',
right: '8px',
color: getThemeColors(props).LIGHT_GRAY1,
cursor: 'pointer',
}));

const getInputStyles = props => ({
width: props.width ? `${props.width}rem` : '100%',
outline: 'none',
Expand Down Expand Up @@ -118,5 +129,6 @@ export {
InputIndicatorTag,
InputRightIconTag,
InputLeftIconTag,
InputClearButtonTag,
theme,
};

0 comments on commit 8fc589a

Please sign in to comment.