-
-
Notifications
You must be signed in to change notification settings - Fork 170
/
select.dom.ts
51 lines (49 loc) · 2.43 KB
/
select.dom.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { createScope, isHTMLElement, nextById, prevById, query, queryAll, getByTypeahead } from "@zag-js/dom-query"
import type { MachineContext as Ctx, Option } from "./select.types"
export const dom = createScope({
getContentId: (ctx: Ctx) => ctx.ids?.content ?? `select:${ctx.id}:content`,
getTriggerId: (ctx: Ctx) => ctx.ids?.trigger ?? `select:${ctx.id}:trigger`,
getLabelId: (ctx: Ctx) => ctx.ids?.label ?? `select:${ctx.id}:label`,
getOptionId: (ctx: Ctx, id: string | number) => ctx.ids?.option?.(id) ?? `select:${ctx.id}:option:${id}`,
getHiddenSelectId: (ctx: Ctx) => `select:${ctx.id}:select`,
getPositionerId: (ctx: Ctx) => `select:${ctx.id}:positioner`,
getOptionGroupId: (ctx: Ctx, id: string | number) => `select:${ctx.id}:optgroup:${id}`,
getOptionGroupLabelId: (ctx: Ctx, id: string | number) => `select:${ctx.id}:optgroup-label:${id}`,
getHiddenSelectElement: (ctx: Ctx) => dom.getById(ctx, dom.getHiddenSelectId(ctx)),
getContentElement: (ctx: Ctx) => dom.getById(ctx, dom.getContentId(ctx)),
getTriggerElement: (ctx: Ctx) => dom.getById(ctx, dom.getTriggerId(ctx)),
getPositionerElement: (ctx: Ctx) => {
return dom.getById(ctx, dom.getPositionerId(ctx))
},
getOptionElements: (ctx: Ctx) => {
return queryAll(dom.getContentElement(ctx), "[role=option]:not([data-disabled])")
},
getFirstOption: (ctx: Ctx) => {
return query(dom.getContentElement(ctx), "[role=option]:not([data-disabled])")
},
getLastOption: (ctx: Ctx) => {
return query(dom.getContentElement(ctx), "[role=option]:not([data-disabled]):last-of-type")
},
getNextOption: (ctx: Ctx, currentId: string) => {
const options = dom.getOptionElements(ctx)
return nextById(options, currentId, ctx.loop)
},
getPreviousOption: (ctx: Ctx, currentId: string) => {
const options = dom.getOptionElements(ctx)
return prevById(options, currentId, ctx.loop)
},
getOptionDetails(option: HTMLElement) {
const { label, value } = option.dataset
return { label, value } as Option
},
getMatchingOption(ctx: Ctx, key: string, current: any) {
return getByTypeahead(dom.getOptionElements(ctx), { state: ctx.typeahead, key, activeId: current })
},
getHighlightedOption(ctx: Ctx) {
if (!ctx.highlightedId) return null
return dom.getById(ctx, ctx.highlightedId)
},
getClosestOption(target: EventTarget | HTMLElement | null) {
return isHTMLElement(target) ? target.closest<HTMLElement>("[data-part=option]") : null
},
})