-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
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] Create unstyled select (+ hook) #30113
Conversation
@material-ui/core: parsed: +21.38% , gzip: +18.11% |
I found another issue - I cannot open the select when pressing Enter (pressing Space works). Also, could we add test cases for the things we noticed that didn't work so far? |
docs/src/pages/components/selects/UnstyledSelectObjectValues.tsx
Outdated
Show resolved
Hide resolved
Co-authored-by: Siriwat K <siriwatkunaporn@gmail.com>
The following example shows a select that opens when hovered over or focused. | ||
It can be controlled by a mouse/touch or a keyboard. | ||
|
||
{{"demo": "pages/components/selects/UseSelect.js"}} |
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 would say we should make sure that keyboard navigation still works.
Done.
I'll create an issue to review all unstyled demos and to adapt them to the dark theme. I don't think I would use a Theme to detect a mode, though. Perhaps using a |
const CustomSelect = React.forwardRef(function CustomSelect<TValue>( | ||
props: SelectUnstyledProps<TValue>, | ||
ref: React.ForwardedRef<HTMLUListElement>, | ||
) { | ||
const components: SelectUnstyledProps<TValue>['components'] = { | ||
Root: StyledButton, | ||
Listbox: StyledListbox, | ||
Popper: StyledPopper, | ||
...props.components, | ||
}; | ||
|
||
return <SelectUnstyled {...props} ref={ref} components={components} />; | ||
}) as <TValue>( | ||
props: SelectUnstyledProps<TValue> & React.RefAttributes<HTMLUListElement>, | ||
) => JSX.Element; |
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.
No need to consider in this PR, would be nice if we provide a utility type or a factory function for building custom component with TS like:
const CustomSelect = createSelect((props, ref) => {
return <SelectUnstyled {...props} ref={ref} components={components} />
})
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.
It's worth considering, I guess. The need for type casting is unfortunate but I haven't found a way to avoid it so a helper function could help indeed :)
/** | ||
* @ignore | ||
*/ | ||
onClick: PropTypes.func, |
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.
is this removed by the script?
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.
Yes, yarn proptypes removed it. When working on the Select I've noticed an issue on how events are handled in ButtonUnstyled and will improve it in a separate PR.
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.
Overall, looks good to me. Can't wait to test it out with Joy!
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.
Well done :)
Co-authored-by: Siriwat K <siriwatkunaporn@gmail.com>
This PR adds a SelectUnstyled component and useSelect hook.
It is pretty much a new component, with a few parts taken over from the existing @mui/material's Select.
The hook is built using the useButton and useListbox hooks.
Closes #30251
Closes #21022
Design highlights:
OptionUnstyled
s in anOptionGroupUnstyled
. Nested groups are also supported.SingleSelectUnstyled
andMultiSelectUnstyled
. They are, however, exposed through aSelectUnstyled
wrapper that renders one of them based on the value of themultiple
prop. I am considering exporting just SingleSelectUnstyled (renamed as SelectUnstyled) and MultiSelectUnstyled, similarly to Mantine)value
is not converted to string in the onChange handler. It's therefore possible to use objects as values (Select support does't support object value #30066)Features to implement
Another thing to consider in the future would be performance. It feels that we should be able to reduce the work going on behind the scenes if perf becomes a problem.
Preview
https://deploy-preview-30113--material-ui.netlify.app/components/selects/#unstyled