Skip to content

Commit 6529863

Browse files
authored
feat: add component Space (#2669)
1 parent 011c839 commit 6529863

File tree

9 files changed

+503
-0
lines changed

9 files changed

+503
-0
lines changed

Diff for: components/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ import { default as Result } from './result';
146146

147147
import { default as Descriptions } from './descriptions';
148148
import { default as PageHeader } from './page-header';
149+
import { default as Space } from './space';
149150

150151
const components = [
151152
Base,
@@ -211,6 +212,7 @@ const components = [
211212
Result,
212213
Descriptions,
213214
PageHeader,
215+
Space,
214216
];
215217

216218
const install = function(Vue) {
@@ -301,6 +303,7 @@ export {
301303
Result,
302304
Descriptions,
303305
PageHeader,
306+
Space,
304307
};
305308

306309
export default {

Diff for: components/space/__tests__/__snapshots__/demo.test.js.snap

+291
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Space should render correct with children 1`] = `
4+
<div class="ant-space ant-space-horizontal ant-space-align-center">
5+
<div class="ant-space-item" style="margin-right: 8px;">text1</div>
6+
<div class="ant-space-item" style="margin-right: 8px;"><span>text1</span></div>
7+
<div class="ant-space-item">text3</div>
8+
</div>
9+
`;

Diff for: components/space/__tests__/demo.test.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import demoTest from '../../../tests/shared/demoTest';
2+
3+
demoTest('space');

Diff for: components/space/__tests__/index.test.js

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import { mount } from '@vue/test-utils';
2+
import Space from '..';
3+
import mountTest from '../../../tests/shared/mountTest';
4+
5+
describe('Space', () => {
6+
mountTest(Space);
7+
8+
it('should render width empty children', () => {
9+
const wrapper = mount({
10+
render() {
11+
return <Space />;
12+
},
13+
});
14+
expect(wrapper.html()).toBeUndefined();
15+
});
16+
17+
it('should render width customize size', () => {
18+
const wrapper = mount({
19+
render() {
20+
return (
21+
<Space size={10}>
22+
<span>1</span>
23+
<span>2</span>
24+
</Space>
25+
);
26+
},
27+
});
28+
expect(wrapper.findAll('.ant-space-item').at(0).element.style.marginRight).toBe('10px');
29+
expect(wrapper.findAll('.ant-space-item').at(1).element.style.marginRight).toBeFalsy();
30+
});
31+
32+
it('should render vertical space width customize size', () => {
33+
const wrapper = mount({
34+
render() {
35+
return (
36+
<Space size={10} direction="vertical">
37+
<span>1</span>
38+
<span>2</span>
39+
</Space>
40+
);
41+
},
42+
});
43+
44+
expect(wrapper.findAll('.ant-space-item').at(0).element.style.marginBottom).toBe('10px');
45+
expect(wrapper.findAll('.ant-space-item').at(1).element.style.marginBottom).toBeFalsy();
46+
});
47+
48+
it('should render correct with children', () => {
49+
const wrapper = mount({
50+
render() {
51+
return (
52+
<Space>
53+
text1<span>text1</span>
54+
text3
55+
</Space>
56+
);
57+
},
58+
});
59+
60+
expect(wrapper.html()).toMatchSnapshot();
61+
});
62+
63+
it('should render with invalidElement', () => {
64+
const wrapper = mount({
65+
render() {
66+
return (
67+
<Space>
68+
text1<span>text1</span>
69+
text1
70+
</Space>
71+
);
72+
},
73+
});
74+
75+
expect(wrapper.findAll('.ant-space-item').length).toBe(3);
76+
});
77+
});

Diff for: components/space/index.jsx

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import PropTypes from '../_util/vue-types';
2+
import { filterEmpty, initDefaultProps } from '../_util/props-util';
3+
import { ConfigConsumerProps } from '../config-provider';
4+
5+
export const SpaceSizeType = PropTypes.oneOfType([
6+
PropTypes.number,
7+
PropTypes.oneOf(['small', 'middle', 'large']),
8+
]);
9+
10+
const spaceSize = {
11+
small: 8,
12+
middle: 16,
13+
large: 24,
14+
};
15+
16+
export const SpaceProps = {
17+
prefixCls: PropTypes.string,
18+
size: SpaceSizeType,
19+
direction: PropTypes.oneOf(['horizontal', 'vertical']),
20+
align: PropTypes.oneOf(['start', 'end', 'center', 'baseline']),
21+
};
22+
23+
const Space = {
24+
functional: true,
25+
name: 'ASpace',
26+
props: initDefaultProps(SpaceProps, {
27+
size: 'small',
28+
direction: 'horizontal',
29+
}),
30+
inject: {
31+
configProvider: { default: () => ConfigConsumerProps },
32+
},
33+
render(h, content) {
34+
const {
35+
prefixCls: customizePrefixCls,
36+
injections: { configProvider },
37+
children,
38+
} = content;
39+
const { align, size, direction } = content.props;
40+
41+
const getPrefixCls = configProvider.getPrefixCls;
42+
const prefixCls = getPrefixCls('space', customizePrefixCls);
43+
const items = filterEmpty(children);
44+
const len = items.length;
45+
46+
if (len === 0) {
47+
return null;
48+
}
49+
50+
const mergedAlign = align === undefined && direction === 'horizontal' ? 'center' : align;
51+
52+
const someSpaceClass = {
53+
[prefixCls]: true,
54+
[`${prefixCls}-${direction}`]: true,
55+
// [`${prefixCls}-rtl`]: directionConfig === 'rtl',
56+
[`${prefixCls}-align-${mergedAlign}`]: mergedAlign,
57+
};
58+
59+
const itemClassName = `${prefixCls}-item`;
60+
const marginDirection = 'marginRight'; // directionConfig === 'rtl' ? 'marginLeft' : 'marginRight';
61+
62+
return (
63+
<div class={someSpaceClass}>
64+
{items.map((child, i) => (
65+
<div
66+
class={itemClassName}
67+
key={`${itemClassName}-${i}`}
68+
style={
69+
i === len - 1
70+
? {}
71+
: {
72+
[direction === 'vertical' ? 'marginBottom' : marginDirection]:
73+
typeof size === 'string' ? `${spaceSize[size]}px` : `${size}px`,
74+
}
75+
}
76+
>
77+
{child}
78+
</div>
79+
))}
80+
</div>
81+
);
82+
},
83+
};
84+
85+
/* istanbul ignore next */
86+
Space.install = function(Vue) {
87+
Vue.component(Space.name, Space);
88+
};
89+
export default Space;

Diff for: components/space/style/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import '../../style/index.less';
2+
import './index.less';

Diff for: components/space/style/index.less

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
@import '../../style/themes/index';
2+
@import '../../style/mixins/index';
3+
4+
@space-prefix-cls: ~'@{ant-prefix}-space';
5+
6+
.@{space-prefix-cls} {
7+
display: inline-flex;
8+
&-vertical {
9+
flex-direction: column;
10+
}
11+
12+
&-align {
13+
&-center {
14+
align-items: center;
15+
}
16+
&-start {
17+
align-items: flex-start;
18+
}
19+
&-end {
20+
align-items: flex-end;
21+
}
22+
&-baseline {
23+
align-items: baseline;
24+
}
25+
}
26+
}
27+
28+
// @import './rtl';

Diff for: components/style.js

+1
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,5 @@ import './result/style';
6060
import './descriptions/style';
6161
import './page-header/style';
6262
import './form-model/style';
63+
import './space/style';
6364
// import './color-picker/style';

0 commit comments

Comments
 (0)