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
Merged
Show file tree
Hide file tree
Changes from 5 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
46 changes: 46 additions & 0 deletions packages/code-studio/src/styleguide/CheckboxGroups.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import React from 'react';
import { CheckboxGroup, Flex, Text } from '@deephaven/components';
// eslint-disable-next-line no-restricted-imports
import { Checkbox } from '@adobe/react-spectrum';
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>Aaa</Checkbox>
</CheckboxGroup>
</Flex>

<Flex direction="column">
<Text>Multiple Children</Text>
<CheckboxGroup aria-label="Multiple Children">
<Checkbox>Aaa</Checkbox>
<Checkbox>Bbb</Checkbox>
<Checkbox>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>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
49 changes: 49 additions & 0 deletions packages/components/src/spectrum/CheckboxGroup.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/* eslint-disable react/no-array-index-key */
import { isElementOfType } from '@deephaven/react-hooks';
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;
} & Omit<SpectrumCheckboxGroupProps, 'children'>;

/**
* Augmented version of the Spectrum CheckboxGroup component that supports
* primitive item children.
*/
export function CheckboxGroup({
children,
...props
}: CheckboxGroupProps): JSX.Element {
const wrappedChildren = useMemo(
() =>
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
key={`${index}-${String(child)}`}
value={`${index}-${String(child)}`}
AkshatJawne marked this conversation as resolved.
Show resolved Hide resolved
>
{String(child)}
</Checkbox>
)
),
[children]
);

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