-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
reham fageer
committed
Aug 15, 2020
1 parent
8822c80
commit b273adf
Showing
5 changed files
with
108 additions
and
12 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import React,{ Component } from 'react'; | ||
|
||
class InputField extends Component{ | ||
constructor(props) { | ||
super(props); | ||
this.state = { | ||
text: null, | ||
blured:false, | ||
}; | ||
} | ||
|
||
updateTextInput(event){ | ||
const newText = event.target.value; | ||
this.setState({text:newText}); | ||
if (newText != "" || !this.props.isRequired) | ||
this.setState({isSet:true}); | ||
this.props.callbackFunction(this.props.id , newText , this.props.isRequired); | ||
|
||
} | ||
componentWillMount(){ | ||
this.props.callbackFunction(this.props.id , '',this.props.isRequired); | ||
} | ||
render(){ | ||
const style = {backgroundColor:'white'}; | ||
if(this.state.blured && this.state.text != null) { | ||
style.backgroundColor = 'green' | ||
} | ||
return ( | ||
<> | ||
<fieldset> | ||
<legend>Enter your {this.props.name}:</legend> | ||
<input placeholder="" type="text" onChange={this.updateTextInput.bind(this)} onBlur={()=>{this.setState({blured:true})}} style={style}/> | ||
{this.props.isRequired == true ? <span> * Required</span> : null} | ||
</fieldset> | ||
</> | ||
) | ||
} | ||
} | ||
|
||
export default InputField; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,51 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import './index.css'; | ||
import App from './App'; | ||
import * as serviceWorker from './serviceWorker'; | ||
import InputField from './components/InputField'; | ||
|
||
class App extends React.Component{ | ||
constructor(props) { | ||
super(props); | ||
this.state= { | ||
displayInputs:false, | ||
name:'', | ||
email:'', | ||
phone:'', | ||
}; | ||
} | ||
handleFieldChange = (id , value,isRequired) => { | ||
if(isRequired && value == '' ) | ||
this.setState({[id]:false}); | ||
else | ||
this.setState({[id]:value}); | ||
} | ||
handleClick = ()=>{ | ||
if(this.state.name !== false && this.state.email !== false && this.state.phone !== false ) | ||
this.setState({displayInputs:true}); | ||
else | ||
alert('There is missing required input !'); | ||
} | ||
|
||
render() { | ||
return( | ||
<div> | ||
<h1> Welcome </h1> | ||
{ !this.state.displayInputs ? | ||
<div className="row"> | ||
<InputField id="name" name={"Name"} callbackFunction={this.handleFieldChange} value={this.state['name']} isRequired={true} /> | ||
<InputField id="email" name={"Email"} callbackFunction={this.handleFieldChange} value={this.state['email']} isRequired={false} /> | ||
<InputField id="phone" name={"Phone"} callbackFunction={this.handleFieldChange} value={this.state['phone']} isRequired={false} /> | ||
<div><Button onClick={this.handleClick} >submit</Button></div> | ||
</div>:<div><h2>{this.state['name'] + " "+ this.state['email'] +" "+ this.state['phone'] }</h2></div>} | ||
</div> | ||
); | ||
} | ||
} | ||
const Button = ({ onClick }) => ( | ||
<button onClick={onClick} type="button"> | ||
Show Inputs | ||
</button> | ||
); | ||
ReactDOM.render( | ||
<React.StrictMode> | ||
<App /> | ||
</React.StrictMode>, | ||
document.getElementById('root') | ||
<App />, | ||
document.getElementById('root') | ||
); | ||
|
||
// If you want your app to work offline and load faster, you can change | ||
// unregister() to register() below. Note this comes with some pitfalls. | ||
// Learn more about service workers: https://bit.ly/CRA-PWA | ||
serviceWorker.unregister(); |