Skip to content

Commit

Permalink
fix(collapse): fix modelValue is undefined when close accrodion item
Browse files Browse the repository at this point in the history
  • Loading branch information
BeADre committed Jan 20, 2022
1 parent 6c7df9f commit 0da3871
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 9 deletions.
8 changes: 4 additions & 4 deletions packages/varlet-ui/src/collapse/Collapse.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@ export default defineComponent({
return true
}
const getValue = (value: number | string | undefined, isExpand: boolean): CollapseModelValue => {
if (!checkValue()) return
const getValue = (value: number | string, isExpand: boolean): CollapseModelValue => {
if (!checkValue()) return null
if (isExpand) return props.accordion ? value : [...(props.modelValue as Array<string | number>), value]
return props.accordion
? undefined
? null
: (props.modelValue as Array<string | number>).filter((name: string | number) => name !== value)
}
const updateItem = (value: number | string | undefined, isExpand: boolean) => {
const updateItem = (value: number | string, isExpand: boolean) => {
const modelValue = getValue(value, isExpand)
props['onUpdate:modelValue']?.(modelValue)
Expand Down
39 changes: 38 additions & 1 deletion packages/varlet-ui/src/collapse/__tests__/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import VarCollapse from '../Collapse'
import VarCollapseItem from '../../collapse-item/CollapseItem'
import { mount } from '@vue/test-utils'
import { createApp } from 'vue'
import { delay } from '../../utils/jest'
import { delay, mockConsole } from '../../utils/jest'

test('test collapse example', () => {
const wrapper = mount(example)
Expand Down Expand Up @@ -71,6 +71,43 @@ test('test collapse modelValue and onChange', async () => {
expect(changeHandle).toHaveBeenCalledTimes(2)
})

test('test invalid modelValue', async () => {
const errorFn = jest.fn()
const { mockRestore } = mockConsole('error', errorFn)
const template = `
<var-collapse v-model="value" :accordion="accordion">
<var-collapse-item title="test1" name="1">test1</var-collapse-item>
<var-collapse-item title="test2" name="2">test2</var-collapse-item>
</var-collapse>
`

const wrapper = mount({
components: {
[VarCollapse.name]: VarCollapse,
[VarCollapseItem.name]: VarCollapseItem,
},
data() {
return {
value: '1',
accordion: false,
}
},
template,
})

await delay(0)

await wrapper.setData({
value: ['1'],
accordion: true,
})

await delay(0)

expect(errorFn).toHaveBeenCalledTimes(2)
mockRestore()
})

test('test collapse accordion', async () => {
const template = `
<var-collapse v-model="value" accordion>
Expand Down
4 changes: 2 additions & 2 deletions packages/varlet-ui/src/collapse/props.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { PropType } from 'vue'

export type CollapseModelValue = undefined | string | number | Array<string | number | undefined>
export type CollapseModelValue = null | string | number | Array<string | number>

export const props = {
modelValue: {
type: [Array, String, Number] as PropType<string | number | Array<string | number>>,
type: [Array, String, Number] as PropType<null | string | number | Array<string | number>>,
},
accordion: {
type: Boolean,
Expand Down
2 changes: 1 addition & 1 deletion packages/varlet-ui/src/collapse/provide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { CollapseItemProvider } from '../collapse-item/provide'
export interface CollapseProvider {
active: ComputedRef<number | string | Array<number | string> | undefined | null>
offset: ComputedRef<boolean>
updateItem: (value: number | string | undefined, isExpand: boolean) => void
updateItem: (value: number | string, isExpand: boolean) => void
}

export const COLLAPSE_BIND_COLLAPSE_ITEM_KEY = Symbol('COLLAPSE_BIND_COLLAPSE_ITEM_KEY')
Expand Down
2 changes: 1 addition & 1 deletion packages/varlet-ui/types/collapse.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { VarComponent } from './varComponent'

export type CollapseModelValue = undefined | string | number | Array<string | number | undefined>
export type CollapseModelValue = null | string | number | Array<string | number>

export interface CollapseProps {
modelValue?: CollapseModelValue
Expand Down

0 comments on commit 0da3871

Please sign in to comment.