Skip to content

Commit

Permalink
chore: 使用if(__X__)替代// #if _X_
Browse files Browse the repository at this point in the history
  • Loading branch information
Wangyaqi committed Apr 30, 2024
1 parent c2fd903 commit 1a8f631
Showing 1 changed file with 32 additions and 33 deletions.
65 changes: 32 additions & 33 deletions packages/uni-components/src/helpers/useField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,18 +232,16 @@ function useBase(
return isNaN(maxlength) ? 140 : maxlength
}
})
// #if _X_
// @ts-expect-error
const value =
getValueString(props.modelValue, props.type, maxlength.value) ||
getValueString(props.value, props.type, maxlength.value)
// #endif
// #if !_X_
// @ts-expect-error
const value =
getValueString(props.modelValue, props.type) ||
getValueString(props.value, props.type)
// #endif
let value = ''
if (__X__) {
value =
getValueString(props.modelValue, props.type, maxlength.value) ||
getValueString(props.value, props.type, maxlength.value)
} else {
value =
getValueString(props.modelValue, props.type) ||
getValueString(props.value, props.type)
}
const state: State = reactive({
value,
valueOrigin: value,
Expand Down Expand Up @@ -278,26 +276,27 @@ function useValueSync(
emit: SetupContext['emit'],
trigger: CustomEventTrigger
) {
// #if _X_
//@ts-expect-error
const valueChangeFn = throttle((val: any) => {
state.value = getValueString(val, props.type, state.maxlength)
}, 100)
// #endif
// #if !_X_
//@ts-expect-error
const valueChangeFn = debounce(
(val: any) => {
state.value = getValueString(val, props.type)
},
100,
{ setTimeout, clearTimeout }
)
// #endif
watch(() => props.modelValue, valueChangeFn)
watch(() => props.value, valueChangeFn)
let valueChangeFn:
| ReturnType<typeof throttle>
| ReturnType<typeof debounce>
| null = null
if (__X__) {
valueChangeFn = throttle((val: any) => {
state.value = getValueString(val, props.type, state.maxlength)
}, 100)
} else {
valueChangeFn = debounce(
(val: any) => {
state.value = getValueString(val, props.type)
},
100,
{ setTimeout, clearTimeout }
)
}
watch(() => props.modelValue, valueChangeFn!)
watch(() => props.value, valueChangeFn!)
const triggerInputFn = throttle((event: Event, detail: InputEventDetail) => {
valueChangeFn.cancel()
valueChangeFn!.cancel()
emit('update:modelValue', detail.value)
emit('update:value', detail.value)
trigger('input', event, detail)
Expand All @@ -307,14 +306,14 @@ function useValueSync(
detail: InputEventDetail,
force: boolean
) => {
valueChangeFn.cancel()
valueChangeFn!.cancel()
triggerInputFn(event, detail)
if (force) {
triggerInputFn.flush()
}
}
onBeforeMount(() => {
valueChangeFn.cancel()
valueChangeFn!.cancel()
triggerInputFn.cancel()
})
return {
Expand Down

0 comments on commit 1a8f631

Please sign in to comment.