-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🚸(frontend) improve Grommet Select component
Created a wrapper on top of Grommet Select component to improve its drop down list positioning. The drop down list is now displayed according to the available space in the viewport. Its width is also set to the width of the select box.
- Loading branch information
Showing
6 changed files
with
140 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
src/frontend/packages/lib_components/src/common/Select/index.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { screen } from '@testing-library/react'; | ||
import { render } from 'lib-tests'; | ||
import React from 'react'; | ||
|
||
import { Select } from './index'; | ||
|
||
window.scrollTo = jest.fn(); | ||
|
||
describe('<Select />', () => { | ||
it('checks drop position under select', async () => { | ||
render( | ||
<Select | ||
aria-label="my-select" | ||
options={['option 1', 'option 2', 'option 3', 'option 4']} | ||
dropProps={{ | ||
'aria-label': 'my-drop', | ||
}} | ||
/>, | ||
); | ||
|
||
screen | ||
.getByRole('button', { | ||
name: 'my-select', | ||
}) | ||
.click(); | ||
|
||
expect(await screen.findByLabelText('my-drop')).toHaveStyle( | ||
'transform-origin: bottom left;', | ||
); | ||
}); | ||
|
||
it('checks drop position above select', async () => { | ||
render( | ||
<Select | ||
aria-label="my-select" | ||
options={['option 1', 'option 2', 'option 3', 'option 4']} | ||
dropProps={{ | ||
'aria-label': 'my-drop', | ||
}} | ||
maxDropHeight={-100} | ||
/>, | ||
); | ||
|
||
screen | ||
.getByRole('button', { | ||
name: 'my-select', | ||
}) | ||
.click(); | ||
|
||
expect(await screen.findByLabelText('my-drop')).toHaveStyle( | ||
'transform-origin: top left;', | ||
); | ||
}); | ||
|
||
it('checks if children are correctly displayed', async () => { | ||
render( | ||
<Select | ||
aria-label="my-select" | ||
options={['option 1', 'option 2', 'option 3', 'option 4']} | ||
> | ||
{(option: string) => <div>My {option}</div>} | ||
</Select>, | ||
); | ||
|
||
screen | ||
.getByRole('button', { | ||
name: 'my-select', | ||
}) | ||
.click(); | ||
|
||
expect(await screen.findByText('My option 1')).toBeInTheDocument(); | ||
expect(screen.getByText('My option 2')).toBeInTheDocument(); | ||
expect(screen.getByText('My option 3')).toBeInTheDocument(); | ||
expect(screen.getByText('My option 4')).toBeInTheDocument(); | ||
}); | ||
}); |
58 changes: 58 additions & 0 deletions
58
src/frontend/packages/lib_components/src/common/Select/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { | ||
Select as SelectGrommet, | ||
SelectProps as SelectPropsGrommet, | ||
} from 'grommet'; | ||
import React, { PropsWithChildren, useEffect, useRef, useState } from 'react'; | ||
|
||
/** | ||
* @param maxDropHeight - in pixel, the place needed to the drop to be displayed at the bottom of the select | ||
*/ | ||
interface SelectProps extends SelectPropsGrommet { | ||
maxDropHeight?: number; // px | ||
} | ||
|
||
export const Select = ({ | ||
children, | ||
maxDropHeight = 400, | ||
...props | ||
}: PropsWithChildren<SelectProps>) => { | ||
const refSelect = useRef<HTMLInputElement>(null); | ||
const [selectDropWidth, setSelectDropWidth] = useState(0); | ||
const [selectDropPosition, setSelectDropPosition] = useState<{ | ||
bottom?: 'bottom' | 'top'; | ||
top?: 'bottom' | 'top'; | ||
}>({ bottom: 'top' }); | ||
|
||
useEffect(() => { | ||
function handleResize() { | ||
setSelectDropWidth(refSelect.current?.clientWidth || 0); | ||
setSelectDropPosition( | ||
document.body.clientHeight - (refSelect.current?.offsetTop || 0) < | ||
maxDropHeight | ||
? { bottom: 'top' } | ||
: { top: 'bottom' }, | ||
); | ||
} | ||
|
||
handleResize(); | ||
window.addEventListener('resize', handleResize); | ||
|
||
return () => { | ||
window.removeEventListener('resize', handleResize); | ||
}; | ||
}, [maxDropHeight]); | ||
|
||
return ( | ||
<SelectGrommet | ||
ref={refSelect} | ||
dropAlign={{ ...selectDropPosition, left: 'left' }} | ||
dropProps={{ | ||
width: `${selectDropWidth}px`, | ||
}} | ||
dropHeight={`${maxDropHeight}px`} | ||
{...props} | ||
> | ||
{children} | ||
</SelectGrommet> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 1 addition & 2 deletions
3
...ponents/common/VideoWidgetProvider/LocalizedTimedTextTrackUpload/LanguageSelect/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters