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

View File

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

View File

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

View File

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

View File

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