|
@@ -1,3 +1,4 @@
|
|
|
+import { generateContext, renderHiddenInputs } from './list-context'
|
|
|
|
|
|
export default function (Alpine) {
|
|
|
Alpine.directive('combobox', (el, directive, { evaluate }) => {
|
|
@@ -9,57 +10,258 @@ export default function (Alpine) {
|
|
|
else handleRoot(el, Alpine)
|
|
|
}).before('bind')
|
|
|
|
|
|
- Alpine.magic('comboboxOption', el => {
|
|
|
- let $data = Alpine.$data(el)
|
|
|
+ Alpine.magic('combobox', el => {
|
|
|
+ let data = Alpine.$data(el)
|
|
|
+
|
|
|
+ return {
|
|
|
+ get value() {
|
|
|
+ return data.__value
|
|
|
+ },
|
|
|
+ get isOpen() {
|
|
|
+ return data.__isOpen
|
|
|
+ },
|
|
|
+ get isDisabled() {
|
|
|
+ return data.__isDisabled
|
|
|
+ },
|
|
|
+ get activeOption() {
|
|
|
+ let active = data.__context?.getActiveItem()
|
|
|
+
|
|
|
+ return active && active.value
|
|
|
+ },
|
|
|
+ get activeIndex() {
|
|
|
+ let active = data.__context?.getActiveItem()
|
|
|
+
|
|
|
+ if (active) {
|
|
|
+ return Object.values(Alpine.raw(data.__context.items)).findIndex(i => Alpine.raw(active) == Alpine.raw(i))
|
|
|
+ }
|
|
|
|
|
|
- return $data.$item
|
|
|
+ return null
|
|
|
+ },
|
|
|
+ }
|
|
|
})
|
|
|
|
|
|
- registerListStuff(Alpine)
|
|
|
+ Alpine.magic('comboboxOption', el => {
|
|
|
+ let data = Alpine.$data(el)
|
|
|
+
|
|
|
+ let optionEl = Alpine.findClosest(el, i => i.__optionKey)
|
|
|
+
|
|
|
+ if (! optionEl) throw 'No x-combobox:option directive found...'
|
|
|
+
|
|
|
+ return {
|
|
|
+ get isActive() {
|
|
|
+ return data.__context.isActiveKey(optionEl.__optionKey)
|
|
|
+ },
|
|
|
+ get isSelected() {
|
|
|
+ return data.__isSelected(optionEl)
|
|
|
+ },
|
|
|
+ get isDisabled() {
|
|
|
+ return data.__context.isDisabled(optionEl.__optionKey)
|
|
|
+ },
|
|
|
+ }
|
|
|
+ })
|
|
|
}
|
|
|
|
|
|
function handleRoot(el, Alpine) {
|
|
|
Alpine.bind(el, {
|
|
|
- 'x-id'() { return ['headlessui-combobox-button', 'headlessui-combobox-options', 'headlessui-combobox-label'] },
|
|
|
- 'x-list': '__value',
|
|
|
+ // Setup...
|
|
|
+ 'x-id'() { return ['alpine-combobox-button', 'alpine-combobox-options', 'alpine-combobox-label'] },
|
|
|
'x-modelable': '__value',
|
|
|
+
|
|
|
+ // Initialize...
|
|
|
'x-data'() {
|
|
|
return {
|
|
|
- init() {
|
|
|
- this.$nextTick(() => {
|
|
|
- this.syncInputValue()
|
|
|
+ /**
|
|
|
+ * Combobox state...
|
|
|
+ */
|
|
|
+ __ready: false,
|
|
|
+ __value: null,
|
|
|
+ __isOpen: false,
|
|
|
+ __context: undefined,
|
|
|
+ __isMultiple: undefined,
|
|
|
+ __isStatic: false,
|
|
|
+ __isDisabled: undefined,
|
|
|
+ __displayValue: undefined,
|
|
|
+ __compareBy: null,
|
|
|
+ __inputName: null,
|
|
|
+ __isTyping: false,
|
|
|
+ __hold: false,
|
|
|
|
|
|
- Alpine.effect(() => this.syncInputValue())
|
|
|
+ /**
|
|
|
+ * Combobox initialization...
|
|
|
+ */
|
|
|
+ init() {
|
|
|
+ this.__isMultiple = Alpine.extractProp(el, 'multiple', false)
|
|
|
+ this.__isDisabled = Alpine.extractProp(el, 'disabled', false)
|
|
|
+ this.__inputName = Alpine.extractProp(el, 'name', null)
|
|
|
+ this.__nullable = Alpine.extractProp(el, 'nullable', false)
|
|
|
+ this.__compareBy = Alpine.extractProp(el, 'by')
|
|
|
+
|
|
|
+ this.__context = generateContext(Alpine, this.__isMultiple, 'vertical', () => this.__activateSelectedOrFirst())
|
|
|
+
|
|
|
+ let defaultValue = Alpine.extractProp(el, 'default-value', this.__isMultiple ? [] : null)
|
|
|
+
|
|
|
+ this.__value = defaultValue
|
|
|
+
|
|
|
+ // We have to wait again until after the "ready" processes are finished
|
|
|
+ // to settle up currently selected Values (this prevents this next bit
|
|
|
+ // of code from running multiple times on startup...)
|
|
|
+ queueMicrotask(() => {
|
|
|
+ Alpine.effect(() => {
|
|
|
+ // Everytime the value changes, we need to re-render the hidden inputs,
|
|
|
+ // if a user passed the "name" prop...
|
|
|
+ this.__inputName && renderHiddenInputs(Alpine, this.$el, this.__inputName, this.__value)
|
|
|
+ })
|
|
|
})
|
|
|
},
|
|
|
- __value: null,
|
|
|
- __disabled: false,
|
|
|
- __static: false,
|
|
|
- __hold: false,
|
|
|
- __displayValue: i => i,
|
|
|
- __isOpen: false,
|
|
|
- __optionsEl: null,
|
|
|
+ __startTyping() {
|
|
|
+ this.__isTyping = true
|
|
|
+ },
|
|
|
+ __stopTyping() {
|
|
|
+ this.__isTyping = false
|
|
|
+ },
|
|
|
+ __resetInput() {
|
|
|
+ let input = this.$refs.__input
|
|
|
+
|
|
|
+ if (! input) return
|
|
|
+
|
|
|
+ let value = this.__getCurrentValue()
|
|
|
+
|
|
|
+ input.value = value
|
|
|
+ },
|
|
|
+ __getCurrentValue() {
|
|
|
+ if (! this.$refs.__input) return ''
|
|
|
+ if (! this.__value) return ''
|
|
|
+ if (this.__displayValue) return this.__displayValue(this.__value)
|
|
|
+ if (typeof this.__value === 'string') return this.__value
|
|
|
+ return ''
|
|
|
+ },
|
|
|
__open() {
|
|
|
- // @todo handle disabling the entire combobox.
|
|
|
if (this.__isOpen) return
|
|
|
this.__isOpen = true
|
|
|
|
|
|
- this.$list.activateSelectedOrFirst()
|
|
|
+ let input = this.$refs.__input
|
|
|
+
|
|
|
+ // Make sure we always notify the parent component
|
|
|
+ // that the starting value is the empty string
|
|
|
+ // when we open the combobox (ignoring any existing value)
|
|
|
+ // to avoid inconsistent displaying.
|
|
|
+ // Setting the input to empty and back to the real value
|
|
|
+ // also helps VoiceOver to annunce the content properly
|
|
|
+ // See https://github.com/tailwindlabs/headlessui/pull/2153
|
|
|
+ if (input) {
|
|
|
+ let value = input.value
|
|
|
+ let { selectionStart, selectionEnd, selectionDirection } = input
|
|
|
+ input.value = ''
|
|
|
+ input.dispatchEvent(new Event('change'))
|
|
|
+ input.value = value
|
|
|
+ if (selectionDirection !== null) {
|
|
|
+ input.setSelectionRange(selectionStart, selectionEnd, selectionDirection)
|
|
|
+ } else {
|
|
|
+ input.setSelectionRange(selectionStart, selectionEnd)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Safari needs more of a "tick" for focusing after x-show for some reason.
|
|
|
+ // Probably because Alpine adds an extra tick when x-showing for @click.outside
|
|
|
+ let nextTick = callback => requestAnimationFrame(() => requestAnimationFrame(callback))
|
|
|
+
|
|
|
+ nextTick(() => {
|
|
|
+ this.$refs.__input.focus({ preventScroll: true })
|
|
|
+ this.__activateSelectedOrFirst()
|
|
|
+ })
|
|
|
},
|
|
|
__close() {
|
|
|
- this.syncInputValue()
|
|
|
+ this.__isOpen = false
|
|
|
|
|
|
- if (this.__static) return
|
|
|
+ this.__context.deactivate()
|
|
|
+ },
|
|
|
+ __activateSelectedOrFirst(activateSelected = true) {
|
|
|
if (! this.__isOpen) return
|
|
|
|
|
|
- this.__isOpen = false
|
|
|
- this.$list.active = null
|
|
|
+ if (this.__context.hasActive() && this.__context.wasActivatedByKeyPress()) return
|
|
|
+
|
|
|
+ let firstSelectedValue
|
|
|
+
|
|
|
+ if (this.__isMultiple) {
|
|
|
+ let selectedItem = this.__context.getItemsByValues(this.__value)
|
|
|
+
|
|
|
+ firstSelectedValue = selectedItem.length ? selectedItem[0].value : null
|
|
|
+ } else {
|
|
|
+ firstSelectedValue = this.__value
|
|
|
+ }
|
|
|
+
|
|
|
+ let firstSelected = null
|
|
|
+ if (activateSelected && firstSelectedValue) {
|
|
|
+ firstSelected = this.__context.getItemByValue(firstSelectedValue)
|
|
|
+ }
|
|
|
+
|
|
|
+ if (firstSelected) {
|
|
|
+ this.__context.activateAndScrollToKey(firstSelected.key)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ this.__context.activateAndScrollToKey(this.__context.firstKey())
|
|
|
+ },
|
|
|
+ __selectActive() {
|
|
|
+ let active = this.__context.getActiveItem()
|
|
|
+ if (active) this.__toggleSelected(active.value)
|
|
|
+ },
|
|
|
+ __selectOption(el) {
|
|
|
+ let item = this.__context.getItemByEl(el)
|
|
|
+
|
|
|
+ if (item) this.__toggleSelected(item.value)
|
|
|
+ },
|
|
|
+ __isSelected(el) {
|
|
|
+ let item = this.__context.getItemByEl(el)
|
|
|
+
|
|
|
+ if (! item) return false
|
|
|
+ if (! item.value) return false
|
|
|
+
|
|
|
+ return this.__hasSelected(item.value)
|
|
|
+ },
|
|
|
+ __toggleSelected(value) {
|
|
|
+ if (! this.__isMultiple) {
|
|
|
+ this.__value = value
|
|
|
+
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ let index = this.__value.findIndex(j => this.__compare(j, value))
|
|
|
+
|
|
|
+ if (index === -1) {
|
|
|
+ this.__value.push(value)
|
|
|
+ } else {
|
|
|
+ this.__value.splice(index, 1)
|
|
|
+ }
|
|
|
},
|
|
|
- syncInputValue() {
|
|
|
- if (this.$list.selected) this.$refs.__input.value = this.__displayValue(this.$list.selected)
|
|
|
+ __hasSelected(value) {
|
|
|
+ if (! this.__isMultiple) return this.__compare(this.__value, value)
|
|
|
+
|
|
|
+ return this.__value.some(i => this.__compare(i, value))
|
|
|
+ },
|
|
|
+ __compare(a, b) {
|
|
|
+ let by = this.__compareBy
|
|
|
+
|
|
|
+ if (! by) by = (a, b) => Alpine.raw(a) === Alpine.raw(b)
|
|
|
+
|
|
|
+ if (typeof by === 'string') {
|
|
|
+ let property = by
|
|
|
+ by = (a, b) => {
|
|
|
+ // Handle null values
|
|
|
+ if ((! a || typeof a !== 'object') || (! b || typeof b !== 'object')) {
|
|
|
+ return Alpine.raw(a) === Alpine.raw(b)
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ return a[property] === b[property];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return by(a, b)
|
|
|
},
|
|
|
}
|
|
|
},
|
|
|
+ // Register event listeners..
|
|
|
'@mousedown.window'(e) {
|
|
|
if (
|
|
|
!! ! this.$refs.__input.contains(e.target)
|
|
@@ -67,6 +269,7 @@ function handleRoot(el, Alpine) {
|
|
|
&& ! this.$refs.__options.contains(e.target)
|
|
|
) {
|
|
|
this.__close()
|
|
|
+ this.__resetInput()
|
|
|
}
|
|
|
}
|
|
|
})
|
|
@@ -74,54 +277,116 @@ function handleRoot(el, Alpine) {
|
|
|
|
|
|
function handleInput(el, Alpine) {
|
|
|
Alpine.bind(el, {
|
|
|
+ // Setup...
|
|
|
'x-ref': '__input',
|
|
|
- ':id'() { return this.$id('headlessui-combobox-input') },
|
|
|
+ ':id'() { return this.$id('alpine-combobox-input') },
|
|
|
+
|
|
|
+ // Accessibility attributes...
|
|
|
'role': 'combobox',
|
|
|
'tabindex': '0',
|
|
|
- ':aria-controls'() { return this.$data.__optionsEl && this.$data.__optionsEl.id },
|
|
|
- ':aria-expanded'() { return this.$data.__disabled ? undefined : this.$data.__isOpen },
|
|
|
- ':aria-activedescendant'() { return this.$data.$list.activeEl ? this.$data.$list.activeEl.id : null },
|
|
|
+ 'aria-autocomplete': 'list',
|
|
|
+
|
|
|
+ // We need to defer this evaluation a bit because $refs that get declared later
|
|
|
+ // in the DOM aren't available yet when x-ref is the result of an Alpine.bind object.
|
|
|
+ async ':aria-controls'() { return await microtask(() => this.$refs.__options && this.$refs.__options.id) },
|
|
|
+ ':aria-expanded'() { return this.$data.__isDisabled ? undefined : this.$data.__isOpen },
|
|
|
+ ':aria-multiselectable'() { return this.$data.__isMultiple ? true : undefined },
|
|
|
+ ':aria-activedescendant'() {
|
|
|
+ if (! this.$data.__context.hasActive()) return
|
|
|
+
|
|
|
+ let active = this.$data.__context.getActiveItem()
|
|
|
+
|
|
|
+ return active ? active.el.id : null
|
|
|
+ },
|
|
|
':aria-labelledby'() { return this.$refs.__label ? this.$refs.__label.id : (this.$refs.__button ? this.$refs.__button.id : null) },
|
|
|
+
|
|
|
+ // Initialize...
|
|
|
'x-init'() {
|
|
|
- queueMicrotask(() => {
|
|
|
- Alpine.effect(() => {
|
|
|
- this.$data.__disabled = Alpine.bound(this.$el, 'disabled', false)
|
|
|
- })
|
|
|
+ let displayValueFn = Alpine.extractProp(this.$el, 'display-value')
|
|
|
+ if (displayValueFn) this.$data.__displayValue = displayValueFn
|
|
|
+ },
|
|
|
|
|
|
- let displayValueFn = Alpine.bound(this.$el, 'display-value')
|
|
|
- if (displayValueFn) this.$data.__displayValue = displayValueFn
|
|
|
- })
|
|
|
+ // Register listeners...
|
|
|
+ '@input.stop'(e) {
|
|
|
+ if(this.$data.__isTyping) {
|
|
|
+ this.$data.__open();
|
|
|
+ this.$dispatch('change')
|
|
|
+ }
|
|
|
+ },
|
|
|
+ '@blur'() { this.$data.__stopTyping(false) },
|
|
|
+ '@keydown'(e) {
|
|
|
+ queueMicrotask(() => this.$data.__context.activateByKeyEvent(e, false, () => this.$data.__isOpen, () => this.$data.__open(), (state) => this.$data.__isTyping = state))
|
|
|
+ },
|
|
|
+ '@keydown.enter.prevent.stop'() {
|
|
|
+ this.$data.__selectActive()
|
|
|
+
|
|
|
+ this.$data.__stopTyping()
|
|
|
+
|
|
|
+ if (! this.$data.__isMultiple) {
|
|
|
+ this.$data.__close()
|
|
|
+ this.$data.__resetInput()
|
|
|
+ }
|
|
|
},
|
|
|
- '@input.stop'() { this.$data.__open(); this.$dispatch('change') },
|
|
|
- '@change.stop'() {},
|
|
|
- '@keydown.enter.prevent.stop'() { this.$list.selectActive(); this.$data.__close() },
|
|
|
- '@keydown'(e) { this.$list.handleKeyboardNavigation(e) },
|
|
|
- '@keydown.down'(e) { if(! this.$data.__isOpen) this.$data.__open(); },
|
|
|
- '@keydown.up'(e) { if(! this.$data.__isOpen) this.$data.__open(); },
|
|
|
'@keydown.escape.prevent'(e) {
|
|
|
if (! this.$data.__static) e.stopPropagation()
|
|
|
|
|
|
+ this.$data.__stopTyping()
|
|
|
this.$data.__close()
|
|
|
+ this.$data.__resetInput()
|
|
|
+
|
|
|
+ },
|
|
|
+ '@keydown.tab'() {
|
|
|
+ this.$data.__stopTyping()
|
|
|
+ if (this.$data.__isOpen) { this.$data.__close() }
|
|
|
+ this.$data.__resetInput()
|
|
|
+ },
|
|
|
+ '@keydown.backspace'(e) {
|
|
|
+ if (this.$data.__isMultiple) return
|
|
|
+ if (! this.$data.__nullable) return
|
|
|
+
|
|
|
+ let input = e.target
|
|
|
+
|
|
|
+ requestAnimationFrame(() => {
|
|
|
+ if (input.value === '') {
|
|
|
+ this.$data.__value = null
|
|
|
+
|
|
|
+ let options = this.$refs.__options
|
|
|
+ if (options) {
|
|
|
+ options.scrollTop = 0
|
|
|
+ }
|
|
|
+
|
|
|
+ this.$data.__context.deactivate()
|
|
|
+ }
|
|
|
+ })
|
|
|
},
|
|
|
- '@keydown.tab'() { if (this.$data.__isOpen) { this.$list.selectActive(); this.$data.__close() }},
|
|
|
})
|
|
|
}
|
|
|
|
|
|
function handleButton(el, Alpine) {
|
|
|
Alpine.bind(el, {
|
|
|
+ // Setup...
|
|
|
'x-ref': '__button',
|
|
|
- ':id'() { return this.$id('headlessui-combobox-button') },
|
|
|
+ ':id'() { return this.$id('alpine-combobox-button') },
|
|
|
+
|
|
|
+ // Accessibility attributes...
|
|
|
'aria-haspopup': 'true',
|
|
|
+ // We need to defer this evaluation a bit because $refs that get declared later
|
|
|
+ // in the DOM aren't available yet when x-ref is the result of an Alpine.bind object.
|
|
|
+ async ':aria-controls'() { return await microtask(() => this.$refs.__options && this.$refs.__options.id) },
|
|
|
':aria-labelledby'() { return this.$refs.__label ? [this.$refs.__label.id, this.$el.id].join(' ') : null },
|
|
|
- ':aria-expanded'() { return this.$data.__disabled ? null : this.$data.__isOpen },
|
|
|
- ':aria-controls'() { return this.$data.__optionsEl ? this.$data.__optionsEl.id : null },
|
|
|
- ':disabled'() { return this.$data.__disabled },
|
|
|
+ ':aria-expanded'() { return this.$data.__isDisabled ? null : this.$data.__isOpen },
|
|
|
+ ':disabled'() { return this.$data.__isDisabled },
|
|
|
'tabindex': '-1',
|
|
|
+
|
|
|
+ // Initialize....
|
|
|
'x-init'() { if (this.$el.tagName.toLowerCase() === 'button' && ! this.$el.hasAttribute('type')) this.$el.type = 'button' },
|
|
|
+
|
|
|
+ // Register listeners...
|
|
|
'@click'(e) {
|
|
|
- if (this.$data.__disabled) return
|
|
|
+ if (this.$data.__isDisabled) return
|
|
|
if (this.$data.__isOpen) {
|
|
|
this.$data.__close()
|
|
|
+ this.$data.__resetInput()
|
|
|
} else {
|
|
|
e.preventDefault()
|
|
|
this.$data.__open()
|
|
@@ -129,422 +394,109 @@ function handleButton(el, Alpine) {
|
|
|
|
|
|
this.$nextTick(() => this.$refs.__input.focus({ preventScroll: true }))
|
|
|
},
|
|
|
- '@keydown.down.prevent.stop'() {
|
|
|
- if (! this.$data.__isOpen) {
|
|
|
- this.$data.__open()
|
|
|
- this.$list.activateSelectedOrFirst()
|
|
|
- }
|
|
|
-
|
|
|
- this.$nextTick(() => this.$refs.__input.focus({ preventScroll: true }))
|
|
|
- },
|
|
|
- '@keydown.up.prevent.stop'() {
|
|
|
- if (! this.$data.__isOpen) {
|
|
|
- this.$data.__open()
|
|
|
- this.$list.activateSelectedOrLast()
|
|
|
- }
|
|
|
-
|
|
|
- this.$nextTick(() => this.$refs.__input.focus({ preventScroll: true }))
|
|
|
- },
|
|
|
- '@keydown.escape.prevent'(e) {
|
|
|
- if (! this.$data.__static) e.stopPropagation()
|
|
|
-
|
|
|
- this.$data.__close()
|
|
|
- this.$nextTick(() => this.$refs.__input.focus({ preventScroll: true }))
|
|
|
- },
|
|
|
})
|
|
|
}
|
|
|
|
|
|
function handleLabel(el, Alpine) {
|
|
|
Alpine.bind(el, {
|
|
|
'x-ref': '__label',
|
|
|
- ':id'() { return this.$id('headlessui-combobox-label') },
|
|
|
+ ':id'() { return this.$id('alpine-combobox-label') },
|
|
|
'@click'() { this.$refs.__input.focus({ preventScroll: true }) },
|
|
|
})
|
|
|
}
|
|
|
|
|
|
function handleOptions(el, Alpine) {
|
|
|
Alpine.bind(el, {
|
|
|
+ // Setup...
|
|
|
'x-ref': '__options',
|
|
|
- 'x-init'() {
|
|
|
- this.$data.__optionsEl = this.$el
|
|
|
-
|
|
|
- queueMicrotask(() => {
|
|
|
- if (Alpine.bound(this.$el, 'static')) {
|
|
|
- this.$data.__open()
|
|
|
- this.$data.__static = true;
|
|
|
- }
|
|
|
-
|
|
|
- if (Alpine.bound(this.$el, 'hold')) {
|
|
|
- this.$data.__hold = true;
|
|
|
- }
|
|
|
- })
|
|
|
+ ':id'() { return this.$id('alpine-combobox-options') },
|
|
|
|
|
|
- // Add `role="none"` to all non option elements.
|
|
|
- this.$nextTick(() => {
|
|
|
- let walker = document.createTreeWalker(
|
|
|
- this.$el,
|
|
|
- NodeFilter.SHOW_ELEMENT,
|
|
|
- { acceptNode: node => {
|
|
|
- if (node.getAttribute('role') === 'option') return NodeFilter.FILTER_REJECT
|
|
|
- if (node.hasAttribute('role')) return NodeFilter.FILTER_SKIP
|
|
|
- return NodeFilter.FILTER_ACCEPT
|
|
|
- }},
|
|
|
- false
|
|
|
- )
|
|
|
-
|
|
|
- while (walker.nextNode()) walker.currentNode.setAttribute('role', 'none')
|
|
|
- })
|
|
|
- },
|
|
|
+ // Accessibility attributes...
|
|
|
'role': 'listbox',
|
|
|
- ':id'() { return this.$id('headlessui-combobox-options') },
|
|
|
- ':aria-labelledby'() { return this.$id('headlessui-combobox-button') },
|
|
|
- ':aria-activedescendant'() { return this.$list.activeEl ? this.$list.activeEl.id : null },
|
|
|
- 'x-show'() { return this.$data.__isOpen },
|
|
|
- })
|
|
|
-}
|
|
|
+ ':aria-labelledby'() { return this.$refs.__label ? this.$refs.__label.id : (this.$refs.__button ? this.$refs.__button.id : null) },
|
|
|
|
|
|
-function handleOption(el, Alpine, directive, evaluate) {
|
|
|
- let value = evaluate(directive.expression)
|
|
|
+ // Initialize...
|
|
|
+ 'x-init'() {
|
|
|
+ this.$data.__isStatic = Alpine.bound(this.$el, 'static', false)
|
|
|
|
|
|
- Alpine.bind(el, {
|
|
|
- 'role': 'option',
|
|
|
- 'x-item'() { return value },
|
|
|
- ':id'() { return this.$id('headlessui-combobox-option') },
|
|
|
- ':tabindex'() { return this.$item.disabled ? undefined : '-1' },
|
|
|
- ':aria-selected'() { return this.$item.selected },
|
|
|
- ':aria-disabled'() { return this.$item.disabled },
|
|
|
- '@click'(e) {
|
|
|
- if (this.$item.disabled) e.preventDefault()
|
|
|
- this.$item.select()
|
|
|
- this.$data.__close()
|
|
|
- this.$nextTick(() => this.$refs.__input.focus({ preventScroll: true }))
|
|
|
- },
|
|
|
- '@focus'() {
|
|
|
- if (this.$item.disabled) return this.$list.deactivate()
|
|
|
- this.$item.activate()
|
|
|
- },
|
|
|
- '@pointermove'() {
|
|
|
- if (this.$item.disabled || this.$item.active) return
|
|
|
- this.$item.activate()
|
|
|
- },
|
|
|
- '@mousemove'() {
|
|
|
- if (this.$item.disabled || this.$item.active) return
|
|
|
- this.$item.activate()
|
|
|
- },
|
|
|
- '@pointerleave'() {
|
|
|
- if (this.$item.disabled || ! this.$item.active || this.$data.__hold) return
|
|
|
- this.$list.deactivate()
|
|
|
- },
|
|
|
- '@mouseleave'() {
|
|
|
- if (this.$item.disabled || ! this.$item.active || this.$data.__hold) return
|
|
|
- this.$list.deactivate()
|
|
|
+ if (Alpine.bound(this.$el, 'hold')) {
|
|
|
+ this.$data.__hold = true;
|
|
|
+ }
|
|
|
},
|
|
|
+
|
|
|
+ 'x-show'() { return this.$data.__isStatic ? true : this.$data.__isOpen },
|
|
|
})
|
|
|
}
|
|
|
|
|
|
-function registerListStuff(Alpine) {
|
|
|
- Alpine.directive('list', (el, { expression, modifiers }, { evaluateLater, effect }) => {
|
|
|
- let wrap = modifiers.includes('wrap')
|
|
|
- let getOuterValue = () => null
|
|
|
- let setOuterValue = () => {}
|
|
|
-
|
|
|
- if (expression) {
|
|
|
- let func = evaluateLater(expression)
|
|
|
- getOuterValue = () => { let result; func(i => result = i); return result; }
|
|
|
- let evaluateOuterSet = evaluateLater(`${expression} = __placeholder`)
|
|
|
- setOuterValue = val => evaluateOuterSet(() => {}, { scope: { '__placeholder': val }})
|
|
|
- }
|
|
|
-
|
|
|
- let listEl = el
|
|
|
-
|
|
|
- el._x_listState = {
|
|
|
- wrap,
|
|
|
- reactive: Alpine.reactive({
|
|
|
- active: null,
|
|
|
- selected: null,
|
|
|
- }),
|
|
|
- get active() { return this.reactive.active },
|
|
|
- get selected() { return this.reactive.selected },
|
|
|
- get activeEl() {
|
|
|
- this.reactive.active
|
|
|
-
|
|
|
- let item = this.items.find(i => i.value === this.reactive.active)
|
|
|
-
|
|
|
- return item && item.el
|
|
|
- },
|
|
|
- get selectedEl() {
|
|
|
- let item = this.items.find(i => i.value === this.reactive.selected)
|
|
|
-
|
|
|
- return item && item.el
|
|
|
- },
|
|
|
- set active(value) { this.setActive(value) },
|
|
|
- set selected(value) { this.setSelected(value) },
|
|
|
- setSelected(value) {
|
|
|
- let item = this.items.find(i => i.value === value)
|
|
|
+function handleOption(el, Alpine) {
|
|
|
+ Alpine.bind(el, {
|
|
|
+ // Setup...
|
|
|
+ 'x-id'() { return ['alpine-combobox-option'] },
|
|
|
+ ':id'() { return this.$id('alpine-combobox-option') },
|
|
|
|
|
|
- if (item && item.disabled) return
|
|
|
+ // Accessibility attributes...
|
|
|
+ 'role': 'option',
|
|
|
+ ':tabindex'() { return this.$comboboxOption.isDisabled ? undefined : '-1' },
|
|
|
|
|
|
- this.reactive.selected = value; setOuterValue(value)
|
|
|
- },
|
|
|
- setActive(value) {
|
|
|
- let item = this.items.find(i => i.value === value)
|
|
|
+ // Only the active element should have aria-selected="true"...
|
|
|
+ 'x-effect'() {
|
|
|
+ this.$comboboxOption.isSelected
|
|
|
+ ? el.setAttribute('aria-selected', true)
|
|
|
+ : el.setAttribute('aria-selected', false)
|
|
|
+ },
|
|
|
|
|
|
- if (item && item.disabled) return
|
|
|
+ ':aria-disabled'() { return this.$comboboxOption.isDisabled },
|
|
|
|
|
|
- this.reactive.active = value
|
|
|
- },
|
|
|
- deactivate() {
|
|
|
- this.reactive.active = null
|
|
|
- },
|
|
|
- selectActive() {
|
|
|
- this.selected = this.active
|
|
|
- },
|
|
|
- activateSelectedOrFirst() {
|
|
|
- if (this.selected) this.active = this.selected
|
|
|
- else this.first()?.activate()
|
|
|
- },
|
|
|
- activateSelectedOrLast() {
|
|
|
- if (this.selected) this.active = this.selected
|
|
|
- else this.last()?.activate()
|
|
|
- },
|
|
|
- items: [],
|
|
|
- get filteredEls() { return this.items.filter(i => ! i.disabled).map(i => i.el) },
|
|
|
- addItem(el, value, disabled = false) {
|
|
|
- this.items.push({ el, value, disabled })
|
|
|
- this.reorderList()
|
|
|
- },
|
|
|
- disableItem(el) {
|
|
|
- this.items.find(i => i.el === el).disabled = true
|
|
|
- },
|
|
|
- removeItem(el) {
|
|
|
- this.items = this.items.filter(i => i.el !== el)
|
|
|
- this.reorderList()
|
|
|
- },
|
|
|
- reorderList() {
|
|
|
- this.items = this.items.slice().sort((a, z) => {
|
|
|
- if (a === null || z === null) return 0
|
|
|
+ // Initialize...
|
|
|
+ 'x-data'() {
|
|
|
+ return {
|
|
|
+ init() {
|
|
|
+ let key = this.$el.__optionKey = (Math.random() + 1).toString(36).substring(7)
|
|
|
|
|
|
- let position = a.el.compareDocumentPosition(z.el)
|
|
|
+ let value = Alpine.extractProp(this.$el, 'value')
|
|
|
+ let disabled = Alpine.extractProp(this.$el, 'disabled', false, false)
|
|
|
|
|
|
- if (position & Node.DOCUMENT_POSITION_FOLLOWING) return -1
|
|
|
- if (position & Node.DOCUMENT_POSITION_PRECEDING) return 1
|
|
|
- return 0
|
|
|
- })
|
|
|
- },
|
|
|
- handleKeyboardNavigation(e) {
|
|
|
- let item
|
|
|
-
|
|
|
- switch (e.key) {
|
|
|
- case 'Tab':
|
|
|
- case 'Backspace':
|
|
|
- case 'Delete':
|
|
|
- case 'Meta':
|
|
|
- break;
|
|
|
-
|
|
|
- break;
|
|
|
- case ['ArrowDown', 'ArrowRight'][0]: // @todo handle orientation switching.
|
|
|
- e.preventDefault(); e.stopPropagation()
|
|
|
- item = this.active ? this.next() : this.first()
|
|
|
- break;
|
|
|
-
|
|
|
- case ['ArrowUp', 'ArrowLeft'][0]:
|
|
|
- e.preventDefault(); e.stopPropagation()
|
|
|
- item = this.active ? this.prev() : this.last()
|
|
|
- break;
|
|
|
- case 'Home':
|
|
|
- case 'PageUp':
|
|
|
- e.preventDefault(); e.stopPropagation()
|
|
|
- item = this.first()
|
|
|
- break;
|
|
|
-
|
|
|
- case 'End':
|
|
|
- case 'PageDown':
|
|
|
- e.preventDefault(); e.stopPropagation()
|
|
|
- item = this.last()
|
|
|
- break;
|
|
|
-
|
|
|
- default:
|
|
|
- if (e.key.length === 1) {
|
|
|
- // item = this.search(e.key)
|
|
|
- }
|
|
|
- break;
|
|
|
+ // memoize the context as it's not going to change
|
|
|
+ // and calling this.$data on mouse action is expensive
|
|
|
+ this.__context.registerItem(key, this.$el, value, disabled)
|
|
|
+ },
|
|
|
+ destroy() {
|
|
|
+ this.__context.unregisterItem(this.$el.__optionKey)
|
|
|
}
|
|
|
+ }
|
|
|
+ },
|
|
|
|
|
|
- item && item.activate(({ el }) => {
|
|
|
- setTimeout(() => el.scrollIntoView({ block: 'nearest' }))
|
|
|
- })
|
|
|
- },
|
|
|
- // Todo: the debounce doesn't work.
|
|
|
- searchQuery: '',
|
|
|
- clearSearch: Alpine.debounce(function () { this.searchQuery = '' }, 350),
|
|
|
- search(key) {
|
|
|
- this.searchQuery += key
|
|
|
-
|
|
|
- let el = this.filteredEls.find(el => {
|
|
|
- return el.textContent.trim().toLowerCase().startsWith(this.searchQuery)
|
|
|
- })
|
|
|
-
|
|
|
- let obj = el ? generateItemObject(listEl, el) : null
|
|
|
-
|
|
|
- this.clearSearch()
|
|
|
-
|
|
|
- return obj
|
|
|
- },
|
|
|
- first() {
|
|
|
- let el = this.filteredEls[0]
|
|
|
-
|
|
|
- return el && generateItemObject(listEl, el)
|
|
|
- },
|
|
|
- last() {
|
|
|
- let el = this.filteredEls[this.filteredEls.length-1]
|
|
|
-
|
|
|
- return el && generateItemObject(listEl, el)
|
|
|
- },
|
|
|
- next() {
|
|
|
- let current = this.activeEl || this.filteredEls[0]
|
|
|
- let index = this.filteredEls.indexOf(current)
|
|
|
-
|
|
|
- let el = this.wrap
|
|
|
- ? this.filteredEls[index + 1] || this.filteredEls[0]
|
|
|
- : this.filteredEls[index + 1] || this.filteredEls[index]
|
|
|
-
|
|
|
- return el && generateItemObject(listEl, el)
|
|
|
- },
|
|
|
- prev() {
|
|
|
- let current = this.activeEl || this.filteredEls[0]
|
|
|
- let index = this.filteredEls.indexOf(current)
|
|
|
-
|
|
|
- let el = this.wrap
|
|
|
- ? (index - 1 < 0 ? this.filteredEls[this.filteredEls.length-1] : this.filteredEls[index - 1])
|
|
|
- : (index - 1 < 0 ? this.filteredEls[0] : this.filteredEls[index - 1])
|
|
|
-
|
|
|
- return el && generateItemObject(listEl, el)
|
|
|
- },
|
|
|
- }
|
|
|
-
|
|
|
- effect(() => {
|
|
|
- el._x_listState.setSelected(getOuterValue())
|
|
|
- })
|
|
|
- })
|
|
|
-
|
|
|
- Alpine.magic('list', (el) => {
|
|
|
- let listEl = Alpine.findClosest(el, el => el._x_listState)
|
|
|
-
|
|
|
- return listEl._x_listState
|
|
|
- })
|
|
|
-
|
|
|
- Alpine.directive('item', (el, { expression }, { effect, evaluate, cleanup }) => {
|
|
|
- let value
|
|
|
- el._x_listItem = true
|
|
|
-
|
|
|
- if (expression) value = evaluate(expression)
|
|
|
-
|
|
|
- let listEl = Alpine.findClosest(el, el => el._x_listState)
|
|
|
-
|
|
|
- console.log(value)
|
|
|
- listEl._x_listState.addItem(el, value)
|
|
|
+ // Register listeners...
|
|
|
+ '@click'() {
|
|
|
+ if (this.$comboboxOption.isDisabled) return;
|
|
|
|
|
|
- queueMicrotask(() => {
|
|
|
- Alpine.bound(el, 'disabled') && listEl._x_listState.disableItem(el)
|
|
|
- })
|
|
|
+ this.__selectOption(this.$el)
|
|
|
|
|
|
- cleanup(() => {
|
|
|
- listEl._x_listState.removeItem(el)
|
|
|
- delete el._x_listItem
|
|
|
- })
|
|
|
- })
|
|
|
+ if (! this.__isMultiple) {
|
|
|
+ this.__close()
|
|
|
+ this.__resetInput()
|
|
|
+ }
|
|
|
|
|
|
- Alpine.magic('item', el => {
|
|
|
- let listEl = Alpine.findClosest(el, el => el._x_listState)
|
|
|
- let itemEl = Alpine.findClosest(el, el => el._x_listItem)
|
|
|
+ this.$nextTick(() => this.$refs.__input.focus({ preventScroll: true }))
|
|
|
+ },
|
|
|
+ '@mouseenter'(e) {
|
|
|
+ this.__context.activateEl(this.$el)
|
|
|
+ },
|
|
|
+ '@mousemove'(e) {
|
|
|
+ if (this.__context.isActiveEl(this.$el)) return
|
|
|
|
|
|
- if (! listEl) throw 'Cant find x-list element'
|
|
|
- if (! itemEl) throw 'Cant find x-item element'
|
|
|
+ this.__context.activateEl(this.$el)
|
|
|
+ },
|
|
|
+ '@mouseleave'(e) {
|
|
|
+ if (this.__hold) return
|
|
|
|
|
|
- return generateItemObject(listEl, itemEl)
|
|
|
+ this.__context.deactivate()
|
|
|
+ },
|
|
|
})
|
|
|
+}
|
|
|
|
|
|
- function generateItemObject(listEl, el) {
|
|
|
- let state = listEl._x_listState
|
|
|
- let item = listEl._x_listState.items.find(i => i.el === el)
|
|
|
-
|
|
|
- return {
|
|
|
- activate(callback = () => {}) {
|
|
|
- state.setActive(item.value)
|
|
|
-
|
|
|
- callback(item)
|
|
|
- },
|
|
|
- deactivate() {
|
|
|
- if (Alpine.raw(state.active) === Alpine.raw(item.value)) state.setActive(null)
|
|
|
- },
|
|
|
- select(callback = () => {}) {
|
|
|
- state.setSelected(item.value)
|
|
|
-
|
|
|
- callback(item)
|
|
|
- },
|
|
|
- isFirst() {
|
|
|
- return state.items.findIndex(i => i.el.isSameNode(el)) === 0
|
|
|
- },
|
|
|
- get active() {
|
|
|
- if (state.reactive.active) return state.reactive.active === item.value
|
|
|
|
|
|
- return null
|
|
|
- },
|
|
|
- get selected() {
|
|
|
- if (state.reactive.selected) return state.reactive.selected === item.value
|
|
|
-
|
|
|
- return null
|
|
|
- },
|
|
|
- get disabled() {
|
|
|
- return item.disabled
|
|
|
- },
|
|
|
- get el() { return item.el },
|
|
|
- get value() { return item.value },
|
|
|
- }
|
|
|
- }
|
|
|
+// Little utility to defer a callback into the microtask queue...
|
|
|
+function microtask(callback) {
|
|
|
+ return new Promise(resolve => queueMicrotask(() => resolve(callback())))
|
|
|
}
|
|
|
-
|
|
|
-/* <div x-data="{
|
|
|
- query: '',
|
|
|
- selected: null,
|
|
|
- people: [
|
|
|
- { id: 1, name: 'Kevin' },
|
|
|
- { id: 2, name: 'Caleb' },
|
|
|
- ],
|
|
|
- get filteredPeople() {
|
|
|
- return this.people.filter(i => {
|
|
|
- return i.name.toLowerCase().includes(this.query.toLowerCase())
|
|
|
- })
|
|
|
- }
|
|
|
-}">
|
|
|
-<p x-text="query"></p>
|
|
|
-<div class="fixed top-16 w-72">
|
|
|
- <div x-combobox x-model="selected">
|
|
|
- <div class="relative mt-1">
|
|
|
- <div class="relative w-full cursor-default overflow-hidden rounded-lg bg-white text-left shadow-md focus:outline-none focus-visible:ring-2 focus-visible:ring-white focus-visible:ring-opacity-75 focus-visible:ring-offset-2 focus-visible:ring-offset-teal-300 sm:text-sm">
|
|
|
- <input x-combobox:input class="w-full border-none py-2 pl-3 pr-10 text-sm leading-5 text-gray-900 focus:ring-0" :display-value="() => (person) => person.name" @change="query = $event.target.value" />
|
|
|
- <button x-combobox:button class="absolute inset-y-0 right-0 flex items-center pr-2">
|
|
|
- <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true" class="h-5 w-5 text-gray-400"><path fill-rule="evenodd" d="M10 3a1 1 0 01.707.293l3 3a1 1 0 01-1.414 1.414L10 5.414 7.707 7.707a1 1 0 01-1.414-1.414l3-3A1 1 0 0110 3zm-3.707 9.293a1 1 0 011.414 0L10 14.586l2.293-2.293a1 1 0 011.414 1.414l-3 3a1 1 0 01-1.414 0l-3-3a1 1 0 010-1.414z" clip-rule="evenodd"></path></svg>
|
|
|
- </button>
|
|
|
- </div>
|
|
|
- <ul x-combobox:options class="absolute mt-1 max-h-60 w-full overflow-auto rounded-md bg-white py-1 text-base shadow-lg ring-1 ring-black ring-opacity-5 focus:outline-none sm:text-sm">
|
|
|
- <div x-show="filteredPeople.length === 0 && query !== ''" class="relative cursor-default select-none py-2 px-4 text-gray-700">
|
|
|
- Nothing found.
|
|
|
- </div>
|
|
|
-
|
|
|
- <template x-for="person in filteredPeople" :key="person.id">
|
|
|
- <li x-combobox:option :value="person" class="relative cursor-default select-none py-2 pl-10 pr-4" :class="{ 'bg-teal-600 text-white': $comboboxOption.active, 'text-gray-900': !$comboboxOption.active, }">
|
|
|
- <span x-text="person.name" class="block truncate" :class="{ 'font-medium': $comboboxOption.selected, 'font-normal': ! $comboboxOption.selected }"></span>
|
|
|
-
|
|
|
- <template x-if="$comboboxOption.selected">
|
|
|
- <span class="absolute inset-y-0 left-0 flex items-center pl-3" :class="{ 'text-white': $comboboxOption.active, 'text-teal-600': !$comboboxOption.active }">
|
|
|
- <CheckIcon class="h-5 w-5" aria-hidden="true" />
|
|
|
- </span>
|
|
|
- </template>
|
|
|
- </li>
|
|
|
- </template>
|
|
|
- </ul>
|
|
|
- </div>
|
|
|
- </div>
|
|
|
- </div>
|
|
|
-</div> */
|