Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: simplify props definition and optimize types #1160

Merged
merged 2 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions packages/varlet-ui/src/rate/Rate.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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
Expand Down
48 changes: 11 additions & 37 deletions packages/varlet-ui/src/rate/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,59 +10,33 @@ 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,
},
rules: {
type: Array as PropType<Array<(value: any) => any>>,
},
clearable: Boolean,
rules: Array as PropType<Array<(value: any) => any>>,
onChange: defineListenerProp<(score: number) => void>(),
'onUpdate:modelValue': defineListenerProp<(score: number) => void>(),
}
19 changes: 4 additions & 15 deletions packages/varlet-ui/src/result/props.ts
Original file line number Diff line number Diff line change
@@ -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<ResultType>,
default: 'success',
validator: typeValidator,
},
title: {
type: String,
},
description: {
type: String,
},
title: String,
description: String,
animation: {
type: Boolean,
default: true,
Expand Down