@awsui/components-react
Version:
On July 19th, 2022, we launched [Cloudscape Design System](https://cloudscape.design). Cloudscape is an evolution of AWS-UI. It consists of user interface guidelines, front-end components, design resources, and development tools for building intuitive, en
45 lines • 2.3 kB
JavaScript
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { isSameMonth, isSameYear } from 'date-fns';
import { KeyCode } from '../../internal/keycode';
import handleKey from '../../internal/utils/handle-key';
import { moveNextDay, moveNextWeek, movePrevDay, movePrevWeek } from '../utils/navigation-day';
import { moveMonthDown, moveMonthUp, moveNextMonth, movePrevMonth } from '../utils/navigation-month';
export default function useCalendarGridKeyboardNavigation({ baseDate, focusableDate, granularity, isDateEnabled, isDateFocusable, onChangePage, onFocusDate, onSelectDate, }) {
const isMonthPicker = granularity === 'month';
const moveDown = isMonthPicker ? moveMonthDown : moveNextWeek;
const moveLeft = isMonthPicker ? movePrevMonth : movePrevDay;
const moveRight = isMonthPicker ? moveNextMonth : moveNextDay;
const moveUp = isMonthPicker ? moveMonthUp : movePrevWeek;
const isSamePage = isMonthPicker ? isSameYear : isSameMonth;
const onGridKeyDownHandler = (event) => {
let updatedFocusDate;
const keys = [KeyCode.up, KeyCode.down, KeyCode.left, KeyCode.right, KeyCode.space, KeyCode.enter];
if (focusableDate === null || keys.indexOf(event.keyCode) === -1) {
return;
}
event.preventDefault();
handleKey(event, {
onActivate: () => {
if (!isDateEnabled(focusableDate)) {
return;
}
onFocusDate(null);
onSelectDate(focusableDate);
},
onBlockEnd: () => (updatedFocusDate = moveDown(focusableDate, isDateFocusable)),
onBlockStart: () => (updatedFocusDate = moveUp(focusableDate, isDateFocusable)),
onInlineStart: () => (updatedFocusDate = moveLeft(focusableDate, isDateFocusable)),
onInlineEnd: () => (updatedFocusDate = moveRight(focusableDate, isDateFocusable)),
});
if (!updatedFocusDate) {
return;
}
if (!isSamePage(updatedFocusDate, baseDate)) {
onChangePage(updatedFocusDate);
}
onFocusDate(updatedFocusDate);
};
return onGridKeyDownHandler;
}
//# sourceMappingURL=use-calendar-grid-keyboard-navigation.js.map