Align admin, community, search, library, learning and course management with the mockup. Add saved courses, download history, public community feed, instructor directory, backend search, course views, admin content tools and community moderation. Harden API and Nginx for production.
78 lines
3.0 KiB
TypeScript
78 lines
3.0 KiB
TypeScript
import React, { useEffect, useRef, useState } from 'react';
|
|
import { createPortal } from 'react-dom';
|
|
import { CheckMockIcon, ChevronDownMockIcon } from './MockupIcons';
|
|
|
|
export type SelectOption = { value: string; label: string };
|
|
|
|
interface SelectProps {
|
|
value: string;
|
|
options: SelectOption[];
|
|
onChange: (value: string) => void;
|
|
ariaLabel: string;
|
|
disabled?: boolean;
|
|
variant?: 'default' | 'field' | 'mobile';
|
|
}
|
|
|
|
/** Custom listbox used instead of the browser's native select. */
|
|
export const Select: React.FC<SelectProps> = ({ value, options, onChange, ariaLabel, disabled = false, variant = 'default' }) => {
|
|
const triggerRef = useRef<HTMLButtonElement>(null);
|
|
const [open, setOpen] = useState(false);
|
|
const [position, setPosition] = useState<{ top: number; left: number; width: number } | null>(null);
|
|
const selected = options.find((option) => option.value === value);
|
|
|
|
const place = () => {
|
|
const trigger = triggerRef.current;
|
|
if (!trigger) return;
|
|
const rect = trigger.getBoundingClientRect();
|
|
const width = Math.max(rect.width, 160);
|
|
const height = Math.min(options.length * 40 + 12, 260);
|
|
setPosition({
|
|
top: Math.max(12, Math.min(rect.bottom + 6, window.innerHeight - height - 12)),
|
|
left: Math.max(12, Math.min(rect.left, window.innerWidth - width - 12)),
|
|
width,
|
|
});
|
|
};
|
|
|
|
const toggle = () => {
|
|
if (disabled) return;
|
|
if (!open) place();
|
|
setOpen((current) => !current);
|
|
};
|
|
|
|
useEffect(() => {
|
|
if (!open) return;
|
|
const close = () => setOpen(false);
|
|
const onKey = (event: KeyboardEvent) => { if (event.key === 'Escape') setOpen(false); };
|
|
window.addEventListener('resize', close);
|
|
window.addEventListener('scroll', close, true);
|
|
document.addEventListener('keydown', onKey);
|
|
return () => {
|
|
window.removeEventListener('resize', close);
|
|
window.removeEventListener('scroll', close, true);
|
|
document.removeEventListener('keydown', onKey);
|
|
};
|
|
}, [open]);
|
|
|
|
return <>
|
|
<button
|
|
ref={triggerRef}
|
|
type="button"
|
|
className={`select-trigger${variant !== 'default' ? ` ${variant}` : ''}`}
|
|
disabled={disabled}
|
|
aria-haspopup="listbox"
|
|
aria-expanded={open}
|
|
aria-label={ariaLabel}
|
|
onClick={toggle}
|
|
>
|
|
<span className="select-value">{selected?.label ?? ''}</span>
|
|
<ChevronDownMockIcon className="ph" />
|
|
</button>
|
|
{open && position && createPortal(<>
|
|
<button type="button" className="menu-scrim" aria-label="Fechar lista" onClick={() => setOpen(false)} />
|
|
<div className="select-menu" role="listbox" aria-label={ariaLabel} style={{ top: position.top, left: position.left, width: position.width }}>
|
|
{options.map((option) => <button key={option.value} type="button" role="option" aria-selected={option.value === value} onClick={() => { onChange(option.value); setOpen(false); }}><span>{option.label}</span>{option.value === value && <CheckMockIcon className="ph" />}</button>)}
|
|
</div>
|
|
</>, document.body)}
|
|
</>;
|
|
};
|