qx-address.tsx
7.65 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import React, { useEffect, useState, useMemo, useRef } from 'react';
import { Input, Cascader } from 'antd';
import { getDictDetail } from '../service';
import { cloneDeep, isEqual, isEmpty } from 'lodash-es';
export interface FRWidgetProps {
title?: string;
placeholder?: string;
format?: string;
/** Widget schema */
schema: any;
/** 隐藏 */
hidden?: boolean;
/** 只读模式 */
readOnly?: boolean;
/** 禁用模式 */
disabled?: boolean;
/** 标签宽度 */
labelWidth?: string | number;
/*默认值*/
default?: any;
onChange: (data: any) => void;
/* 编辑模式下无此属性*/
addons?: {
setValueByPath: (path: string, value: any) => void;
getSchemaByPath: (path: string) => object;
setSchemaByPath: (path: string, value: any) => void;
setSchema: (settings: any) => void;
setValues: (formData: any) => void;
getValues: () => any;
resetFields: (v: any) => void;
dependValues: any[];
[propName: string]: any;
};
value?: any;
props?: any;
qxProps?: any;
relAppCode?: string;
relFunCode?: string;
relParameter?: any;
needSmallIcon?: boolean;
isQuick?: boolean;
type?: string;
isNavigation?: boolean;
}
interface FinalPropsType extends FRWidgetProps {
casDefaultValue?: any[];
textDefaultValue?: string;
[key: string]: any;
}
/**
* 表单设计器(XRender)
* @constructor
*/
const QxAddress: React.FC<FRWidgetProps> = (props) => {
const [localCasValue, setLocalCasValue] = useState<string[]>([]);
const [localTextValue, setLocalTextValue] = useState<string | undefined>();
const [allOptions, setAllOptions] = useState<any>([]);
const [options, setOptions] = useState<any>([]);
const [detail, setDetail] = useState<boolean>(false);
const [showLevel, setShowLevel] = useState<number>(0);
const isDisabled = !props.addons && !props.schema.$ref && props.readOnly;
const countRef = useRef(1);
// 默认值处理
const handlePropChange = (v: any) => {
props.onChange(v);
if (countRef.current !== 0) {
countRef.current = 0;
}
};
const genOption = (array: any) => {
// eslint-disable-next-line @typescript-eslint/no-shadow
const options: any[] = [];
Object.keys(array).forEach((key: string) => {
const option: any = {
key: array[key].id,
value: array[key].name,
label: array[key].name,
};
options.push(option);
if (array[key].children) {
option.children = genOption(array[key].children);
}
});
return options;
};
const excuteOption = (level: number, opArr: any) => {
// eslint-disable-next-line @typescript-eslint/no-shadow
const options: any = [];
Object.keys(opArr).forEach((key) => {
let num = cloneDeep(level);
const option: any = {
value: opArr[key].value,
label: opArr[key].label,
};
options.push(option);
if (opArr[key].children && num > 1) {
num--;
option.children = excuteOption(num, opArr[key].children);
}
});
return options;
};
const handleSearch = () => {
getDictDetail('F2021110010').then((res: any) => {
if (res.children) {
const option = genOption(res.children);
setAllOptions(option);
}
});
};
// useEffect(() => {
// handleSearch()
// }, []);
const schema = props.schema;
useEffect(() => {
let _defaultVal: any = '';
if (props.addons && props.value) {
_defaultVal = props.value.toString();
}
let _localCasValue = localCasValue;
let _localTextValue = localTextValue;
if (_defaultVal) {
_defaultVal = _defaultVal.split('|');
_localCasValue = _defaultVal[0].split(',');
_localTextValue = _defaultVal[1];
} else {
_localCasValue = [];
_localTextValue = '';
}
if (!isEqual(_localCasValue, localCasValue)) {
setLocalCasValue(_localCasValue);
}
if (!isEqual(_localTextValue, localTextValue)) {
setLocalTextValue(_localTextValue);
}
}, [props.value]);
useEffect(() => {
const _finalProps: FinalPropsType = {
...props,
};
const _mode = schema.qxProps && schema.qxProps.mode;
let level = 3;
if (_mode) {
setDetail(_mode === 'P_C_D_A');
// console.log("=======level==" + schema.addressLevel);
}
switch (_mode) {
case 'P':
level = 1;
break;
case 'P_C':
level = 2;
break;
}
if (level !== showLevel && allOptions.length > 0) {
setOptions(excuteOption(level, allOptions));
setShowLevel(level);
}
if (!_finalProps.placeholder) {
_finalProps.placeholder = schema.placeholder;
}
}, [schema.qxProps, schema.placeholder, allOptions, props.value]);
const needAutoSave = useMemo(() => {
return schema.props?.editable && !schema.readOnly && schema.autoSave;
}, [schema.props?.editable, schema.readOnly, schema.autoSave]);
function filter(inputValue: string, path: any) {
return path.some(
(option: any) => option.label.toLowerCase().indexOf(inputValue.toLowerCase()) > -1,
);
}
const handleChange = (changeObj: any) => {
const newObj = { localCasValue, localTextValue, ...changeObj };
if (!isEqual(localCasValue, newObj.localCasValue)) {
setLocalCasValue(newObj.localCasValue);
}
if (!isEqual(localTextValue, newObj.localTextValue)) {
setLocalTextValue(newObj.localTextValue);
}
const curValString = [newObj.localCasValue?.join(',') ?? '', newObj.localTextValue || '']
.filter((a) => !!a)
.join('|');
// setFinalProps({ ...other, ..._props });
//需要自动保存
if (needAutoSave) {
return;
}
console.log(curValString);
handlePropChange(curValString);
};
return (
<>
<div style={{ width: '100%' }}>
<Cascader
disabled={props.disabled || isDisabled}
value={localCasValue}
placeholder={props.placeholder}
style={{ width: '100%' }}
showSearch={{ filter }}
onFocus={() => {
if (!allOptions.length) {
handleSearch();
}
}} // @ts-ignore
changeOnSelect={props.changeOnSelect}
options={options}
onChange={(value: any) => {
handleChange({
localCasValue: value || [],
...(!value && detail ? { localTextValue: undefined } : null),
});
}}
getPopupContainer={(triggerNode: any) => {
if (schema.belongSubForm || schema.belongEditRelForm) {
return triggerNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode
.parentNode.parentNode;
} else if (schema.queryList || !!schema?.popupOnBody) {
//schema.queryList 适用于查询器
// schema?.popupOnBody 自定义筛选 地址下拉弹框 不跟随
return document.body;
}
return triggerNode;
}}
/>
{detail ? (
<Input
disabled={props.disabled || isEmpty(localCasValue) || isDisabled}
// defaultValue={finalProps.textDefaultValue}
value={localTextValue}
style={{ marginTop: '10px' }}
placeholder={'请输入地址详情'}
showCount
maxLength={200}
onChange={(e: any) => {
const val = e.target.value;
// handleChange({ textDefaultValue: val });
setLocalTextValue(val || undefined);
}}
onBlur={(e: any) => {
const val = e.target.value;
handleChange({ localTextValue: val });
}}
/>
) : null}
</div>
</>
);
};
export default QxAddress;