forked from IDuxFE/idux
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(comp: space): add component space
fix IDuxFE#63
- Loading branch information
1 parent
ce3d3e5
commit 4a9d878
Showing
25 changed files
with
688 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
packages/components/space/__tests__/__snapshots__/space.spec.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Space.vue render work 1`] = `"<div class=\\"ix-space ix-space-baseline ix-space-horizontal\\"><div class=\\"ix-space-item ix-space-item-small\\"> Space </div><!--v-if--><div class=\\"ix-space-item ix-space-item-small\\"><button class=\\"ix-button ix-button-primary\\"><!--v-if--><span>Button</span></button></div><!--v-if--><div class=\\"ix-space-item ix-space-item-small\\"><button class=\\"ix-button\\"><!--v-if--><span>Button</span></button></div><!--v-if--></div>"`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
import { mount } from '@vue/test-utils' | ||
import { defineComponent, PropType } from 'vue' | ||
import IxSpace from '../src/Space.vue' | ||
import { IxButton } from '../../button' | ||
import { IxDivider } from '../../divider' | ||
import { SpaceAlign, SpaceDirection } from '../src/types' | ||
import { SpaceSize } from '@idux/components' | ||
import { isNil } from '@idux/cdk/utils' | ||
|
||
const TestComponent = defineComponent({ | ||
components: { IxSpace, IxButton, IxDivider }, | ||
template: ` | ||
<IxSpace :align='align' :direction='direction' :size='size' :wrap='wrap' :split='split'> | ||
Space | ||
<IxButton mode='primary'>Button</IxButton> | ||
<IxButton>Button</IxButton> | ||
<template v-slot:split v-if='showSplit'> | ||
<IxDivider :type='dividerType' /> | ||
</template> | ||
</IxSpace> | ||
`, | ||
props: { | ||
align: { type: String as PropType<SpaceAlign>, default: undefined }, | ||
direction: { type: String as PropType<SpaceDirection>, default: undefined }, | ||
size: { type: [String, Number, Array] as PropType<SpaceSize>, default: undefined }, | ||
split: { type: String, default: undefined }, | ||
wrap: { type: Boolean, default: undefined }, | ||
}, | ||
data() { | ||
return { | ||
showSplit: false, | ||
} | ||
}, | ||
computed: { | ||
dividerType(): SpaceDirection { | ||
const hashmap = { | ||
horizontal: 'vertical', | ||
vertical: 'horizontal', | ||
} | ||
return hashmap[this.direction] as SpaceDirection | ||
}, | ||
}, | ||
}) | ||
|
||
describe('Space.vue', () => { | ||
test('render work', () => { | ||
const wrapper = mount(TestComponent) | ||
expect(wrapper.classes()).toContain('ix-space') | ||
expect(wrapper.html()).toMatchSnapshot() | ||
}) | ||
|
||
test('align work', async () => { | ||
const wrapper = mount(TestComponent) | ||
expect(wrapper.classes()).not.toContain('ix-space-start') | ||
expect(wrapper.classes()).not.toContain('ix-space-center') | ||
expect(wrapper.classes()).not.toContain('ix-space-end') | ||
expect(wrapper.classes()).toContain('ix-space-baseline') | ||
|
||
await wrapper.setProps({ align: 'start' }) | ||
expect(wrapper.classes()).toContain('ix-space-start') | ||
expect(wrapper.classes()).not.toContain('ix-space-center') | ||
expect(wrapper.classes()).not.toContain('ix-space-end') | ||
expect(wrapper.classes()).not.toContain('ix-space-baseline') | ||
|
||
await wrapper.setProps({ align: 'center' }) | ||
expect(wrapper.classes()).not.toContain('ix-space-start') | ||
expect(wrapper.classes()).toContain('ix-space-center') | ||
expect(wrapper.classes()).not.toContain('ix-space-end') | ||
expect(wrapper.classes()).not.toContain('ix-space-baseline') | ||
|
||
await wrapper.setProps({ align: 'end' }) | ||
expect(wrapper.classes()).not.toContain('ix-space-start') | ||
expect(wrapper.classes()).not.toContain('ix-space-center') | ||
expect(wrapper.classes()).toContain('ix-space-end') | ||
expect(wrapper.classes()).not.toContain('ix-space-baseline') | ||
|
||
await wrapper.setProps({ align: 'baseline' }) | ||
expect(wrapper.classes()).not.toContain('ix-space-start') | ||
expect(wrapper.classes()).not.toContain('ix-space-center') | ||
expect(wrapper.classes()).not.toContain('ix-space-end') | ||
expect(wrapper.classes()).toContain('ix-space-baseline') | ||
}) | ||
|
||
test('direction work', async () => { | ||
const wrapper = mount(TestComponent) | ||
expect(wrapper.classes()).toContain('ix-space-horizontal') | ||
expect(wrapper.classes()).not.toContain('ix-space-vertical') | ||
|
||
await wrapper.setProps({ direction: 'vertical' }) | ||
expect(wrapper.classes()).not.toContain('ix-space-horizontal') | ||
expect(wrapper.classes()).toContain('ix-space-vertical') | ||
}) | ||
|
||
test('wrap work', async () => { | ||
const wrapper = mount(TestComponent) | ||
expect(wrapper.classes()).not.toContain('ix-space-wrap') | ||
|
||
await wrapper.setProps({ wrap: true }) | ||
expect(wrapper.classes()).toContain('ix-space-wrap') | ||
}) | ||
|
||
test('split work', async () => { | ||
const wrapper = mount(TestComponent) | ||
expect(wrapper.find('.ix-divider').exists()).toBeFalsy() | ||
expect(wrapper.find('.ix-space-split').exists()).toBeFalsy() | ||
|
||
await wrapper.setData({ showSplit: true }) | ||
expect(wrapper.find('.ix-divider').exists()).toBeTruthy() | ||
expect(wrapper.find('.ix-space-split').exists()).toBeFalsy() | ||
|
||
await wrapper.setProps({ split: '/' }) | ||
expect(wrapper.find('.ix-divider').exists()).toBeTruthy() | ||
expect(wrapper.find('.ix-space-split').exists()).toBeFalsy() | ||
|
||
await wrapper.setData({ showSplit: false }) | ||
expect(wrapper.find('.ix-divider').exists()).toBeFalsy() | ||
expect(wrapper.find('.ix-space-split').exists()).toBeTruthy() | ||
}) | ||
|
||
test('size work', async () => { | ||
const wrapper = mount(TestComponent) | ||
expect(wrapper.findAll('.ix-space-item-small').length).toEqual(3) | ||
expect(wrapper.findAll('.ix-space-item-medium').length).toEqual(0) | ||
expect(wrapper.findAll('.ix-space-item-large').length).toEqual(0) | ||
|
||
await wrapper.setProps({ size: 'small' }) | ||
expect(wrapper.findAll('.ix-space-item-small').length).toEqual(3) | ||
expect(wrapper.findAll('.ix-space-item-medium').length).toEqual(0) | ||
expect(wrapper.findAll('.ix-space-item-large').length).toEqual(0) | ||
|
||
await wrapper.setProps({ size: 'medium' }) | ||
expect(wrapper.findAll('.ix-space-item-small').length).toEqual(0) | ||
expect(wrapper.findAll('.ix-space-item-medium').length).toEqual(3) | ||
expect(wrapper.findAll('.ix-space-item-large').length).toEqual(0) | ||
|
||
await wrapper.setProps({ size: 'large' }) | ||
expect(wrapper.findAll('.ix-space-item-small').length).toEqual(0) | ||
expect(wrapper.findAll('.ix-space-item-medium').length).toEqual(0) | ||
expect(wrapper.findAll('.ix-space-item-large').length).toEqual(3) | ||
|
||
await wrapper.setProps({ size: 20 }) | ||
expect(wrapper.findAll('.ix-space-item-small').length).toEqual(0) | ||
expect(wrapper.findAll('.ix-space-item-medium').length).toEqual(0) | ||
expect(wrapper.findAll('.ix-space-item-large').length).toEqual(0) | ||
expect( | ||
wrapper.findAll('.ix-space-item').every(item => item.attributes('style') === 'margin-right: 20px;'), | ||
).toBeTruthy() | ||
|
||
await wrapper.setProps({ split: '/' }) | ||
expect(wrapper.findAll('.ix-space-item-small').length).toEqual(0) | ||
expect(wrapper.findAll('.ix-space-item-medium').length).toEqual(0) | ||
expect(wrapper.findAll('.ix-space-item-large').length).toEqual(0) | ||
expect(wrapper.findAll('.ix-space-item').every(item => isNil(item.attributes('style')))).toBeTruthy() | ||
|
||
await wrapper.setProps({ split: undefined, size: ['small', 'medium'] }) | ||
expect(wrapper.findAll('.ix-space-item-small').length).toEqual(1) | ||
expect(wrapper.findAll('.ix-space-item-medium').length).toEqual(1) | ||
expect(wrapper.findAll('.ix-space-item-large').length).toEqual(0) | ||
|
||
const size = new Map([ | ||
[0, 20], | ||
[1, 10], | ||
]) | ||
await wrapper.setProps({ size: Array.from(size.values()) }) | ||
expect(wrapper.findAll('.ix-space-item-small').length).toEqual(0) | ||
expect(wrapper.findAll('.ix-space-item-medium').length).toEqual(0) | ||
expect(wrapper.findAll('.ix-space-item-large').length).toEqual(0) | ||
expect( | ||
wrapper.findAll('.ix-space-item').reduce((pre, cur, index) => { | ||
return pre && cur.attributes('style') === `margin-right: ${size.get(index)}px;` | ||
}, true), | ||
) | ||
|
||
await wrapper.setProps({ split: '/' }) | ||
expect(wrapper.findAll('.ix-space-item-small').length).toEqual(0) | ||
expect(wrapper.findAll('.ix-space-item-medium').length).toEqual(0) | ||
expect(wrapper.findAll('.ix-space-item-large').length).toEqual(0) | ||
expect(wrapper.findAll('.ix-space-item').every(item => isNil(item.attributes('style')))).toBeTruthy() | ||
|
||
size.set(2, 30) | ||
const warn = jest.spyOn(console, 'warn').mockImplementation(() => {}) | ||
await wrapper.setProps({ split: undefined, size: Array.from(size.values()) }) | ||
expect(warn).toBeCalled() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<template> | ||
<ix-space :size="size"> | ||
Space | ||
<ix-button mode="primary">Button</ix-button> | ||
<ix-button>Button</ix-button> | ||
</ix-space> | ||
<br /> | ||
<input v-model="num" type="number" /> | ||
</template> | ||
|
||
<script lang="ts"> | ||
import { computed, defineComponent, ref } from 'vue' | ||
import { IxButton, IxSpace } from '@idux/components' | ||
export default defineComponent({ | ||
name: 'CustomSize', | ||
components: { IxButton, IxSpace }, | ||
setup() { | ||
const num = ref(20) | ||
const size = computed(() => +num.value) | ||
return { num, size } | ||
}, | ||
}) | ||
</script> |
Oops, something went wrong.