Skip to content

Commit

Permalink
feat(space): add space component (#716)
Browse files Browse the repository at this point in the history
  • Loading branch information
WORLDI authored Jan 14, 2021
1 parent 5e2a6a3 commit 8482636
Show file tree
Hide file tree
Showing 10 changed files with 754 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/components/space/Item.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import React, { useContext } from 'react';
import { SpaceContext } from './Space';
import { ItemProps } from './interface';

const Item = ({
className,
index,
direction,
children,
split,
autoWrap,
} : ItemProps) => {
const { horizontalSize, verticalSize, latestIndex } = useContext(SpaceContext);

let style : React.CSSProperties = {};

if(direction === 'vertical') {
if(index < latestIndex) {
style = { marginBottom: horizontalSize / (split ? 2 : 1) };
}
}else {
style = {
...(index < latestIndex && { marginRight: horizontalSize / (split ? 2 : 1) }),
...(autoWrap && { marginBottom: verticalSize }),
};
}

if(children === null || children === undefined) {
return null;
}

return (
<>
<div className={className} style={style}>
{children}
</div>
{
index < latestIndex && split && (
<span className={`${className}-split`} style={style}>
{split}
</span>
)
}
</>
)
}

export default Item;
22 changes: 22 additions & 0 deletions src/components/space/Space.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React from 'react';
import { Meta, Story } from '@storybook/react/types-6-0';
import { PlusCircleFilled } from '@gio-design/icons';
import Space, { SpaceProps } from './index';
import { Button } from '../..';

export default {
title: 'Components/Basic/Space',
component: Space,
} as Meta;

const Template : Story<SpaceProps> = (args) => (
<Space {...args}>
<Button>主要按钮</Button>
<Button type="secondary">次要按钮</Button>
<Button type="secondary" icon={<PlusCircleFilled />}>次要按钮</Button>
</Space>
);

export const Default = Template.bind({});
Default.args = {}

83 changes: 83 additions & 0 deletions src/components/space/Space.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import React,{ useContext, useMemo } from 'react';
import classnames from 'classnames';
import toArray from 'rc-util/lib/Children/toArray';
import { ConfigContext } from '../config-provider'
import Item from './Item';
import { SpaceProps, SpaceSize } from './interface';
import './style'

export const SpaceContext = React.createContext({
horizontalSize: 0,
verticalSize: 0,
latestIndex: 0,
});

const Space : React.FC<SpaceProps> = props => {
const { getPrefixCls } = useContext(ConfigContext);

const {
size = 8,
className,
children,
direction = 'horizontal',
prefixCls: customizePrefixCls,
split,
style,
autoWrap = false,
align = 'center',
} = props;

const [horizontalSize, verticalSize] = useMemo(() => ((Array.isArray(size) ? size : [size, size]) as [SpaceSize, SpaceSize]),[size]);

const childNodes = toArray(children, { keepEmpty: true });

if(childNodes.length === 0) {
return null;
}

const prefixCls = getPrefixCls('space', customizePrefixCls);
const itemCls = `${prefixCls}-item`;
const cls = classnames(
prefixCls,
`${prefixCls}-${direction}`,
`${prefixCls}-align-${align}`,
className,
);

let latestIndex = 0;
const nodes = childNodes.map((child, index) => {
if(child !== null && child !== undefined) {
latestIndex = index;
}

return (
<Item
className={itemCls}
// eslint-disable-next-line react/no-array-index-key
key={index}
index={index}
direction={direction}
split={split}
autoWrap={autoWrap}
>
{child}
</Item>
)
})

return (
<div
className={cls}
style={{
...(autoWrap && { flexWrap: 'wrap', marginBottom: -verticalSize }),
...style
}}
>
<SpaceContext.Provider value={{ horizontalSize, verticalSize, latestIndex }}>
{nodes}
</SpaceContext.Provider>
</div>
)
}

export default Space;
Loading

1 comment on commit 8482636

@vercel
Copy link

@vercel vercel bot commented on 8482636 Jan 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.