4444import { computed , PropType , ref , watch , WatchStopHandle } from ' vue'
4545import { deepEqual , isNullable } from ' cosmokit'
4646import { useI18n } from ' vue-i18n'
47- import { check , getChoices , getFallback , Schema , useI18nText } from ' ../utils'
47+ import { check , getChoices , getFallback , Schema , useModel , useI18nText } from ' ../utils'
4848import zhCN from ' ../locales/zh-CN.yml'
4949import enUS from ' ../locales/en-US.yml'
5050
@@ -57,21 +57,13 @@ const props = defineProps({
5757 extra: {} as PropType <any >,
5858})
5959
60- const emit = defineEmits ([' update:modelValue' ])
60+ defineEmits ([' update:modelValue' ])
6161
6262const tt = useI18nText ()
6363
64- const config = ref ()
6564const choices = ref <Schema []>()
6665const cache = ref <any []>()
6766const 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 })
7567
7668watch (() => props .schema , (value ) => {
7769 choices .value = getChoices (props .schema )
@@ -81,36 +73,35 @@ watch(() => props.schema, (value) => {
8173 })
8274}, { immediate: true })
8375
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
10298 }
103- hasTransform = true
104- config .value = value
105- value ?? = schema .meta .default
106- } else {
107- active .value = item
99+ break
108100 }
109- break
110101 }
111- }
112- stop = doWatch ()
113- }, { immediate: true , deep: true } )
102+ return value
103+ },
104+ })
114105
115106const selectModel = computed ({
116107 get() {
0 commit comments