UNPKG

@fruits-chain/react-native-xiaoshu

Version:
80 lines (78 loc) 2.35 kB
"use strict"; import React, { memo, Children, isValidElement } from 'react'; import { View } from 'react-native'; import { varCreator as varCreatorBlank } from "../blank/style.js"; import { getDefaultValue } from "../helpers/index.js"; import Theme from "../theme/index.js"; import { jsx as _jsx } from "react/jsx-runtime"; const NO_GAP = 0; const getMarginGap = (d, gap) => d ? typeof d === 'number' ? d : gap : 0; /** * Space 间距 */ const Space = ({ direction = 'vertical', wrap = false, gap = 's', gapVertical, gapHorizontal, head, tail, justify, align, minWidth, shrink = false, style, children, ...restProps }) => { const TOKENS = Theme.useThemeTokens(); const CV_BLANK = Theme.createVar(TOKENS, varCreatorBlank); const defaultGap = typeof gap === 'string' ? CV_BLANK[`blank_size_${gap}`] : gap; const isVertical = direction === 'vertical'; const _gapVertical = getDefaultValue(gapVertical, defaultGap); const _gapHorizontal = getDefaultValue(gapHorizontal, defaultGap); const wrapperStyle = { flexDirection: isVertical ? 'column' : 'row', flexWrap: wrap ? 'wrap' : 'nowrap', justifyContent: justify, alignItems: align, ...(isVertical ? { paddingTop: getMarginGap(head, _gapVertical), paddingBottom: getMarginGap(tail, _gapVertical) } : { paddingLeft: getMarginGap(head, _gapHorizontal), paddingRight: getMarginGap(tail, _gapHorizontal) }), ...(shrink && direction === 'horizontal' ? { marginBottom: -_gapVertical } : {}) }; const itemStyle = { marginBottom: isVertical || wrap ? _gapVertical : 0, marginRight: isVertical ? 0 : _gapHorizontal, minWidth }; const childArray = Children.toArray(children); const count = childArray.length; return /*#__PURE__*/_jsx(View, { ...restProps, style: [wrapperStyle, style], children: childArray.map((child, index) => { let key = index; if (/*#__PURE__*/isValidElement(child)) { key = child.key ?? index; } return /*#__PURE__*/_jsx(View, { style: [itemStyle, index + 1 === count ? isVertical ? { marginBottom: NO_GAP } : { marginRight: NO_GAP } : null], children: child }, key); }) }); }; export default /*#__PURE__*/memo(Space); //# sourceMappingURL=index.js.map