Я пытаюсь управлять состоянием 2 входов отдельно.Эти 2 входа имеют анимацию плавающей метки.Когда вы сосредотачиваетесь на вводе, заполнитель переходит наверх и onBlur возвращается в исходное положение.
Сейчас у меня есть функция с именем handleFocusAndBlur
, в которой мне нужно реализовать эту логику.На данный момент поведение немного странное, так как даже когда текст присутствует только в одном входе, если вы перейдете к пустому входу, метка заполненного ввода вернется в исходное положение, что не должно быть так.
Это компонент, который я использую для этого:
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { View, TextInput, Animated } from 'react-native';
import styles from '../../styles/SigningScreenStyles';
export default class SigningInputs extends Component {
state = { isFocused: false };
componentWillMount() {
this.animatedIsFocused = new Animated.Value(0);
}
componentDidUpdate() {
const { isFocused } = this.state;
Animated.timing(this.animatedIsFocused, {
toValue: isFocused ? 1 : 0,
duration: 200,
}).start();
}
// SEE THIS FUNCTION
handleFocusAndBlur = () => {
const { usernameLength, passwordLength } = this.props;
if (usernameLength || passwordLength) {
this.setState({ isFocused: false });
} else {
this.setState({ isFocused: true });
}
};
render() {
const { secureTextEntry, onChangeText, labelText } = this.props;
const labelStyle = {
position: 'absolute',
left: 0,
top: this.animatedIsFocused.interpolate({
inputRange: [0, 1],
outputRange: [10, -10],
}),
fontSize: this.animatedIsFocused.interpolate({
inputRange: [0, 1],
outputRange: [25, 14],
}),
color: this.animatedIsFocused.interpolate({
inputRange: [0, 1],
outputRange: ['black', 'gray'],
}),
};
return (
<>
<View style={styles.inputContainer}>
<Animated.Text style={labelStyle}>{labelText}</Animated.Text>
<TextInput
style={styles.inputs}
onChangeText={onChangeText}
onFocus={this.handleFocusAndBlur}
onBlur={this.handleFocusAndBlur}
blurOnSubmit
secureTextEntry={secureTextEntry}
propsLength
/>
</View>
</>
);
}
}
SigningInputs.defaultProps = {
secureTextEntry: false,
};
SigningInputs.propTypes = {
secureTextEntry: PropTypes.oneOfType([PropTypes.bool]),
onChangeText: PropTypes.func.isRequired,
labelText: PropTypes.oneOfType([PropTypes.string]).isRequired,
usernameLength: PropTypes.oneOfType([PropTypes.string, PropTypes.number])
.isRequired,
passwordLength: PropTypes.oneOfType([PropTypes.string, PropTypes.number])
.isRequired,
};
И вот как я называю этот компонент:
const { username, password } = this.state;
<SigningInputs
onChangeText={user => this.setState({ username: user })}
labelText="User Name"
usernameLength={username.length}
/>
<SigningInputs
onChangeText={pass => this.setState({ password: pass })}
secureTextEntry
labelText="Password"
passwordLength={password.length}
/>
Когда у меня был только один вход, он былдовольно просто, но теперь с 2 входами я вижу, что мне нужно реализовать еще немного логики.
Есть ли шансы, что кто-то может взглянуть?