Skip to content

Instantly share code, notes, and snippets.

@joeporpeglia
Last active June 30, 2017 16:25
Show Gist options
  • Select an option

  • Save joeporpeglia/aab0f72ed26959a95575c6a246d89049 to your computer and use it in GitHub Desktop.

Select an option

Save joeporpeglia/aab0f72ed26959a95575c6a246d89049 to your computer and use it in GitHub Desktop.
Component Reusability
document
.querySelector('.btn-save')
.addEventListener(
'click',
handleSave
);
const isSaving = false;
const handleSave = () => {
if (isSaving) {
return;
}
isSaving = true;
showLoader();
saveForm();
};
document
.querySelector('.btn-save')
.addEventListener(
'click',
handleSave
);
const isSaving = false;
const handleSave = () => {
if (isSaving) {
return;
}
isSaving = true;
saveForm();
};
document
.querySelector('.btn-save')
.addEventListener(
'click',
handleSave
);
const handleSave = () => {
saveForm();
};
const composed = () => outer(inner());
const Composed = () => (
<Outer>
<Inner />
</Outer>
);
const Composed = () => (
<Outer children={<Inner />} />
);
type DropdownProps = {
expanded: boolean,
value: string | number,
options: { [optionValue: string | number]: string | React$Element },
};
const Dropdown = props => (
<div>
<button aria-haspopup="true" aria-expanded={props.expanded}>
{props.options[props.value]}
</button>
<ul aria-hidden={!props.expanded}>
{Object.keys(props.options).map(value => (
<li key={value}>
{props.options[value]}
</li>
))}
</ul>
</div>
);
type DropdownProps = {
className: string,
options: Array<string>
| Array<number>
| Array<{ label: React$Element, value: string | number }>
onChange: Function,
placeholder: string | Element,
value: string | number,
defaultValue: string | number,
readOnly: boolean,
optionsMaxHeight: number,
calculateWidth: boolean,
};
.btn-save {
background: #1ab7ea;
color: #ffffff;
}
<form>
<button class=”btn-save”>
Save
</button>
</form>
const withDropdownHandlers = WrappedComponent => {
return props => (
<WrappedComponent
{...props}
onExpand={() => props.setExpanded(true)}
onCollapse={() => props.setExpanded(false)}
onChange={props.setValue}
/>
);
};
const withDropdownState = WrappedComponent => {
return class extends Component {
render() {
return (
<WrappedComponent
{...this.props}
expanded={this.state.expanded}
value={this.state.value}
setExpanded={expanded => this.setState({ expanded })}
setValue={value => this.setState({ value })}
/>
);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment