Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to reduce copy/paste about focus/blur #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 16 additions & 32 deletions blocks/Button/Button.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {decl} from 'bem-react-core';
import React from 'react';
import ReactDom from 'react-dom';
import React, {PropTypes} from 'React';
import Focusable from '../Focusable/Focusable';
import warning from 'warning';
import ButtonText from 'e:Text';

Expand Down Expand Up @@ -41,18 +41,6 @@ export default decl({
this.setState(newState);
},

didMount() {
this.state.focused?
this._focus() :
this._blur();
},

didUpdate() {
this.state.focused?
this._focus() :
this._blur();
},

mods({ disabled, checked }) {
const { focused, hovered, pressed } = this.state;
return {
Expand All @@ -79,8 +67,6 @@ export default decl({
if(!disabled) {
res = {
...res,
onFocus : this._onFocus,
onBlur : this._onBlur,
onMouseEnter : this._onMouseEnter,
onMouseLeave : this._onMouseLeave,
onMouseDown : this._onMouseDown,
Expand All @@ -101,6 +87,14 @@ export default decl({
return res;
},

render() {
return (
<Focusable focused={!!this.state.focused} onFocus={this._onFocus} onBlur={this._onBlur}>
{ this.__base.apply(this, arguments) }
</Focusable>
);
},

content({ children, icon, text }) {
if(children) return children;
const content = [];
Expand Down Expand Up @@ -176,25 +170,15 @@ export default decl({

_onClick() {
this.props.onClick();
},

_focus() {
const domNode = ReactDom.findDOMNode(this);
document.activeElement !== domNode && domNode.focus();
},

_blur() {
const domNode = ReactDom.findDOMNode(this);
document.activeElement === domNode && domNode.blur();
}
}, {
propTypes : {
type : React.PropTypes.oneOf([undefined, 'link']),
disabled : React.PropTypes.bool,
focused : React.PropTypes.bool,
onClick : React.PropTypes.func,
onFocusChange : React.PropTypes.func,
onCheckChange : React.PropTypes.func
type : PropTypes.oneOf([undefined, 'link']),
disabled : PropTypes.bool,
focused : PropTypes.bool,
onClick : PropTypes.func,
onFocusChange : PropTypes.func,
onCheckChange : PropTypes.func
},

defaultProps : {
Expand Down
39 changes: 39 additions & 0 deletions blocks/Focusable/Focusable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import React, {Component, cloneElement, PropTypes} from 'react';
import {findDOMNode} from 'react-dom';

export default class Focusable extends Component {
render() {
const { children, onFocus, onBlur } = this.props;
return React.cloneElement(children, { onFocus, onBlur });
}

componentDidMount() {
this.props.focused?
this._focus() :
this._blur();
}

componentDidUpdate() {
this.props.focused?
this._focus() :
this._blur();
}

_focus() {
const domNode = findDOMNode(this);
document.activeElement !== domNode && domNode.focus();
}

_blur() {
const domNode = findDOMNode(this);
document.activeElement === domNode && domNode.blur();
}
}

Focusable.propTypes = {
focused : PropTypes.bool,
onFocus : PropTypes.func,
onBlur : PropTypes.func
};


44 changes: 14 additions & 30 deletions blocks/Link/Link.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {decl} from 'bem-react-core';
import React from 'react';
import ReactDom from 'react-dom';
import React, {PropTypes} from 'React';
import Focusable from '../Focusable/Focusable';
import warning from 'warning';

export default decl({
Expand Down Expand Up @@ -30,18 +30,6 @@ export default decl({
this.setState(newState);
},

didMount() {
this.state.focused?
this._focus() :
this._blur();
},

didUpdate() {
this.state.focused?
this._focus() :
this._blur();
},

mods({ disabled }) {
const { focused, hovered } = this.state;
return {
Expand Down Expand Up @@ -72,8 +60,6 @@ export default decl({
res = {
...res,
onClick : this._onClick,
onFocus : this._onFocus,
onBlur : this._onBlur,
onMouseEnter : this._onMouseEnter,
onMouseLeave : this._onMouseLeave
};
Expand All @@ -84,6 +70,14 @@ export default decl({
return res;
},

render() {
return (
<Focusable focused={!!this.state.focused} onFocus={this._onFocus} onBlur={this._onBlur}>
{ this.__base.apply(this, arguments) }
</Focusable>
);
},

_onClick(e) {
this.props.onClick(e);
},
Expand All @@ -106,23 +100,13 @@ export default decl({

_onMouseLeave() {
this.setState({ hovered : false });
},

_focus() {
const domNode = ReactDom.findDOMNode(this);
document.activeElement !== domNode && domNode.focus();
},

_blur() {
const domNode = ReactDom.findDOMNode(this);
document.activeElement === domNode && domNode.blur();
}
}, {
propTypes : {
disabled : React.PropTypes.bool,
focused : React.PropTypes.bool,
onClick : React.PropTypes.func,
onFocusChange : React.PropTypes.func
disabled : PropTypes.bool,
focused : PropTypes.bool,
onClick : PropTypes.func,
onFocusChange : PropTypes.func
},

defaultProps : {
Expand Down
4 changes: 1 addition & 3 deletions blocks/Link/Link.tests/simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ class App extends React.Component {
onClick={() => console.log('click!')}
>link</Link>
<br/>
<Link
onFocusChange={linkFocused => { this.setState({ linkFocused }); console.log('focusChange! ' + linkFocused); }}
onClick={() => console.log('click!')}
<Link onClick={() => console.log('click!')}
>link</Link>
<br/>
<Link disabled={this.state.linkDisabled} url="https://yandex.ru" target="_blank">link disabled</Link>
Expand Down
39 changes: 12 additions & 27 deletions blocks/TextInput/Control/TextInput-Control.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {decl} from 'bem-react-core';
import ReactDom from 'react-dom';
import React from 'React';
import Focusable from '../../Focusable/Focusable';

export default decl({
block : 'TextInput',
Expand All @@ -12,18 +13,6 @@ export default decl({
this._onBlur = this._onBlur.bind(this);
},

didMount() {
this.props.focused?
this._focus() :
this._blur();
},

didUpdate() {
this.props.focused?
this._focus() :
this._blur();
},

tag : 'input',

attrs({ id, name, maxLength, tabIndex, placeholder, autoComplete, type, value, disabled }) {
Expand All @@ -36,17 +25,23 @@ export default decl({
type,
value,
disabled,
onChange : this._onChange,
onBlur : this._onBlur
onChange : this._onChange
};

autoComplete === false && (res.autoComplete = 'off');

disabled || (res.onFocus = this._onFocus);

return res;
},

render() {
const { focused, onFocus, onBlur } = this.props;
return (
<Focusable focused={!!focused} onFocus={this._onFocus} onBlur={this._onBlur}>
{ this.__base.apply(this, arguments) }
</Focusable>
);
},

_onChange({ target }) {
this.props.onChange(target.value);
},
Expand All @@ -57,15 +52,5 @@ export default decl({

_onBlur() {
this.props.onFocusChange(false);
},

_focus() {
const domNode = ReactDom.findDOMNode(this);
document.activeElement !== domNode && domNode.focus();
},

_blur() {
const domNode = ReactDom.findDOMNode(this);
document.activeElement === domNode && domNode.blur();
}
});
28 changes: 14 additions & 14 deletions blocks/TextInput/TextInput.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Bem, {decl} from 'bem-react-core';
import React from 'react';
import React, {PropTypes} from 'React';
import warning from 'warning';
import TextInputControl from 'e:Control';
import 'e:Clear';
Expand Down Expand Up @@ -63,19 +63,19 @@ export default decl({
}
}, {
propTypes : {
id : React.PropTypes.string,
name : React.PropTypes.string,
value : React.PropTypes.any,
type : React.PropTypes.oneOf(['text', 'password', 'search']),
maxLength : React.PropTypes.number,
tabIndex : React.PropTypes.number,
placeholder : React.PropTypes.string,
autoComplete : React.PropTypes.bool,
hasClear : React.PropTypes.bool,
disabled : React.PropTypes.bool,
focused : React.PropTypes.bool,
onFocusChange : React.PropTypes.func,
onChange : React.PropTypes.func
id : PropTypes.string,
name : PropTypes.string,
value : PropTypes.any,
type : PropTypes.oneOf(['text', 'password', 'search']),
maxLength : PropTypes.number,
tabIndex : PropTypes.number,
placeholder : PropTypes.string,
autoComplete : PropTypes.bool,
hasClear : PropTypes.bool,
disabled : PropTypes.bool,
focused : PropTypes.bool,
onFocusChange : PropTypes.func,
onChange : PropTypes.func
},

defaultProps : {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"vow-fs": "^0.3.5",
"webpack": "^1.13.1",
"webpack-dev-server": "^1.14.1",
"webpack-bem-loader": "^0.0.1"
"webpack-bem-loader": "^0.0.2"
},
"scripts": {
"build": "webpack --config=webpack.config.js",
Expand Down