Last active
March 30, 2021 05:36
-
-
Save vamsee9/2ea5b569557d1578a966e5df3a933f8f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from "react"; | |
import "./App.css"; | |
import TextField from "@material-ui/core/TextField"; | |
import Button from "@material-ui/core/Button"; | |
const initialState = { | |
pounds: "", | |
kilograms: "", | |
}; | |
class App extends Component { | |
state = initialState; | |
handlePounds = (e) => { | |
const pounds = e.target.value; | |
this.setState({ | |
pounds, | |
kilograms: pounds / 2.2046, | |
}); | |
if (pounds === "") { | |
this.setState(initialState); | |
} | |
}; | |
handleKilograms = (e) => { | |
const kilograms = e.target.value; | |
this.setState({ | |
kilograms, | |
pounds: kilograms * 2.2046, | |
}); | |
if (kilograms === "") { | |
this.setState(initialState); | |
} | |
}; | |
clearAll = () => { | |
this.setState(initialState); | |
}; | |
render() { | |
return ( | |
<div className="App"> | |
<form | |
onSubmit={(e) => e.preventDefault()} | |
noValidate | |
autoComplete="off" | |
> | |
<h1 className="title">Weight Converter</h1> | |
<TextField | |
id="standard-number" | |
label="Enter pounds" | |
value={this.state.pounds} | |
onChange={this.handlePounds} | |
type="number" | |
margin="normal" | |
className="input-field" | |
/> | |
<TextField | |
id="standard-number" | |
label="Enter kilograms" | |
value={this.state.kilograms} | |
onChange={this.handleKilograms} | |
type="number" | |
margin="normal" | |
className="input-field" | |
/> | |
<Button | |
variant="contained" | |
color="primary" | |
onClick={this.clearAll} | |
className="button" | |
> | |
Clear All | |
</Button> | |
</form> | |
</div> | |
); | |
} | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment