-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(UI): passer le select en composition (#3204)
- Loading branch information
1 parent
73c8b55
commit c575388
Showing
8 changed files
with
164 additions
and
100 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { createContext, useContext } from 'react'; | ||
|
||
import NoProviderError from '~/client/Errors/NoProviderError'; | ||
|
||
|
||
type SelectContext = { | ||
activeDescendant: string | undefined, | ||
onOptionSelection: (optionId: string) => void, | ||
isCurrentItemSelected: (optionValue: string) => boolean | ||
} | ||
|
||
export const SelectContext = createContext<SelectContext | null>(null); | ||
|
||
export function useSelectContext() { | ||
const selectContext = useContext(SelectContext); | ||
|
||
if (selectContext == null) { | ||
throw new NoProviderError(SelectContext); | ||
} | ||
|
||
return selectContext; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import classNames from 'classnames'; | ||
import React, { useCallback, useId } from 'react'; | ||
|
||
import styles from './Select.module.scss'; | ||
import { useSelectContext } from './SelectContext'; | ||
|
||
type SelectOptionProps = Omit<React.ComponentPropsWithoutRef<'li'>, 'value'> & { | ||
value: { toString: () => string }, | ||
}; | ||
|
||
export function SelectOption({ className, value: valueProps, id: idProps, ...rest }: SelectOptionProps) { | ||
const value = valueProps.toString(); | ||
const defaultId = useId(); | ||
const id = idProps ?? value ?? defaultId; | ||
const { onOptionSelection, activeDescendant, isCurrentItemSelected } = useSelectContext(); | ||
|
||
// NOTE (BRUJ 17-05-2023): Sinon on perd le focus avant la fin du clique ==> élément invalid pour la sélection. | ||
const onMouseDown = useCallback(function preventBlurOnOptionSelection(event: React.MouseEvent<HTMLLIElement>) { | ||
event.preventDefault(); | ||
}, []); | ||
|
||
return <li | ||
className={classNames(className, { [styles.optionVisuallyFocus] : activeDescendant === id })} | ||
id={id} | ||
role="option" | ||
onMouseDown={onMouseDown} | ||
data-value={value.toString()} | ||
onClick={() => onOptionSelection(id)} | ||
aria-selected={isCurrentItemSelected(value)} | ||
{...rest}> | ||
</li>; | ||
} |
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
Oops, something went wrong.