Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('performs no action if non-tab key is pressed', () => {
const { getByTestId } = render();
fireEvent.keyDown(getByTestId('container'), { keyCode: KEY_CODES.END });
// Container is still focused during initial mount
expect(focusSpy).toHaveBeenCalledTimes(2);
});
const getTriggerProps = (
{ tabIndex = 0, onMouseEnter, onMouseLeave, onFocus, onBlur, onKeyDown, ...other } = {} as any
) => {
return {
tabIndex,
onMouseEnter: composeEventHandlers(onMouseEnter, () => openTooltip()),
onMouseLeave: composeEventHandlers(onMouseLeave, () => closeTooltip()),
onFocus: composeEventHandlers(onFocus, () => openTooltip()),
// Close menu immediately when blurred
onBlur: composeEventHandlers(onBlur, () => closeTooltip(0)),
onKeyDown: composeEventHandlers(onKeyDown, (event: KeyboardEvent) => {
if (event.keyCode === KEY_CODES.ESCAPE && visibility) {
closeTooltip(0);
}
}),
'aria-describedby': _id,
'data-garden-container-id': 'containers.tooltip',
'data-garden-container-version': PACKAGE_VERSION,
...other
};
};
onKeyDown: composeEventHandlers(onKeyDown, (e: KeyboardEvent) => {
const PREVIOUS_KEY = rtl ? KEY_CODES.RIGHT : KEY_CODES.LEFT;
const NEXT_KEY = rtl ? KEY_CODES.LEFT : KEY_CODES.RIGHT;
if (downshift.isOpen) {
// Select previous item if available
if (
e.keyCode === PREVIOUS_KEY &&
previousIndexRef.current !== null &&
previousIndexRef.current !== undefined &&
!downshift.inputValue
) {
e.preventDefault();
e.stopPropagation();
downshift.selectItemAtIndex(previousIndexRef.current);
}
onKeyDown: composeEventHandlers(onKeyDown, (e: KeyboardEvent) => {
const PREVIOUS_KEY = rtl ? KEY_CODES.RIGHT : KEY_CODES.LEFT;
const NEXT_KEY = rtl ? KEY_CODES.LEFT : KEY_CODES.RIGHT;
if (downshift.isOpen) {
// Select previous item if available
if (
e.keyCode === PREVIOUS_KEY &&
previousIndexRef.current !== null &&
previousIndexRef.current !== undefined &&
!downshift.inputValue
) {
e.preventDefault();
e.stopPropagation();
downshift.selectItemAtIndex(previousIndexRef.current);
}
// Select current next item if available
downshift.selectItemAtIndex(previousIndexRef.current);
}
// Select current next item if available
if (e.keyCode === NEXT_KEY) {
const nextItemIndexes = Object.values(nextItemsHashRef.current);
if (nextItemIndexes.includes(downshift.highlightedIndex)) {
e.preventDefault();
e.stopPropagation();
downshift.selectItemAtIndex(downshift.highlightedIndex!);
}
}
} else if (
(e.keyCode === KEY_CODES.ENTER || e.keyCode === KEY_CODES.SPACE) &&
!downshift.isOpen
) {
e.preventDefault();
e.stopPropagation();
downshift.openMenu();
}
}),
...other
defaultSelectedIndex,
rtl,
selectedItem,
focusedItem,
onSelect,
onFocus
}: IUseSelectionProps = {}): UseSelectionReturnValue {
const refs: React.MutableRefObject[] = [];
const items: Item[] = [];
const [state, dispatch] = useReducer(stateReducer, {
selectedItem,
focusedItem
});
const controlledFocusedItem = getControlledValue(focusedItem, state.focusedItem);
const controlledSelectedItem = getControlledValue(selectedItem, state.selectedItem);
useEffect(() => {
if (controlledFocusedItem !== undefined) {
const focusedIndex = items.indexOf(controlledFocusedItem);
refs[focusedIndex] && refs[focusedIndex].current!.focus();
}
}, [controlledFocusedItem]); // eslint-disable-line react-hooks/exhaustive-deps
useEffect(() => {
if (selectedItem === undefined && defaultSelectedIndex !== undefined) {
dispatch({
type: 'KEYBOARD_SELECT',
payload: items[defaultSelectedIndex],
onSelect
rtl,
selectedItem,
focusedItem,
onSelect,
onFocus
}: IUseSelectionProps = {}): UseSelectionReturnValue {
const refs: React.MutableRefObject[] = [];
const items: Item[] = [];
const [state, dispatch] = useReducer(stateReducer, {
selectedItem,
focusedItem
});
const controlledFocusedItem = getControlledValue(focusedItem, state.focusedItem);
const controlledSelectedItem = getControlledValue(selectedItem, state.selectedItem);
useEffect(() => {
if (controlledFocusedItem !== undefined) {
const focusedIndex = items.indexOf(controlledFocusedItem);
refs[focusedIndex] && refs[focusedIndex].current!.focus();
}
}, [controlledFocusedItem]); // eslint-disable-line react-hooks/exhaustive-deps
useEffect(() => {
if (selectedItem === undefined && defaultSelectedIndex !== undefined) {
dispatch({
type: 'KEYBOARD_SELECT',
payload: items[defaultSelectedIndex],
onSelect
});
({ children, inputRef: controlledInputRef, ...props }, ref) => {
const {
popperReferenceElementRef,
downshift: { getToggleButtonProps, getInputProps, isOpen }
} = useDropdownContext();
const { isLabelHovered } = useFieldContext();
const inputRef = useCombinedRefs(controlledInputRef);
const triggerRef = useCombinedRefs(ref);
const previousIsOpenRef = useRef(undefined);
const [isFocused, setIsFocused] = useState(false);
useEffect(() => {
// Focus internal input when Menu is opened
if (isOpen && !previousIsOpenRef.current) {
inputRef.current && inputRef.current.focus();
}
// Focus trigger when Menu is closed
if (!isOpen && previousIsOpenRef.current) {
triggerRef.current && triggerRef.current.focus();
}
previousIsOpenRef.current = isOpen;
}, [isOpen, inputRef, triggerRef]);
const Range = React.forwardRef(({ min, max, step, ...props }, ref) => {
const [backgroundSize, setBackgroundSize] = useState(0);
const rangeRef = useCombinedRefs(ref);
const { getInputProps } = useFieldContext();
const updateBackgroundWidthFromInput = useCallback(
rangeTarget => {
let relativeMax = max;
const { value } = rangeTarget;
if (parseFloat(relativeMax) < parseFloat(min)) {
relativeMax = 100;
}
const percentage = (100 * (value - min)) / (relativeMax - min);
setBackgroundSize(`${percentage}%`);
},
/* eslint-disable-next-line react-hooks/exhaustive-deps */
({ children, inputRef: controlledInputRef, ...props }, ref) => {
const {
popperReferenceElementRef,
downshift: { getToggleButtonProps, getInputProps, isOpen }
} = useDropdownContext();
const { isLabelHovered } = useFieldContext();
const inputRef = useCombinedRefs(controlledInputRef);
const triggerRef = useCombinedRefs(ref);
const previousIsOpenRef = useRef(undefined);
const [isFocused, setIsFocused] = useState(false);
useEffect(() => {
// Focus internal input when Menu is opened
if (isOpen && !previousIsOpenRef.current) {
inputRef.current && inputRef.current.focus();
}
// Focus trigger when Menu is closed
if (!isOpen && previousIsOpenRef.current) {
triggerRef.current && triggerRef.current.focus();
}
previousIsOpenRef.current = isOpen;
}, [isOpen, inputRef, triggerRef]);