fix: 调整重构 setters 组件下几处 watch 初始值引发无法监听问题 (#178)

This commit is contained in:
alwayrun 2024-05-28 18:16:55 +08:00 committed by sudoooooo
parent a64e820e80
commit de2af7931f
8 changed files with 60 additions and 52 deletions

View File

@ -38,11 +38,9 @@ const handleCheckboxChange = (value: boolean) => {
emit(FORM_CHANGE_EVENT_KEY, { key, value }) emit(FORM_CHANGE_EVENT_KEY, { key, value })
} }
const watchOptionOrigin = computed(() => props.moduleConfig.optionOrigin) watch(
const watchExtraOptions = computed(() => props.moduleConfig?.extraOptions?.length || []) () => props.moduleConfig.optionOrigin,
const watchValue = computed(() => props.formConfig.value) (newVal) => {
watch(watchOptionOrigin, (newVal) => {
const key = props.formConfig.key const key = props.formConfig.key
const extraLen = props.moduleConfig?.extraOptions?.length const extraLen = props.moduleConfig?.extraOptions?.length
@ -50,9 +48,12 @@ watch(watchOptionOrigin, (newVal) => {
emit(FORM_CHANGE_EVENT_KEY, { key: 'randomSort', value: false }) emit(FORM_CHANGE_EVENT_KEY, { key: 'randomSort', value: false })
modelValue.value = false modelValue.value = false
} }
}) }
)
watch(watchExtraOptions, (newVal) => { watch(
() => props.moduleConfig?.extraOptions?.length || [],
(newVal) => {
const key = props.formConfig.key const key = props.formConfig.key
const origin = props.moduleConfig?.optionOrigin const origin = props.moduleConfig?.optionOrigin
@ -60,10 +61,11 @@ watch(watchExtraOptions, (newVal) => {
emit(FORM_CHANGE_EVENT_KEY, { key: 'randomSort', value: false }) emit(FORM_CHANGE_EVENT_KEY, { key: 'randomSort', value: false })
modelValue.value = false modelValue.value = false
} }
}) }
)
watch( watch(
watchValue, () => props.formConfig.value,
(newVal: boolean) => { (newVal: boolean) => {
if (newVal !== modelValue.value) { if (newVal !== modelValue.value) {
modelValue.value == !!newVal modelValue.value == !!newVal

View File

@ -62,8 +62,9 @@ const handleSwitchChange = (value: string | null) => {
emit(FORM_CHANGE_EVENT_KEY, { key: formdataBackfillHourKey, value: value ? '24' : null }) emit(FORM_CHANGE_EVENT_KEY, { key: formdataBackfillHourKey, value: value ? '24' : null })
} }
const watchValue = computed(() => props.formConfig.value) watch(
watch(watchValue, (config) => { () => props.formConfig.value,
(config) => {
const formdataBackfill = config[formdataBackfillKey] const formdataBackfill = config[formdataBackfillKey]
const formdataBackfillHour = !!config[formdataBackfillHourKey] const formdataBackfillHour = !!config[formdataBackfillHourKey]
@ -74,7 +75,8 @@ watch(watchValue, (config) => {
if (formdataBackfillHour !== switchModelValue.value) { if (formdataBackfillHour !== switchModelValue.value) {
switchModelValue.value = formdataBackfillHour switchModelValue.value = formdataBackfillHour
} }
}) }
)
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>
.row { .row {

View File

@ -6,7 +6,7 @@
></el-input> ></el-input>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { ref, watch, computed } from 'vue' import { ref, watch } from 'vue'
import { FORM_CHANGE_EVENT_KEY } from '@/materials/setters/constant' import { FORM_CHANGE_EVENT_KEY } from '@/materials/setters/constant'
interface Props { interface Props {
@ -21,7 +21,6 @@ const emit = defineEmits<Emit>()
const props = defineProps<Props>() const props = defineProps<Props>()
const modelValue = ref(props.formConfig.value || '') const modelValue = ref(props.formConfig.value || '')
const lazyValue = computed(() => props.formConfig.value)
const handleInputBlur = () => { const handleInputBlur = () => {
const { key, validate, value } = props.formConfig const { key, validate, value } = props.formConfig
@ -40,9 +39,12 @@ const handleInputBlur = () => {
} }
} }
watch(lazyValue, (value) => { watch(
() => props.formConfig.value,
(value) => {
if (value !== modelValue.value) { if (value !== modelValue.value) {
modelValue.value = value modelValue.value = value
} }
}) }
)
</script> </script>

View File

@ -20,7 +20,7 @@
</div> </div>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { computed, ref, watch } from 'vue' import { ref, watch } from 'vue'
import moment from 'moment' import moment from 'moment'
import 'moment/locale/zh-cn' import 'moment/locale/zh-cn'
import zhCn from 'element-plus/es/locale/lang/zh-cn' import zhCn from 'element-plus/es/locale/lang/zh-cn'
@ -49,14 +49,11 @@ const begTimeStr = ref(moment(defaultBeginTime).format(format))
const endTimeStr = ref(moment(defaultEndTime).format(format)) const endTimeStr = ref(moment(defaultEndTime).format(format))
const handleDatePickerChange = (key: string, value: string) => { const handleDatePickerChange = (key: string, value: string) => {
console.log(key, value)
emit(FORM_CHANGE_EVENT_KEY, { key, value }) emit(FORM_CHANGE_EVENT_KEY, { key, value })
} }
const watchValue = computed(() => props.formConfig.value)
watch( watch(
watchValue, () => props.formConfig.value,
([begTime, endTime]: any) => { ([begTime, endTime]: any) => {
if (!!begTime && begTime !== begTimeStr.value) { if (!!begTime && begTime !== begTimeStr.value) {
begTimeStr.value = begTime begTimeStr.value = begTime

View File

@ -17,7 +17,7 @@
</div> </div>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { ref, computed, watch } from 'vue' import { ref, watch } from 'vue'
import moment from 'moment' import moment from 'moment'
import 'moment/locale/zh-cn' import 'moment/locale/zh-cn'
import zhCn from 'element-plus/es/locale/lang/zh-cn' import zhCn from 'element-plus/es/locale/lang/zh-cn'
@ -52,9 +52,8 @@ const handleTimePickerChange = (values: Array<string>) => {
times.forEach((value, idx) => emit(FORM_CHANGE_EVENT_KEY, { key: keys[idx], value })) times.forEach((value, idx) => emit(FORM_CHANGE_EVENT_KEY, { key: keys[idx], value }))
} }
const watchValue = computed(() => props.formConfig.value)
watch( watch(
watchValue, () => props.formConfig.value,
([startTime = '00:00:00', endTime = '23:59:59']: Array<string>) => { ([startTime = '00:00:00', endTime = '23:59:59']: Array<string>) => {
if (startTime !== timeValue.value[0] || endTime !== timeValue.value[1]) { if (startTime !== timeValue.value[0] || endTime !== timeValue.value[1]) {
const times = [startTime, endTime] const times = [startTime, endTime]

View File

@ -42,7 +42,10 @@ const handleRadioGroupChange = (value: string) => {
} }
watch( watch(
props.formConfig, () => ({
value: props.formConfig?.value,
defaultValue: props.formConfig?.defaultValue
}),
(config) => { (config) => {
if (config.value == null || config.value == undefined) { if (config.value == null || config.value == undefined) {
modelValue.value = config.defaultValue modelValue.value = config.defaultValue

View File

@ -53,7 +53,10 @@ const handleRadioGroupChange = (value: string) => {
} }
watch( watch(
props.formConfig, () => ({
value: props.formConfig?.value,
defaultValue: props.formConfig?.defaultValue
}),
(config) => { (config) => {
if (config.value == null || config.value == undefined) { if (config.value == null || config.value == undefined) {
modelValue.value = config.defaultValue modelValue.value = config.defaultValue

View File

@ -77,10 +77,10 @@ const handleSelectChange = (value: string) => {
} }
watch( watch(
props.formConfig, () => props.formConfig?.value,
(newValue) => { (val) => {
if (modelValue.value != newValue.value) { if (modelValue.value != val) {
modelValue.value = newValue.value modelValue.value = val
} }
}, },
{ deep: true } { deep: true }