@@ -69,6 +69,7 @@ import { useMagicKeys } from '@vueuse/core'
6969import { onClickOutside as vOnClickOutside } from ' @/directives/onClickOutside.ts'
7070import { ref , toRefs , computed } from ' vue'
7171import {
72+ useFieldAttributes ,
7273 useFieldEmits ,
7374 useFieldProps ,
7475 useFormModel
@@ -80,6 +81,7 @@ const { controlLeft, metaLeft } = useMagicKeys()
8081
8182const isOpened = ref (false )
8283const { field , model } = toRefs (props)
84+ const { hint } = useFieldAttributes (model .value , field .value )
8385
8486/** Names of the selected values */
8587const selectedNames = computed (() => {
@@ -93,23 +95,19 @@ const selectedNames = computed(() => {
9395
9496/** Whether the field has a value */
9597const hasValue = computed (() => field .value .multiple ? currentModelValue .value .length : currentModelValue .value )
98+ /** Whether the user is pressing a modifier key (CMD or left CTRL) */
9699const isPressingModifierKey = computed (() => metaLeft .value || controlLeft .value )
97100
98101const { currentModelValue } = useFormModel (model .value , field .value )
99102
100- function onClickInput () {
101- if (isOpened .value ) {
102- isOpened .value = false
103- return
104- }
105- isOpened .value = true
106- }
107-
108- function resetSelection () {
109- emits (' onInput' , field .value .multiple ? [] : ' ' )
110- }
103+ /** Toggles the isOpened state when the input is clicked. */
104+ const onClickInput = () => isOpened .value = ! isOpened .value
105+ /** Unselect all items. */
106+ const resetSelection = () => emits (' onInput' , field .value .multiple ? [] : ' ' )
111107
108+ /** Determine whether an option is currently selected by the user */
112109function isSelected (option ) {
110+ if (! field .value .multiple ) return currentModelValue .value === option .value
113111 return currentModelValue .value ? .includes (option .value ) ?? false
114112}
115113
@@ -123,14 +121,14 @@ function handleClickOutside (event) {
123121}
124122
125123function selectOption (option ) {
124+ const optionSelected = isSelected (option)
125+
126126 if (! field .value .multiple ) {
127- const isSelected = currentModelValue .value === option .value
128- emits (' onInput' , isSelected ? ' ' : option .value )
127+ emits (' onInput' , optionSelected ? ' ' : option .value )
129128 } else {
130129 let selectedValues = [ ... currentModelValue .value ]
131- const isSelected = selectedValues .includes (option .value )
132130
133- if (isSelected ) {
131+ if (optionSelected ) {
134132 selectedValues = selectedValues .filter (o => o !== option .value )
135133 } else {
136134 selectedValues .push (option .value )
@@ -141,4 +139,6 @@ function selectOption (option) {
141139 }
142140 isOpened .value = false
143141}
142+
143+ defineExpose ({ hint })
144144< / script>
0 commit comments