Skip to content

Commit

Permalink
fix: datepicker controlled focus
Browse files Browse the repository at this point in the history
  • Loading branch information
segunadebayo committed Feb 2, 2024
1 parent b9057aa commit 57095b6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .xstate/date-picker.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const fetchMachine = createMachine({
"isOpenControlled": false,
"isOpenControlled": false,
"isOpenControlled": false,
"shouldRestoreFocus && isInteractOutsideEvent": false,
"shouldRestoreFocus": false,
"isMonthView": false,
"isYearView": false,
Expand Down Expand Up @@ -167,6 +168,10 @@ const fetchMachine = createMachine({
exit: ["clearHoveredDate", "resetView"],
on: {
"CONTROLLED.CLOSE": [{
cond: "shouldRestoreFocus && isInteractOutsideEvent",
target: "focused",
actions: ["focusTriggerElement"]
}, {
cond: "shouldRestoreFocus",
target: "focused",
actions: ["focusInputElement"]
Expand Down Expand Up @@ -387,6 +392,7 @@ const fetchMachine = createMachine({
"isYearView": ctx => ctx["isYearView"],
"isMonthView": ctx => ctx["isMonthView"],
"isOpenControlled": ctx => ctx["isOpenControlled"],
"shouldRestoreFocus && isInteractOutsideEvent": ctx => ctx["shouldRestoreFocus && isInteractOutsideEvent"],
"shouldRestoreFocus": ctx => ctx["shouldRestoreFocus"],
"isRangePicker && hasSelectedRange": ctx => ctx["isRangePicker && hasSelectedRange"],
"isRangePicker && isSelectingEndDate && closeOnSelect && isOpenControlled": ctx => ctx["isRangePicker && isSelectingEndDate && closeOnSelect && isOpenControlled"],
Expand Down
10 changes: 8 additions & 2 deletions packages/machines/date-picker/src/date-picker.machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,11 @@ export function machine(userContext: UserDefinedContext) {
exit: ["clearHoveredDate", "resetView"],
on: {
"CONTROLLED.CLOSE": [
{
guard: and("shouldRestoreFocus", "isInteractOutsideEvent"),
target: "focused",
actions: ["focusTriggerElement"],
},
{
guard: "shouldRestoreFocus",
target: "focused",
Expand Down Expand Up @@ -524,6 +529,7 @@ export function machine(userContext: UserDefinedContext) {
isSelectingEndDate: (ctx) => ctx.activeIndex === 1,
closeOnSelect: (ctx) => !!ctx.closeOnSelect,
isOpenControlled: (ctx) => !!ctx.__controlled,
isInteractOutsideEvent: (_ctx, evt) => evt.previousEvent?.type === "INTERACT_OUTSIDE",
},
activities: {
trackPositioning(ctx) {
Expand Down Expand Up @@ -817,8 +823,8 @@ export function machine(userContext: UserDefinedContext) {
invokeOnClose(ctx) {
ctx.onOpenChange?.({ open: false })
},
toggleVisibility(ctx, _evt, { send }) {
send({ type: ctx.open ? "CONTROLLED.OPEN" : "CONTROLLED.CLOSE" })
toggleVisibility(ctx, evt, { send }) {
send({ type: ctx.open ? "CONTROLLED.OPEN" : "CONTROLLED.CLOSE", previousEvent: evt })
},
},
compareFns: {
Expand Down

0 comments on commit 57095b6

Please sign in to comment.