iep-ui
Version:
An enterprise-class UI design language and Vue-based implementation
124 lines (112 loc) • 4.25 kB
JavaScript
import _extends from 'babel-runtime/helpers/extends';
import _defineProperty from 'babel-runtime/helpers/defineProperty';
import PropTypes from '../_util/vue-types';
import { filterEmpty, initDefaultProps } from '../_util/props-util';
import { ConfigConsumerProps } from '../config-provider/configConsumerProps';
import { detectFlexGapSupported } from '../_util/styleChecker';
import classNames from 'classnames';
export var SpaceSizeType = PropTypes.oneOfType([PropTypes.number, PropTypes.oneOf(['small', 'middle', 'large']), PropTypes.arrayOf([PropTypes.number, PropTypes.number])]);
var spaceSize = {
small: 8,
middle: 16,
large: 24
};
export var SpaceProps = {
prefixCls: PropTypes.string,
size: SpaceSizeType,
wrap: PropTypes.bool.def(false),
direction: PropTypes.oneOf(['horizontal', 'vertical']),
align: PropTypes.oneOf(['start', 'end', 'center', 'baseline'])
};
function getNumberSize(size) {
return typeof size === 'string' ? spaceSize[size] : size || 0;
}
var Space = {
functional: true,
name: 'ASpace',
props: initDefaultProps(SpaceProps, {
size: 'small',
direction: 'horizontal'
}),
inject: {
configProvider: { 'default': function _default() {
return ConfigConsumerProps;
} }
},
render: function render(h, content) {
var customizePrefixCls = content.prefixCls,
configProvider = content.injections.configProvider,
children = content.children;
var _content$props = content.props,
align = _content$props.align,
size = _content$props.size,
direction = _content$props.direction,
wrap = _content$props.wrap;
var supportFlexGap = detectFlexGapSupported();
var getPrefixCls = configProvider.getPrefixCls;
var prefixCls = getPrefixCls('space', customizePrefixCls);
var items = filterEmpty(children);
var len = items.length;
var horizontalSize = 0,
verticalSize = 0;
var directionConfig = 'ltr';
if (len === 0) {
return null;
}
var watchSize = Array.isArray(size) ? size : [size, size].map(function (item) {
return getNumberSize(item);
});
if (watchSize) {
horizontalSize = watchSize[0];
verticalSize = watchSize[1];
}
var split = content.slots.split || (content.scopedSlots.split ? content.scopedSlots.split() : false);
var itemClassName = prefixCls + '-item';
var horizontalSizeVal = horizontalSize;
var latestIndex = len - 1;
var mergedAlign = align === undefined && direction === 'horizontal' ? 'center' : align;
var cn = function cn() {
var _classNames;
return classNames(prefixCls, prefixCls + '-' + direction, (_classNames = {}, _defineProperty(_classNames, prefixCls + '-rtl', directionConfig === 'rtl'), _defineProperty(_classNames, prefixCls + '-align-' + mergedAlign, mergedAlign), _classNames));
};
var style = function style() {
var gapStyle = {};
if (supportFlexGap) {
gapStyle.columnGap = horizontalSize + 'px';
gapStyle.rowGap = verticalSize + 'px';
}
return _extends({}, gapStyle, wrap && { flexWrap: 'wrap', marginBottom: -verticalSize + 'px' });
};
var marginDirection = directionConfig === 'rtl' ? 'marginLeft' : 'marginRight';
return h(
'div',
{ 'class': cn(), style: style() },
[items.map(function (child, index) {
var itemStyle = {};
if (!supportFlexGap) {
if (direction === 'vertical') {
if (index < latestIndex) {
itemStyle = { marginBottom: horizontalSizeVal / (split ? 2 : 1) + 'px' };
}
} else {
itemStyle = _extends({}, index < latestIndex && _defineProperty({}, marginDirection, horizontalSizeVal / (split ? 2 : 1) + 'px'), wrap && { paddingBottom: verticalSize + 'px' });
}
}
return [h(
'div',
{ 'class': itemClassName, style: itemStyle },
[child]
), index < latestIndex && split && h(
'span',
{ 'class': itemClassName + '-split', style: itemStyle },
[split]
)];
})]
);
}
};
/* istanbul ignore next */
Space.install = function (Vue) {
Vue.component(Space.name, Space);
};
export default Space;