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

[joy-ui][Select] Support selection of multiple options #39454

Merged
merged 29 commits into from
Oct 30, 2023

Conversation

sai6855
Copy link
Contributor

@sai6855 sai6855 commented Oct 15, 2023

@sai6855 sai6855 added new feature New feature or request component: select This is the name of the generic UI component, not the React module! package: joy-ui Specific to @mui/joy labels Oct 15, 2023
@sai6855 sai6855 marked this pull request as draft October 15, 2023 05:47
@mui-bot
Copy link

mui-bot commented Oct 15, 2023

Netlify deploy preview

Bundle size report

Details of bundle changes (Toolpad)
Details of bundle changes

Generated by 🚫 dangerJS against 5ba3592

@sai6855 sai6855 marked this pull request as ready for review October 19, 2023 14:11

const [SlotRoot, rootProps] = useSlot('root', {
ref: handleRef,
className: classes.root,
elementType: SelectRoot,
externalForwardedProps,
ownerState,
ownerState: ownerState as SelectOwnerState<any, boolean>,
Copy link
Member

Choose a reason for hiding this comment

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

I tried to improve this but failed 😣. TypeScript is one of the reasons I try to avoid components with union value type 🥲. It's really hard to get it right.

Thanks for working on this!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I feel you, types in this PR tested my patience 😅

Note that in multiple selection mode, the `value` prop (and `defaultValue`) is an array.

{{"demo": "SelectMultiple.js", "defaultCodeOpen": false}}

Copy link
Member

Choose a reason for hiding this comment

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

I suggest to add a couple of more demos:

#### Selected value appearance

Display avatars of selected users end with "x friends".

#### Form submission

Show what the value looks like after submitted a form.

Copy link
Member

@siriwatknp siriwatknp left a comment

Choose a reason for hiding this comment

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

The code looks good to me. Maybe adding more demos as suggested.

renderValue={(selected) => (
<Box sx={{ display: 'flex', gap: '0.25rem' }}>
{selected.map((selectedOption) => (
<Chip variant="outlined" color="success">
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
<Chip variant="outlined" color="success">
<Chip variant="soft" color="primary">

I think it looks better than outlined and success.

image

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed, I'll update

@sai6855
Copy link
Contributor Author

sai6855 commented Oct 20, 2023

@siriwatknp PR is ready for review, added demos as suggested

onSubmit={(event) => {
event.preventDefault();
const formData = new FormData(event.currentTarget);
const formJson = Object.fromEntries(formData.entries());
Copy link
Member

Choose a reason for hiding this comment

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

It is strange that formJson.pets is a string, not an array. Is this expected?

I got "[\"dog\"\" instead of ["dog"].

Copy link
Contributor Author

@sai6855 sai6855 Oct 23, 2023

Choose a reason for hiding this comment

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

When you console.log formJson.pets it indeed prints array, so issue you described maybe have to do something with alert On logging typeof formJson.pets i'm getting type as string. Ideally It should be object, let me check

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is this expected?

Yes, this is expected because if Select has multiple prop then value of hidden input is stringified. so users need to do JSON.parse to convert it to array.

if (Array.isArray(selectedOption)) {
if (selectedOption.length === 0) {
return '';
}
return JSON.stringify(selectedOption.map((o) => o.value));
}

So I've modified demo and parsed string to array. Now demo is looking good

Copy link
Member

Choose a reason for hiding this comment

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

Got it.

@sai6855 sai6855 changed the title [Select][joy-ui] Add multiple prop [Select][joy-ui] Support selection of multiple options Oct 23, 2023
Copy link
Member

@siriwatknp siriwatknp left a comment

Choose a reason for hiding this comment

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

👍 Well done!

@sai6855
Copy link
Contributor Author

sai6855 commented Oct 27, 2023

@siriwatknp Can this be merged ?

@siriwatknp siriwatknp changed the title [Select][joy-ui] Support selection of multiple options [joy-ui][Select] Support selection of multiple options Oct 30, 2023
@siriwatknp siriwatknp merged commit f39a827 into mui:master Oct 30, 2023
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: select This is the name of the generic UI component, not the React module! new feature New feature or request package: joy-ui Specific to @mui/joy
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Select][joy-ui] support multiple selection
3 participants