|
| 1 | +import { isArray, isNull, isNumber, isString, isUndefined } from 'lodash'; |
| 2 | +import React from 'react'; |
| 3 | + |
| 4 | +import { getClassName } from '@react-devui/utils'; |
| 5 | + |
| 6 | +export interface AppDetailViewProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'children'> { |
| 7 | + aList: { |
| 8 | + label: string; |
| 9 | + content: React.ReactNode; |
| 10 | + isEmpty?: boolean; |
| 11 | + }[]; |
| 12 | + aCol?: number | true | Record<'xs' | 'sm' | 'md' | 'lg' | 'xl' | 'xxl', number | true>; |
| 13 | + aGutter?: number | [number?, number?]; |
| 14 | + aLabelWidth?: string | number; |
| 15 | + aEmpty?: React.ReactNode; |
| 16 | + aVertical?: boolean; |
| 17 | +} |
| 18 | + |
| 19 | +export function AppDetailView(props: AppDetailViewProps): JSX.Element | null { |
| 20 | + const { |
| 21 | + aList, |
| 22 | + aCol = { xs: 12, md: 6, lg: 4, xxl: 3 }, |
| 23 | + aGutter, |
| 24 | + aLabelWidth, |
| 25 | + aEmpty = '-', |
| 26 | + aVertical = false, |
| 27 | + |
| 28 | + ...restProps |
| 29 | + } = props; |
| 30 | + |
| 31 | + const [gutterX, gutterY] = isArray(aGutter) ? aGutter : [aGutter, aGutter]; |
| 32 | + const col = (() => { |
| 33 | + if (aCol === true) { |
| 34 | + return 'col'; |
| 35 | + } |
| 36 | + if (isNumber(aCol)) { |
| 37 | + return `col-${aCol}`; |
| 38 | + } |
| 39 | + |
| 40 | + const classNames: string[] = []; |
| 41 | + Object.entries(aCol).forEach(([breakpoint, col]) => { |
| 42 | + const className: (string | number)[] = ['col']; |
| 43 | + if (breakpoint !== 'xs') { |
| 44 | + className.push(breakpoint); |
| 45 | + } |
| 46 | + if (col !== true) { |
| 47 | + className.push(col); |
| 48 | + } |
| 49 | + classNames.push(className.join('-')); |
| 50 | + }); |
| 51 | + return classNames.join(' '); |
| 52 | + })(); |
| 53 | + |
| 54 | + const labelWidth = (() => { |
| 55 | + if (aVertical) { |
| 56 | + return undefined; |
| 57 | + } |
| 58 | + |
| 59 | + let maxLength = 0; |
| 60 | + if (aList) { |
| 61 | + aList.forEach((item) => { |
| 62 | + maxLength = Math.max(item.label.length, maxLength); |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + return isUndefined(aLabelWidth) ? maxLength + 1 + 'em' : aLabelWidth; |
| 67 | + })(); |
| 68 | + |
| 69 | + return ( |
| 70 | + <div |
| 71 | + {...restProps} |
| 72 | + className={getClassName(restProps.className, 'app-detail-view', 'row', { |
| 73 | + 'app-detail-view--vertical': aVertical, |
| 74 | + [`gx-${gutterX}`]: gutterX, |
| 75 | + [`gy-${gutterY}`]: gutterY, |
| 76 | + })} |
| 77 | + > |
| 78 | + {aList.map(({ label, content, isEmpty: _isEmpty }) => { |
| 79 | + const isEmpty = isUndefined(_isEmpty) |
| 80 | + ? (isString(content) && content.length === 0) || isUndefined(content) || isNull(content) |
| 81 | + : _isEmpty; |
| 82 | + return ( |
| 83 | + <div key={label} className={getClassName('app-detail-view__item', col)}> |
| 84 | + <div className="app-detail-view__item-label" style={{ width: labelWidth }}> |
| 85 | + {label} |
| 86 | + </div> |
| 87 | + <div className="app-detail-view__item-content">{isEmpty ? aEmpty : content}</div> |
| 88 | + </div> |
| 89 | + ); |
| 90 | + })} |
| 91 | + </div> |
| 92 | + ); |
| 93 | +} |
0 commit comments