I have a problem with making my function smarter. I achieve good results, but I think about the other solution.
I can take boolean or object as parameter. When the parameter is the object I'd like to keep it as now. When the parameter is a boolean value, I'd like to create an object, where keys are taken from another variable (less important) and values are this boolean.
My solution assumes
Is that OK?
Check my code
const onAddAll = value => {
let items = {}
if (typeof value === 'boolean') {
terms.forEach(term => items[ term ] = value)
}
if (typeof value === 'object') {
items = value
}
const checked = Object.keys(items).filter(k => items[ k ])
setChecked(checked)
}
Imo, I wouldn't need the second condition and make it smarter, bot no idea how. Please help!
I use it here
<Checkbox
items={getItems()}
onChange={onAddAll}
>
<label className="sr-only">
{i18n.t('DROPDOWN.SELECT_ALL')}
</label>
</Checkbox>
and here
<Dropdown>
{[ 'all', 'none' ].map((key, i) => (
<li key={key}
className="dropdown__item">
<div className="dropdown__link"
onClick={onAddAll.bind(this, !i)}>
{i18n.t(`DROPDOWN.${key.toUpperCase()}`)}
</div>
</li>
))}
</Dropdown>