Skip to content

Commit

Permalink
fix: fix checkbox group all selection
Browse files Browse the repository at this point in the history
  • Loading branch information
glenkurniawan-adslot committed Feb 27, 2024
1 parent 4711886 commit 2ace265
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/components/CheckboxGroup/CheckboxGroup.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,13 @@ export const NestedCheckboxes: Story = {
<CheckboxGroup indent>
<CheckboxGroup.All label="Sports" values={allHobbies.sports} />
{allHobbies.sports.map((item) => (
<CheckboxGroup.Item key={item} value={item} label={_.capitalize(item)} />
<CheckboxGroup.Item key={item} value={item} label={_.capitalize(item)} disabled={item === 'swimming' }/>
))}
</CheckboxGroup>
<CheckboxGroup indent>
<CheckboxGroup.All label="Other" values={allHobbies.other} />
{allHobbies.other.map((item) => (
<CheckboxGroup.Item key={item} value={item} label={_.capitalize(item)} />
<CheckboxGroup.Item key={item} value={item} label={_.capitalize(item)} disabled/>
))}
</CheckboxGroup>
</CheckboxGroup>
Expand Down
22 changes: 21 additions & 1 deletion src/components/CheckboxGroup/CheckboxGroupContext.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,37 @@ const CheckboxGroupProvider = ({
const value = parentCtx.value || valueProp;

const context = React.useMemo(() => {
const disabledComponents = [];
const findDisabledComponents = (c) => {
if (c.props?.children) {
findDisabledComponents(c.props.children);
} else if (Array.isArray(c)) {
_.forEach(c, (child) => {
findDisabledComponents(child);
});
} else {
if (c.props?.disabled) {
disabledComponents.push(c);
}
}
};

findDisabledComponents(children);

const disabledValues = _.map(disabledComponents, (dc) => dc.props?.value);

const getIsItemChecked = (checkboxValue) => {
if (getIsChecked) return getIsChecked(checkboxValue, value);

return value.includes(checkboxValue);
};

const getIsAllChecked = (values) => {
if (_.isEmpty(values)) return false;
const result = _(values)
.map((item) => getIsItemChecked(item))
.uniq()
.value();

return result.length === 1 ? result[0] : 'partial';
};

Expand Down Expand Up @@ -64,6 +83,7 @@ const CheckboxGroupProvider = ({
getIsAllChecked,
onItemChange,
onAllChange,
disabledValues,
};
}, [getIsChecked, value, name, onChange, variant]);

Check failure on line 88 in src/components/CheckboxGroup/CheckboxGroupContext.jsx

View workflow job for this annotation

GitHub Actions / build (20.x)

React Hook React.useMemo has a missing dependency: 'children'. Either include it or remove the dependency array

Expand Down
9 changes: 6 additions & 3 deletions src/components/CheckboxGroup/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,20 @@ const CheckboxGroupAll = ({ className, label = 'All', values, ...rest }) => {
const groupCtx = useCheckboxGroup();
invariant(!_.isEmpty(groupCtx), 'CheckboxGroup.All: must be used as children of CheckboxGroup');

const { onAllChange, getIsAllChecked, name, variant } = groupCtx;
const { onAllChange, getIsAllChecked, name, variant, disabledValues } = groupCtx;

const enabledValues = _.filter(values, (value) => !disabledValues.includes(value));

return (
<Checkbox
{...rest}
className={classnames(className, 'is-all')}
name={name}
label={label}
checked={getIsAllChecked(values)}
onChange={onAllChange(values)}
checked={getIsAllChecked(enabledValues)}
onChange={onAllChange(enabledValues)}
variant={variant}
disabled={_.isEqual(values, disabledValues)}
/>
);
};
Expand Down

0 comments on commit 2ace265

Please sign in to comment.