Skip to content

Commit

Permalink
changing input active/passive and value states when it has a differen…
Browse files Browse the repository at this point in the history
…t value
  • Loading branch information
halilb committed Jul 13, 2016
1 parent 298a4c3 commit 8e5e4c8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
24 changes: 22 additions & 2 deletions lib/BaseInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,25 @@ export default class BaseInput extends Component {
};
}

componentWillReceiveProps(newProps) {
const newValue = newProps.value;

This comment has been minimized.

Copy link
@mijia

mijia Jul 25, 2016

Hi, I think there is a problem here for the uncontrolled components. In some components we don't want to control the value state, so the newProps.value would be null or undefined which will be set as the new state. We cannot input texts in such mode, have to turn every uncontrolled components to controlled components.

Thank you very much.

if (newValue !== this.state.value) {
this.setState({
value: newValue,
});

// animate input if it's active state has changed with the new value
// and input is not focused currently.
const isFocused = this.refs.input.isFocused();
if (!isFocused) {
const isActive = Boolean(newValue);
if (isActive !== this.isActive) {
this._toggle(isActive);
}
}
}
}

_onLayout(event) {
this.setState({
width: event.nativeEvent.layout.width,
Expand Down Expand Up @@ -84,10 +103,11 @@ export default class BaseInput extends Component {
this.refs.input.focus();
}

_toggle(displayed) {
_toggle(isActive) {
this.isActive = isActive;
Animated.timing(
this.state.focusedAnim, {
toValue: displayed ? 1 : 0,
toValue: isActive ? 1 : 0,
duration: this.props.animationDuration,
easing: this.props.easing,
},
Expand Down
6 changes: 3 additions & 3 deletions lib/Jiro.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ export default class Jiro extends BaseInput {
};
}

_toggle(displayed) {
const animationValue = displayed ? 1 : 0;
_toggle(isActive) {
const animationValue = isActive ? 1 : 0;
const borderPositionAnimation = Animated.timing(this.state.borderPositionAnim, {
toValue: animationValue,
eaasing: Easing.bezier(0.2, 1, 0.3, 1),
Expand All @@ -55,7 +55,7 @@ export default class Jiro extends BaseInput {
duration: 200,
});

if (displayed) {
if (isActive) {
Animated.sequence([
borderPositionAnimation,
Animated.parallel([labelPositionAnimation, borderHeightAnimation]),
Expand Down

0 comments on commit 8e5e4c8

Please sign in to comment.