Skip to content

Instantly share code, notes, and snippets.

@nahumzs
Last active March 14, 2018 22:56
Show Gist options
  • Save nahumzs/912c28e3b4cf55e99f97801b31786429 to your computer and use it in GitHub Desktop.
Save nahumzs/912c28e3b4cf55e99f97801b31786429 to your computer and use it in GitHub Desktop.
export default class Form extends Component {
canIDoSomething = () => {
return Promise.resolve(true)
}
render() {
const extendedProps = {
isFormDisabled: this.props.isDisabled,
canIDoSomething: this.canIDoSomething,
};
// this is a React utility to map over the component's children and
// allows us to clone and extend the Form’s children
const children = React.Children.map(this.props.children, (child) => {
return React.cloneElement(child, extendedProps);
});
return (
<form>
{children}
</form>
);
}
}
export const Input = (props) => {
return <input type="text" disabled={props.isFormDisabled} />
}
export const Button = (props) => {
const doSomething = () => {
props.canIDoSomething().then(response => alert(response))
}
return (
<button
disabled={props.isFormDisabled}
onClick={doSomething}
>
go
</button>
)
}
<Form>
<Input />
<Button />
</Form>
{
// will extend its children with the prop isDisabled so
// internally each child will have access to it
}
<Form isDisabled>
<Input />
<Button />
</Form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment