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

Update label in SingleSelect and MultiSelect #2354

Merged
merged 11 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .changeset/thirty-shirts-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@khanacademy/wonder-blocks-dropdown": minor
---

Allow use of JSX Element as label in SingleSelect and MultiSelect
12 changes: 12 additions & 0 deletions __docs__/wonder-blocks-dropdown/multi-select.argtypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ const argTypes: ArgTypes = {
type: {summary: "Labels"},
},
},
showOpenerLabelAsText: {
control: {type: "boolean"},
description: `When false, the SelectOpener can show a Node as a label. When true, the
SelectOpener will use a string as a label. If using custom OptionItems, a
plain text label can be provided with the \`labelAsText\` prop.
Defaults to true.`,

table: {
type: {summary: "boolean"},
defaultValue: {summary: "true"},
},
},
};

export default argTypes;
58 changes: 57 additions & 1 deletion __docs__/wonder-blocks-dropdown/multi-select.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ import ComponentInfo from "../../.storybook/components/component-info";
import packageConfig from "../../packages/wonder-blocks-dropdown/package.json";
import multiSelectArgtypes from "./multi-select.argtypes";
import {defaultLabels} from "../../packages/wonder-blocks-dropdown/src/util/constants";
import {allCountries, allProfilesWithPictures} from "./option-item-examples";
import {
allCountries,
allProfilesWithPictures,
locales,
chatIcon,
} from "./option-item-examples";
import {OpenerProps} from "../../packages/wonder-blocks-dropdown/src/util/types";
import Strut from "../../packages/wonder-blocks-layout/src/components/strut";

Expand Down Expand Up @@ -650,3 +655,54 @@ export const CustomOptionItems: StoryComponentType = {
),
],
};

/**
* This example illustrates how a JSX Element can appear as the label by setting
* `showOpenerLabelAsText` to false. Note that in this example, we define
* `labelAsText` on the OptionItems to ensure that filtering works correctly.
*/
export const CustomOptionItemsWithNodeLabel: StoryComponentType = {
render: function Render() {
const [opened, setOpened] = React.useState(true);
const [selectedValues, setSelectedValues] = React.useState<
Array<string>
>([]);

const handleChange = (selectedValues: Array<string>) => {
setSelectedValues(selectedValues);
};

const handleToggle = (opened: boolean) => {
setOpened(opened);
};

return (
<MultiSelect
Copy link
Member

Choose a reason for hiding this comment

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

I played around with the changes and found if we set isFilterable in this story, the filtering functionality doesn't work as expected! However, if we add a labelAsText prop to the OptionItem components with the content (ie. the locale in this example), filtering works as expected.

I'm wondering, should we use labelAsText prop when we use a node label? And if so, would that solve for the original issue where the select opener is labeled with an empty string? Let me know though if there's some context I'm missing!

cc: @jandrade , I think he was running into similar things when he worked on the combobox!

Copy link
Contributor Author

@daniellewhyte daniellewhyte Nov 12, 2024

Choose a reason for hiding this comment

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

That's a good callout, and the proposed solution does solve the original issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@beaesguerra Actually I don't think this works. If you add labelAsText to the option item, you see plain text as the label for the menu.
Screenshot 2024-11-13 at 9 07 33 AM

That said, my specific use case does not use filtering.

Copy link
Member

Choose a reason for hiding this comment

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

Ah I see, we want to include the icon in the field! Thanks for trying that out!

For other use cases that would need filtering, the labelAsText prop would fix the filtering issue (with the drawback that the menu opener would show the labelAsText instead of the custom option). With that said, the changes look good to me, though I would also like to get @jandrade's thoughts since he has more context around this!

Copy link
Member

Choose a reason for hiding this comment

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

I understand that the specific use case in this PR doesn't need filtering, but I worry that adding this feature while introducing a known issue might be counterproductive if there's another call site that needs both things (custom options + filtering).

I'd really recommend applying the fix/change in this PR before landing this change.

I was thinking that it might be solved by changing the filter logic to use .toString() after the getSelectOpenerLabel invocation. I'm not fully sure if this is the right solution but it might be worth trying it out for both MultiSelect and SingleSelect.

onChange={handleChange}
selectedValues={selectedValues}
onToggle={handleToggle}
opened={opened}
showOpenerLabelAsText={false}
isFilterable={true}
>
{locales.map((locale, index) => (
<OptionItem
key={index}
value={String(index)}
label={
<span>
{chatIcon} {locale}
</span>
}
labelAsText={locale}
/>
))}
</MultiSelect>
);
},
decorators: [
(Story): React.ReactElement<React.ComponentProps<typeof View>> => (
<View style={styles.wrapper}>{Story()}</View>
),
],
};
25 changes: 25 additions & 0 deletions __docs__/wonder-blocks-dropdown/option-item-examples.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import * as React from "react";
import userCircleIcon from "@phosphor-icons/core/duotone/user-circle-duotone.svg";
import chatBubbleIcon from "@phosphor-icons/core/regular/chats.svg";
import bitcoinIcon from "@phosphor-icons/core/regular/currency-btc.svg";
import euroIcon from "@phosphor-icons/core/regular/currency-eur.svg";
import dollarIcon from "@phosphor-icons/core/regular/currency-dollar.svg";
import yenIcon from "@phosphor-icons/core/regular/currency-jpy.svg";
import {PhosphorIcon} from "@khanacademy/wonder-blocks-icon";

export const allCountries = [
Expand Down Expand Up @@ -301,3 +306,23 @@ export const allProfilesWithPictures = [
picture: icon,
},
];

export const currencies = [
{name: "Bitcoin", icon: bitcoinIcon},
{name: "Dollars", icon: dollarIcon},
{name: "Yen", icon: yenIcon},
{name: "Euros", icon: euroIcon},
];

export const locales = [
"অসমীয়া",
"Azərbaycanca",
"čeština",
"dansk",
"Ελληνικά",
"ગુજરાતી",
"magyar",
"Bahasa Indonesia",
];

export const chatIcon = <PhosphorIcon icon={chatBubbleIcon} size={"small"} />;
12 changes: 12 additions & 0 deletions __docs__/wonder-blocks-dropdown/single-select.argtypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ const argTypes: ArgTypes = {
type: {summary: "Labels"},
},
},
showOpenerLabelAsText: {
control: {type: "boolean"},
description: `When false, the SelectOpener can show a Node as a label. When true, the
SelectOpener will use a string as a label. If using custom OptionItems, a
plain text label can be provided with the \`labelAsText\` prop.
Defaults to true.`,

table: {
type: {summary: "boolean"},
defaultValue: {summary: "true"},
},
},
};

export default argTypes;
58 changes: 57 additions & 1 deletion __docs__/wonder-blocks-dropdown/single-select.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ import ComponentInfo from "../../.storybook/components/component-info";
import singleSelectArgtypes from "./single-select.argtypes";
import {IconMappings} from "../wonder-blocks-icon/phosphor-icon.argtypes";
import {defaultLabels} from "../../packages/wonder-blocks-dropdown/src/util/constants";
import {allCountries, allProfilesWithPictures} from "./option-item-examples";
import {
allCountries,
allProfilesWithPictures,
currencies,
} from "./option-item-examples";
import {OpenerProps} from "../../packages/wonder-blocks-dropdown/src/util/types";

type StoryComponentType = StoryObj<typeof SingleSelect>;
Expand Down Expand Up @@ -884,6 +888,58 @@ export const CustomOptionItems: StoryComponentType = {
},
};

/**
* This example illustrates how a JSX Element can appear as the label if
* `labelAsText` is undefined. Note that in this example, we define `labelAsText`
* on the OptionItems to ensure that filtering works correctly.
*/
export const CustomOptionItemWithNodeLabel: StoryComponentType = {
render: function Render() {
const [opened, setOpened] = React.useState(true);
const [selectedValue, setSelectedValue] = React.useState("");

const handleChange = (selectedValue: string) => {
setSelectedValue(selectedValue);
};

const handleToggle = (opened: boolean) => {
setOpened(opened);
};

return (
<View style={styles.wrapper}>
<SingleSelect
placeholder="Select your currency"
onChange={handleChange}
selectedValue={selectedValue}
onToggle={handleToggle}
opened={opened}
showOpenerLabelAsText={false}
isFilterable={true}
>
{currencies.map((currency, index) => (
<OptionItem
key={index}
value={String(index)}
horizontalRule="full-width"
label={
<span>
Copy link
Member

Choose a reason for hiding this comment

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

This tested well in VoiceOver! I couldn't get my Windows VM to load the local Storybook environment for NVDA but I'm pretty sure it will be fine.

<PhosphorIcon
icon={currency.icon}
size={"small"}
/>
{currency.name}
</span>
}
labelAsText={currency.name}
/>
))}
</SingleSelect>
</View>
);
},
};

/**
* This example illustrates how you can use the `OptionItem` component to
* display a virtualized list with custom option items. Note that in this
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
/* eslint-disable no-constant-condition */
/* eslint-disable max-lines */
import * as React from "react";
import {fireEvent, render, screen, waitFor} from "@testing-library/react";
import {
fireEvent,
render,
screen,
waitFor,
within,
} from "@testing-library/react";
import {
userEvent as ue,
PointerEventsCheckLevel,
Expand Down Expand Up @@ -264,6 +270,28 @@ describe("MultiSelect", () => {
// Assert
expect(opener).toHaveAttribute("data-testid", "some-test-id");
});

it("can render a Node as a label", async () => {
// Arrange
doRender(
<MultiSelect
onChange={onChange}
selectedValues={["1"]}
showOpenerLabelAsText={false}
>
<OptionItem label={<div>custom item 1</div>} value="1" />
<OptionItem label={<div>custom item 2</div>} value="2" />
<OptionItem label={<div>custom item 3</div>} value="3" />
</MultiSelect>,
);

// Act
const opener = await screen.findByRole("button");
const menuLabel = within(opener).getByText("custom item 1");

// Assert
expect(menuLabel).toBeVisible();
});
});

describe("Controlled component", () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable max-lines */
import * as React from "react";
import {fireEvent, render, screen} from "@testing-library/react";
import {fireEvent, render, screen, within} from "@testing-library/react";
import {
userEvent as ue,
PointerEventsCheckLevel,
Expand Down Expand Up @@ -135,6 +135,35 @@ describe("SingleSelect", () => {
// Assert
expect(opener).toHaveTextContent("Plain Toggle A");
});
it("can render a Node as a label", async () => {
// Arrange
doRender(
<SingleSelect
placeholder="Default placeholder"
onChange={jest.fn()}
selectedValue="toggle_a"
showOpenerLabelAsText={false}
>
<OptionItem
label={<div>custom item A</div>}
value="toggle_a"
labelAsText="Plain Toggle A"
/>
<OptionItem
label={<div>custom item B</div>}
value="toggle_b"
labelAsText="Plain Toggle B"
/>
</SingleSelect>,
);

// Act
const opener = await screen.findByRole("button");
const menuLabel = within(opener).getByText("custom item A");

// Assert
expect(menuLabel).toBeVisible();
});
});

describe("mouse", () => {
Expand Down
20 changes: 16 additions & 4 deletions packages/wonder-blocks-dropdown/src/components/multi-select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import type {
OptionItemComponent,
OptionItemComponentArray,
} from "../util/types";
import {getLabel} from "../util/helpers";
import {getLabel, getSelectOpenerLabel} from "../util/helpers";

export type Labels = {
/**
Expand Down Expand Up @@ -91,6 +91,13 @@ type DefaultProps = Readonly<{
* Whether to display shortcuts for Select All and Select None.
*/
shortcuts: boolean;
/**
* When false, the SelectOpener can show a Node as a label. When true, the
* SelectOpener will use a string as a label. If using custom OptionItems, a
* plain text label can be provided with the `labelAsText` prop.
* Defaults to true.
*/
showOpenerLabelAsText: boolean;
}>;

type Props = AriaProps &
Expand Down Expand Up @@ -227,6 +234,7 @@ export default class MultiSelect extends React.Component<Props, State> {
light: false,
shortcuts: false,
selectedValues: [],
showOpenerLabelAsText: true,
};

constructor(props: Props) {
Expand Down Expand Up @@ -315,8 +323,9 @@ export default class MultiSelect extends React.Component<Props, State> {
onChange([]);
};

getMenuText(children: OptionItemComponentArray): string {
const {implicitAllEnabled, selectedValues} = this.props;
getMenuText(children: OptionItemComponentArray): string | JSX.Element {
const {implicitAllEnabled, selectedValues, showOpenerLabelAsText} =
this.props;
const {noneSelected, someSelected, allSelected} = this.state.labels;
const numSelectedAll = children.filter(
(option) => !option.props.disabled,
Expand All @@ -338,7 +347,10 @@ export default class MultiSelect extends React.Component<Props, State> {
);

if (selectedItem) {
const selectedLabel = getLabel(selectedItem?.props);
const selectedLabel = getSelectOpenerLabel(
showOpenerLabelAsText,
selectedItem?.props,
);
if (selectedLabel) {
return selectedLabel;
// If the label is a ReactNode and `labelAsText` is not set,
Expand Down
Loading