forked from shesha-io/shesha-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.tsx
More file actions
164 lines (149 loc) · 5.07 KB
/
index.tsx
File metadata and controls
164 lines (149 loc) · 5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { Space, Switch } from 'antd';
import Checkbox from 'antd/lib/checkbox/Checkbox';
import { ValueRenderer } from '@/components/valueRenderer/index';
import React, { FC, useMemo } from 'react';
import { getMoment } from '@/utils/date';
import { ISelectOption } from '@/components/autocomplete';
import QuickView, { GenericQuickView } from '@/components/quickView';
import { IReadOnlyDisplayFormItemProps } from './models';
import { useStyles } from './styles/styles';
import ReflistTag from '../refListDropDown/reflistTag';
type AutocompleteType = ISelectOption;
export const Icon = ({ type, ...rest }) => {
const icons = require(`@ant-design/icons`);
const Component = icons[type];
return <Component {...rest} />;
};
export const ReadOnlyDisplayFormItem: FC<IReadOnlyDisplayFormItemProps> = (props) => {
const {
value,
type = 'string',
dateFormat = 'DD-MM-YYYY',
timeFormat = 'hh:mm',
dropdownDisplayMode = 'raw',
render,
checked,
defaultChecked,
quickviewEnabled,
quickviewFormPath,
quickviewDisplayPropertyName,
quickviewGetEntityUrl,
quickviewWidth,
style,
showIcon,
solidColor,
showItemName
} = props;
const { styles } = useStyles();
const renderValue = useMemo(() => {
if (render) {
return typeof render === 'function' ? render() : render;
}
if ((typeof value === 'undefined' || value === null) && (type === 'dropdown' || type === 'dropdownMultiple')) {
return '';
//eliminating null values
} else if ((typeof value === 'undefined' || value === null) && type === 'string') {
return '';
}
const entityId = typeof value === 'object' ? value?.id ?? value?.data?.id ?? value?.data : value;
const className = value?._className ?? value?.data?._className;
const displayName = value?.label || value?._displayName || value?.data?._displayName;
switch (type) {
case 'dropdown':
if (!Array.isArray(value)) {
if (quickviewEnabled && quickviewFormPath) {
return quickviewFormPath && quickviewGetEntityUrl
? <QuickView
entityId={entityId}
formIdentifier={quickviewFormPath}
getEntityUrl={quickviewGetEntityUrl}
displayProperty={quickviewDisplayPropertyName}
width={quickviewWidth}
/>
: <GenericQuickView
entityId={entityId}
className={className}
displayName={displayName}
displayProperty={quickviewDisplayPropertyName}
width={quickviewWidth}
/>;
} else {
return dropdownDisplayMode === 'tags'
? <ReflistTag
value={value}
color={value?.color}
icon={value?.icon}
showIcon={showIcon}
tagStyle={style}
description={value?.description}
solidColor={solidColor}
showItemName={showItemName}
label={displayName}
/>
: displayName ?? (typeof value === 'object' ? null : value);
}
}
return null;
case 'dropdownMultiple': {
if (Array.isArray(value)) {
const values = (value as AutocompleteType[])?.map(({ label }) => label);
return dropdownDisplayMode === 'raw'
? values?.join(', ')
: <Space size={8}>
{value?.map(({ label, color, icon, value, description }) => {
return <ReflistTag
key={value}
value={value}
color={color}
icon={icon}
description={description}
showIcon={showIcon}
tagStyle={style}
solidColor={solidColor}
showItemName={showItemName}
label={label}
/>;
})}
</Space>;
}
throw new Error(
`Invalid data type passed. Expected IGuidNullableEntityReferenceDto[] but found ${typeof value}`
);
}
case 'time': {
return <ValueRenderer value={value} meta={{ dataType: 'time', dataFormat: timeFormat }} />;
}
case 'datetime': {
return getMoment(value, dateFormat)?.format(dateFormat) || '';
}
case 'checkbox': {
return <Checkbox checked={checked} defaultChecked={defaultChecked} disabled />;
}
case 'switch': {
return <Switch checked={checked} defaultChecked={defaultChecked} disabled />;
}
default:
break;
}
return Boolean(value) && typeof value === 'object' ? JSON.stringify(value, null, 2) : value;
}, [value,
type,
dateFormat,
timeFormat,
dropdownDisplayMode,
render,
checked,
defaultChecked,
quickviewEnabled,
quickviewFormPath,
quickviewDisplayPropertyName,
quickviewGetEntityUrl,
quickviewWidth
]);
return (
<span className={styles.readOnlyDisplayFormItem}>
{renderValue}
</span>
);
};
export default ReadOnlyDisplayFormItem;