-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
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] Improve exported types #30895
Changes from all commits
c24b386
589b32f
c5fe360
fe12fce
5bb87f3
914de68
9ff6496
c0bde88
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import * as React from 'react'; | ||
import { useListbox } from '@mui/base/ListboxUnstyled'; | ||
import { expect } from 'chai'; | ||
import { createRenderer } from 'test/utils'; | ||
|
||
describe('useListbox', () => { | ||
const { render } = createRenderer(); | ||
describe('prop: id', () => { | ||
it('propagates it to the root element', () => { | ||
const Listbox = () => { | ||
const { getRootProps } = useListbox({ options: [], id: 'test-id' }); | ||
return <ul {...getRootProps()} />; | ||
}; | ||
const { getByRole } = render(<Listbox />); | ||
|
||
const listbox = getByRole('listbox'); | ||
expect(listbox).to.have.attribute('id', 'test-id'); | ||
}); | ||
|
||
it('uses the provided id to create option ids', () => { | ||
const options = ['one', 'two']; | ||
const Listbox = () => { | ||
const { getRootProps, getOptionProps } = useListbox({ options, id: 'test-id' }); | ||
return ( | ||
<ul {...getRootProps()}> | ||
{options.map((o) => ( | ||
<li key={o} {...getOptionProps(o)}> | ||
{o} | ||
</li> | ||
))} | ||
</ul> | ||
); | ||
}; | ||
const { getAllByRole } = render(<Listbox />); | ||
|
||
const optionElements = getAllByRole('option'); | ||
optionElements.forEach((opt, index) => | ||
expect(opt).to.have.attribute('id', `test-id-option-${index}`), | ||
); | ||
}); | ||
|
||
it('generates a unique id if not provided explicitly', () => { | ||
const Listbox = () => { | ||
const { getRootProps } = useListbox({ options: [] }); | ||
return <ul {...getRootProps()} />; | ||
}; | ||
|
||
const { getAllByRole } = render( | ||
<React.Fragment> | ||
<Listbox /> | ||
<Listbox /> | ||
</React.Fragment>, | ||
); | ||
|
||
const listboxes = getAllByRole('listbox'); | ||
expect(listboxes[0].id).not.to.equal(listboxes[1].id); | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import * as React from 'react'; | ||
import { SelectUnstyled } from '@mui/base'; | ||
|
||
const SelectUnstyledComponentsPropsOverridesTest = ( | ||
<SelectUnstyled | ||
componentsProps={{ | ||
root: { | ||
// @ts-expect-error - requires module augmentation | ||
size: 'red', | ||
className: 'test', | ||
}, | ||
popper: { | ||
className: 'popper', | ||
disablePortal: true, | ||
}, | ||
listbox: { | ||
className: 'listbox', | ||
onMouseOver: () => {}, | ||
}, | ||
}} | ||
/> | ||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,7 +29,8 @@ export interface SelectUnstyledCommonProps { | |
componentsProps?: { | ||
root?: React.ComponentPropsWithRef<'button'> & SelectUnstyledComponentsPropsOverrides; | ||
listbox?: React.ComponentPropsWithRef<'ul'> & SelectUnstyledComponentsPropsOverrides; | ||
popper?: React.ComponentPropsWithRef<typeof PopperUnstyled> & | ||
// PopperUnstyled has a required prop: open, but it is not necessary to provide it in componentsProps. | ||
popper?: Partial<React.ComponentPropsWithRef<typeof PopperUnstyled>> & | ||
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. Why is 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. Popper requires an 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. Alright, could we leave an comment for it? 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. Sure, it's done! |
||
SelectUnstyledComponentsPropsOverrides; | ||
}; | ||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import * as React from 'react'; | ||
import { SelectUnstyled, MultiSelectUnstyled } from '@mui/base'; | ||
|
||
declare module '@mui/base' { | ||
interface SelectUnstyledComponentsPropsOverrides { | ||
variant?: 'one' | 'two'; | ||
} | ||
} | ||
|
||
<SelectUnstyled componentsProps={{ root: { variant: 'one' } }} />; | ||
|
||
// @ts-expect-error unknown variant | ||
<SelectUnstyled componentsProps={{ root: { variant: 'three' } }} />; | ||
|
||
<MultiSelectUnstyled componentsProps={{ root: { variant: 'one' } }} />; | ||
|
||
// @ts-expect-error unknown variant | ||
<MultiSelectUnstyled componentsProps={{ root: { variant: 'three' } }} />; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"extends": "../../../../../tsconfig", | ||
"files": ["selectUnstyledComponentsProps.spec.tsx"] | ||
} |
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.
Do we have test for the module augmentation?
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.
Now we do 😊
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.
Awesomeness :D