|
| 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; |
0 commit comments