Skip to content

Commit

Permalink
feat(platform): add detail-view component
Browse files Browse the repository at this point in the history
  • Loading branch information
xiejay97 committed Dec 1, 2022
1 parent 736d900 commit f752d3f
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 27 deletions.
93 changes: 93 additions & 0 deletions packages/platform/src/app/components/detail-view/DetailView.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { isArray, isNull, isNumber, isString, isUndefined } from 'lodash';
import React from 'react';

import { getClassName } from '@react-devui/utils';

export interface AppDetailViewProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'children'> {
aList: {
label: string;
content: React.ReactNode;
isEmpty?: boolean;
}[];
aCol?: number | true | Record<'xs' | 'sm' | 'md' | 'lg' | 'xl' | 'xxl', number | true>;
aGutter?: number | [number?, number?];
aLabelWidth?: string | number;
aEmpty?: React.ReactNode;
aVertical?: boolean;
}

export function AppDetailView(props: AppDetailViewProps): JSX.Element | null {
const {
aList,
aCol = { xs: 12, md: 6, lg: 4, xxl: 3 },
aGutter,
aLabelWidth,
aEmpty = '-',
aVertical = false,

...restProps
} = props;

const [gutterX, gutterY] = isArray(aGutter) ? aGutter : [aGutter, aGutter];
const col = (() => {
if (aCol === true) {
return 'col';
}
if (isNumber(aCol)) {
return `col-${aCol}`;
}

const classNames: string[] = [];
Object.entries(aCol).forEach(([breakpoint, col]) => {
const className: (string | number)[] = ['col'];
if (breakpoint !== 'xs') {
className.push(breakpoint);
}
if (col !== true) {
className.push(col);
}
classNames.push(className.join('-'));
});
return classNames.join(' ');
})();

const labelWidth = (() => {
if (aVertical) {
return undefined;
}

let maxLength = 0;
if (aList) {
aList.forEach((item) => {
maxLength = Math.max(item.label.length, maxLength);
});
}

return isUndefined(aLabelWidth) ? maxLength + 1 + 'em' : aLabelWidth;
})();

return (
<div
{...restProps}
className={getClassName(restProps.className, 'app-detail-view', 'row', {
'app-detail-view--vertical': aVertical,
[`gx-${gutterX}`]: gutterX,
[`gy-${gutterY}`]: gutterY,
})}
>
{aList.map(({ label, content, isEmpty: _isEmpty }) => {
const isEmpty = isUndefined(_isEmpty)
? (isString(content) && content.length === 0) || isUndefined(content) || isNull(content)
: _isEmpty;
return (
<div key={label} className={getClassName('app-detail-view__item', col)}>
<div className="app-detail-view__item-label" style={{ width: labelWidth }}>
{label}
</div>
<div className="app-detail-view__item-content">{isEmpty ? aEmpty : content}</div>
</div>
);
})}
</div>
);
}
1 change: 1 addition & 0 deletions packages/platform/src/app/components/detail-view/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './DetailView';
3 changes: 3 additions & 0 deletions packages/platform/src/app/components/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
export type { AppChartProps } from './chart';
export { AppChart } from './chart';

export type { AppDetailViewProps } from './detail-view';
export { AppDetailView } from './detail-view';

export { AppFCPLoader } from './fcp-loader';

export { AppLanguage } from './language';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ export default function StandardTable(): JSX.Element | null {
isEmpty: deviceQuery.status.length === 0,
},
]}
aLabelWidth={72}
aSearchValue={deviceQuery.keyword}
aSearchPlaceholder="ID, Name"
onSearchValueChange={(value) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { useAsync, useImmer, useMount } from '@react-devui/hooks';
import { EditOutlined } from '@react-devui/icons';
import { DButton, DCard, DSeparator, DSpinner, DTable, FormControl, FormGroup, useForm, Validators } from '@react-devui/ui';

import { AppRouteHeader } from '../../../../components';
import { AppDetailView, AppRouteHeader } from '../../../../components';
import { useAPI } from '../../../../hooks';
import { AppDeviceModal } from '../DeviceModal';
import styles from './Detail.module.scss';
Expand Down Expand Up @@ -128,24 +128,24 @@ export default function Detail(): JSX.Element | null {
<DCard>
<DCard.Content>
<div className="app-title mb-3">Title 1</div>
<div className="row g-3">
{Array.from({ length: 5 }).map((_, n) => (
<div key={n} className="col-12 col-md-6 col-lg-4">
<span className="app-label">Label {n}</span>
<span>Content {n}</span>
</div>
))}
</div>
<AppDetailView
aGutter={3}
aList={Array.from({ length: 5 }).map((_, n) => ({
label: `Label ${n}`,
content: n === 1 ? null : n === 3 ? 'This is a long long long long long long long long text' : `Content ${n}`,
}))}
aLabelWidth={72}
/>
<DSeparator />
<div className="app-title mb-3">Title 2</div>
<div className="row g-3">
{Array.from({ length: 5 }).map((_, n) => (
<div key={n} className="col-12 col-md-6 col-lg-4">
<span className="app-label">Label {n}</span>
<span>Content {n}</span>
</div>
))}
</div>
<AppDetailView
aGutter={3}
aList={Array.from({ length: 5 }).map((_, n) => ({
label: `Label ${n}`,
content: n === 1 ? null : n === 3 ? 'This is a long long long long long long long long text' : `Content ${n}`,
}))}
aVertical
/>
</DCard.Content>
</DCard>
<DCard className="mt-3">
Expand Down
10 changes: 0 additions & 10 deletions packages/platform/src/styles/_app.scss
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,3 @@ body {
font-size: 1.1em;
font-weight: 500;
}

.app-label {
margin-right: 8px;
color: var(--#{$rd-prefix}text-color-sub);

&::after {
margin-left: 2px;
content: ':';
}
}
28 changes: 28 additions & 0 deletions packages/platform/src/styles/components/detail-view.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
@include b(detail-view) {
@include m(vertical) {
@include e(item) {
flex-direction: column;
}

@include e(item-label) {
&::after {
content: none;
}
}
}

@include e(item) {
display: inline-flex;
gap: 8px;
}

@include e(item-label) {
flex-shrink: 0;
color: var(--#{$rd-prefix}text-color-sub);

&::after {
margin-left: 2px;
content: ':';
}
}
}
1 change: 1 addition & 0 deletions packages/platform/src/styles/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
@import 'root';

@import 'components/chart';
@import 'components/detail-view';
@import 'components/fcp-loader';
@import 'components/language';
@import 'components/list';
Expand Down

0 comments on commit f752d3f

Please sign in to comment.