fix(components): [checkbox] fix true-value and false-value (#16051)

* fix(components): [checkbox] fix true-value and false-value

closed #16049

* fix(components): update test
This commit is contained in:
kooriookami 2024-03-06 16:51:20 +08:00 committed by GitHub
parent 3ff8bbeaf8
commit d2b8633f98
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 11 additions and 11 deletions

View File

@ -247,14 +247,14 @@ describe('Checkbox', () => {
<Checkbox
label="Foobar"
true-value="a"
false-value={3}
false-value={0}
v-model={checked.value}
/>
))
await wrapper.trigger('click')
await nextTick()
expect(checked.value).toBe(3)
expect(checked.value).toBe(0)
await wrapper.trigger('click')
await nextTick()
expect(checked.value).toBe('a')
@ -263,14 +263,14 @@ describe('Checkbox', () => {
test('with label as slot content', async () => {
const checked = ref('a')
const wrapper = mount(() => (
<Checkbox true-value="a" false-value={3} v-model={checked.value}>
<Checkbox true-value="a" false-value={0} v-model={checked.value}>
Foobar
</Checkbox>
))
await wrapper.trigger('click')
await nextTick()
expect(checked.value).toBe(3)
expect(checked.value).toBe(0)
await wrapper.trigger('click')
await nextTick()
expect(checked.value).toBe('a')

View File

@ -8,8 +8,8 @@
:name="name"
:tabindex="tabindex"
:disabled="isDisabled"
:true-value="trueValue || trueLabel"
:false-value="falseValue || falseLabel"
:true-value="trueValue ?? trueLabel"
:false-value="falseValue ?? falseLabel"
@change="handleChange"
@focus="isFocused = true"
@blur="isFocused = false"

View File

@ -16,8 +16,8 @@
:name="name"
:tabindex="tabindex"
:disabled="isDisabled"
:true-value="trueValue || trueLabel"
:false-value="falseValue || falseLabel"
:true-value="trueValue ?? trueLabel"
:false-value="falseValue ?? falseLabel"
@change="handleChange"
@focus="isFocused = true"
@blur="isFocused = false"

View File

@ -30,8 +30,8 @@ export const useCheckboxEvent = (
function getLabeledValue(value: string | number | boolean) {
return [true, props.trueValue, props.trueLabel].includes(value)
? (props.trueValue || props.trueLabel) ?? true
: (props.falseValue || props.falseLabel) ?? false
? props.trueValue ?? props.trueLabel ?? true
: props.falseValue ?? props.falseLabel ?? false
}
function emitChangeEvent(

View File

@ -43,7 +43,7 @@ export const useCheckbox = (
if (isArray(model.value) && !model.value.includes(actualValue.value)) {
model.value.push(actualValue.value)
} else {
model.value = props.trueValue || props.trueLabel || true
model.value = props.trueValue ?? props.trueLabel ?? true
}
}
props.checked && addToStore()