/
index.tsx
221 lines (212 loc) · 5.52 KB
/
index.tsx
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
import { CloseCircleFilled, DownOutlined } from '@ant-design/icons';
import { useIntl } from '@ant-design/pro-provider';
import { ConfigProvider } from 'antd';
import type { SizeType } from 'antd/lib/config-provider/SizeContext';
import classNames from 'classnames';
import React, { useContext, useImperativeHandle, useRef } from 'react';
import { useStyle } from './style';
export type FieldLabelProps = {
label?: React.ReactNode;
value?: any;
disabled?: boolean;
onClear?: () => void;
size?: SizeType;
ellipsis?: boolean;
placeholder?: React.ReactNode;
className?: string;
formatter?: (value: any) => React.ReactNode;
style?: React.CSSProperties;
bordered?: boolean;
allowClear?: boolean;
downIcon?: React.ReactNode | false;
onClick?: () => void;
valueMaxLength?: number;
/**
* 点击标签的事件,用来唤醒 down menu 状态
*/
onLabelClick?: () => void;
};
const FieldLabelFunction: React.ForwardRefRenderFunction<
any,
FieldLabelProps
> = (props, ref) => {
const {
label,
onClear,
value,
disabled,
onLabelClick,
ellipsis,
placeholder,
className,
formatter,
bordered,
style,
downIcon,
allowClear = true,
valueMaxLength = 41,
} = props;
const { componentSize } = ConfigProvider?.useConfig?.() || {
componentSize: 'middle',
};
const size = componentSize;
const { getPrefixCls } = useContext(ConfigProvider.ConfigContext);
const prefixCls = getPrefixCls('pro-core-field-label');
const { wrapSSR, hashId } = useStyle(prefixCls);
const intl = useIntl();
const clearRef = useRef<HTMLElement>(null);
const labelRef = useRef<HTMLElement>(null);
useImperativeHandle(ref, () => ({
labelRef,
clearRef,
}));
const wrapElements = (
array: (string | JSX.Element)[],
): JSX.Element[] | string => {
if (array.every((item) => typeof item === 'string')) return array.join(',');
return array.map((item, index) => {
const comma = index === array.length - 1 ? '' : ',';
if (typeof item === 'string') {
return (
<span key={index}>
{item}
{comma}
</span>
);
}
return (
<span key={index} style={{ display: 'flex' }}>
{item}
{comma}
</span>
);
});
};
const formatterText = (aValue: any) => {
if (formatter) {
return formatter(aValue);
}
return Array.isArray(aValue) ? wrapElements(aValue) : aValue;
};
const getTextByValue = (
aLabel?: React.ReactNode | React.ReactNode[],
aValue?: string | string[],
): React.ReactNode => {
if (
aValue !== undefined &&
aValue !== null &&
aValue !== '' &&
(!Array.isArray(aValue) || aValue.length)
) {
const prefix = aLabel ? (
<span
onClick={() => {
onLabelClick?.();
}}
className={`${prefixCls}-text`}
>
{aLabel}
{': '}
</span>
) : (
''
);
const str = formatterText(aValue);
if (!ellipsis) {
return (
<span
style={{
display: 'inline-flex',
alignItems: 'center',
}}
>
{prefix}
{formatterText(aValue)}
</span>
);
}
const getText = () => {
const isArrayValue = Array.isArray(aValue) && aValue.length > 1;
const unitText = intl.getMessage('form.lightFilter.itemUnit', '项');
if (
typeof str === 'string' &&
str.length > valueMaxLength &&
isArrayValue
) {
return `...${aValue.length}${unitText}`;
}
return '';
};
const tail = getText();
return (
<span
title={typeof str === 'string' ? str : undefined}
style={{
display: 'inline-flex',
alignItems: 'center',
}}
>
{prefix}
<span style={{ paddingInlineStart: 4, display: 'flex' }}>
{typeof str === 'string'
? str?.toString()?.substr?.(0, valueMaxLength)
: str}
</span>
{tail}
</span>
);
}
return aLabel || placeholder;
};
return wrapSSR(
<span
className={classNames(
prefixCls,
hashId,
`${prefixCls}-${props.size ?? size ?? 'middle'}`,
{
[`${prefixCls}-active`]: !!value || value === 0,
[`${prefixCls}-disabled`]: disabled,
[`${prefixCls}-bordered`]: bordered,
[`${prefixCls}-allow-clear`]: allowClear,
},
className,
)}
style={style}
ref={labelRef}
onClick={() => {
props?.onClick?.();
}}
>
{getTextByValue(label, value)}
{(value || value === 0) && allowClear && (
<CloseCircleFilled
role="button"
title={intl.getMessage('form.lightFilter.clear', '清除')}
className={classNames(
`${prefixCls}-icon`,
hashId,
`${prefixCls}-close`,
)}
onClick={(e) => {
if (!disabled) onClear?.();
e.stopPropagation();
}}
ref={clearRef}
/>
)}
{downIcon !== false
? downIcon ?? (
<DownOutlined
className={classNames(
`${prefixCls}-icon`,
hashId,
`${prefixCls}-arrow`,
)}
/>
)
: null}
</span>,
);
};
export const FieldLabel = React.forwardRef(FieldLabelFunction);