-
Notifications
You must be signed in to change notification settings - Fork 10
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@khanacademy/wonder-blocks-dropdown": patch | ||
--- | ||
|
||
Allow use of JSX Element as label in SingleSelect and MultiSelect |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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>; | ||
|
@@ -884,6 +888,55 @@ export const CustomOptionItems: StoryComponentType = { | |
}, | ||
}; | ||
|
||
/** | ||
* This example illustrates how a JSX Element can appear as the label if | ||
* `labelAsText` is undefined. | ||
* **Note** this is only supported for SingleSelect and MultiSelect, not Combobox. | ||
*/ | ||
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} | ||
> | ||
{currencies.map((currency, index) => ( | ||
<OptionItem | ||
key={index} | ||
value={String(index)} | ||
horizontalRule="full-width" | ||
label={ | ||
<span> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> | ||
} | ||
/> | ||
))} | ||
</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 | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -150,7 +150,7 @@ describe("MultiSelect", () => { | |||||||||||||||||
|
||||||||||||||||||
// Assert | ||||||||||||||||||
expect(await screen.findByRole("button")).toHaveTextContent( | ||||||||||||||||||
"1 student", | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wow, that's a pretty confusing test result looking at this older code. Where did "student" even come from? Your change seems way more intuitive. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was wondering the same thing! Looks like "1 student" was coming from the wonder-blocks/packages/wonder-blocks-dropdown/src/components/__tests__/multi-select.test.tsx Lines 28 to 35 in 5eedd04
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bea's assumption is correct. This is part of what Danielle fixed with this PR. Previously, we used the label associated for that, but from now on we'll be using whatever it is passed in as a custom option item label. This means that if we don't use a custom label, then |
||||||||||||||||||
"custom item 1", | ||||||||||||||||||
); | ||||||||||||||||||
}); | ||||||||||||||||||
|
||||||||||||||||||
|
There was a problem hiding this comment.
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 alabelAsText
prop to theOptionItem
components with the content (ie. thelocale
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!
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.That said, my specific use case does not use filtering.
There was a problem hiding this comment.
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 thelabelAsText
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!There was a problem hiding this comment.
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 thegetSelectOpenerLabel
invocation. I'm not fully sure if this is the right solution but it might be worth trying it out for bothMultiSelect
andSingleSelect
.