hooks#useEventListener TypeScript Examples
The following examples show how to use
hooks#useEventListener.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: useOutsideClick.ts From frontend with GNU General Public License v3.0 | 5 votes |
useOutsideClick = (ref: RefObject<HTMLElement>, callback: () => void): void => {
const handleClick = (e: Event) => {
if (ref.current && !ref.current.contains(e.target as HTMLElement)) {
callback();
}
};
useEventListener('click', handleClick);
}
Example #2
Source File: Options.tsx From ebs-design with MIT License | 4 votes |
Options: React.FC<OptionsProps> = ({
mode = 'single',
scrollMode = 'regular',
loading,
options = [],
value,
emptyLabel = 'No found',
newOption,
onNext,
onClose,
onChange,
onClickAddNew,
...props
}) => {
const ref = React.useRef<HTMLDivElement | null>(null);
const { offsetBottom, maxHeight, setState } = React.useContext(Context);
const [activeItem, setActiveItem] = React.useState(0);
const onScroll = (e): void => {
const scrollTop = Math.floor(e.target.scrollTop);
if (scrollMode !== 'regular' && onNext && e.target.scrollHeight - e.target.offsetHeight - scrollTop <= 3) {
onNext();
}
};
React.useEffect(() => {
const rect = ref.current?.getBoundingClientRect();
if (!maxHeight && rect?.height && offsetBottom && options.length && !loading) {
const height = window.innerHeight - offsetBottom;
setState({ maxHeight: height <= rect.height ? height : rect.height - +(scrollMode === 'scroll') });
}
}, [ref.current, offsetBottom, scrollMode, options, loading, maxHeight]);
React.useEffect(() => {
if (ref.current && !scrollMode && options?.length) {
ref.current.scrollTop = 0;
}
}, [ref.current, options, scrollMode]);
React.useEffect(() => {
if (ref.current && scrollMode === 'scroll') {
ref.current.addEventListener('scroll', onScroll);
}
return () => {
if (ref.current && scrollMode === 'scroll') {
ref.current.removeEventListener('scroll', onScroll);
}
};
}, [ref.current, onScroll, scrollMode]);
useEventListener(
'keydown',
({ key }: { key: string }) => {
if (['ArrowUp', 'ArrowDown', 'Escape', 'Enter'].includes(key)) {
if (key === 'Escape' && mode === 'single' && onClose !== undefined) {
onClose();
}
if (key === 'Enter' && onChange !== undefined) {
const option = options[activeItem - 1];
onChange(option.value);
if (mode === 'single' && onClose !== undefined) {
onClose();
}
}
// User pressed the up arrow
if (key === 'ArrowUp') {
setActiveItem((s) => (s !== 0 ? s - 1 : options.length));
}
// User pressed the down arrow
if (key === 'ArrowDown') {
setActiveItem((s) => (s < options.length ? s + 1 : 0));
}
}
},
ref,
);
const onChangeHandler = (value: any): void => {
if (onChange !== undefined) {
onChange(value);
}
if (onClose !== undefined) {
onClose();
}
};
return (
<div
ref={ref}
{...props}
className={cn(
'ebs-select__options-items',
{
'ebs-select__options--multiple': ['multiple', 'tags'].includes(mode),
},
props.className,
)}
style={maxHeight ? { ...props.style, maxHeight } : props.style}
>
{newOption && onClickAddNew && (
<Item
value={newOption}
text={newOption}
selected
onClick={() => onClickAddNew(newOption)}
suffix={<Label type="ghost" text="CTRL+Enter" />}
/>
)}
<Loader
loading={scrollMode !== 'scroll' ? loading || false : false}
height={!ref.current || ref.current?.offsetHeight < 300 ? 300 : ref.current.offsetHeight}
>
{options.length
? options.map((option, key) => (
<Item
key={key}
active={
['multiple', 'tags'].includes(mode) && Array.isArray(value)
? value.includes(option.value)
: value === option.value
}
mode={mode}
text={option.text}
selected={activeItem === key + 1}
onClick={onChangeHandler}
{...option}
/>
))
: !loading && (
<Space size="large" justify="center" className="ebs-select__options--empty">
{emptyLabel}
</Space>
)}
</Loader>
{loading ? (
<Space justify="center" className="mt-10">
<Loader.Inline />
</Space>
) : null}
</div>
);
}