I created a basic input
, with few effects, but a simple input [type='text']
in the base.
But, I'm not able to get the value
or any property of the state
from my Input
control.
How can I expose a property from my Input
control to any other Component
?
input.js
import React from "react";
class Input extends React.Component {
constructor(props) {
super();
this.state = {
inputValue: "",
fieldActive: false,
label: props.label,
placeholder: props.placeholder,
type: props.type,
maxLength: props.maxLength,
error: false,
required: props.required
};
this.updateInputValue = this.updateInputValue.bind(this);
this.activateField = this.activateField.bind(this);
this.disableFocus = this.disableFocus.bind(this);
}
activateField() {
this.setState({
fieldActive: true
});
}
disableFocus(e) {
if (e.target.value == "") {
this.setState({
fieldActive: false
});
this.setState({
error: this.state.required
});
} else {
if (this.state.type == "email") {
this.setState({
error: !/^[a-zA-Z0-9]+@[a-zA-Z0-9]+\.[A-Za-z]+$/.test(e.target.value)
});
}
}
}
updateInputValue(e) {
this.setState({
inputValue: e.target.value
});
this.activateField(e);
e.preventDefault();
}
render() {
return (
<div className="form-group field-group">
<label
htmlFor=""
className={
this.state.fieldActive
? this.state.error
? "field-active form-label floating error"
: "field-active form-label floating"
: "form-label floating hide"
}
>
{this.props.label}
</label>
<input
className={
this.state.error
? "form-input floating-label error"
: "form-input floating-label"
}
type={this.props.type}
placeholder={this.props.placeholder}
maxLength={this.props.maxLength}
value={this.state.inputValue}
name={this.props.id}
id={this.props.id}
autoComplete="off"
onFocus={this.activateField}
onBlur={this.disableFocus}
onChange={this.updateInputValue}
/>
</div>
);
}
}
export default Input;
MyView.js
<Input
label="Firstname"
placeholder="Firstname"
id="firstname"
type="text"
required='true'
value=''
maxLength='20'
onChange={this.handleChange}
/>
</div>