|
| 1 | +import classNames from '../_util/classNames'; |
| 2 | +import type { DirectionType, SizeType } from '../config-provider'; |
| 3 | +import createContext from '../_util/createContext'; |
| 4 | +import useConfigInject from '../config-provider/hooks/useConfigInject'; |
| 5 | + |
| 6 | +import useStyle from './style'; |
| 7 | +import { computed, defineComponent } from 'vue'; |
| 8 | +import type { PropType, ExtractPropTypes, Ref } from 'vue'; |
| 9 | +import PropTypes from '../_util/vue-types'; |
| 10 | +import { booleanType, tuple } from '../_util/type'; |
| 11 | +import { isEmpty } from 'lodash-es'; |
| 12 | + |
| 13 | +export const spaceCompactItemProps = () => ({ |
| 14 | + compactSize: String as PropType<SizeType>, |
| 15 | + compactDirection: PropTypes.oneOf(tuple('horizontal', 'vertical')).def('horizontal'), |
| 16 | + isFirstItem: booleanType(), |
| 17 | + isLastItem: booleanType(), |
| 18 | +}); |
| 19 | + |
| 20 | +export type SpaceCompactItemContextType = Partial< |
| 21 | + ExtractPropTypes<ReturnType<typeof spaceCompactItemProps>> |
| 22 | +>; |
| 23 | + |
| 24 | +export const SpaceCompactItemContext = createContext<SpaceCompactItemContextType | null>(null); |
| 25 | + |
| 26 | +export const useCompactItemContext = (prefixCls: Ref<string>, direction: Ref<DirectionType>) => { |
| 27 | + const compactItemContext = SpaceCompactItemContext.useInject(); |
| 28 | + |
| 29 | + const compactItemClassnames = computed(() => { |
| 30 | + if (!compactItemContext || isEmpty(compactItemContext)) return ''; |
| 31 | + |
| 32 | + const { compactDirection, isFirstItem, isLastItem } = compactItemContext; |
| 33 | + const separator = compactDirection === 'vertical' ? '-vertical-' : '-'; |
| 34 | + |
| 35 | + return classNames({ |
| 36 | + [`${prefixCls.value}-compact${separator}item`]: true, |
| 37 | + [`${prefixCls.value}-compact${separator}first-item`]: isFirstItem, |
| 38 | + [`${prefixCls.value}-compact${separator}last-item`]: isLastItem, |
| 39 | + [`${prefixCls.value}-compact${separator}item-rtl`]: direction.value === 'rtl', |
| 40 | + }); |
| 41 | + }); |
| 42 | + |
| 43 | + return { |
| 44 | + compactSize: computed(() => compactItemContext?.compactSize), |
| 45 | + compactDirection: computed(() => compactItemContext?.compactDirection), |
| 46 | + compactItemClassnames, |
| 47 | + }; |
| 48 | +}; |
| 49 | + |
| 50 | +export const NoCompactStyle = defineComponent({ |
| 51 | + name: 'NoCompactStyle', |
| 52 | + setup(_, { slots }) { |
| 53 | + SpaceCompactItemContext.useProvide(null); |
| 54 | + return () => { |
| 55 | + return slots.default?.(); |
| 56 | + }; |
| 57 | + }, |
| 58 | +}); |
| 59 | + |
| 60 | +export const spaceCompactProps = () => ({ |
| 61 | + prefixCls: String, |
| 62 | + size: { |
| 63 | + type: [String, Number, Array] as PropType<SizeType>, |
| 64 | + }, |
| 65 | + direction: PropTypes.oneOf(tuple('horizontal', 'vertical')).def('horizontal'), |
| 66 | + align: PropTypes.oneOf(tuple('start', 'end', 'center', 'baseline')), |
| 67 | + block: { type: Boolean, default: undefined }, |
| 68 | +}); |
| 69 | + |
| 70 | +export type SpaceCompactProps = Partial<ExtractPropTypes<ReturnType<typeof spaceCompactProps>>>; |
| 71 | + |
| 72 | +const CompactItem = defineComponent({ |
| 73 | + name: 'CompactItem', |
| 74 | + props: spaceCompactItemProps(), |
| 75 | + setup(props, { slots }) { |
| 76 | + SpaceCompactItemContext.useProvide(props); |
| 77 | + |
| 78 | + return () => slots.default?.(); |
| 79 | + }, |
| 80 | +}); |
| 81 | + |
| 82 | +const Compact = defineComponent({ |
| 83 | + name: 'ASpaceCompact', |
| 84 | + inheritAttrs: false, |
| 85 | + props: spaceCompactProps(), |
| 86 | + setup(props, { attrs, slots }) { |
| 87 | + const { prefixCls, direction: directionConfig } = useConfigInject('space-compact', props); |
| 88 | + const compactItemContext = SpaceCompactItemContext.useInject(); |
| 89 | + |
| 90 | + const [wrapSSR, hashId] = useStyle(prefixCls); |
| 91 | + |
| 92 | + const clx = computed(() => { |
| 93 | + return classNames(prefixCls.value, hashId.value, { |
| 94 | + [`${prefixCls.value}-rtl`]: directionConfig.value === 'rtl', |
| 95 | + [`${prefixCls.value}-block`]: props.block, |
| 96 | + [`${prefixCls.value}-vertical`]: props.direction === 'vertical', |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + return () => { |
| 101 | + // =========================== Render =========================== |
| 102 | + if (slots.default?.()?.length === 0) { |
| 103 | + return null; |
| 104 | + } |
| 105 | + |
| 106 | + const childNodes = slots.default?.() || []; |
| 107 | + |
| 108 | + return wrapSSR( |
| 109 | + <div class={clx.value} {...attrs}> |
| 110 | + {childNodes.map((child, i) => { |
| 111 | + const key = (child && child.key) || `${prefixCls.value}-item-${i}`; |
| 112 | + const noCompactItemContext = !compactItemContext || isEmpty(compactItemContext); |
| 113 | + |
| 114 | + return ( |
| 115 | + <CompactItem |
| 116 | + key={key} |
| 117 | + compactSize={props.size} |
| 118 | + compactDirection={props.direction} |
| 119 | + isFirstItem={i === 0 && (noCompactItemContext || compactItemContext?.isFirstItem)} |
| 120 | + isLastItem={ |
| 121 | + i === childNodes.length - 1 && |
| 122 | + (noCompactItemContext || compactItemContext?.isLastItem) |
| 123 | + } |
| 124 | + > |
| 125 | + {child} |
| 126 | + </CompactItem> |
| 127 | + ); |
| 128 | + })} |
| 129 | + </div>, |
| 130 | + ); |
| 131 | + }; |
| 132 | + }, |
| 133 | +}); |
| 134 | + |
| 135 | +export default Compact; |
0 commit comments