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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions packages/code-studio/src/styleguide/CheckboxGroups.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import React from 'react';
import { Checkbox, CheckboxGroup, Flex, Text } from '@deephaven/components';
AkshatJawne marked this conversation as resolved.
Show resolved Hide resolved
import SampleSection from './SampleSection';

export function CheckboxGroups(): JSX.Element {
return (
<SampleSection name="checkbox-groups">
<h2 className="ui-title">Checkbox Groups</h2>
<Flex gap="size-100" gridColumn="span 3" height="100%">
<Flex direction="column">
<Text>Single Child</Text>
<CheckboxGroup aria-label="Single Child">
<Checkbox checked={false}>Aaa</Checkbox>
</CheckboxGroup>
</Flex>

<Flex direction="column">
<Text>Multiple Children</Text>
<CheckboxGroup aria-label="Multiple Children">
<Checkbox checked={false}>Aaa</Checkbox>
AkshatJawne marked this conversation as resolved.
Show resolved Hide resolved
<Checkbox checked={false}>Bbb</Checkbox>
<Checkbox checked={false}>Ccc</Checkbox>
</CheckboxGroup>
</Flex>

<Flex direction="column">
<Text>Mixed Children Types</Text>
<CheckboxGroup aria-label="Mixed Children Types">
{/* eslint-disable react/jsx-curly-brace-presence */}
{'String 1'}
{'String 2'}
{444}
{999}
{true}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the styleguide, I think there's a bug with handling of true / false

image

There's probably a stringify that needs to happen somewhere

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I wrap my children in wrappedChildren, I pass in String(child) into the checkbox, which seems to work for the other primitives. Is there any other way I can stringify?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the issue here is that React.Children.map converts true / false to null.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might try ensureArray from @deephaven/utils instead

Copy link
Contributor

@bmingles bmingles Sep 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, you may still need React.Children.map to deal with key props, but you'll need to convert booleans to strings before passing into it.

{false}
<Checkbox checked={false}>Aaa</Checkbox>
</CheckboxGroup>
</Flex>
</Flex>
</SampleSection>
);
}

export default CheckboxGroups;
2 changes: 2 additions & 0 deletions packages/code-studio/src/styleguide/StyleGuide.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import Pickers from './Pickers';
import ListViews from './ListViews';
import ErrorViews from './ErrorViews';
import XComponents from './XComponents';
import CheckboxGroups from './CheckboxGroups';

const stickyProps = {
position: 'sticky',
Expand Down Expand Up @@ -118,6 +119,7 @@ function StyleGuide(): React.ReactElement {
<ItemListInputs />
<DraggableLists />
<TimeSliderInputs />
<CheckboxGroups />
<Dialog />
<Modals />
<ContextMenus />
Expand Down
59 changes: 59 additions & 0 deletions packages/components/src/spectrum/CheckboxGroup.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { isElementOfType } from '@deephaven/react-hooks';
import React, { ReactNode, useMemo, useState } from 'react';
import {
CheckboxGroup as SpectrumCheckboxGroup,
SpectrumCheckboxGroupProps,
} from '@adobe/react-spectrum';
import Checkbox from '../Checkbox';
AkshatJawne marked this conversation as resolved.
Show resolved Hide resolved

export type CheckboxGroupProps = {
children: ReactNode;
} & Omit<SpectrumCheckboxGroupProps, 'children'>;

/**
* Augmented version of the Spectrum CheckboxGroup component that supports
* primitive item children.
*/
export function CheckboxGroup({
children,
...props
}: CheckboxGroupProps): JSX.Element {
const [checkedState, setCheckedState] = useState<{ [key: number]: boolean }>(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to store checkbox state?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I use the DH Checkbox, there is this weird effect where I had to store state, since when I was checking one of the checkbox's in the group, all of the checkboxes were going into a checked state.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So is this unnecessary if you switch to SpectrumCheckbox ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think something like this will work:

const wrappedChildren = useMemo(
  () =>
    ensureArray(children).map(child =>
      isElementOfType(child, Checkbox) ? (
        child
      ) : (
        <Checkbox key={String(child)} value={String(child)}>
          {String(child)}
        </Checkbox>
      )
    ),
  [children]
);

{}
);

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, {
AkshatJawne marked this conversation as resolved.
Show resolved Hide resolved
checked: checkedState[index] || false,
onChange: () => handleCheckboxChange(index),
});
}
return (
<Checkbox
checked={checkedState[index] || false}
onChange={() => handleCheckboxChange(index)}
>
{String(child)}
</Checkbox>
);
}) || [],
[children, checkedState]
);

return (
// eslint-disable-next-line react/jsx-props-no-spreading
<SpectrumCheckboxGroup {...props}>{wrappedChildren}</SpectrumCheckboxGroup>
);
}

export default CheckboxGroup;
2 changes: 0 additions & 2 deletions packages/components/src/spectrum/forms.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
export {
Checkbox as SpectrumCheckbox,
type SpectrumCheckboxProps,
CheckboxGroup,
type SpectrumCheckboxGroupProps as CheckboxGroupProps,
Form,
type SpectrumFormProps as FormProps,
NumberField,
Expand Down
1 change: 1 addition & 0 deletions packages/components/src/spectrum/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export * from './picker';
export * from './Heading';
export * from './Text';
export * from './View';
export * from './CheckboxGroup';

/**
* Custom DH spectrum utils
Expand Down
Loading