-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
42f62ba
Show custom option item as label for single select
67f2c24
add custom label support for multiselect
7240da9
add changeset
60c63e2
add stories
5eedd04
remove unused
bb10fdf
rename stories to make them clearer
5af63d2
add showLabelAsText prop
313539e
Merge branch 'main' into select-opener-label
f7969af
update argtypes
b1c8b50
update prop name
1409fab
update changeset
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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,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> | ||
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> | ||
} | ||
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
.