Last active
March 14, 2018 22:56
-
-
Save nahumzs/912c28e3b4cf55e99f97801b31786429 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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> | |
) | |
} |
This file contains hidden or 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
<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