Skip to content

Instantly share code, notes, and snippets.

@tps2015gh
Last active October 8, 2018 18:41
Show Gist options
  • Save tps2015gh/7a7d3b1834fe9c81558feb5748bf030e to your computer and use it in GitHub Desktop.
Save tps2015gh/7a7d3b1834fe9c81558feb5748bf030e to your computer and use it in GitHub Desktop.
Sample React communication between child and parent !!
this code I create + tested
on
https://codesandbox.io/s/14yyjqk1m3
it 's OK
since 2018-10-09
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="theme-color" content="#000000">
<!--
manifest.json provides metadata used when your web app is added to the
homescreen on Android. See https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json">
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>
You need to enable JavaScript to run this app.
</noscript>
<div id="root"></div>
<div id="section_form"></div>
<div id="section_form2"></div>
<div id="name_form"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
import React from "react";
import ReactDOM from "react-dom";
//==========================================
class InputIdCard extends React.Component {
constructor(props) {
super(props);
this.state = { value: "12234", parent: props.parent };
}
onOK100() {
console.log("ON OK 100 ");
}
onChangeHandler(e) {
console.log("InputIdCard.onChangeHanlder " + e.target.value);
var val1 = e.target.value;
var state = { value: val1 };
this.setState(state);
console.log(this.state.parent);
if (this.state.parent === undefined) {
return;
}
console.log(this.state.parent.getType());
if (val1 === "1000") {
this.state.parent.setFormColor("lightgreen");
this.state.parent.setAllowSubmit();
} else {
this.state.parent.setFormColor("pink");
this.state.parent.setDenySubmit();
}
}
render() {
//var style = { backgroundColor: 'lightyellow', maxLength: 13 }
return (
<div>
<label for={this.props.name}>{this.props.label}: </label>
<input
className="form-control "
type="text"
parent={this.props.parent}
value={this.state.value}
name={this.props.name}
id={this.props.name}
placeholder={this.props.label}
onChange={this.onChangeHandler.bind(this)}
/>
</div>
);
}
}
//==========================================
/**
* @param: action
* @param: name
* @param: title
*/
class FormCheck extends React.Component {
constructor(props) {
super(props);
var style1 = {
border: "1px solid silver",
padding: "3px",
backgroundColor: props.bgcolor,
borderRadius: "25px"
};
let bt1style = {
display: "visible",
disabled: "disabled"
};
this.state = { style: style1, bt1style: bt1style };
}
getType() {
return "FormCheck";
}
setAllowSubmit() {
let bt1s1 = Object.assign({}, this.state.bt1style);
bt1s1.disabled = false;
this.setState({ bt1style: bt1s1 });
}
setDenySubmit() {
let bt1s1 = Object.assign({}, this.state.bt1style);
bt1s1.disabled = true;
this.setState({ bt1style: bt1s1 });
}
setFormColor(colorName) {
console.log("> FormCheck setFormColor(" + colorName + ")");
let style1 = Object.assign({}, this.state.style);
style1.backgroundColor = colorName;
this.setState({ style: style1 });
}
render() {
return (
<form
action={this.props.action}
method="POST"
className="form"
name={this.props.name}
id={this.props.name}
style={this.state.style}
>
<div>
<h1>{this.props.title}</h1>
<InputIdCard
name="idcard"
parent={this}
onChange={this.props.onIdCardChange}
label="กรุณาป้อนเลข ( ป้อน 1000 เพื่อแบันทึกได้ )"
/>
<br />
<input
type="submit"
name="bt1"
style={this.state.bt1style}
className="form-control btn btn-info "
value="ค้นหา"
disabled={this.state.bt1style.disabled}
/>
<br />
<br />
</div>
</form>
);
}
}
var onIdCardChange = function(e) {
console.log("onIdCardChange(e) " + e.target);
};
//==========================================
ReactDOM.render(
<FormCheck onIdCardChange={onIdCardChange} name="f1" bgcolor="lightgrey" />,
document.getElementById("section_form")
);
ReactDOM.render(
<FormCheck onIdCardChange={onIdCardChange} name="f2" bgcolor="lightyellow" />,
document.getElementById("section_form2")
);
ReactDOM.render(<InputIdCard />, document.getElementById("name_form"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment