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

[RFC][Select] Rendering the selected value in SSR & during hydration #54

Open
mj12albert opened this issue Oct 16, 2023 · 2 comments Β· May be fixed by #541
Open

[RFC][Select] Rendering the selected value in SSR & during hydration #54

mj12albert opened this issue Oct 16, 2023 · 2 comments Β· May be fixed by #541
Labels
component: select This is the name of the generic UI component, not the React module!

Comments

@mj12albert
Copy link
Member

mj12albert commented Oct 16, 2023

What's the problem? πŸ€”

Raised in #37, to refine the renderValue API to support rendering Select's selected value in SSR and during hydration

What are the requirements? ❓

  • I would expect to be able to render the value to look exactly like a (custom) option
  • Avoid breaking changes required of Material UI v6

What are our options? πŸ’‘

1. Provide an options prop

useSelect already accepts an optional options param, though it will ignore JSX children

The Select component does not pass (or even accept) options to the underlying useSelect.

Though it may be asking too much from the user, it could be optional for those that need it.

Related - possibly related, raised in this (old) issue, why options aren't provided by renderValue:

const options = [ ... ];

return (
  <Select
    renderValue={value => {
      const item = options.find(option => option.value === value ); 
      return item.label
    }}
  />
)

2. An Autocomplete-like API

Suggested in #37

Autocomplete has getOptionLabel for the simple case of rendering a string (usually when the value and option are the same type), and renderOption that is more like a render prop.

Applying this pattern to Base's Select could look like:

  • Keep the renderValue: (selectedValue) => ReactNode for the "simple" case, maybe rename it to be the equivalent of getOptionLabel (e.g. getValueLabel? getValueDisplay?)
  • Have a "proper" render prop for the value that provides additional arguments, e.g. ownerState

This may allow more flexibility, but it is quite different from Material UI's current API

Proposed solution 🟒

I think Base UI's Select's current renderValue is already quite sufficient, it has parity with Material UI, and the layout shift issue was fixed

Having two props for rendering the value like Autocomplete doesn't seem worth it for the additional mental overhead of a new prop, and it would deviate more from the current Select APIs

Maybe it would be enough if we pass ownerState to renderValue as a 2nd argument to provide more customization flexibility. At the risk of bloating this too much, we could even additionally accept options and pass that into ownerState as well

Resources and benchmarks πŸ”—

@DiegoAndai
Copy link
Member

DiegoAndai commented Oct 16, 2023

I think Base UI's Select's current renderValue is already quite sufficient, it has parity with Material UI, and the layout shift issue was fixed

The proposed solution looks good to me

At the risk of bloating this too much, we could even additionally accept options and pass that into ownerState as well

I wouldn't take this route until requested by users

@mnajdova
Copy link
Member

I think Base UI's Select's current renderValue is already quite sufficient, it has parity with Material UI, and the layout shift mui/material-ui#38796 was fixed

+1, seems like the best option

@michaldudak michaldudak transferred this issue from mui/material-ui Feb 27, 2024
@michaldudak michaldudak changed the title [RFC][Select][base-ui] Rendering the selected value in SSR & during hydration [RFC][Select] Rendering the selected value in SSR & during hydration Feb 27, 2024
@michaldudak michaldudak added the component: select This is the name of the generic UI component, not the React module! label Feb 27, 2024
@atomiks atomiks linked a pull request Sep 11, 2024 that will close this issue
2 tasks
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!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants