Skip to content

Commit

Permalink
refactor: simplify focus logic
Browse files Browse the repository at this point in the history
  • Loading branch information
benjycui committed Feb 28, 2017
1 parent 6893eed commit 04a5d13
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 21 deletions.
7 changes: 3 additions & 4 deletions assets/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
line-height: 1;
vertical-align: middle;

&:hover, &-focused {
.@{checkboxInnerPrefixCls} {
border-color: #bcbcbc;
}
&:hover .@{checkboxInnerPrefixCls},
&-input:focus + .@{checkboxInnerPrefixCls} {
border-color: #3dbcf6;
}

&-inner {
Expand Down
32 changes: 15 additions & 17 deletions src/Checkbox.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export default class Checkbox extends React.Component {

this.state = {
checked,
focus: false,
};
}

Expand All @@ -52,16 +51,6 @@ export default class Checkbox extends React.Component {
return PureRenderMixin.shouldComponentUpdate.apply(this, args);
}

handleFocus = (e) => {
this.setState({ focus: true });
this.props.onFocus(e);
};

handleBlur = (e) => {
this.setState({ focus: false });
this.props.onBlur(e);
};

handleChange = (e) => {
const { props } = this;
if (props.disabled) {
Expand All @@ -88,18 +77,26 @@ export default class Checkbox extends React.Component {

render() {
const {
prefixCls, className, style, name, type, disabled, readOnly, tabIndex, onClick,
prefixCls,
className,
style,
name,
type,
disabled,
readOnly,
tabIndex,
onClick,
onFocus,
onBlur,
} = this.props;
const { checked, focus } = this.state;
const { checked } = this.state;
const classString = classNames(prefixCls, className, {
[`${prefixCls}-checked`]: checked,
[`${prefixCls}-focused`]: focus,
[`${prefixCls}-disabled`]: disabled,
});

return (
<span className={classString} style={style}>
<span className={`${prefixCls}-inner`} />
<input
name={name}
type={type}
Expand All @@ -109,10 +106,11 @@ export default class Checkbox extends React.Component {
className={`${prefixCls}-input`}
checked={!!checked}
onClick={onClick}
onFocus={this.handleFocus}
onBlur={this.handleBlur}
onFocus={onFocus}
onBlur={onBlur}
onChange={this.handleChange}
/>
<span className={`${prefixCls}-inner`} />
</span>
);
}
Expand Down

0 comments on commit 04a5d13

Please sign in to comment.