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

[SelectUnstyled] Add ability to post the select's value when submitting a form #33697

Merged
merged 7 commits into from Aug 12, 2022

Conversation

michaldudak
Copy link
Member

@michaldudak michaldudak commented Jul 29, 2022

When a name prop is provided, the (Multi)SelectUnstyled component renders a hidden input holding the currently selected value(s).
As the value of a hidden input must be a string, the new formValueProvider prop was added to control the serialization of the (Multi)Select's value.

Closes #30992

@mui-bot
Copy link

mui-bot commented Jul 29, 2022

Details of bundle changes

@material-ui/unstyled: parsed: +0.67% , gzip: +0.53%

Generated by 🚫 dangerJS against 061d915

@michaldudak michaldudak added component: select This is the name of the generic UI component, not the React module! package: base-ui Specific to @mui/base labels Aug 1, 2022
@michaldudak michaldudak requested a review from a team August 1, 2022 08:49
@michaldudak michaldudak added the new feature New feature or request label Aug 1, 2022
@michaldudak michaldudak marked this pull request as ready for review August 1, 2022 08:49
* Used to set a value of a hidden input associated with the select,
* so that the selected value can be posted with a form.
*/
formValueProvider?: (option: SelectOption<TValue> | null) => string | number;
Copy link
Member

Choose a reason for hiding this comment

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

Why is this here string | number, but in the MultiSelect it is only string | string[]. Should this be string only?

Copy link
Member

Choose a reason for hiding this comment

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

Not sure on the name. Should it be simply getValue?

Copy link
Member Author

Choose a reason for hiding this comment

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

Why is this here string | number, but in the MultiSelect it is only string | string[]. Should this be string only?

The native select's (and hidden input's as well) value is typed as string | number | readonly string[] | undefined. We don't have to artificially restrict numbers.

Should it be simply getValue?

getValue doesn't indicate it's a value serialized for a form. Perhaps serializeValue (or getSerializedValue, or getFormValue)?

Copy link
Member

Choose a reason for hiding this comment

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

getSerializedValue sounds great 👍

The native select's (and hidden input's as well) value is typed as string | number | readonly string[] | undefined. We don't have to artificially restrict numbers.

Can we just use this type then? We can omit some values in the single select

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point. I suppose we don't even need to omit values in a single select - if someone really wants to convert a single value to string[], there are no technical reasons why they shouldn't.

/**
* Callback fired when an option is selected.
*/
onChange?: (value: TValue | null) => void;
/**
* A function used to convert the option value to a string.
* It is used to navigate through values with keyboard.
Copy link
Member

Choose a reason for hiding this comment

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

I don't really understand from the description what the prop is used for and how it is different than formValueProvider

*
* @default defaultOptionStringifier
*/
optionStringifier: PropTypes.func,
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
optionStringifier: PropTypes.func,
getStringifiedOption: PropTypes.func,

Maybe? I am not sure, but I would use getX as a prefix.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm fine with both. It's an existing parameter of useSelect, however, so I wouldn't rename it in this PR.

Copy link
Member

@mnajdova mnajdova left a comment

Choose a reason for hiding this comment

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

Logic looks good, left one final comment on the description for one of the props.

@michaldudak michaldudak merged commit 2b96840 into mui:master Aug 12, 2022
@michaldudak michaldudak deleted the select-form-submit branch August 12, 2022 07:21
z-index: 1;
`;

const Header = styled('h1')(
Copy link
Member

Choose a reason for hiding this comment

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

This h1 is wrong at multiple levels:

  • WAVE

Screenshot 2022-08-28 at 13 34 50

  • Ahrefs

Screenshot 2022-08-28 at 13 35 54

I'm taking care of it in #34116.


function CustomSelect(props) {
const components = {
Root: StyledButton,
Copy link
Member

Choose a reason for hiding this comment

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

Are we sure about the use of a <button>? In https://www.w3.org/WAI/ARIA/apg/example-index/combobox/combobox-select-only.html, it's a <div role="combobox">. Curiously, all the other libraries I can benchmark use a button like us.

Copy link
Member Author

Choose a reason for hiding this comment

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

Unless it causes any issues, I'd like to keep it this way. The link you mentioned points to an example, not reference documentation, so I assume it's not an error to use a button here.

daniel-rabe pushed a commit to daniel-rabe/material-ui that referenced this pull request Nov 29, 2022
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: base-ui Specific to @mui/base
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[SelectUnstyled] Allow the component's value to be posted to a server
4 participants