Skip to content

Commit

Permalink
feat(time-picker): ✨ add keyboard navigation to the column
Browse files Browse the repository at this point in the history
  • Loading branch information
navin-moorthy committed Oct 12, 2020
1 parent 502eda3 commit 0a514ad
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
9 changes: 0 additions & 9 deletions src/timepicker/TimePickerColumn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,6 @@ export const useTimePickerColumn = createHook<
name: "TimePickerColumn",
compose: useComposite,
keys: TIME_PICKER_COLUMN_KEYS,

useProps(options, htmlProps) {
return {
onKeyDown: (e: React.KeyboardEvent) => {
// console.log("%c e", "color: #0088cc", e);
},
...htmlProps,
};
},
});

export const TimePickerColumn = createComponent({
Expand Down
37 changes: 37 additions & 0 deletions src/timepicker/TimePickerContent.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import * as React from "react";
import { createComponent, createHook } from "reakit-system";
import { createOnKeyDown, useForkRef } from "reakit-utils";
import { focus, getNextTabbable, getPreviousTabbable } from "@chakra-ui/utils";

import {
PickerBaseContentHTMLProps,
Expand All @@ -21,6 +24,40 @@ export const useTimePickerContent = createHook<
name: "TimePickerContent",
compose: usePickerBaseContent,
keys: TIME_PICKER_CONTENT_KEYS,

useProps(_, { ref: htmlRef, onKeyDown: htmlOnKeyDown, ...htmlProps }) {
const ref = React.useRef<HTMLElement>(null);

const onKeyDown = React.useMemo(() => {
return createOnKeyDown({
onKeyDown: htmlOnKeyDown,
keyMap: () => {
return {
ArrowRight: () => {
if (ref.current) {
const nextTabbableElement = getNextTabbable(ref.current);
if (nextTabbableElement) {
focus(nextTabbableElement);
}
}
},
ArrowLeft: () => {
if (ref.current) {
const previousTabbableElement = getPreviousTabbable(
ref.current,
);
if (previousTabbableElement) {
focus(previousTabbableElement);
}
}
},
};
},
});
}, [htmlOnKeyDown]);

return { ref: useForkRef(ref, htmlRef), onKeyDown, ...htmlProps };
},
});

export const TimePickerContent = createComponent({
Expand Down

0 comments on commit 0a514ad

Please sign in to comment.