44
44
import { computed , PropType , ref , watch , WatchStopHandle } from ' vue'
45
45
import { deepEqual , isNullable } from ' cosmokit'
46
46
import { useI18n } from ' vue-i18n'
47
- import { check , getChoices , getFallback , Schema , useI18nText } from ' ../utils'
47
+ import { check , getChoices , getFallback , Schema , useModel , useI18nText } from ' ../utils'
48
48
import zhCN from ' ../locales/zh-CN.yml'
49
49
import enUS from ' ../locales/en-US.yml'
50
50
@@ -57,21 +57,13 @@ const props = defineProps({
57
57
extra: {} as PropType <any >,
58
58
})
59
59
60
- const emit = defineEmits ([' update:modelValue' ])
60
+ defineEmits ([' update:modelValue' ])
61
61
62
62
const tt = useI18nText ()
63
63
64
- const config = ref ()
65
64
const choices = ref <Schema []>()
66
65
const cache = ref <any []>()
67
66
const active = ref <Schema >()
68
- let stop: WatchStopHandle
69
-
70
- const doWatch = () => watch (config , (value ) => {
71
- const index = choices .value .indexOf (active .value )
72
- if (index >= 0 ) cache .value [index ] = value
73
- emit (' update:modelValue' , deepEqual (value , props .schema .meta .default ) ? null : value )
74
- }, { deep: true })
75
67
76
68
watch (() => props .schema , (value ) => {
77
69
choices .value = getChoices (props .schema )
@@ -81,36 +73,35 @@ watch(() => props.schema, (value) => {
81
73
})
82
74
}, { immediate: true })
83
75
84
- watch (() => [props .modelValue , props .schema ] as const , ([value , schema ]) => {
85
- stop ?.()
86
- config .value = value
87
- value ?? = schema .meta .default
88
- active .value = null
89
- let hasTransform = true , depth = 0
90
- while (! active .value && hasTransform && ++ depth < 10 ) {
91
- hasTransform = false
92
- for (const item of schema .list ) {
93
- if (item .meta .hidden ) continue
94
- if (! check (item , value )) continue
95
- if (item .type === ' transform' ) {
96
- if (! item .callback ) continue
97
- try {
98
- value = item .callback (value )
99
- } catch (error ) {
100
- console .error (error )
101
- continue
76
+ const config = useModel ({
77
+ input(value ) {
78
+ active .value = null
79
+ let hasTransform = true , depth = 0
80
+ while (! active .value && hasTransform && ++ depth < 10 ) {
81
+ hasTransform = false
82
+ for (const [index, item] of props .schema .list .entries ()) {
83
+ if (item .meta .hidden ) continue
84
+ if (! check (item , value )) continue
85
+ if (item .type === ' transform' ) {
86
+ if (! item .callback ) continue
87
+ try {
88
+ value = item .callback (value )
89
+ } catch (error ) {
90
+ console .error (error )
91
+ continue
92
+ }
93
+ hasTransform = true
94
+ value ?? = getFallback (props .schema )
95
+ } else {
96
+ active .value = item
97
+ cache .value [index ] = value
102
98
}
103
- hasTransform = true
104
- config .value = value
105
- value ?? = schema .meta .default
106
- } else {
107
- active .value = item
99
+ break
108
100
}
109
- break
110
101
}
111
- }
112
- stop = doWatch ()
113
- }, { immediate: true , deep: true } )
102
+ return value
103
+ },
104
+ })
114
105
115
106
const selectModel = computed ({
116
107
get() {
0 commit comments