Published
Edited
Dec 19, 2019
Insert cell
Insert cell
html`
<form>
<label>
Name:
<input type="text" name="name" />
</label>
<input type="submit" value="Submit" />
</form>
`
Insert cell
{
const root = html`<div></div>`;
class NameForm extends React.Component {
constructor(props) {
super(props);
this.state = {value: ''};

this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}

handleChange(event) {
this.setState({value: event.target.value.toUpperCase()}); // make input all capital letter
}

handleSubmit(event) {
console.log('A name was submitted: ' + this.state.value);
event.preventDefault(); // without it the page will be directed elsewhere, but no idea why
}

render() {
return jsx`
<form onSubmit=${this.handleSubmit}>
<label>
Name:
<input type="text" value=${this.state.value} onChange=${this.handleChange} />
</label>
<input type="submit" value="Submit" />
</form>
`;
}
}
ReactDOM.render(
jsx`<${NameForm} />`,
root
);
return root;
}
Insert cell
{
const root = html`<div></div>`;
class EssayForm extends React.Component {
constructor(props) {
super(props);
this.state = {
value: 'Please write an essay about your favorite DOM element.'
};

this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}

handleChange(event) {
this.setState({value: event.target.value});
}

handleSubmit(event) {
console.log('An essay was submitted: ' + this.state.value);
event.preventDefault();
}

render() {
return jsx`
<form onSubmit=${this.handleSubmit}>
<label>
Essay:
<textarea value=${this.state.value} onChange=${this.handleChange} />
</label>
<input type="submit" value="Submit" />
</form>
`;
}
}
ReactDOM.render(
jsx`<${EssayForm} />`,
root
);
return root;
}
Insert cell
Insert cell
html`
<select>
<option value="grapefruit">Grapefruit</option>
<option value="lime">Lime</option>
<option selected value="coconut">Coconut</option>
<option value="mango">Mango</option>
</select>
`
Insert cell
{
const root = html`<div></div>`;
class FlavorForm extends React.Component {
constructor(props) {
super(props);
this.state = {value: 'coconut'};

this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}

handleChange(event) {
this.setState({value: event.target.value});
}

handleSubmit(event) {
console.log('Your favorite flavor is: ' + this.state.value);
event.preventDefault();
}

render() {
return jsx`
<form onSubmit=${this.handleSubmit}>
<label>
Pick your favorite flavor:
<select value=${this.state.value} onChange=${this.handleChange}>
<option value="grapefruit">Grapefruit</option>
<option value="lime">Lime</option>
<option value="coconut">Coconut</option>
<option value="mango">Mango</option>
</select>
</label>
<input type="submit" value="Submit" />
</form>
`;
}
}
ReactDOM.render(
jsx`<${FlavorForm} />`,
root
);
return root;
}
Insert cell
Insert cell
html`<input type="file" />`
Insert cell
Insert cell
{
const root = html`<div></div>`;
class Reservation extends React.Component {
constructor(props) {
super(props);
this.state = {
isGoing: true,
numberOfGuests: 2
};

this.handleInputChange = this.handleInputChange.bind(this);
}

handleInputChange(event) {
const target = event.target;
const value = target.type === 'checkbox' ? target.checked : target.value;
const name = target.name;

this.setState({
[name]: value
});
}

render() {
return jsx`
<form>
<label>
Is going:
<input
name="isGoing"
type="checkbox"
checked=${this.state.isGoing}
onChange=${this.handleInputChange} />
</label>
<br />
<label>
Number of guests:
<input
name="numberOfGuests"
type="number"
value=${this.state.numberOfGuests}
onChange=${this.handleInputChange} />
</label>
</form>
`;
}
}
ReactDOM.render(
jsx`<${Reservation} />`,
root
);
return root;
}
Insert cell
Insert cell
{
const root = html`<div></div>`;
ReactDOM.render(jsx`<input value="hi" />`, root);

setTimeout(function() {
ReactDOM.render(jsx`<input value=${null} />`, root); // use null, make it editable; use "world" won't
}, 1000);

return root;
}
Insert cell
import {React, ReactDOM, jsx } from "@j-f1/react"
Insert cell
import { htm, none_doc, hide_display} from "@embracelife/tutorial-utilities"
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more