Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: checkbox_group re-export #2212

Merged
merged 10 commits into from
Sep 25, 2024
42 changes: 15 additions & 27 deletions packages/components/src/spectrum/CheckboxGroup.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/* eslint-disable react/no-array-index-key */
import { isElementOfType } from '@deephaven/react-hooks';
import React, { ReactNode, useMemo, useState } from 'react';
import React, { ReactNode, useMemo } from 'react';

Check failure on line 3 in packages/components/src/spectrum/CheckboxGroup.tsx

View workflow job for this annotation

GitHub Actions / unit

Import "ReactNode" is only used as types

Check failure on line 3 in packages/components/src/spectrum/CheckboxGroup.tsx

View workflow job for this annotation

GitHub Actions / unit

Import "ReactNode" is only used as types
import {

Check failure on line 4 in packages/components/src/spectrum/CheckboxGroup.tsx

View workflow job for this annotation

GitHub Actions / unit

Import "SpectrumCheckboxGroupProps" is only used as types

Check failure on line 4 in packages/components/src/spectrum/CheckboxGroup.tsx

View workflow job for this annotation

GitHub Actions / unit

Import "SpectrumCheckboxGroupProps" is only used as types
Checkbox,
CheckboxGroup as SpectrumCheckboxGroup,
SpectrumCheckboxGroupProps,
} from '@adobe/react-spectrum';
import { ensureArray } from '@deephaven/utils';

export type CheckboxGroupProps = {
children: ReactNode;
Expand All @@ -18,38 +20,24 @@
children,
...props
}: CheckboxGroupProps): JSX.Element {
const [checkedState, setCheckedState] = useState<{ [key: number]: boolean }>(
{}
);

const handleCheckboxChange = (index: number) => {
setCheckedState(prevState => ({
...prevState,
[index]: !prevState[index],
}));
};

const wrappedChildren = useMemo(
() =>
React.Children.map(children, (child, index) => {
if (isElementOfType(child, Checkbox)) {
return React.cloneElement(child, {
isSelected: true,
value: `checkbox-${index}`,
onChange: () => handleCheckboxChange(index),
});
}
return (
ensureArray(children).map((child, index) =>
isElementOfType(child, Checkbox) ? (
React.cloneElement(child, {
key: `${index}-${String(child)}`,
value: `${index}-${String(child)}`,
AkshatJawne marked this conversation as resolved.
Show resolved Hide resolved
})
) : (
<Checkbox
isSelected={checkedState[index] || false}
value={`checkbox-${index}`}
onChange={() => handleCheckboxChange(index)}
key={`${index}-${String(child)}`}
value={`${index}-${String(child)}`}
AkshatJawne marked this conversation as resolved.
Show resolved Hide resolved
>
{String(child)}
</Checkbox>
);
}) || [],
[children, checkedState]
)
),
[children]
);

return (
Expand Down
Loading