From ebdd79299ec20cfe1b87a82fe01bf6a90fd08432 Mon Sep 17 00:00:00 2001 From: binbin Date: Fri, 25 Aug 2023 10:23:20 +0800 Subject: [PATCH 1/2] refactor: simplify rate and result props definition and optimize types --- packages/varlet-ui/src/rate/Rate.vue | 18 ++++++----- packages/varlet-ui/src/rate/props.ts | 44 ++++++-------------------- packages/varlet-ui/src/result/props.ts | 19 +++-------- 3 files changed, 24 insertions(+), 57 deletions(-) diff --git a/packages/varlet-ui/src/rate/Rate.vue b/packages/varlet-ui/src/rate/Rate.vue index 0dfdde2b65f..2abda99012f 100644 --- a/packages/varlet-ui/src/rate/Rate.vue +++ b/packages/varlet-ui/src/rate/Rate.vue @@ -84,7 +84,9 @@ export default defineComponent({ const { modelValue, disabled, disabledColor, color, half, emptyColor, icon, halfIcon, emptyIcon } = props let iconColor = color - if (disabled || form?.disabled.value) iconColor = disabledColor + if (disabled || form?.disabled.value) { + iconColor = disabledColor + } if (index <= toNumber(modelValue)) { return { color: iconColor, name: icon } @@ -99,16 +101,16 @@ export default defineComponent({ const changeValue = (score: number, event: MouseEvent) => { const { half, clearable } = props + const { offsetWidth } = event.target as HTMLDivElement - if (half) { - const { offsetWidth } = event.target as HTMLDivElement - - if (event.offsetX <= Math.floor(offsetWidth / 2)) score -= 0.5 + if (half && event.offsetX <= Math.floor(offsetWidth / 2)) { + score -= 0.5 } - // set score to 0 when last score is equal to current score - // and the value of clearable is true - if (lastScore === score && clearable) score = 0 + // set score to 0 when last score is equal to current score and clearable is true + if (lastScore === score && clearable) { + score = 0 + } // update last score lastScore = score diff --git a/packages/varlet-ui/src/rate/props.ts b/packages/varlet-ui/src/rate/props.ts index 20414a78f80..84255c22376 100644 --- a/packages/varlet-ui/src/rate/props.ts +++ b/packages/varlet-ui/src/rate/props.ts @@ -10,56 +10,32 @@ export const props = { type: [String, Number], default: 5, }, - color: { - type: String, - }, + color: String, icon: { type: String, default: 'star', }, - emptyColor: { - type: String, - }, + emptyColor: String, emptyIcon: { type: String, default: 'star-outline', }, - size: { - type: [String, Number], - }, - gap: { - type: [String, Number], - }, - namespace: { - type: String, - }, - half: { - type: Boolean, - default: false, - }, + size: [String, Number], + gap: [String, Number], + namespace: String, + half: Boolean, halfIcon: { type: String, default: 'star-half-full', }, - disabled: { - type: Boolean, - default: false, - }, - disabledColor: { - type: String, - }, - readonly: { - type: Boolean, - default: false, - }, + disabled: Boolean, + disabledColor: String, + readonly: Boolean, ripple: { type: Boolean, default: true, }, - clearable: { - type: Boolean, - default: false, - }, + clearable: Boolean, rules: { type: Array as PropType any>>, }, diff --git a/packages/varlet-ui/src/result/props.ts b/packages/varlet-ui/src/result/props.ts index 0773d7ef6ff..b23a9368c63 100644 --- a/packages/varlet-ui/src/result/props.ts +++ b/packages/varlet-ui/src/result/props.ts @@ -1,26 +1,15 @@ -import type { PropType } from 'vue' - -function typeValidator(type: string) { - return ['info', 'success', 'warning', 'error', 'question', 'empty'].includes(type) -} +import { type PropType } from 'vue' export type ResultType = 'info' | 'success' | 'warning' | 'error' | 'question' | 'empty' export const props = { - imageSize: { - type: [String, Number], - }, + imageSize: [String, Number], type: { type: String as PropType, default: 'success', - validator: typeValidator, - }, - title: { - type: String, - }, - description: { - type: String, }, + title: String, + description: String, animation: { type: Boolean, default: true, From 9bfe114beca97de49fd16fca2f00c5666a9be859 Mon Sep 17 00:00:00 2001 From: binbin Date: Fri, 25 Aug 2023 10:39:25 +0800 Subject: [PATCH 2/2] refactor: optimize types --- packages/varlet-ui/src/rate/props.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/varlet-ui/src/rate/props.ts b/packages/varlet-ui/src/rate/props.ts index 84255c22376..6145958519b 100644 --- a/packages/varlet-ui/src/rate/props.ts +++ b/packages/varlet-ui/src/rate/props.ts @@ -36,9 +36,7 @@ export const props = { default: true, }, clearable: Boolean, - rules: { - type: Array as PropType any>>, - }, + rules: Array as PropType any>>, onChange: defineListenerProp<(score: number) => void>(), 'onUpdate:modelValue': defineListenerProp<(score: number) => void>(), }