-
Notifications
You must be signed in to change notification settings - Fork 140
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
- Loading branch information
1 parent
bc8ff33
commit 83ada1a
Showing
24 changed files
with
647 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import type { Slots, VNode } from 'vue' | ||
|
||
interface GetSlotNodeList { | ||
(slots: Slots, key?: string, options?: unknown[]): VNode[] | ||
} | ||
|
||
export const getSlotNodeList: GetSlotNodeList = (slots, key = 'default', options = []) => { | ||
if (!slots[key]) return [] | ||
|
||
const currentSlot = slots[key]!(options) | ||
if (currentSlot.length === 1 && currentSlot[0].dynamicChildren) return currentSlot[0].dynamicChildren | ||
return currentSlot | ||
} |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './installComponent' | ||
export * from './isDevMode' | ||
export * from './getSlotNodeList' |
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,24 @@ | ||
<template> | ||
<ix-space :size="size"> | ||
<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> |
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,26 @@ | ||
<template> | ||
<ix-space :size="size"> | ||
<ix-button>Button</ix-button> | ||
<ix-button mode="primary">Button</ix-button> | ||
</ix-space> | ||
<br /> | ||
<select v-model="size"> | ||
<option value="small">small</option> | ||
<option value="medium">medium</option> | ||
<option value="large">large</option> | ||
</select> | ||
</template> | ||
|
||
<script lang="ts"> | ||
import { defineComponent, ref } from 'vue' | ||
import { IxButton, IxSpace } from '@idux/components' | ||
export default defineComponent({ | ||
name: 'Size', | ||
components: { IxButton, IxSpace }, | ||
setup() { | ||
const size = ref('small') | ||
return { size } | ||
}, | ||
}) | ||
</script> |
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,15 @@ | ||
<template> | ||
<ix-space :wrap="true"> | ||
<ix-button v-for="item in 20" :key="item">Button</ix-button> | ||
</ix-space> | ||
</template> | ||
|
||
<script lang="ts"> | ||
import { defineComponent } from 'vue' | ||
import { IxButton, IxSpace } from '@idux/components' | ||
export default defineComponent({ | ||
name: 'Wrap', | ||
components: { IxButton, IxSpace }, | ||
}) | ||
</script> |
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,23 @@ | ||
--- | ||
order: 0 | ||
title: | ||
zh: 基本用法 | ||
en: Basic usage | ||
--- | ||
|
||
## zh | ||
|
||
相邻组件水平间距。 | ||
|
||
## demo | ||
|
||
```html | ||
<template> | ||
<ix-space> | ||
Space | ||
<ix-button mode='primary'>Button</ix-button> | ||
<ix-button>Button</ix-button> | ||
<ix-button mode='danger'>Button</ix-button> | ||
</ix-space> | ||
</template> | ||
``` |
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,10 @@ | ||
--- | ||
order: 4 | ||
title: | ||
zh: 自定义间距 | ||
en: Custom space size | ||
--- | ||
|
||
## zh | ||
|
||
自定义间距大小。 |
Oops, something went wrong.