i am trying to create an accounting page wherein I try to add percentage and net to get the get value as well as the total value however I don't know how to implement a function without a button I don't have any examples to go through as well if anyone can help me that would be really grateful
the code is as follows
import React, { Component } from 'react';
import { connect } from "react-redux";
import { getData } from "../actions/company"
import "./css/InvoiceAdd.css"
import DatePicker from "react-datepicker";
import "react-datepicker/dist/react-datepicker.css";
import "./css/Togglebox.css"
export class InvoiceSales extends Component {
constructor(props) {
super(props);
this.state = {
Invoice_Date: '',
company_name: '',
GSTIN: '',
invoice_no: '',
percentage: 0,
net: 0,
igst: 0,
cgst: 0,
sgst: 0,
total_invoice: 0,
};
console.log(this.state);
this.onChange = this.onChange.bind(this);
this.onCreate = this.onCreate.bind(this);
this.handlenum1Change = this.handlenum1Change.bind(this);
this.handlenum2Change = this.handlenum2Change.bind(this);
}
componentDidMount() {
document.body.classList.add("background-black");
}
componentDidMount() {
this.props.getData();
}
componentWillUnmount() {
document.body.classList.remove("background-black");
}
onChange = event => {
this.setState({ [event.target.name]: event.target.value });
};
handlenum1Change = evt => {
console.log(evt.target.value);
this.setState({percentage:Number(evt.target.value) });
}
handlenum2Change = evt => {
console.log(evt.target.value);
this.setState({net:Number(evt.target.value) });
};
onCreate(e) {
e.preventDefault();
let Invoice = {
Invoice_Date: this.state.Invoice_Date,
company_name: this.state.company_name,
GSTIN: this.state.GSTIN,
invoice_no: this.state.invoice_no,
percentage: this.state.percentage,
net: this.state.net,
igst: this.state.igst,
sgst: this.state.sgst,
total_invoice: this.state.total_invoice
};
console.log(Invoice);
this.props.postData(Invoice);
this.setState({
Invoice_Date: '',
company_name: '',
GSTIN: '',
invoice_no: '',
percentage: 0,
net: 0,
igst: 0,
cgst: '',
sgst: '',
total_invoice: ''
});
}
render() {
return (
<div classname="masterfile-add" >
<form className="masterfileadd-form">
<p><label className="masterfileadd-text">
Invoice Date :
<DatePicker dateFormat="dd-MM-yyyy" selected={this.state.date} onChange={this.handleDateChange} className="itextbox1" />
</label>
</p>
<label className="masterfileadd-text">
Company Name:
< select value={this.state.company} onchange={this.onChange} name="company_name" className="itextbox2">
{this.props.company.map(company => {
return <option value={company.company_name}>{company.company_name}</option>
})}
</select>
</label>
<p><label className="masterfileadd-text">
GSTIN :
<input type="text" name="GSTIN" value={this.state.GSTIN} onChange={this.onChange} className="itextbox3"></input>
</label>
</p>
<p><label className="masterfileadd-text">
Invoice No :
<input type="text" name="invoice_no" value={this.state.invoice_no} onChange={this.onChange} className="itextbox4"></input>
</label>
</p>
<p> <label className="masterfileadd-text">
Percentage:
<input type="number" step="0.1" name="percentage" value={this.state.percentage} handlenum1Change={this.handlenum1Change} className="itextbox5"></input>
</label>
</p>
<p><label className="masterfileadd-text">
NET :
<input type="number" name="net" value={this.state.net} handlenum2Change={this.handlenum2Change} className="itextbox6"></input>
</label>
</p>
<p><label className="masterfileadd-select">
IGST :
<input type="text" value={this.state.igst} readOnly />
</label>
</p>
<p><label className="masterfileadd-select">
CGST :
<input type="text" name="cgst" value={this.state.opening_balance} onChange={this.onChange} className="itextbox8"></input>
</label>
</p>
<p><label className="masterfileadd-select">
SGST:
<input type="text" name="sgst" value={this.state.opening_balance} onChange={this.onChange} className="itextbox9"></input>
</label>
</p>
<p><label className="masterfileadd-select">
Total Invoice :
<input type="text" name="total_invoice" value={this.state.head_of_account} onChange={this.onChange} className="itextbox10"></input>
</label>
</p>
<p className="iButton" ><input type="submit" value="Submit" onChange={this.onChange} onClick={this.onCreate} className="mButtonAdd" />
</p>
</form>
</div>
)
}
}
const mapStateToProps = state => ({
company: state.companyReducer.company,
error: state.journalReducer.error
});
export default connect(
mapStateToProps,
{ getData }
)(InvoiceSales)
Hı ,
in onchange event
if(event.target.name =="input1" || event.target.name=="input2")
{
this.setState({result: Number(this.state.input1)+ Number(this.state.input2)})
}
try something like this. input1 and input2 are your textboxes. And be sure textbox value is number .may be you can check.
Related
in my react form I need to save each data item into the state.
I currently have on change methods for each of the inputs but its a lot of very similar code and looks messy. (But it does work...)
Here is the code
class EnterMortgage extends React.Component {
constructor(props) {
super(props);
this.state = {
repaymentType: '',
propVal: '',
bal: '',
fullTerm: '',
remainTerm: '',
intRate: '',
};
this.handleRepaymentChange = this.handleRepaymentChange.bind(this);
this.handlePropValChange = this.handlePropValChange.bind(this);
this.handleBalChange = this.handleBalChange.bind(this);
this.handleFullTermChange = this.handleFullTermChange.bind(this);
this.handleRemainTermChange = this.handleRemainTermChange.bind(this);
this.handleIntRateChange = this.handleIntRateChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleRepaymentChange(event) {
this.setState({ repaymentType: event.target.value });
}
handlePropValChange(event) {
this.setState({ propVal: event.target.value });
}
handleBalChange(event) {
this.setState({ bal: event.target.value });
}
handleFullTermChange(event) {
this.setState({ fullTerm: event.target.value });
}
handleRemainTermChange(event) {
this.setState({ remainTerm: event.target.value });=
}
handleIntRateChange(event) {
this.setState({ intRate: event.target.value });
}
handleSubmit(event) {
this.props.history.push('/EnterSavings', this.state);
}
// renders to display on page
render() {
return (
<div>
<div >
<p> Enter your mortgage details </p>
</div>
<div>
<form onSubmit={this.handleSubmit}>
<label>
Property Value {'\u00A3'}
<input type="Number" name="propVal" onChange={this.handlePropValChange} />
</label>
<label>
Current Balance
<input type="Number" name="bal" onChange={this.handleBalChange}/>
</label>
<label>
Full Mortgage Term (months)
<input type="Number" name="fullTerm" onChange={this.handleFullTermChange} />
</label>
<label>
Remaining Mortgage Term (months)
<input type="Number" name="remainTerm" onChange={this.handleRemainTermChange} />
</label>
<label>
InterestRate
<input type="Number" name="intRate" onChange={this.handleIntRateChange} />
</label>
<label>
Repayment Method
<select onChange={this.handleRepaymentChange}>
<option value="repayment">Repayment</option>
<option value="interest">Interest Only</option>
<option value="pap">Part and Part</option>
</select>
</label>
<input type="submit" value="Submit" />
</form>
</div>
</div>
);
}
}
export default EnterMortgage;
Is there a way to refactor this rather than having multiple functions? I have tried to combined into one method but I couldn't manage to get each item updated.
I am creating a contact form using React JS and this is my code:
import React, { Component } from 'react';
export default class Create extends Component {
constructor(props) {
super(props);
this.onChangeFirstname = this.onChangeFirstname.bind(this);
this.onChangeLastname = this.onChangeLastname.bind(this);
this.onChangeMessage = this.onChangeMessage.bind(this);
this.onSubmit = this.onSubmit.bind(this);
this.state = {
person_firstname: '',
person_lastname: '',
message:''
}
}
onChangeFirstname(e) {
this.setState({
person_firstname: e.target.value
});
}
onChangeLastname(e) {
this.setState({
person_lastname: e.target.value
})
}
onChangeMessage(e) {
this.setState({
message: e.target.value
})
}
onSubmit(e) {
e.preventDefault();
console.log(`The values are ${this.state.person_firstname}, ${this.state.person_lastname}, and ${this.state.message}`)
this.setState({
person_firstname: '',
person_lastname: '',
message: ''
})
}
render() {
return (
<div style={{marginTop: 10}}>
<h3>Contact Form</h3>
<form onSubmit={this.onSubmit}>
<div className="form-group">
<label>First Name: </label>
<input type="text" className="form-control"
onChange={this.onChangeFirstName}/>
</div>
<div className="form-group">
<label>Last Name: </label>
<input type="text" className="form-control"
onChange={this.onChangeLastname}/>
</div>
<div className="form-group">
<label>Message: </label>
<textarea className="form-control"
onChange={this.onChangeMessage}>
</textarea>
</div>
<div className="form-group">
<input type="submit" value="Submit" className="btn btn-primary"/>
</div>
</form>
</div>
)
}
}
Everything seems fine, but the code gives a wrong result.
When I enter the following,
I get the following result
No matter what I enter in the first name field, it is ignored and I get a blank in the result.
I tried moving the Lastname input field above the Firstname input field and I still get the first input (Lastname) as blank in the console.
i refactor your code and everything looks fine ...
import React, { Component } from "react";
import ReactDOM from "react-dom";
import "./styles.css";
export default class App extends Component {
constructor(props) {
super(props);
this.handleChange = this.handleChange.bind(this);
this.onSubmit = this.onSubmit.bind(this);
this.state = {
firstname: "",
lastname: "",
message: ""
};
}
handleChange(e) {
this.setState({ [e.target.name]: e.target.value });
}
onSubmit(e) {
e.preventDefault();
console.log(this.state);
this.setState({
firstname: "",
lastname: "",
message: ""
});
}
render() {
return (
<div style={{ marginTop: 10 }}>
<h3>Contact Form</h3>
<form onSubmit={this.onSubmit}>
<div className="form-group">
<label>First Name: </label>
<input
type="text"
name="firstname"
className="form-control"
onChange={this.handleChange}
/>
</div>
<div className="form-group">
<label>Last Name: </label>
<input
type="text"
name="lastname"
className="form-control"
onChange={this.handleChange}
/>
</div>
<div className="form-group">
<label>Message: </label>
<textarea
name="message"
className="form-control"
onChange={this.handleChange}
/>
</div>
<div className="form-group">
<input type="submit" value="Submit" className="btn btn-primary" />
</div>
</form>
</div>
);
}
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
you have a typo, onChangeFirstname should have a capital N onChangeFirstName
I have this app that adds products and updates it.As it is, the product form can be submitted without a name, and it creates a new product with a blank line in the name column.how do i Add a validation message so that it requires a 'name' field before submitting the results, and shows a message if you try to submit the form without a name value.
import React from 'react';
const RESET_VALUES = {id: '', category: '', price: '', stocked: false, name: ''};
class ProductForm extends React.Component {
constructor(props) {
super(props);
this.handleChange = this.handleChange.bind(this);
this.handleSave = this.handleSave.bind(this);
this.state = {
product: Object.assign({}, RESET_VALUES),
errors: {}
};
}
handleChange(e) {
const target = e.target;
const value = target.type === 'checkbox' ? target.checked : target.value;
const name = target.name;
this.setState((prevState) => {
prevState.product[name] = value;
return { product: prevState.product };
});
}
handleSave(e) {
this.props.onSave(this.state.product);
this.setState({
product: Object.assign({}, RESET_VALUES),
errors: {}
});
e.preventDefault();
}
render() {
return (
<form>
<h3>Enter a new product</h3>
<p>
<label>
Name
<br />
<input type="text" name="name" onChange={this.handleChange} value={this.state.product.name}/>
</label>
</p>
<p>
<label>
Category
<br />
<input type="text" name="category" onChange={this.handleChange} value={this.state.product.category} />
</label>
</p>
<p>
<label>
Price
<br />
<input type="text" name="price" onChange={this.handleChange} value={this.state.product.price} />
</label>
</p>
<p>
<label>
<input type="checkbox" name="stocked" onChange={this.handleChange} checked={this.state.product.stocked}/>
In stock?
</label>
</p>
<input type="submit" value="Save" onClick={this.handleSave}/>
</form>
);
}
}
export default ProductForm;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
Set errors to null by default.
this.state = {
product: Object.assign({}, RESET_VALUES),
errors: null
};
Update your handleSave to something like this:
handleSave(e) {
e.preventDefault();
if (this.state.product.name.trim() === '') {
this.setState({errors: {name: 'Please fill name'}});
return;
}
this.props.onSave(this.state.product);
this.setState({
product: Object.assign({}, RESET_VALUES),
errors: null
});
}
And then in Form render method under Input field show error.
<label>
Name
<br />
<input type="text" name="name" onChange={this.handleChange} value={this.state.product.name}/>
{this.state.errors && <p>{this.state.errors.name}</p>}
</label>
Working example https://codesandbox.io/s/cocky-ride-2p8ym
I'm having a lot of troubles trying to render the data I passed into my inputs to render a "contact" card.
I'm handling the inputs, changing the states accordingly, but when it comes to the final state, rendering a "Contact card" with all my data I just don't know how to make it works.
I think the problem is at my "renderCard" method.
If someone could give me a hand I think i'm really close but yet so far..
import React, { Component } from 'react';
export default class Todo extends Component {
state = {
nom: '',
age: '',
ville: '',
items: [],
start: 0
};
onChange = (event) => {
this.setState({
[event.target.name]: event.target.value
});
}
onSubmit = (event) => {
event.preventDefault();
this.setState({
nom: '',
age: '',
ville: '',
items: [...this.state.items, this.state.nom, this.state.age, this.state.ville],
start: 1
});
}
renderCard = () => {
return this.state.items.map((item, index) => {
return (
<div className="card" key={index}>
{item.nom}
{item.age}
{item.ville}
</div>
);
});
}
render() {
return (
<div>
<div className="card mb-3">
<div className="card-header">Ajouter une Personne</div>
<div className="card-body">
<form onSubmit={this.onSubmit}>
<div className="form-group">
<label htmlFor="name">Nom</label>
<input
type="text"
className="form-control form-control-lg"
name="nom"
value={this.state.nom}
onChange={this.onChange}
/>
</div>
<div className="form-group">
<label htmlFor="name">Age</label>
<input
type="text"
className="form-control form-control-lg"
name="age"
value={this.state.age}
onChange={this.onChange}
/>
</div>
<div className="form-group">
<label htmlFor="name">Ville</label>
<input
type="text"
className="form-control form-control-lg"
name="ville"
value={this.state.ville}
onChange={this.onChange}
/>
</div>
<button className="btn btn-primary btn-block">Créez votre Fiche !</button>
</form>
</div>
</div>
{this.renderCard()}
</div>
);
}
}
You are making mistake in setting the items in state on submit. I hope the items should be a array of objects
Please check the below code
onSubmit = (event) => {
event.preventDefault();
this.setState({
nom: '',
age: '',
ville: '',
items: [...this.state.items, {nom:this.state.nom, age:this.state.age, ville: this.state.ville}],
start: 1
});
I am on the beginning of my programming and that's my duty to code a forum web page with login. After login I am supposed to put come content and they should be editable but I am still on the beginning. I would be really happy if someone can help because right now I need to put the data into the database (I am able to get data drom the database I can consolo them)
It is really hard coding :(
But how to get the form to the database :/
This is tsx file on frontend side.
import * as React from 'react';
import { RouteComponentProps } from 'react-router';
//import { Link, NavLink } from 'react-router-dom';
export class SignUp extends React.Component<RouteComponentProps<{}>, {}> {
constructor(props: RouteComponentProps<{}> | undefined) {
super(props);
this.state = {
name: '',
surname: '',
username: '',
email: '',
password: ''
};
this.handleNameChange = this.handleNameChange.bind(this);
this.handleSurnameChange = this.handleSurnameChange.bind(this);
}
handleNameChange(e: { preventDefault: () => void; target: { value: any; }; }) {
e.preventDefault();
this.setState({ name: e.target.value });
}
handleSurnameChange(e: { preventDefault: () => void; target: { value: any; }; }) {
e.preventDefault();
this.setState({ surname: e.target.value });
}
handleSubmit(e: any) {
e.preventDefault();
const formKayit = {
name: this.state.name,
surname: this.state.surname
};
console.log("uye oldunuz", formKayit);
}
public render() {
return (
<div>
<h1>We are glad to see you here. </h1>
<form >
<label>
Name:
<input className="form-control" type="text" onChange={(e) => { this.handleNameChange(name); }} //name="Name" value={this.state.Name} onChange={this.handleChange.bind(this)}
/>
</label>
<div>
<label>
Surname:
<input className="form-control" type="text" onChange={(e) => { this.handleSurnameChange(e); }} //name="Surname" value={this.state.Surname} onChange={this.handleChange.bind(this)}
/>
</label>
</div>
<div>
<label>
Username:
<input className="form-control" type="text" //name="Username" value={this.state.Username} onChange={this.handleChange.bind(this)}
/>
</label>
</div>
<div>
<label>
E-mail:
<input className="form-control" type="text" //name="Surname" value={this.state.Surname} onChange={this.handleChange.bind(this)}
/>
</label>
</div>
<label>
Password:
<input className="form-control" type="password" //name="Password" value={this.state.Password} onChange={this.handleChange.bind(this)}
/>
</label>
<div>
<button onClick={(e) => { this.handleSubmit(e); }} className="btn">SignUp </button>
</div>
</form>
</div>
);
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>