Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('decrements minValue on LEFT key', () => {
const { getAllByTestId } = render(
);
fireEvent.keyDown(getAllByTestId('thumb')[0], { keyCode: KEY_CODES.LEFT });
expect(onChangeSpy).toHaveBeenCalledWith({ minValue: 10, maxValue: 75 });
});
it('increments maxValue on LEFT key', () => {
const { getAllByTestId } = renderRtl(
);
const maxThumb = getAllByTestId('thumb')[1];
fireEvent.keyDown(maxThumb, { keyCode: KEY_CODES.LEFT });
expect(onChangeSpy).toHaveBeenCalledWith({ minValue: 15, maxValue: 80 });
});
it('decrements focusedIndex if currently greater than 0', () => {
const { getAllByTestId } = render();
const [item, secondItem] = getAllByTestId('item');
fireEvent.focus(secondItem);
fireEvent.keyDown(secondItem, { keyCode: KEY_CODES.LEFT });
expect(item).toHaveAttribute('data-focused', 'true');
});
it('decrements and wraps focusedIndex if currently less than or equal to 0', () => {
const { getAllByTestId } = render();
const [item, , lastItem] = getAllByTestId('item');
fireEvent.focus(item);
fireEvent.keyDown(item, { keyCode: KEY_CODES.LEFT });
expect(lastItem).toHaveAttribute('data-focused', 'true');
});
});
onKeyDown: composeEventHandlers(onKeyDown, (e: React.KeyboardEvent) => {
if (
(e.keyCode === KEY_CODES.UP && verticalDirection) ||
(e.keyCode === KEY_CODES.LEFT && horizontalDirection)
) {
if (rtl && !verticalDirection) {
dispatch({ type: 'INCREMENT', items, focusedItem, selectedItem, onFocus });
} else {
dispatch({ type: 'DECREMENT', items, focusedItem, selectedItem, onFocus });
}
e.preventDefault();
} else if (
(e.keyCode === KEY_CODES.DOWN && verticalDirection) ||
(e.keyCode === KEY_CODES.RIGHT && horizontalDirection)
) {
if (rtl && !verticalDirection) {
dispatch({ type: 'DECREMENT', items, focusedItem, selectedItem, onFocus });
} else {
dispatch({ type: 'INCREMENT', items, focusedItem, selectedItem, onFocus });
if (e.keyCode === KEY_CODES.END && !inputValue) {
inputRef.current && inputRef.current.focus();
e.preventDefault();
}
if (isRtl(props)) {
if (e.keyCode === KEY_CODES.RIGHT && index === 0) {
e.preventDefault();
}
if (e.keyCode === KEY_CODES.LEFT && index === selectedItems.length - 1) {
e.preventDefault();
inputRef.current && inputRef.current.focus();
}
} else {
if (e.keyCode === KEY_CODES.LEFT && index === 0) {
e.preventDefault();
}
if (e.keyCode === KEY_CODES.RIGHT && index === selectedItems.length - 1) {
e.preventDefault();
inputRef.current && inputRef.current.focus();
}
}
},
onClick: (e: MouseEvent) => {
updateMinThumbSlider(minValue - step);
} else {
updateMaxThumbSlider(maxValue - step);
}
};
const incrementThumb = () => {
if (isMinThumb) {
updateMinThumbSlider(minValue + step);
} else {
updateMaxThumbSlider(maxValue + step);
}
};
switch (e.keyCode) {
case KEY_CODES.LEFT:
if (rtl) {
incrementThumb();
} else {
decrementThumb();
}
keyIntercepted = true;
break;
case KEY_CODES.DOWN:
decrementThumb();
keyIntercepted = true;
break;
case KEY_CODES.RIGHT:
if (rtl) {
onKeyDown: (e: KeyboardEvent) => {
if (!inputValue) {
if (isRtl(props) && e.keyCode === KEY_CODES.RIGHT && selectedItems.length > 0) {
setFocusedItem(selectedItems[selectedItems.length - 1]);
} else if (
!isRtl(props) &&
e.keyCode === KEY_CODES.LEFT &&
selectedItems.length > 0
) {
setFocusedItem(selectedItems[selectedItems.length - 1]);
} else if (e.keyCode === KEY_CODES.BACKSPACE && selectedItems.length > 0) {
(setDownshiftState as any)({
type: REMOVE_ITEM_STATE_TYPE,
selectedItem: selectedItems[selectedItems.length - 1]
});
(e as any).nativeEvent.preventDownshiftDefault = true;
e.preventDefault();
e.stopPropagation();
}
}
},
isVisible: isFocused || inputValue || selectedItems.length === 0,