Skip to content

Commit

Permalink
fga-eps-mds#166 - Initial state for working generic field
Browse files Browse the repository at this point in the history
  • Loading branch information
matheusoliveira30 committed Apr 12, 2018
1 parent 24849b0 commit c7ed599
Showing 1 changed file with 26 additions and 20 deletions.
46 changes: 26 additions & 20 deletions src/components/GenericField.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,27 @@ export default class GenericField extends Component {
super(props);

this.state = {
styleInUse: styles.InputFieldStyle,
errorTextArea: <Text />,
text: '',
validateMessage: -1,
validValue: -1,
};
}

handleInput(newText) {
this.setState({
text: newText.trim(),
},
// Callback from setState
() => { this.validateText(this.state.text, this.props.validorRegex); },
);
this.setState({ text: newText.trim() }, () => {
this.validateText(this.state.text, this.props.validorRegex);
});
}

handleUpdate() {
if (this.state.validValue) {
this.setState({ errorTextArea: <Text /> });
this.setState({ styleInUse: [styles.InputFieldStyle, { borderColor: '#80FF80', borderWidth: 2 }] });
} else {
this.setState({ errorTextArea: <Text>{this.props.errorMessage}</Text> });
this.setState({ styleInUse: [styles.InputFieldStyle, { borderColor: '#FF9999', borderWidth: 2 }] });
}
}

validateText(text, regexTest) {
Expand All @@ -33,38 +42,35 @@ export default class GenericField extends Component {

if (isValid) {
console.warn('Valido');
this.setState({ validateMessage: true });
this.setState({ validValue: true }, () => {
this.handleUpdate();
});
} else {
console.warn('Invalido');
this.setState({ validateMessage: false });
this.setState({ validValue: false }, () => {
this.handleUpdate();
});
}
}

render() {
// Provavelmente mudar isso para um Style diferente. User uma linha vermelha, nao sei.
// Não usar uma msg.
let p;
if (this.state.validateMessage || this.state.validateMessage === -1) {
p = <Text />;
} else {
p = <Text>{this.props.errorMessage}</Text>;
}

return (
<View>
{p}
<Text> {this.props.header.toUpperCase()} </Text>
<TextInput
style={styles.InputFieldStyle}
style={this.state.styleInUse}
placeholder={this.props.message}
value={this.state.test}
onChangeText={text => this.handleInput(text)}
/>

{this.state.errorTextArea}

</View>
);
}
}

GenericField.propTypes = {
header: PropTypes.string.isRequired,
message: PropTypes.string.isRequired,
Expand Down

0 comments on commit c7ed599

Please sign in to comment.