|
|
|
@ -1,5 +1,5 @@
|
|
|
|
import type { PropsWithChildren } from 'react';
|
|
|
|
import type { PropsWithChildren } from 'react';
|
|
|
|
import { useCallback, useState, useRef } from 'react';
|
|
|
|
import { useCallback, useState, useRef, useId } from 'react';
|
|
|
|
|
|
|
|
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
|
|
|
|
|
|
|
|
@ -16,6 +16,8 @@ interface DropdownProps {
|
|
|
|
options: SelectItem[];
|
|
|
|
options: SelectItem[];
|
|
|
|
disabled?: boolean;
|
|
|
|
disabled?: boolean;
|
|
|
|
onChange: (value: string) => void;
|
|
|
|
onChange: (value: string) => void;
|
|
|
|
|
|
|
|
'aria-labelledby': string;
|
|
|
|
|
|
|
|
'aria-describedby'?: string;
|
|
|
|
placement?: Placement;
|
|
|
|
placement?: Placement;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@ -24,51 +26,33 @@ const Dropdown: React.FC<DropdownProps> = ({
|
|
|
|
options,
|
|
|
|
options,
|
|
|
|
disabled,
|
|
|
|
disabled,
|
|
|
|
onChange,
|
|
|
|
onChange,
|
|
|
|
|
|
|
|
'aria-labelledby': ariaLabelledBy,
|
|
|
|
|
|
|
|
'aria-describedby': ariaDescribedBy,
|
|
|
|
placement: initialPlacement = 'bottom-end',
|
|
|
|
placement: initialPlacement = 'bottom-end',
|
|
|
|
}) => {
|
|
|
|
}) => {
|
|
|
|
const activeElementRef = useRef<Element | null>(null);
|
|
|
|
const containerRef = useRef<HTMLDivElement>(null);
|
|
|
|
const containerRef = useRef(null);
|
|
|
|
const buttonRef = useRef<HTMLButtonElement>(null);
|
|
|
|
const [isOpen, setOpen] = useState<boolean>(false);
|
|
|
|
const [isOpen, setOpen] = useState<boolean>(false);
|
|
|
|
const [placement, setPlacement] = useState<Placement>(initialPlacement);
|
|
|
|
const [placement, setPlacement] = useState<Placement>(initialPlacement);
|
|
|
|
|
|
|
|
const uniqueId = useId();
|
|
|
|
const handleToggle = useCallback(() => {
|
|
|
|
const menuId = `${uniqueId}-menu`;
|
|
|
|
if (
|
|
|
|
const buttonLabelId = `${uniqueId}-button`;
|
|
|
|
isOpen &&
|
|
|
|
|
|
|
|
activeElementRef.current &&
|
|
|
|
|
|
|
|
activeElementRef.current instanceof HTMLElement
|
|
|
|
|
|
|
|
) {
|
|
|
|
|
|
|
|
activeElementRef.current.focus({ preventScroll: true });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
setOpen(!isOpen);
|
|
|
|
|
|
|
|
}, [isOpen, setOpen]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const handleMouseDown = useCallback(() => {
|
|
|
|
|
|
|
|
if (!isOpen) activeElementRef.current = document.activeElement;
|
|
|
|
|
|
|
|
}, [isOpen]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const handleKeyDown = useCallback(
|
|
|
|
|
|
|
|
(e: React.KeyboardEvent) => {
|
|
|
|
|
|
|
|
switch (e.key) {
|
|
|
|
|
|
|
|
case ' ':
|
|
|
|
|
|
|
|
case 'Enter':
|
|
|
|
|
|
|
|
if (!isOpen) activeElementRef.current = document.activeElement;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
[isOpen],
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const handleClose = useCallback(() => {
|
|
|
|
const handleClose = useCallback(() => {
|
|
|
|
if (
|
|
|
|
if (isOpen && buttonRef.current) {
|
|
|
|
isOpen &&
|
|
|
|
buttonRef.current.focus({ preventScroll: true });
|
|
|
|
activeElementRef.current &&
|
|
|
|
}
|
|
|
|
activeElementRef.current instanceof HTMLElement
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
activeElementRef.current.focus({ preventScroll: true });
|
|
|
|
|
|
|
|
setOpen(false);
|
|
|
|
setOpen(false);
|
|
|
|
}, [isOpen]);
|
|
|
|
}, [isOpen]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const handleToggle = useCallback(() => {
|
|
|
|
|
|
|
|
if (isOpen) {
|
|
|
|
|
|
|
|
handleClose();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
setOpen(true);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}, [isOpen, handleClose]);
|
|
|
|
|
|
|
|
|
|
|
|
const handleOverlayEnter = useCallback(
|
|
|
|
const handleOverlayEnter = useCallback(
|
|
|
|
(state: Partial<PopperState>) => {
|
|
|
|
(state: Partial<PopperState>) => {
|
|
|
|
if (state.placement) setPlacement(state.placement);
|
|
|
|
if (state.placement) setPlacement(state.placement);
|
|
|
|
@ -82,13 +66,18 @@ const Dropdown: React.FC<DropdownProps> = ({
|
|
|
|
<div ref={containerRef}>
|
|
|
|
<div ref={containerRef}>
|
|
|
|
<button
|
|
|
|
<button
|
|
|
|
type='button'
|
|
|
|
type='button'
|
|
|
|
|
|
|
|
ref={buttonRef}
|
|
|
|
onClick={handleToggle}
|
|
|
|
onClick={handleToggle}
|
|
|
|
onMouseDown={handleMouseDown}
|
|
|
|
|
|
|
|
onKeyDown={handleKeyDown}
|
|
|
|
|
|
|
|
disabled={disabled}
|
|
|
|
disabled={disabled}
|
|
|
|
|
|
|
|
aria-expanded={isOpen}
|
|
|
|
|
|
|
|
aria-controls={menuId}
|
|
|
|
|
|
|
|
aria-labelledby={`${ariaLabelledBy} ${buttonLabelId}`}
|
|
|
|
|
|
|
|
aria-describedby={ariaDescribedBy}
|
|
|
|
className={classNames('dropdown-button', { active: isOpen })}
|
|
|
|
className={classNames('dropdown-button', { active: isOpen })}
|
|
|
|
>
|
|
|
|
>
|
|
|
|
<span className='dropdown-button__label'>{valueOption?.text}</span>
|
|
|
|
<span id={buttonLabelId} className='dropdown-button__label'>
|
|
|
|
|
|
|
|
{valueOption?.text}
|
|
|
|
|
|
|
|
</span>
|
|
|
|
<Icon id='down' icon={ArrowDropDownIcon} />
|
|
|
|
<Icon id='down' icon={ArrowDropDownIcon} />
|
|
|
|
</button>
|
|
|
|
</button>
|
|
|
|
|
|
|
|
|
|
|
|
@ -101,7 +90,7 @@ const Dropdown: React.FC<DropdownProps> = ({
|
|
|
|
popperConfig={{ strategy: 'fixed', onFirstUpdate: handleOverlayEnter }}
|
|
|
|
popperConfig={{ strategy: 'fixed', onFirstUpdate: handleOverlayEnter }}
|
|
|
|
>
|
|
|
|
>
|
|
|
|
{({ props, placement }) => (
|
|
|
|
{({ props, placement }) => (
|
|
|
|
<div {...props}>
|
|
|
|
<div {...props} id={menuId}>
|
|
|
|
<div
|
|
|
|
<div
|
|
|
|
className={`dropdown-animation privacy-dropdown__dropdown ${placement}`}
|
|
|
|
className={`dropdown-animation privacy-dropdown__dropdown ${placement}`}
|
|
|
|
>
|
|
|
|
>
|
|
|
|
@ -123,6 +112,8 @@ const Dropdown: React.FC<DropdownProps> = ({
|
|
|
|
interface Props {
|
|
|
|
interface Props {
|
|
|
|
value: string;
|
|
|
|
value: string;
|
|
|
|
options: SelectItem[];
|
|
|
|
options: SelectItem[];
|
|
|
|
|
|
|
|
label: string | React.ReactElement;
|
|
|
|
|
|
|
|
hint: string | React.ReactElement;
|
|
|
|
disabled?: boolean;
|
|
|
|
disabled?: boolean;
|
|
|
|
onChange: (value: string) => void;
|
|
|
|
onChange: (value: string) => void;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -130,13 +121,26 @@ interface Props {
|
|
|
|
export const SelectWithLabel: React.FC<PropsWithChildren<Props>> = ({
|
|
|
|
export const SelectWithLabel: React.FC<PropsWithChildren<Props>> = ({
|
|
|
|
value,
|
|
|
|
value,
|
|
|
|
options,
|
|
|
|
options,
|
|
|
|
|
|
|
|
label,
|
|
|
|
|
|
|
|
hint,
|
|
|
|
disabled,
|
|
|
|
disabled,
|
|
|
|
children,
|
|
|
|
|
|
|
|
onChange,
|
|
|
|
onChange,
|
|
|
|
}) => {
|
|
|
|
}) => {
|
|
|
|
|
|
|
|
const uniqueId = useId();
|
|
|
|
|
|
|
|
const labelId = `${uniqueId}-label`;
|
|
|
|
|
|
|
|
const descId = `${uniqueId}-desc`;
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
return (
|
|
|
|
|
|
|
|
// This label is only used for its click-forwarding behaviour,
|
|
|
|
|
|
|
|
// accessible names are assigned manually
|
|
|
|
|
|
|
|
// eslint-disable-next-line jsx-a11y/label-has-associated-control
|
|
|
|
<label className='app-form__toggle'>
|
|
|
|
<label className='app-form__toggle'>
|
|
|
|
<div className='app-form__toggle__label'>{children}</div>
|
|
|
|
<div className='app-form__toggle__label'>
|
|
|
|
|
|
|
|
<strong id={labelId}>{label}</strong>
|
|
|
|
|
|
|
|
<span className='hint' id={descId}>
|
|
|
|
|
|
|
|
{hint}
|
|
|
|
|
|
|
|
</span>
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<div className='app-form__toggle__toggle'>
|
|
|
|
<div className='app-form__toggle__toggle'>
|
|
|
|
<div>
|
|
|
|
<div>
|
|
|
|
@ -144,6 +148,8 @@ export const SelectWithLabel: React.FC<PropsWithChildren<Props>> = ({
|
|
|
|
value={value}
|
|
|
|
value={value}
|
|
|
|
onChange={onChange}
|
|
|
|
onChange={onChange}
|
|
|
|
disabled={disabled}
|
|
|
|
disabled={disabled}
|
|
|
|
|
|
|
|
aria-labelledby={labelId}
|
|
|
|
|
|
|
|
aria-describedby={descId}
|
|
|
|
options={options}
|
|
|
|
options={options}
|
|
|
|
/>
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|