Skip to content

Commit

Permalink
chore: update navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
anubra266 committed Jan 4, 2025
1 parent a82fe35 commit 42c2828
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 58 deletions.
4 changes: 2 additions & 2 deletions packages/machines/cascader/src/cascader.connect.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { NormalizeProps, PropTypes } from "@zag-js/types"
import { getEventKey, isLeftClick, type EventKeyMap } from "@zag-js/dom-event"
import type { NormalizeProps, PropTypes, EventKeyMap } from "@zag-js/types"
import { getEventKey, isLeftClick } from "@zag-js/dom-query"

import type { State, Send, MachineApi, NodeProps, NodeState } from "./cascader.types"
import { parts } from "./cascader.anatomy"
Expand Down
27 changes: 19 additions & 8 deletions packages/machines/cascader/src/cascader.machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,19 +455,26 @@ export function machine<V>(userContext: UserDefinedContext<V>) {
highlightItemFirstChild(ctx) {
const item = ctx.collection.at(ctx.highlightedIndexPath)
if (!ctx.collection.isBranchNode(item)) return
ctx.highlightedIndexPath.push(0)

const newPath = [...ctx.highlightedIndexPath, 0]
set.highlightedItem(ctx, newPath)
},
highlightItemParent(ctx) {
ctx.highlightedIndexPath.pop()
const parentPath = ctx.highlightedIndexPath.slice(0, -1)
set.highlightedItem(ctx, parentPath)
},
highlightFirstItem(ctx) {
ctx.highlightedIndexPath[ctx.highlightedIndexPath.length - 1] = 0
const newPath = [...ctx.highlightedIndexPath]
newPath[newPath.length - 1] = 0
set.highlightedItem(ctx, newPath)
},
highlightLastItem(ctx) {
const siblings = utils.getSiblings(ctx)
if (!siblings) return

ctx.highlightedIndexPath[ctx.highlightedIndexPath.length - 1] = siblings.length - 1
const newPath = [...ctx.highlightedIndexPath]
newPath[newPath.length - 1] = siblings.length - 1
set.highlightedItem(ctx, newPath)
},
highlightFirstItemInRoot(ctx) {
set.highlightedItem(ctx, [0])
Expand All @@ -477,12 +484,16 @@ export function machine<V>(userContext: UserDefinedContext<V>) {
set.highlightedItem(ctx, [rootNodes.length - 1])
},
highlightNextItem(ctx) {
const nextSibling = utils.getNextSibling(ctx)
set.highlightedItem(ctx, nextSibling)
const nextSibling = ctx.collection.getNextSibling(ctx.highlightedIndexPath)
if (!nextSibling) return
const value = ctx.collection.getNodeValue(nextSibling)
set.highlightedItem(ctx, ctx.collection.getIndexPath(value))
},
highlightPreviousItem(ctx) {
const previousSibling = utils.getPreviousSibling(ctx)
set.highlightedItem(ctx, previousSibling)
const previousSibling = ctx.collection.getPreviousSibling(ctx.highlightedIndexPath)
if (!previousSibling) return
const value = ctx.collection.getNodeValue(previousSibling)
set.highlightedItem(ctx, ctx.collection.getIndexPath(value))
},
highlightLastSelectedItem(ctx) {
const lastSelected = ctx.valueIndexPaths.at(-1)
Expand Down
48 changes: 0 additions & 48 deletions packages/machines/cascader/src/cascader.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,51 +12,3 @@ export function getSiblings(ctx: MachineContext) {
if (siblings.length <= 1) return
return siblings
}

export function getNextSibling(ctx: MachineContext) {
const siblings = getSiblings(ctx)
if (!siblings) return
const currentIndex = ctx.highlightedIndexPath[ctx.highlightedIndexPath.length - 1]

// Find next enabled sibling
for (let i = currentIndex + 1; i < siblings.length; i++) {
if (!ctx.collection.getNodeDisabled(siblings[i])) {
return [...ctx.highlightedIndexPath.slice(0, -1), i]
}
}

// If loop is enabled, search from start
if (ctx.loopFocus) {
for (let i = 0; i < currentIndex; i++) {
if (!ctx.collection.getNodeDisabled(siblings[i])) {
return [...ctx.highlightedIndexPath.slice(0, -1), i]
}
}
}

return ctx.highlightedIndexPath
}

export function getPreviousSibling(ctx: MachineContext) {
const siblings = getSiblings(ctx)
if (!siblings) return
const currentIndex = ctx.highlightedIndexPath[ctx.highlightedIndexPath.length - 1]

// Find previous enabled sibling
for (let i = currentIndex - 1; i >= 0; i--) {
if (!ctx.collection.getNodeDisabled(siblings[i])) {
return [...ctx.highlightedIndexPath.slice(0, -1), i]
}
}

// If loop is enabled, search from end
if (ctx.loopFocus) {
for (let i = siblings.length - 1; i > currentIndex; i--) {
if (!ctx.collection.getNodeDisabled(siblings[i])) {
return [...ctx.highlightedIndexPath.slice(0, -1), i]
}
}
}

return ctx.highlightedIndexPath
}

0 comments on commit 42c2828

Please sign in to comment.