Skip to content
This repository has been archived by the owner on Jan 31, 2024. It is now read-only.

Commit

Permalink
0.5.3 - Add onFocus and onBlur on TokenField
Browse files Browse the repository at this point in the history
  • Loading branch information
jducro committed Jul 17, 2018
1 parent a5e8c4a commit 9cf7d1c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 31 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.5.3 - 2018-07-17

* Add onFocus and onBlur on TokenField

## 0.5.2 - 2018-07-17

* Fix Styling
Expand Down
2 changes: 1 addition & 1 deletion npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@deskpro/token-field",
"version": "0.5.2",
"version": "0.5.3",
"description": "",
"main": "dist/index.js",
"private": false,
Expand Down
56 changes: 27 additions & 29 deletions src/Components/TokenField.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,23 @@ export default class TokenField extends React.Component {
value: PropTypes.array.isRequired,
zIndex: PropTypes.number,
showTokensOnFocus: PropTypes.bool,
blurTimeout: PropTypes.number,
};

static defaultProps = {
onChange() {},
onFocus() {},
onBlur() {},
zIndex: 100,
blurTimeout: 300,
showTokensOnFocus: false,
};

constructor(props) {
super(props);
this.state = {
value: props.value,
value: props.value,
focused: false,
blurred: true,
};

this.inputs = [];
Expand All @@ -64,6 +65,30 @@ export default class TokenField extends React.Component {
}
}

onFocus = () => {
if (!this.state.focused) {
this.props.onFocus();
this.setState({
focused: true
});
}
if (this.pendingBlur) {
window.clearTimeout(this.pendingBlur);
}
};

onBlur = () => {
if (this.pendingBlur) {
window.clearTimeout(this.pendingBlur);
}
this.pendingBlur = window.setTimeout(() => {
this.setState({
focused: false
});
this.props.onBlur();
}, this.props.blurTimeout);
};

getInputField = (key, value) => (
<TokenFieldInput
ref={(c) => { this.inputs[key] = c; }}
Expand All @@ -83,33 +108,6 @@ export default class TokenField extends React.Component {
/>
);

onFocus = () => {
console.log('focus');
if (this.state.blurred) {
this.props.onFocus();
}
this.setState({
focused: true
});
};

onBlur = () => {
console.log('blur');
this.setState({
focused: false
});
setTimeout(() => {
console.log('check_blur');
if (!this.state.focused) {
this.props.onBlur();
} else {
this.setState({
focused: true
});
}
}, 10);
};

addInputAndFocus = (key) => {
const { value } = this.state;
const inputKey = (!key) ? value.length : key;
Expand Down

0 comments on commit 9cf7d1c

Please sign in to comment.