@bchr/bchr-front-lib 0.6.9 → 0.6.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,9 +1,16 @@
|
|
|
1
|
-
import React, {Fragment, useEffect, useRef, useState} from "react";
|
|
1
|
+
import React, {Fragment, useCallback, useEffect, useRef, useState} from "react";
|
|
2
2
|
import {Input, message} from "antd";
|
|
3
|
+
import {LoadingOutlined} from "@ant-design/icons";
|
|
3
4
|
import HttpService from "../../../services/HttpService";
|
|
4
5
|
import {Values} from "../../utils";
|
|
5
6
|
|
|
6
|
-
const
|
|
7
|
+
const DEFAULT_MASK_LENGTH = 8;
|
|
8
|
+
const DEFAULT_VISIBLE_DURATION = 5000;
|
|
9
|
+
const PASSWORD_ICON_STYLE = {
|
|
10
|
+
cursor: "pointer",
|
|
11
|
+
color: "#337DB7",
|
|
12
|
+
fontWeight: "bolder"
|
|
13
|
+
};
|
|
7
14
|
|
|
8
15
|
const normalizeValue = (value) => {
|
|
9
16
|
if (value === null || value === undefined) {
|
|
@@ -26,136 +33,198 @@ const limitText = (text, maxLength) => {
|
|
|
26
33
|
return text;
|
|
27
34
|
};
|
|
28
35
|
|
|
36
|
+
const getErrorMessage = (error) => {
|
|
37
|
+
return (error && error.responseJSON && error.responseJSON.msg) || (error && error.message) || "解密失败";
|
|
38
|
+
};
|
|
39
|
+
|
|
29
40
|
const InputPasswordDecrypt = (props) => {
|
|
30
|
-
|
|
41
|
+
const {
|
|
31
42
|
ui_render_type,
|
|
32
43
|
initValue,
|
|
33
44
|
value,
|
|
34
45
|
onChange,
|
|
35
46
|
requestUrl,
|
|
36
|
-
appconfig,
|
|
37
|
-
appConfig,
|
|
47
|
+
appconfig: serviceAppconfig,
|
|
48
|
+
appConfig: serviceAppConfig,
|
|
38
49
|
maskLength,
|
|
39
50
|
placeholder,
|
|
40
51
|
maxLength,
|
|
52
|
+
onChangeValueType = "event",
|
|
41
53
|
allowClear = true,
|
|
54
|
+
disabled,
|
|
55
|
+
className,
|
|
56
|
+
style,
|
|
42
57
|
onKeyDown,
|
|
43
58
|
onPaste,
|
|
44
59
|
...restInputProps
|
|
45
60
|
} = props;
|
|
46
|
-
const resolvedMaskLength =
|
|
47
|
-
const serviceConfig =
|
|
61
|
+
const resolvedMaskLength = Values.isInteger(maskLength) && maskLength > 0 ? maskLength : DEFAULT_MASK_LENGTH;
|
|
62
|
+
const serviceConfig = serviceAppconfig || serviceAppConfig;
|
|
63
|
+
const requestContextPath = Values.isNotBlankString(requestUrl)
|
|
64
|
+
? requestUrl
|
|
65
|
+
: (Values.isNotBlankString(serviceConfig)
|
|
66
|
+
? serviceConfig
|
|
67
|
+
: (serviceConfig && (serviceConfig.requestContextPath || serviceConfig.requestUrl)) || "");
|
|
48
68
|
const incomingValue = normalizeValue(value !== undefined ? value : initValue);
|
|
49
69
|
const [realValue, setRealValue] = useState(incomingValue);
|
|
50
70
|
const [toggle, setToggle] = useState(false);
|
|
51
|
-
const [toggleVal, setToggleVal] = useState(maskValue(
|
|
71
|
+
const [toggleVal, setToggleVal] = useState(maskValue(incomingValue, resolvedMaskLength));
|
|
72
|
+
const [revealIndex, setRevealIndex] = useState(-1);
|
|
73
|
+
const [compositionDisplay, setCompositionDisplay] = useState(null);
|
|
52
74
|
const [loading, setLoading] = useState(false);
|
|
53
|
-
const
|
|
75
|
+
const hideTimerRef = useRef(null);
|
|
76
|
+
const revealTimerRef = useRef(null);
|
|
54
77
|
const requestSeqRef = useRef(0);
|
|
55
78
|
const realValueRef = useRef(incomingValue);
|
|
56
|
-
const originalRef = useRef(incomingValue);
|
|
57
79
|
const emittedRef = useRef(incomingValue);
|
|
58
80
|
const editingRef = useRef(false);
|
|
59
81
|
const maskLengthRef = useRef(resolvedMaskLength);
|
|
82
|
+
const composingRef = useRef(false);
|
|
83
|
+
const composeBaseDisplayRef = useRef("");
|
|
84
|
+
const inputRef = useRef(null);
|
|
60
85
|
|
|
61
|
-
realValueRef.current = ui_render_type === "modify" ? realValue :
|
|
86
|
+
realValueRef.current = ui_render_type === "modify" ? realValue : incomingValue;
|
|
62
87
|
maskLengthRef.current = resolvedMaskLength;
|
|
63
88
|
|
|
64
|
-
const clearHideTimer = () => {
|
|
65
|
-
if (
|
|
66
|
-
clearTimeout(
|
|
67
|
-
|
|
89
|
+
const clearHideTimer = useCallback(() => {
|
|
90
|
+
if (hideTimerRef.current) {
|
|
91
|
+
clearTimeout(hideTimerRef.current);
|
|
92
|
+
hideTimerRef.current = null;
|
|
93
|
+
}
|
|
94
|
+
}, []);
|
|
95
|
+
|
|
96
|
+
const clearRevealTimer = useCallback(() => {
|
|
97
|
+
if (revealTimerRef.current) {
|
|
98
|
+
clearTimeout(revealTimerRef.current);
|
|
99
|
+
revealTimerRef.current = null;
|
|
100
|
+
}
|
|
101
|
+
}, []);
|
|
102
|
+
|
|
103
|
+
const resetReveal = useCallback(() => {
|
|
104
|
+
clearRevealTimer();
|
|
105
|
+
setRevealIndex(-1);
|
|
106
|
+
}, [clearRevealTimer]);
|
|
107
|
+
|
|
108
|
+
const flashLastChar = (nextValue) => {
|
|
109
|
+
clearRevealTimer();
|
|
110
|
+
if (!nextValue) {
|
|
111
|
+
setRevealIndex(-1);
|
|
112
|
+
return;
|
|
68
113
|
}
|
|
114
|
+
setRevealIndex(nextValue.length - 1);
|
|
115
|
+
revealTimerRef.current = setTimeout(() => {
|
|
116
|
+
revealTimerRef.current = null;
|
|
117
|
+
setRevealIndex(-1);
|
|
118
|
+
}, 500);
|
|
69
119
|
};
|
|
70
120
|
|
|
71
121
|
const hidePlainText = () => {
|
|
72
122
|
clearHideTimer();
|
|
123
|
+
resetReveal();
|
|
73
124
|
setToggleVal(maskValue(realValueRef.current, maskLengthRef.current));
|
|
74
125
|
setToggle(false);
|
|
75
126
|
};
|
|
76
127
|
|
|
77
|
-
const showPlainText = (plainText) => {
|
|
128
|
+
const showPlainText = (plainText, autoHide = true) => {
|
|
78
129
|
clearHideTimer();
|
|
130
|
+
resetReveal();
|
|
79
131
|
setToggleVal(plainText);
|
|
80
132
|
setToggle(true);
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
133
|
+
if (autoHide) {
|
|
134
|
+
hideTimerRef.current = setTimeout(() => {
|
|
135
|
+
hideTimerRef.current = null;
|
|
136
|
+
setToggleVal(maskValue(realValueRef.current, maskLengthRef.current));
|
|
137
|
+
setToggle(false);
|
|
138
|
+
}, DEFAULT_VISIBLE_DURATION);
|
|
139
|
+
}
|
|
86
140
|
};
|
|
87
141
|
|
|
88
|
-
useEffect(() => {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
}, []);
|
|
142
|
+
useEffect(() => () => {
|
|
143
|
+
requestSeqRef.current += 1;
|
|
144
|
+
clearHideTimer();
|
|
145
|
+
clearRevealTimer();
|
|
146
|
+
}, [clearHideTimer, clearRevealTimer]);
|
|
94
147
|
|
|
95
148
|
useEffect(() => {
|
|
96
149
|
if (ui_render_type === "modify") {
|
|
97
150
|
return undefined;
|
|
98
151
|
}
|
|
152
|
+
requestSeqRef.current += 1;
|
|
153
|
+
clearHideTimer();
|
|
154
|
+
resetReveal();
|
|
155
|
+
setCompositionDisplay(null);
|
|
99
156
|
setToggle(false);
|
|
100
|
-
setToggleVal(maskValue(
|
|
157
|
+
setToggleVal(maskValue(incomingValue, resolvedMaskLength));
|
|
101
158
|
setLoading(false);
|
|
102
|
-
return
|
|
103
|
-
|
|
104
|
-
clearHideTimer();
|
|
105
|
-
};
|
|
106
|
-
}, [ui_render_type, initValue, resolvedMaskLength]);
|
|
159
|
+
return clearHideTimer;
|
|
160
|
+
}, [clearHideTimer, resetReveal, ui_render_type, incomingValue, resolvedMaskLength]);
|
|
107
161
|
|
|
108
162
|
useEffect(() => {
|
|
109
163
|
if (ui_render_type !== "modify" || incomingValue === emittedRef.current) {
|
|
110
164
|
return undefined;
|
|
111
165
|
}
|
|
112
166
|
emittedRef.current = incomingValue;
|
|
113
|
-
originalRef.current = incomingValue;
|
|
114
167
|
editingRef.current = false;
|
|
115
168
|
realValueRef.current = incomingValue;
|
|
116
169
|
setRealValue(incomingValue);
|
|
117
170
|
requestSeqRef.current += 1;
|
|
118
171
|
clearHideTimer();
|
|
172
|
+
resetReveal();
|
|
119
173
|
setToggle(false);
|
|
120
174
|
setToggleVal(maskValue(incomingValue, resolvedMaskLength));
|
|
121
175
|
setLoading(false);
|
|
122
|
-
|
|
176
|
+
return undefined;
|
|
177
|
+
}, [clearHideTimer, resetReveal, ui_render_type, incomingValue, resolvedMaskLength]);
|
|
123
178
|
|
|
124
|
-
const
|
|
179
|
+
const emitChange = (nextValue) => {
|
|
180
|
+
if (onChange) {
|
|
181
|
+
onChange(onChangeValueType === "value" ? nextValue : {target: {value: nextValue}});
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
const commitValue = (nextValue, flash) => {
|
|
125
186
|
const normalized = limitText(normalizeValue(nextValue), maxLength);
|
|
126
187
|
emittedRef.current = normalized;
|
|
127
188
|
editingRef.current = true;
|
|
128
189
|
realValueRef.current = normalized;
|
|
190
|
+
setCompositionDisplay(null);
|
|
129
191
|
setRealValue(normalized);
|
|
130
|
-
if (
|
|
131
|
-
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
onChange({target: {value: normalized}});
|
|
192
|
+
if (flash) {
|
|
193
|
+
flashLastChar(normalized);
|
|
194
|
+
} else {
|
|
195
|
+
resetReveal();
|
|
135
196
|
}
|
|
197
|
+
emitChange(normalized);
|
|
136
198
|
};
|
|
137
199
|
|
|
138
200
|
const onToggleFull = async () => {
|
|
139
|
-
if (toggle) {
|
|
140
|
-
hidePlainText();
|
|
141
|
-
return;
|
|
142
|
-
}
|
|
143
201
|
if (loading) {
|
|
144
202
|
return;
|
|
145
203
|
}
|
|
146
204
|
|
|
205
|
+
if (toggle) {
|
|
206
|
+
hidePlainText();
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
resetReveal();
|
|
147
210
|
const secret = realValueRef.current;
|
|
148
|
-
if (ui_render_type === "modify"
|
|
149
|
-
if (secret
|
|
211
|
+
if (ui_render_type === "modify") {
|
|
212
|
+
if (!secret) {
|
|
213
|
+
commitValue("", false);
|
|
214
|
+
if (inputRef.current && inputRef.current.focus) {
|
|
215
|
+
inputRef.current.focus();
|
|
216
|
+
}
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
if (editingRef.current) {
|
|
220
|
+
showPlainText(secret);
|
|
150
221
|
return;
|
|
151
222
|
}
|
|
152
|
-
showPlainText(secret);
|
|
153
|
-
return;
|
|
154
223
|
}
|
|
155
224
|
if (!secret) {
|
|
156
225
|
return;
|
|
157
226
|
}
|
|
158
|
-
if (!
|
|
227
|
+
if (!requestContextPath) {
|
|
159
228
|
message.error("请为组件注入属性【requestUrl】");
|
|
160
229
|
return;
|
|
161
230
|
}
|
|
@@ -168,7 +237,7 @@ const InputPasswordDecrypt = (props) => {
|
|
|
168
237
|
requestSeqRef.current = requestSeq;
|
|
169
238
|
setLoading(true);
|
|
170
239
|
try {
|
|
171
|
-
const result = await new HttpService(
|
|
240
|
+
const result = await new HttpService(requestContextPath, serviceConfig).post("decrypt", {data: secret});
|
|
172
241
|
if (requestSeq !== requestSeqRef.current) {
|
|
173
242
|
return;
|
|
174
243
|
}
|
|
@@ -181,11 +250,10 @@ const InputPasswordDecrypt = (props) => {
|
|
|
181
250
|
if (requestSeq !== requestSeqRef.current) {
|
|
182
251
|
return;
|
|
183
252
|
}
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
}
|
|
253
|
+
resetReveal();
|
|
254
|
+
setToggle(false);
|
|
255
|
+
setToggleVal(maskValue(secret, resolvedMaskLength));
|
|
256
|
+
message.error(getErrorMessage(error));
|
|
189
257
|
} finally {
|
|
190
258
|
if (requestSeq === requestSeqRef.current) {
|
|
191
259
|
setLoading(false);
|
|
@@ -197,27 +265,26 @@ const InputPasswordDecrypt = (props) => {
|
|
|
197
265
|
if (onKeyDown) {
|
|
198
266
|
onKeyDown(event);
|
|
199
267
|
}
|
|
200
|
-
if (toggle || event.defaultPrevented || event.ctrlKey || event.metaKey || event.altKey || event.isComposing) {
|
|
268
|
+
if (disabled || toggle || composingRef.current || event.defaultPrevented || event.ctrlKey || event.metaKey || event.altKey || event.isComposing) {
|
|
201
269
|
return;
|
|
202
270
|
}
|
|
203
271
|
if (event.key === "Backspace" || event.key === "Delete") {
|
|
204
272
|
event.preventDefault();
|
|
205
|
-
commitValue("", false);
|
|
273
|
+
commitValue(editingRef.current ? realValueRef.current.slice(0, -1) : "", false);
|
|
206
274
|
return;
|
|
207
275
|
}
|
|
208
276
|
if (event.key.length !== 1) {
|
|
209
277
|
return;
|
|
210
278
|
}
|
|
211
279
|
event.preventDefault();
|
|
212
|
-
|
|
213
|
-
commitValue(nextValue, false);
|
|
280
|
+
commitValue(editingRef.current ? realValueRef.current + event.key : event.key, true);
|
|
214
281
|
};
|
|
215
282
|
|
|
216
283
|
const onModifyPaste = (event) => {
|
|
217
284
|
if (onPaste) {
|
|
218
285
|
onPaste(event);
|
|
219
286
|
}
|
|
220
|
-
if (toggle || event.defaultPrevented) {
|
|
287
|
+
if (disabled || toggle || composingRef.current || event.defaultPrevented) {
|
|
221
288
|
return;
|
|
222
289
|
}
|
|
223
290
|
const text = event.clipboardData ? event.clipboardData.getData("text") : "";
|
|
@@ -225,44 +292,110 @@ const InputPasswordDecrypt = (props) => {
|
|
|
225
292
|
if (!text) {
|
|
226
293
|
return;
|
|
227
294
|
}
|
|
228
|
-
|
|
229
|
-
commitValue(nextValue, false);
|
|
295
|
+
commitValue(editingRef.current ? realValueRef.current + text : text, false);
|
|
230
296
|
};
|
|
231
297
|
|
|
232
|
-
const
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
298
|
+
const onCompositionStart = (event) => {
|
|
299
|
+
clearRevealTimer();
|
|
300
|
+
const compositionDisplayValue = event && event.target ? event.target.value : (typeof event === "string" ? event : "");
|
|
301
|
+
composingRef.current = true;
|
|
302
|
+
composeBaseDisplayRef.current = compositionDisplayValue;
|
|
303
|
+
setCompositionDisplay(compositionDisplayValue);
|
|
304
|
+
};
|
|
305
|
+
|
|
306
|
+
const onCompositionEnd = (event) => {
|
|
307
|
+
if (!composingRef.current) {
|
|
238
308
|
return;
|
|
239
309
|
}
|
|
240
|
-
|
|
310
|
+
composingRef.current = false;
|
|
311
|
+
if (disabled || toggle) {
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
const domValue = event && event.target ? event.target.value : (typeof event === "string" ? event : "");
|
|
315
|
+
const baseValue = composeBaseDisplayRef.current;
|
|
316
|
+
const composedText = baseValue && domValue.startsWith(baseValue)
|
|
317
|
+
? domValue.slice(baseValue.length)
|
|
318
|
+
: domValue.replace(/^\*+/, "");
|
|
319
|
+
if (!composedText) {
|
|
320
|
+
setCompositionDisplay(null);
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
commitValue(editingRef.current ? realValueRef.current + composedText : composedText, true);
|
|
324
|
+
};
|
|
325
|
+
|
|
326
|
+
const onModifyChange = (valueOrEvent) => {
|
|
327
|
+
const nextValue = valueOrEvent && valueOrEvent.target ? valueOrEvent.target.value : valueOrEvent;
|
|
328
|
+
if (composingRef.current && !toggle) {
|
|
329
|
+
setCompositionDisplay(nextValue);
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
if (disabled || (!toggle && nextValue !== "")) {
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
335
|
+
commitValue(nextValue, false);
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
const onToggleClick = () => {
|
|
339
|
+
Promise.resolve(onToggleFull()).catch((error) => {
|
|
340
|
+
message.error(getErrorMessage(error));
|
|
341
|
+
});
|
|
342
|
+
};
|
|
343
|
+
|
|
344
|
+
const renderToggleIcon = (extraStyle = {}) => {
|
|
345
|
+
if (loading) {
|
|
346
|
+
return <LoadingOutlined spin style={{color: PASSWORD_ICON_STYLE.color}}/>;
|
|
347
|
+
}
|
|
348
|
+
return (
|
|
349
|
+
<span className={`iconfont ${toggle ? "icon-bchr-show" : "icon-bchr-hide"}`}
|
|
350
|
+
style={{...PASSWORD_ICON_STYLE, ...extraStyle}}
|
|
351
|
+
onClick={onToggleClick}/>
|
|
352
|
+
);
|
|
241
353
|
};
|
|
242
354
|
|
|
243
355
|
if (ui_render_type === "modify") {
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
356
|
+
let modifyDisplay;
|
|
357
|
+
if (compositionDisplay !== null) {
|
|
358
|
+
modifyDisplay = compositionDisplay;
|
|
359
|
+
} else if (toggle) {
|
|
360
|
+
modifyDisplay = editingRef.current ? realValue : toggleVal;
|
|
361
|
+
} else if (editingRef.current && realValue) {
|
|
362
|
+
const fullMask = "*".repeat(resolvedMaskLength);
|
|
363
|
+
modifyDisplay = revealIndex >= 0
|
|
364
|
+
? fullMask.slice(0, -1) + realValue.charAt(realValue.length - 1)
|
|
365
|
+
: fullMask;
|
|
366
|
+
} else {
|
|
367
|
+
modifyDisplay = maskValue(realValue, resolvedMaskLength);
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
return (
|
|
371
|
+
<Input
|
|
372
|
+
{...restInputProps}
|
|
373
|
+
ref={inputRef}
|
|
374
|
+
className={className}
|
|
375
|
+
style={style}
|
|
376
|
+
value={modifyDisplay}
|
|
377
|
+
placeholder={placeholder}
|
|
378
|
+
disabled={disabled}
|
|
379
|
+
allowClear={allowClear}
|
|
380
|
+
maxLength={toggle && typeof maxLength === "number" ? maxLength : undefined}
|
|
381
|
+
onChange={onModifyChange}
|
|
382
|
+
onKeyDown={onModifyKeyDown}
|
|
383
|
+
onPaste={onModifyPaste}
|
|
384
|
+
onCompositionStart={onCompositionStart}
|
|
385
|
+
onCompositionEnd={onCompositionEnd}
|
|
386
|
+
suffix={renderToggleIcon()}
|
|
387
|
+
/>
|
|
388
|
+
);
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
return (
|
|
392
|
+
<Fragment>
|
|
393
|
+
<span className={className} style={style}>
|
|
259
394
|
{toggleVal}
|
|
260
|
-
{Values.isNotBlankString(incomingValue) && (
|
|
261
|
-
<span className={`iconfont ${toggle ? "icon-bchr-show" : "icon-bchr-hide"}`} style={{cursor: "pointer", margin: "0 5px", color: "#337DB7", fontWeight: "bolder"}} onClick={onToggleFull}/>
|
|
262
|
-
)}
|
|
395
|
+
{Values.isNotBlankString(incomingValue) && renderToggleIcon({margin: "0 5px"})}
|
|
263
396
|
</span>
|
|
264
|
-
</Fragment
|
|
265
|
-
|
|
397
|
+
</Fragment>
|
|
398
|
+
);
|
|
266
399
|
};
|
|
267
400
|
|
|
268
401
|
export default InputPasswordDecrypt;
|