Validate the Input for the react form - reactjs

NewBie in React so bear any non-technical words. I have to work on form with given code below the code is somewhat incomplete in which I have to achieve validation on each input. If any error occurs displaying it below the input
element inside the span element. on onChange handler state gets updated whereas respective validation function is called
import React from "react";
class Form extends React.component{
state = {
firstName: { value: "" },
LastName: { value: "" }
};
handleInputChange = (e, validationFun) => {
e.preventDefault();
const target = e.target;
const inputName = target.name;
const inputValue = target.value;
this.setState({
[inputName]: {
value: inputValue,
...validationFun(inputValue)
}
});
};
render() {
return (
<div>
<div>
<label>First Name</label>
<input
type="text"
name="firstName"
value={this.state.firstName.value}
onChange={event =>
this.handleInputChange(event, this.validatefirstName)
}
/>
</div>
<div>
<label>First Name</label>
<input
type="text"
name="LastName"
value={this.state.LastName.value}
onChange={event =>
this.handleInputChange(event, this.validateLastName)
}
/>
</div>
</div>
);
}
validatefirstName = firstName => {
if (!firstName) {
return {
validateStatus: "error",
errorMsg: "firstName may not be empty"
};
} else if (firstName.length > 12) {
return {
validationStatus: "error",
errorMsg: `firstName is too long 11 characters allowed.)`
};
} else {
return {
validateStatus: "success",
errorMsg: null
};
}
};
validateLastName = LastName => {
if (!LastName) {
return {
validateStatus: "error",
errorMsg: "LastName may not be empty"
};
} else if (LastName.length > 12) {
return {
validationStatus: "error",
errorMsg: `LastName is too long 11 characters allowed.)`
};
} else {
return {
validateStatus: "success",
errorMsg: null
};
}
};
};

Yep after lot work, I was able to find Solution
import React from "react";
class Form extends React.component{
state = {
firstName: { value: "" },
LastName: { value: "" }
};
handleInputChange = (e, validationFun) => {
e.preventDefault();
const target = e.target;
const inputName = target.name;
const inputValue = target.value;
this.setState({
[inputName]: {
value: inputValue,
...validationFun(inputValue)
}
});
};
render() {
return (
<div>
<div>
<label>First Name</label>
<input
type="text"
name="firstName"
value={this.state.firstName.value}
onChange={event =>
this.handleInputChange(event, this.validatefirstName)
}
/>
<span style={{ color: "red" }}>
{this.state.firstName.errorMsg}
</span>
</div>
<div>
<label>Last Name</label>
<input
type="text"
name="LastName"
value={this.state.LastName.value}
onChange={event =>
this.handleInputChange(event, this.validateLastName)
}
/>
<span style={{ color: "red" }}>
{this.state.LastName.errorMsg}
</span>
</div>
</div>
);
}
validatefirstName = firstName => {
if (!firstName) {
return {
validateStatus: "error",
errorMsg: "firstName may not be empty"
};
} else if (firstName.length > 12) {
return {
validationStatus: "error",
errorMsg: `firstName is too long 11 characters allowed.)`
};
} else {
return {
validateStatus: "success",
errorMsg: null
};
}
};
validateLastName = LastName => {
if (!LastName) {
return {
validateStatus: "error",
errorMsg: "LastName may not be empty"
};
} else if (LastName.length > 12) {
return {
validationStatus: "error",
errorMsg: `LastName is too long 11 characters allowed.)`
};
} else {
return {
validateStatus: "success",
errorMsg: null
}}
};

Related

Condtional Renderin React using If-else not working

i'm new at React, i want to ask about contidional recndering, i'm making project about sending message, and i use validation to check all field is not empty, when one field is empty it should show error message that message not send. I using if-else to render the error message, but it doesn't work.
Here i add my code
....
export default class Message extends React.Component {
constructor(props){
super(props);
this.state = {
isLoggedIn: SystemStore.isLoggedIn(),
profile: ProfileStore.getProfile(),
fullName: SystemStore.systemUser().fullName,
site: '',
email: '',
phone: '',
subject: '',
description: '',
type: '',
errorMessage: '',
errorDialog: '',
isSubmited: false,
successMessage: '',
submitting: false
};
this.clearForm = this.clearForm.bind(this);
this.handleProfileChange = this.handleProfileChange.bind(this);
this.handleSubjectChange = this.handleSubjectChange.bind(this);
this.handleMessageChange = this.handleMessageChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
this.handleSubmitComplete = this.handleSubmitComplete.bind(this);
this.handleSubmitError = this.handleSubmitError.bind(this);
}
componentDidMount(){
ProfileStore.addProfileChangeListener(this.handleProfileChange);
if(!this.state.profile){
ProfileActions.reload()
}
MessageStore.addSubmitMessageChangeListener(this.handleSubmitComplete);
MessageStore.addSubmitMessageFailChangeListener(this.handleSubmitError);
}
componentWillUnmount(){
ProfileStore.removeProfileChangeListener(this.handleProfileChange);
MessageStore.removeSubmitMessageChangeListener(this.handleSubmitComplete);
MessageStore.removeSubmitMessageFailChangeListener(this.handleSubmitError);
}
render(){
return(
<Layout>
<div className='hs-dashboard row'>
<div className='col-md-12'>
<div className='col-xs-12 col-sm-6 col-md-5'>
<div className='col-xs-12 hs-message-form'>
<div className='row hs-message-form-head'>
<div className='hs-message-form-logo-container'>
<img className='col hs-message-form-logo' src='../../images/gii-logo-black.png'/>
<text className='hs-message-form-logo-label'>{T.translate('gii')}</text>
</div>
<div className='hs-message-form-label'>
{ T.translate('message.title') }
</div>
<div className='hs-message-form-label-1'>
{ T.translate('message.subtitle') }
</div>
</div>
<div className='row hs-message-form-body'>
<form className='hs-message-form-body-content'>
<label>
{ T.translate('message.type') }
</label>
<select
id="subject"
value={ this.state.subject }
onChange={ this.handleSubjectChange }
className="form-control"
required="true"
>
<option value="">{ T.translate('placeholder.selectSubject') }</option>
<option value="PRAYER">{ T.translate('message.pray') }</option>
<option value="ADDRESS">{ T.translate('message.address') }</option>
<option value="VISIT">{ T.translate('message.visit') }</option>
</select>
<label>
{T.translate('message.message')}
</label>
<textarea
type="text"
id="description"
className="form-control"
width=''
placeholder={ T.translate('placeholder.message') }
onChange={ this.handleMessageChange }
value={ this.state.description }
required
/>
{ this.state.errorDialog &&
<div className='hs-message-empty'>
{ this.state.errorDialog }
</div>
}
{ this.state.isSubmited === true ?
<div className='hs-message-success'>
{ this.state.successMessage }
</div> :
<div className='hs-message-error'>
{ this.state.errorMessage }
</div>
}
<LaddaButton
loading={ this.state.submitting }
onClick={ this.handleSubmit }
data-spinner-size={ 30 }
data-style={ SLIDE_RIGHT }
>
{ T.translate('action.send') }
</LaddaButton>
</form>
</div>
</div>
</div>
</div>
</div>
</Layout>
)
}
clearForm(){
this.setState({ subject: '', description: '' });
}
handleProfileChange(){
this.setState({
site: ProfileStore.getProfile().primarySite.name,
email: ProfileStore.getProfile().emailAddresses[0].email,
phone: ProfileStore.getProfile().contactNumbers[0].countryCode + ProfileStore.getProfile().contactNumbers[0].number
});
}
handleSubjectChange(evt){
this.setState({ subject: evt.target.value }, () => {
if(this.state.subject === 'PRAYER') {
this.setState({ type: 'REQUEST' });
} else if(this.state.subject === 'ADDRESS') {
this.setState({ type: 'INFORMATION' });
} else if(this.state.subject === 'VISIT'){
this.setState({ type: 'REQUEST' });
}
});
}
handleMessageChange(evt){
this.setState({ description: evt.target.value });
}
handleSubmit(evt) {
evt.preventDefault();
var errorDialog;
if(this.state.subject === ''){
errorDialog = 'Error:' + T.translate('msg.subjectRequired');
} else if(this.state.description === ''){
errorDialog = 'Error:' + T.translate('msg.mailDescriptionRequired');
}
this.setState({errorDialog: errorDialog});
this.handleProfileChange();
this.handleSubjectChange(evt);
this.handleMessageChange(evt);
var messageInfo = {
fullName: this.state.fullName,
site: this.state.site,
email: this.state.email,
phone: this.state.phone,
subject: this.state.subject,
description: this.state.description,
type: this.state.type
};
this.setState({ submitting: true }, () => {
MessageActions.sendMessage(messageInfo);
});
console.log(this.state.isSubmited);
}
handleSubmitComplete(){
this.setState({
submitting: false,
isSubmited: true,
errorMessage: null,
successMessage: T.translate('msg.mailSent')
});
this.clearForm();
}
handleSubmitError(){
this.setState({
submitting: false,
isSubmited: false,
errorMessage: T.translate('msg.mailSentFailed'),
successMessage: null
});
}
}
//updated code
This is my store.js
....
var MessageStore = assign({}, EventEmitter.prototype, {
emitSubmitMessageChange: function(){
this.emit(SUBMIT_MESSAGE);
},
addSubmitMessageChangeListener: function(callback){
this.on(SUBMIT_MESSAGE, callback);
},
removeSubmitMessageChangeListener: function(callback){
this.removeListener(SUBMIT_MESSAGE, callback);
},
emitSubmitMessageFailChange: function() {
this.emit(SUBMIT_MESSAGE_FAILED);
},
addSubmitMessageFailChangeListener: function(callback) {
this.on(SUBMIT_MESSAGE_FAILED, callback);
},
removeSubmitMessageFailChangeListener: function(callback) {
this.removeListener(SUBMIT_MESSAGE_FAILED, callback);
}
});
Dispatcher.register(function(action) {
switch(action.actionType){
case MessageConstants.PERFORM_SEND_MESSAGE:
MessageStore.emitSubmitMessageChange();
break;
case MessageConstants.PERFORM_SEND_MESSAGE_FAIL:
MessageStore.emitSubmitMessageFailChange();
break;
default:
//noop
}
})
export default MessageStore;
And this is my action.js
.....
function _sendMessage(messageInfo, callback) {
jQuery.ajax({
method: 'POST',
url: api('supportTickets'),
contentType: 'application/json',
data: JSON.stringify(messageInfo)
})
.done(function(messageInfo) {
callback(null, messageInfo);
})
.fail(function(err){
console.error('Failed to send message : ' + JSON.stringify(err));
callback(err, null);
});
}
var MessageActions = {
sendMessage: function(messageInfo) {
_sendMessage(messageInfo, function(err, messageInfo) {
if(err) {
Dispatcher.dispatch({
actionType: MessageConstants.PERFOM_SEND_MESSAGE_FAIL
});
}
Dispatcher.dispatch({
actionType: MessageConstants.PERFORM_SEND_MESSAGE,
messageInfo
});
});
}
};
module.exports = MessageActions;
Your form should be responsible for handling errors. You should know your errors at the time of submission of the form.
handleSubmit(evt) {
evt.preventDefault();
var errorDialog;
if(this.state.subject === ''){
errorDialog = 'Error:' + T.translate('msg.subjectRequired');
} else if(this.state.description === ''){
errorDialog = 'Error:' + T.translate('msg.mailDescriptionRequired');
}
if (errorDialog) {
this.setState({
// set your error here
})
return;
}
this.handleProfileChange();
this.handleSubjectChange(evt);
this.handleMessageChange(evt);
var messageInfo = {
fullName: this.state.fullName,
site: this.state.site,
email: this.state.email,
phone: this.state.phone,
subject: this.state.subject,
description: this.state.description,
type: this.state.type
};
this.setState({ submitting: true }, () => {
MessageActions.sendMessage(messageInfo);
});
console.log(this.state.isSubmited);
}
Your component seems to be a little complex. Think about breaking your component into smaller components. Using functional components I would do this task in a way something like that.
function validateForm() {
// validate form logic here. If it's ok, returns true, otherwise, returns false
}
function Message({ form }) {
if (!validateForm(form)) {
return (
<div>Please, fill the empty fields!!</div>
)
}
return (
<div>All good. Submit the form!!</div>
)
}

React-native TypeError: Cannot read property 'data' of undefined

I have propblems in my project
enter image description here
D:\React-Native\Expo\mobile\node_modules\expo\build\logs\RemoteConsole.js:80 Possible Unhandled Promise Rejection (id: 0):
TypeError: Cannot read property 'data' of undefined
TypeError: Cannot read property 'data' of undefined
SignUp.js
import React from 'react';
import {ScrollView, KeyboardAvoidingView, CheckBox, View, Text, Platform,} from 'react-native';
import styles from './styles/authStyles';
import ScreenTitle from '../components/ScreenTitle';
import Input from '../components/input';
import Button from '../components/Button';
import axios from '../config/axios';
import { SINGNUP_URL } from '../config/urls';
import Loader from '../components/Loader';
import Alert from '../components/Alert';
export default class SignUpScreen extends React.Component
{
constructor(props)
{
super(props);
this.state =
{
name: "",
email: "",
password: "",
specialization: "",
phone: "",
address: "",
workingHours: "",
userType: false,
location: null,
isLoading: false,
alert:
{
messages: null,
type: "",
}
}
}
componentDidUpdate()
{
if(this.state.alert.messages)
{
setTimeout(() =>
{
this.setState({alert: {messages: null}})
}, 3000)
}
}
componentWillUnMount()
{
clearTimeout();
}
changeNameHandler = (value) =>
{
this.setState({name: value})
};
changeEmailHandler = (value) =>
{
this.setState({email: value})
};
changePasswordHandler= (value) =>
{
this.setState({password: value})
};
changeSpecializationHandler = (value) =>
{
this.setState({specialization: value})
};
changePhoneHandler = (value) =>
{
this.setState({phone: value})
};
changeAddressHandler = (value) =>
{
this.setState({address: value })
};
changeWorkingHoursHandler = (value) =>
{
this.setState({workingHours: value})
};
changeUserTypeHandler = () =>
{
this.setState({ userType: !this.state.userType})
}
validate()
{
const {name, email, password, specialization, address, phone, workingHours, userType} = this.state;
let validationErrors = [];
let passed = true;
if(!name)
{
validationErrors.push(" Please put your name");
passed= false;
}
if(!email)
{
validationErrors.push(" Please put your email");
passed= false;
}
if(!password)
{
validationErrors.push(" Please put your password");
passed= false;
}
if(userType)
{
if(!specialization)
{
validationErrors.push(" Please put your specialization");
passed= false;
}
if(!address)
{
validationErrors.push(" Please put your address");
passed= false;
}
if(!phone)
{
validationErrors.push(" Please put your phone nuber");
passed= false;
}
if(!workingHours)
{
validationErrors.push(" Please put your working hours");
passed= false;
}
}
if(validationErrors.length > 0)
{
this.setState({alert: {messages: validationErrors, type: "danger"}})
}
return passed
}
_SignUp = async () =>
{
if(!this.validate()) return;
this.setState({isLoading: true})
const { name, email, password, specialization, address, phone, workingHours, userType } = this.state;
const body =
{
name,
email,
password,
specialization,
address,
phone,
workingHours,
userType: userType ? "doctor" : 'normal',
location:
{
latitude: 1,
longitude: 2
},
isLoading: false
}
try
{
const response = await axios.post(SINGNUP_URL, body);
this.setState({
name:'',
email: '',
password: '',
specialization: '',
address: '',
phone: '',
workingHours: '',
userType: false,
location: null
});
this.props.navigation.navigate("SignIn",
{
alert:{messages:"Your account has been successfully registered", type:"success"}
});
}
catch(e){
this.setState({
alert: { messages: e.response.data.message, type: "danger"},
isLoading: false
});
}
}
render()
{
const { name, email, password, specialization, address, phone, workingHours, userType, isLoading, alert } = this.state;
return(
<ScrollView contentContainerStyle={{paddingVertical: 40}}>
<Loader title="A new account is creating" loading={isLoading} />
<Alert messages={alert.messages} type={alert.type} />
<View style={styles.container}>
<ScreenTitle
title="Create new account"
icon="md-person-add"
/>
<KeyboardAvoidingView behavior="padding" enabled >
<Input
placeholder="Name"
onChangeText={this.changeNameHandler}
value={name} />
<Input
placeholder="Email"
onChangeText={this.changeEmailHandler}
value={email} />
<Input
placeholder="Password"
secureTextEntry
onChangeText={this.changePasswordHandler}
value={password} />
<View
style={styles.checkBoxContainer}
>
<CheckBox
style={styles.checkBoxLabel}
value={userType}
onChange={this.changeUserTypeHandler} />
<Text style={styles.checkBoxLabel} >Doctors</Text>
</View>
{userType && (
<React.Fragment>
<Input
onChangeText={this.changeSpecializationHandler}
placeholder="Specialization" value = {specialization} />
<Input
onChangeText={this.changeWorkingHoursHandler}
placeholder="Hours work" value={workingHours} />
<Input
onChangeText={this.changeAddressHandler}
placeholder="Address" value={address} />
<Input
onChangeText={this.changePhoneHandler}
placeholder="Phone Number" value={phone} />
</React.Fragment>
)}
<Button
text="New Account"
onPress={this._SignUp}
/>
<View style={{height: 30}} />
{/* <Button text="Home" onPress={() => this.props.navigation.navigate("Main")} /> */}
</KeyboardAvoidingView>
</View>
</ScrollView>
)
}
}
SignIn.js
import React, { Component } from 'react'
import { Text, View, KeyboardAvoidingView, AsyncStorage } from 'react-native'
import styles from './styles/authStyles';
import ScreenTitle from '../components/ScreenTitle';
import Input from '../components/input';
import Button from '../components/Button';
import axios from '../config/axios';
import { SIGNIN_URL } from '../config/urls';
import Loader from '../components/Loader';
import Alert from '../components/Alert';
import Container from '../components/Container';
import { ScrollView } from 'react-native-gesture-handler';
export default class SignIn extends Component {
constructor(props)
{
super(props);
this.state =
{
email: "",
password: "",
isLoading: false,
alert:
{
messages: null,
type: ""
}
}
}
componentDidMount()
{
const alert = this.props.navigation.getParam('alert');
if(alert)
{
this.setState({alert});
}
}
componentDidUpdate()
{
if(this.state.alert.messages)
{
setTimeout(() =>
{
this.setState({alert: {messages: null}})
}, 3000)
}
}
componentWillUnMount()
{
clearTimeout();
}
changeEmailHandler = (value) =>
{
this.setState({email: value})
};
changePasswordHandler= (value) =>
{
this.setState({password: value})
};
validate()
{
const { email, password } = this.state;
let validationErrors = [];
let passed = true;
if(!email)
{
validationErrors.push(" Please put your email");
passed= false;
}
if(!password)
{
validationErrors.push(" Please put your password");
passed= false;
}
if(validationErrors.length > 0)
{
this.setState({alert: {messages: validationErrors, type: "danger"}})
}
return passed
}
_SignIn= async () => {
if(!this.validate()) return;
this.setState({ isLoading: true });
const body = {
email: this.state.email,
password: this.state.password
};
try {
const response = await axios.post(SIGNIN_URL, body);
this.setState({ email: "", password: "", isLoading: false });
AsyncStorage.setItem("accessToken", response.data.accessToken);
} catch (e) {
this.setState({
alert: { messages: e.response.data.message, type: "danger" },
isLoading: false
});
}
}
render() {
const {email, password, isLoading, alert} = this.state;
return (
<Container>
<Alert messages={alert.messages} type={alert.type} />
<Loader title="Login" loading={isLoading} />
<ScrollView
keyboardShouldPersistTaps="handled"
contentContainerStyle={styles.container} >
<ScreenTitle title="Login" icon="md-log-in" />
<KeyboardAvoidingView behavior="padding" enabled>
<Input
onChangeText={this.changeEmailHandler}
value={email}
placeholder="Email" />
<Input
onChangeText={this.changePasswordHandler}
value={password}
secureTextEntry
placeholder="Password" />
</KeyboardAvoidingView>
<Button
text="Login"
onPress={this._SignIn}
/>
<View style={{height: 30}} />
{/* <Button text="Home" onPress={() => this.props.navigation.navigate("Main")} /> */}
</ScrollView>
</Container>
)
}
}
Try changing
catch (e) {
this.setState({
alert: { messages: e.response.data.message, type: "danger" },
isLoading: false
});
}
To
catch (e) {
let {response: {data: {message = ''} = {}} = {}} = e
this.setState({
alert: { messages: message, type: "danger" },
isLoading: false
});
}
And before even doing that please check the value of e using console.log and make sure message exist in the nested object.

How to retrieve a react component value

I'm trying to make my own custom input[type='text'] on React, with my own effects.
To do that, I created my component:
Textbox.js
import React from "react";
class Textbox extends React.Component {
constructor(props) {
super();
this.state = {
inputValue: props.value,
fieldActive: false,
label: props.label,
placeholder: props.placeholder,
type: props.type,
maxLength: props.maxLength,
error: false,
required: props.required,
inputChange: props.onChange,
id: props.id,
valid: !props.required,
submitted: props.submitted,
_value: props.value,
updated: false
};
//console.log(this.props);
this.updateInputValue = this.updateInputValue.bind(this);
this.activateField = this.activateField.bind(this);
this.disableFocus = this.disableFocus.bind(this);
}
componentWillReceiveProps(nextProps) {
this.setState({
fieldActive: (nextProps.value != ''),
inputValue: nextProps.value
});
}
componentWillUpdate(nextProps, nextState) {
//console.log(nextState.inputValue);
if(/*this.state.inputValue != this.props.value*/ this.props.value != '' && !this.state.updated ){
this.setState({
inputValue: nextProps.value,//this.props.value,
updated: true,
fieldActive: true
});
}
}
componentDidUpdate(prevProps) {
//console.log('updating value: ' + prevProps.submitted);
if (this.props.submitted !== undefined) {
if (this.props.submitted !== prevProps.submitted) {
if(!this.state.required) return;
this.setState({
error: this.state.required && this.state.inputValue == ""
});
}
}
}
activateField() {
this.setState({
fieldActive: true
});
}
disableFocus(e) {
if (e.target.value == "") {
this.setState({
fieldActive: false,
error: this.state.required,
valid: !this.state.required
});
} else {
this.setState({
error: false
});
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) {
//console.log('writing: ' + e.target.value);
this.setState({
inputValue: e.target.value,
submitted: false,
});
//this.props.value = e.target.value
if (this.state.inputChange != undefined && this.state.inputChange != null)
this.state.inputChange(e.target.id, e.target.value, this.state.valid);
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}
/>
<label
className={this.state.error ? "error" : "error hide"}
style={{ fontSize: 14, fontWeight: 400 }}
>
You must complete this field
</label>
</div>
);
}
}
export default Textbox;
myjsfile.js
constructor(props) {
super(props);
this.state = {
clients: [],
client: {
name: "",
},
};
//... More methods and properties
<Textbox
label="Client name"
placeholder="Client name / Customer"
id="name"
type="text"
required={true}
value={this.state.client.name}
maxLength="20"
//onChange={this.handleChange}
submitted={this.state.submitted}
/>
In my file, I'm using Textbox as a component, but each time that I want to retrieve the value inside the Textbox I get empty.
What am I doing wrong? Why the value is not updating? How can I solve it?

Load data into inputs when entering the code

I have updated the Code.
Here I have a functional Select Autocomple showing the list of records from DB "Register". When selecting a Code, the Name value is automatically renamed.
The same thing I want to do but with the not with , I want to call more than two values like this in the image and in select is only Label and Value
Capture: [1]: https://i.stack.imgur.com/ELf1a.png
class Register extends Component {
state = {
status: "initial",
data: [],
name:'',
code:''
}
componentDidMount = () => {
this. getInfo()
}
getInfo= async () => {
try {
const response = await getAll('register')
console.log(response.data)
this.setState({
status: "done",
data: response.data
});
} catch (error) {
this.setState({
status: "error"
});
}
};
handleChange = (selectedOption) => {
this.setState({
selectedOption,
name: selectedOption.value
});
render() {
//show Name and code on Select from Register
const data = this.state.data.map( st => ({value: st.Name, label: st.Code}));
return (
<Container>
<RowContainer margin="1px" >
<ColumnContainer margin="10px">
<h3>Info</h3>
<label>Code</label>
<Select
width='215px'
value={selectedOption}
onChange={this.handleChange}
options={data}
name={"Code"}
/>
<label>Name</label>
<Input
width='150px'
type="text"
name={"Name"}
placeholder="Name"
value={this.state.name} />
</ColumnContainer>
</RowContainer>
</Container>
)
}
};
export default Register;
You want to know how change the state for <input/>
try this
constructor(props){
super(props)
this.state = {
status: "initial",
data: [],
codigo: "",
nombre: ""
}
}
handleChange(event){
let stateUpdate = this.state;
stateUpdate[event.target.name] = event.target.value}
this.setState(stateUpdate);
}
render() {
const data = [...this.state.data];
return (
<Container>
<RowContainer margin="1px" >
<ColumnContainer margin="10px">
<h3>Info</h3>
<label>Codigo</label>
<Input
name="codigo"
width='150px'
type="text"
placeholder="Digite el codigo"
value={data.codigo } ref="codigo" />
<label>Nombre</label>
<Input
name="nombre"
width='150px'
type="text"
placeholder="Nombre completo"
value={this.state.nombre} />
</ColumnContainer>
</RowContainer>
</Container>
)
}

react update screen after changing values in the state

I am new to react and I am working on a project where I was ask to reset a form to its defaults.
I created a function that gets call after I click the reset button
<input id="reset_button"
type="button"
name="reset"
value="Reset"
onClick={this.resetSearch}/>
This is my function:
resetSearch: function() {
this.setState({ID: 'Moo'});
},
I do see the ID change value in the console but it does not update on the screen.
Other things that I have tried
# when I do this the element despairs from then screen
resetSearch: function() {
var values = this.fields.state.values;
this.setState({
defaultValues: {
values
},
ignoreDefault: false
});
}
#render function
render: function() {
return (
<div className="card-body with-padding-bottom-0">
<form id={this.formId}>
<div id="sn-fields" className="usa-grid-full sn-search">
<SNFields ref={(fields) => { this.fields = fields; }} ddl_id='sn_search_card_type' snOptions={ this.getProp('snOptions')} fields={this.getProp('fields')} updateParentState={this.updateStateByField} defaultFieldValues={this.getProp('defaultValues')} ignoreDefault={this.state.ignoreDefault}></SNFields>
</div>
<div className="usa-grid-full with-margin-top-10 validation-div">
<div id="sn_search_card_search_button_container" className="usa-width-one-whole">
<label htmlFor="system_validator"></label>
<input hidden name="system_validator" id="system_validator"/>
<input id="search_button" type="button" name="search" value="Search" onClick={this.personSearch}/>
<input id="reset_button" type="button" name="reset" value="Reset" onClick={this.resetSearch}/>
</div>
</div>
</form>
</div>
);
}
I was able to find a class SNFields
var SNFields = React.createClass({
filterFields: function(searchVal) {
console.log('PCQSFields - filterFields ')
var filterLabels = [];
//filter in this component since the filtering can't be done on the ruby side
switch(searchVal) {
case 'APPLICATION_ID':
case 'ENUMERATOR':
case 'ENCOUNTER_ID': {
filterLabels = ['ID'];
break;
}
case 'NAME_AND_DOB': {
filterLabels = ['Date of Birth', 'Last Name', 'Date Range', 'First Name'];
break;
}
default: {
break;
}
}
var fields = this.props.fields.slice();
for (var i = fields.length - 1; i > -1; i--) {
if (filterLabels.indexOf(fields[i].label) < 0) {
fields.splice(i, 1);
}
}
return fields;
},
render: function() {
console.log('NSFields - render ')
return (
<div>
<div className="usa-width-one-third">
<label htmlFor={this.props.ddl_id} className="card-label bold">Search Type</label>
<Dropdown id={this.props.ddl_id} onChange={this.updateFields} selectableArray={this.props.nsOptions} classes="" selectedOption={this.state.ddl}/>
</div>
<div className="flex-container" style={{'flexWrap': 'row'}}>
{this.nsFieldsHelper(this.state.fields)}
</div>
</div>
);
}
});
I guess what I really want to do is when I press the reset to call
SNFields.filterFields('NAME_AND_DOB')
but when I try that I get a message in the console that reads: Uncaught TypeError: NSFields.filterFields is not a function
How does your componentDidMount() and componentWillReceiveProps(newProps) look like?
This is how I have done an Input component:
import React, { Component } from 'react';
export default class Input extends Component {
displayName: 'Input';
constructor(props) {
super(props);
this.state = {
value: this.props.value,
disabled: this.props.disabled,
checked: this.props.checked,
className:this.props.className,
maxLength:this.props.maxLength,
placeholder:this.props.placeholder,
id:this.props.id,
name:this.props.name,
type:this.props.name,
oldValue:this.props.value,
backgroundColor:''
};
this.handleBlur = this.handleBlur.bind(this);
this.handleChange = this.handleChange.bind(this);
};
componentWillReceiveProps(nextProps) {
if (this.state.value !== nextProps.value) {
this.setState({ value: nextProps.value});
};
if (this.state.disabled !== nextProps.disabled) {
this.setState({ disabled: nextProps.disabled});
};
if (this.state.checked !== nextProps.checked) {
this.setState({ checked: nextProps.checked});
};
if (this.state.className !== nextProps.className) {
this.setState({ className: nextProps.className});
};
if (this.state.maxLength !== nextProps.maxLength) {
this.setState({ maxLength: nextProps.maxLength});
};
if (this.state.placeholder !== nextProps.placeholder) {
this.setState({ placeholder: nextProps.placeholder});
};
};
componentDidMount() {
this.setState({ value: this.props.value,
disabled: this.props.disabled,
checked: this.props.checked,
className:this.props.className,
maxLength:this.props.maxLength,
placeholder:this.props.placeholder
});
};
handleBlur(event) {
if ((this.props.checkError===null)||(this.props.checkError(event,false) === true)) {
this.setState({ value: event.target.value,
oldValue: event.target.value
})
}
else
{
this.setState({ value: this.state.oldValue })
}
this.setState({ backgroundColor: ''})
};
handleChange(event) {
if (this.state.value !== event.target.value) {
this.setState({ value: event.target.value })
if ((this.props.checkError!==null)&&(this.props.checkError(event,true) === false)) {
this.setState({ backgroundColor: 'red'})
}
else
{
this.setState({ backgroundColor: ''})
}
}
if (this.props.onClick!==null) {
this.props.onClick();
}
};
render() {
return <input value={this.state.value}
maxLength={this.state.maxLength}
placeholder={this.state.placeholder}
className={this.state.className}
id={this.props.id}
name={this.props.name}
type={this.props.type}
disabled={this.state.disabled}
checked={this.state.checked}
onBlur={this.handleBlur}
onChange={this.handleChange}
style={{background:this.state.backgroundColor}}/>
}
};
Input.propTypes=
{
value:React.PropTypes.string,
placeholder:React.PropTypes.string,
maxLength: React.PropTypes.number,
disabled:React.PropTypes.bool,
checked:React.PropTypes.bool,
className:React.PropTypes.string,
id:React.PropTypes.string,
name:React.PropTypes.string,
type:React.PropTypes.string,
checkError: React.PropTypes.func,
onClick: React.PropTypes.func
}
Input.defaultProps =
{
placeholder:'',
maxLength:100,
disabled:false,
checked:false,
value:'',
className:'',
id:'',
name:'',
type:'text',
checkError:null,
onClick:null
}

Resources