Skip to content

Commit

Permalink
make changes based on PR
Browse files Browse the repository at this point in the history
  • Loading branch information
AkshatJawne committed Sep 20, 2024
1 parent 99abc1d commit 64f50bd
Showing 1 changed file with 15 additions and 27 deletions.
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 @@ export function CheckboxGroup({
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)}`,
})
) : (
<Checkbox
isSelected={checkedState[index] || false}
value={`checkbox-${index}`}
onChange={() => handleCheckboxChange(index)}
key={`${index}-${String(child)}`}
value={`${index}-${String(child)}`}
>
{String(child)}
</Checkbox>
);
}) || [],
[children, checkedState]
)
),
[children]
);

return (
Expand Down

0 comments on commit 64f50bd

Please sign in to comment.