@bchr/bchr-front-lib 0.6.3 → 0.6.5

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.
@@ -7,61 +7,290 @@ Object.defineProperty(exports, "__esModule", {
7
7
  exports["default"] = void 0;
8
8
  var _react = _interopRequireWildcard(require("react"));
9
9
  var _antd = require("antd");
10
+ var _excluded = ["ui_render_type", "initValue", "value", "onChange", "maskLength", "placeholder", "maxLength", "allowClear", "disabled", "onKeyDown", "onPaste"];
10
11
  function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
12
+ function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
11
13
  function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
12
14
  function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
13
15
  function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
14
16
  function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
15
17
  function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
16
18
  function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
19
+ function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
20
+ function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
21
+ var MASK_LENGTH = 8;
22
+ var normalizeValue = function normalizeValue(value) {
23
+ if (value === null || value === undefined) {
24
+ return "";
25
+ }
26
+ return String(value);
27
+ };
28
+ var maskValue = function maskValue(value, maskLength) {
29
+ if (value === null || value === undefined || value === "") {
30
+ return "";
31
+ }
32
+ return "*".repeat(maskLength);
33
+ };
34
+ var limitText = function limitText(text, maxLength) {
35
+ if (typeof maxLength === "number" && maxLength > 0 && text.length > maxLength) {
36
+ return text.slice(0, maxLength);
37
+ }
38
+ return text;
39
+ };
17
40
  var InputPassword = function InputPassword(props) {
18
41
  var ui_render_type = props.ui_render_type,
19
- initValue = props.initValue;
20
- var _useState = (0, _react.useState)(false),
42
+ initValue = props.initValue,
43
+ value = props.value,
44
+ onChange = props.onChange,
45
+ maskLength = props.maskLength,
46
+ placeholder = props.placeholder,
47
+ maxLength = props.maxLength,
48
+ _props$allowClear = props.allowClear,
49
+ allowClear = _props$allowClear === void 0 ? true : _props$allowClear,
50
+ _props$disabled = props.disabled,
51
+ disabled = _props$disabled === void 0 ? false : _props$disabled,
52
+ onKeyDown = props.onKeyDown,
53
+ onPaste = props.onPaste,
54
+ restInputProps = _objectWithoutProperties(props, _excluded);
55
+ var resolvedMaskLength = Number.isInteger(maskLength) && maskLength > 0 ? maskLength : MASK_LENGTH;
56
+ var incomingValue = normalizeValue(value !== undefined ? value : initValue);
57
+ var _useState = (0, _react.useState)(incomingValue),
21
58
  _useState2 = _slicedToArray(_useState, 2),
22
- toggle = _useState2[0],
23
- setToggle = _useState2[1];
24
- var _useState3 = (0, _react.useState)(initValue && initValue.split('').map(function (item) {
25
- return '*';
26
- }).join('')),
59
+ realValue = _useState2[0],
60
+ setRealValue = _useState2[1];
61
+ var _useState3 = (0, _react.useState)(false),
27
62
  _useState4 = _slicedToArray(_useState3, 2),
28
- toggleVal = _useState4[0],
29
- setToggleVal = _useState4[1];
30
- var onToggleFull = function onToggleFull() {
31
- if (toggle) {
32
- setToggleVal(initValue && initValue.split('').map(function (item) {
33
- return '*';
34
- }).join(''));
63
+ toggle = _useState4[0],
64
+ setToggle = _useState4[1];
65
+ var _useState5 = (0, _react.useState)(maskValue(initValue, resolvedMaskLength)),
66
+ _useState6 = _slicedToArray(_useState5, 2),
67
+ toggleVal = _useState6[0],
68
+ setToggleVal = _useState6[1];
69
+ var _useState7 = (0, _react.useState)(-1),
70
+ _useState8 = _slicedToArray(_useState7, 2),
71
+ revealIndex = _useState8[0],
72
+ setRevealIndex = _useState8[1];
73
+ var timerRef = (0, _react.useRef)(null);
74
+ var revealTimerRef = (0, _react.useRef)(null);
75
+ var realValueRef = (0, _react.useRef)(incomingValue);
76
+ var originalRef = (0, _react.useRef)(incomingValue);
77
+ var emittedRef = (0, _react.useRef)(incomingValue);
78
+ var editingRef = (0, _react.useRef)(false);
79
+ var maskLengthRef = (0, _react.useRef)(resolvedMaskLength);
80
+ realValueRef.current = ui_render_type === "modify" ? realValue : incomingValue;
81
+ maskLengthRef.current = resolvedMaskLength;
82
+ var clearHideTimer = function clearHideTimer() {
83
+ if (timerRef.current) {
84
+ clearTimeout(timerRef.current);
85
+ timerRef.current = null;
86
+ }
87
+ };
88
+ var clearRevealTimer = function clearRevealTimer() {
89
+ if (revealTimerRef.current) {
90
+ clearTimeout(revealTimerRef.current);
91
+ revealTimerRef.current = null;
92
+ }
93
+ };
94
+ var resetReveal = function resetReveal() {
95
+ clearRevealTimer();
96
+ setRevealIndex(-1);
97
+ };
98
+ var flashLastChar = function flashLastChar(val) {
99
+ clearRevealTimer();
100
+ if (!val) {
101
+ setRevealIndex(-1);
102
+ return;
103
+ }
104
+ setRevealIndex(val.length - 1);
105
+ revealTimerRef.current = setTimeout(function () {
106
+ revealTimerRef.current = null;
107
+ setRevealIndex(-1);
108
+ }, 500);
109
+ };
110
+ var hidePlainText = function hidePlainText() {
111
+ clearHideTimer();
112
+ resetReveal();
113
+ setToggleVal(maskValue(realValueRef.current, maskLengthRef.current));
114
+ setToggle(false);
115
+ };
116
+ var showPlainText = function showPlainText(plainText) {
117
+ clearHideTimer();
118
+ resetReveal();
119
+ setToggleVal(plainText);
120
+ setToggle(true);
121
+ timerRef.current = setTimeout(function () {
122
+ timerRef.current = null;
123
+ setToggleVal(maskValue(realValueRef.current, maskLengthRef.current));
35
124
  setToggle(false);
125
+ }, 5000);
126
+ };
127
+ (0, _react.useEffect)(function () {
128
+ return function () {
129
+ clearHideTimer();
130
+ clearRevealTimer();
131
+ };
132
+ }, []);
133
+ (0, _react.useEffect)(function () {
134
+ if (ui_render_type === "modify") {
135
+ return undefined;
136
+ }
137
+ clearHideTimer();
138
+ resetReveal();
139
+ setToggle(false);
140
+ setToggleVal(maskValue(incomingValue, resolvedMaskLength));
141
+ return function () {
142
+ return clearHideTimer();
143
+ };
144
+ }, [ui_render_type, incomingValue, resolvedMaskLength]);
145
+ (0, _react.useEffect)(function () {
146
+ if (ui_render_type !== "modify" || incomingValue === emittedRef.current) {
147
+ return undefined;
148
+ }
149
+ emittedRef.current = incomingValue;
150
+ originalRef.current = incomingValue;
151
+ editingRef.current = false;
152
+ realValueRef.current = incomingValue;
153
+ setRealValue(incomingValue);
154
+ clearHideTimer();
155
+ resetReveal();
156
+ setToggle(false);
157
+ setToggleVal(maskValue(incomingValue, resolvedMaskLength));
158
+ }, [ui_render_type, incomingValue, resolvedMaskLength]);
159
+ var commitValue = function commitValue(nextValue, flash) {
160
+ var normalized = limitText(normalizeValue(nextValue), maxLength);
161
+ emittedRef.current = normalized;
162
+ editingRef.current = true;
163
+ realValueRef.current = normalized;
164
+ setRealValue(normalized);
165
+ if (flash) {
166
+ flashLastChar(normalized);
36
167
  } else {
37
- setToggleVal(initValue);
38
- setToggle(true);
39
- setTimeout(function () {
40
- setToggleVal(initValue && initValue.split('').map(function (item) {
41
- return '*';
42
- }).join(''));
43
- setToggle(false);
44
- }, 5000);
168
+ resetReveal();
169
+ }
170
+ if (onChange) {
171
+ onChange({
172
+ target: {
173
+ value: normalized
174
+ }
175
+ });
45
176
  }
46
177
  };
47
- if (ui_render_type === 'modify') {
48
- return /*#__PURE__*/_react["default"].createElement(_antd.Input, {
49
- defaultValue: toggleVal,
178
+ var onToggleFull = function onToggleFull() {
179
+ if (disabled) {
180
+ return;
181
+ }
182
+ if (toggle) {
183
+ hidePlainText();
184
+ return;
185
+ }
186
+ resetReveal();
187
+ var plainText = realValueRef.current;
188
+ if (ui_render_type === "modify" && (plainText === "" || editingRef.current || plainText !== originalRef.current)) {
189
+ if (plainText) {
190
+ showPlainText(plainText);
191
+ }
192
+ return;
193
+ }
194
+ if (plainText) {
195
+ showPlainText(plainText);
196
+ }
197
+ };
198
+ var onModifyKeyDown = function onModifyKeyDown(event) {
199
+ if (onKeyDown) {
200
+ onKeyDown(event);
201
+ }
202
+ if (disabled || toggle || event.defaultPrevented || event.ctrlKey || event.metaKey || event.altKey || event.isComposing) {
203
+ return;
204
+ }
205
+ if (event.key === "Backspace" || event.key === "Delete") {
206
+ event.preventDefault();
207
+ commitValue(editingRef.current ? realValueRef.current.slice(0, -1) : "", false);
208
+ return;
209
+ }
210
+ if (event.key.length !== 1) {
211
+ return;
212
+ }
213
+ event.preventDefault();
214
+ commitValue(editingRef.current ? realValueRef.current + event.key : event.key, true);
215
+ };
216
+ var onModifyPaste = function onModifyPaste(event) {
217
+ if (onPaste) {
218
+ onPaste(event);
219
+ }
220
+ if (disabled || toggle || event.defaultPrevented) {
221
+ return;
222
+ }
223
+ var text = event.clipboardData ? event.clipboardData.getData("text") : "";
224
+ event.preventDefault();
225
+ if (text) {
226
+ commitValue(editingRef.current ? realValueRef.current + text : text, false);
227
+ }
228
+ };
229
+ var onModifyChange = function onModifyChange(event) {
230
+ var nextValue = event.target.value;
231
+ if (!toggle) {
232
+ if (nextValue === "") {
233
+ commitValue("", false);
234
+ }
235
+ return;
236
+ }
237
+ var normalized = limitText(normalizeValue(nextValue), maxLength);
238
+ emittedRef.current = normalized;
239
+ editingRef.current = true;
240
+ realValueRef.current = normalized;
241
+ setRealValue(normalized);
242
+ setToggleVal(normalized);
243
+ if (onChange) {
244
+ onChange({
245
+ target: {
246
+ value: normalized
247
+ }
248
+ });
249
+ }
250
+ };
251
+ if (ui_render_type === "modify") {
252
+ var modifyDisplay;
253
+ if (toggle) {
254
+ modifyDisplay = toggleVal;
255
+ } else if (editingRef.current && realValue) {
256
+ modifyDisplay = realValue.split("").map(function (_char, index) {
257
+ return index === revealIndex ? _char : "*";
258
+ }).join("");
259
+ } else if (realValue) {
260
+ modifyDisplay = maskValue(realValue, resolvedMaskLength);
261
+ } else {
262
+ modifyDisplay = "";
263
+ }
264
+ return /*#__PURE__*/_react["default"].createElement(_antd.Input, _extends({}, restInputProps, {
265
+ value: modifyDisplay,
266
+ placeholder: placeholder,
267
+ disabled: disabled,
268
+ allowClear: allowClear && !disabled,
269
+ maxLength: toggle && typeof maxLength === "number" ? maxLength : undefined,
270
+ onChange: onModifyChange,
271
+ onKeyDown: onModifyKeyDown,
272
+ onPaste: onModifyPaste,
50
273
  addonAfter: /*#__PURE__*/_react["default"].createElement("span", {
51
- className: "iconfont ".concat(toggle ? 'icon-bchr-show' : 'icon-bchr-hide')
274
+ className: "iconfont ".concat(toggle ? "icon-bchr-show" : "icon-bchr-hide"),
275
+ style: {
276
+ cursor: disabled ? "not-allowed" : "pointer"
277
+ },
278
+ onClick: onToggleFull
52
279
  })
53
- });
54
- } else if (ui_render_type === 'browse') {
280
+ }));
281
+ }
282
+ if (ui_render_type === "browse") {
55
283
  return /*#__PURE__*/_react["default"].createElement(_react.Fragment, null, /*#__PURE__*/_react["default"].createElement("span", null, toggleVal, /*#__PURE__*/_react["default"].createElement("span", {
56
- className: "iconfont ".concat(toggle ? 'icon-bchr-show' : 'icon-bchr-hide'),
284
+ className: "iconfont ".concat(toggle ? "icon-bchr-show" : "icon-bchr-hide"),
57
285
  style: {
58
- cursor: 'pointer',
59
- margin: '0 5px',
60
- color: '#337DB7',
61
- fontWeight: 'bolder'
286
+ cursor: disabled ? "not-allowed" : "pointer",
287
+ margin: "0 5px",
288
+ color: "#337DB7",
289
+ fontWeight: "bolder"
62
290
  },
63
291
  onClick: onToggleFull
64
292
  })));
65
293
  }
294
+ return null;
66
295
  };
67
296
  var _default = exports["default"] = InputPassword;
@@ -74,11 +74,16 @@ var InputPasswordDecrypt = function InputPasswordDecrypt(props) {
74
74
  _useState6 = _slicedToArray(_useState5, 2),
75
75
  toggleVal = _useState6[0],
76
76
  setToggleVal = _useState6[1];
77
- var _useState7 = (0, _react.useState)(false),
77
+ var _useState7 = (0, _react.useState)(-1),
78
78
  _useState8 = _slicedToArray(_useState7, 2),
79
- loading = _useState8[0],
80
- setLoading = _useState8[1];
79
+ revealIndex = _useState8[0],
80
+ setRevealIndex = _useState8[1];
81
+ var _useState9 = (0, _react.useState)(false),
82
+ _useState0 = _slicedToArray(_useState9, 2),
83
+ loading = _useState0[0],
84
+ setLoading = _useState0[1];
81
85
  var timerRef = (0, _react.useRef)(null);
86
+ var revealTimerRef = (0, _react.useRef)(null);
82
87
  var requestSeqRef = (0, _react.useRef)(0);
83
88
  var realValueRef = (0, _react.useRef)(incomingValue);
84
89
  var originalRef = (0, _react.useRef)(incomingValue);
@@ -93,13 +98,37 @@ var InputPasswordDecrypt = function InputPasswordDecrypt(props) {
93
98
  timerRef.current = null;
94
99
  }
95
100
  };
101
+ var clearRevealTimer = function clearRevealTimer() {
102
+ if (revealTimerRef.current) {
103
+ clearTimeout(revealTimerRef.current);
104
+ revealTimerRef.current = null;
105
+ }
106
+ };
107
+ var resetReveal = function resetReveal() {
108
+ clearRevealTimer();
109
+ setRevealIndex(-1);
110
+ };
111
+ var flashLastChar = function flashLastChar(val) {
112
+ clearRevealTimer();
113
+ if (!val) {
114
+ setRevealIndex(-1);
115
+ return;
116
+ }
117
+ setRevealIndex(val.length - 1);
118
+ revealTimerRef.current = setTimeout(function () {
119
+ revealTimerRef.current = null;
120
+ setRevealIndex(-1);
121
+ }, 500);
122
+ };
96
123
  var hidePlainText = function hidePlainText() {
97
124
  clearHideTimer();
125
+ resetReveal();
98
126
  setToggleVal(maskValue(realValueRef.current, maskLengthRef.current));
99
127
  setToggle(false);
100
128
  };
101
129
  var showPlainText = function showPlainText(plainText) {
102
130
  clearHideTimer();
131
+ resetReveal();
103
132
  setToggleVal(plainText);
104
133
  setToggle(true);
105
134
  timerRef.current = setTimeout(function () {
@@ -112,6 +141,7 @@ var InputPasswordDecrypt = function InputPasswordDecrypt(props) {
112
141
  return function () {
113
142
  requestSeqRef.current += 1;
114
143
  clearHideTimer();
144
+ clearRevealTimer();
115
145
  };
116
146
  }, []);
117
147
  (0, _react.useEffect)(function () {
@@ -137,18 +167,21 @@ var InputPasswordDecrypt = function InputPasswordDecrypt(props) {
137
167
  setRealValue(incomingValue);
138
168
  requestSeqRef.current += 1;
139
169
  clearHideTimer();
170
+ resetReveal();
140
171
  setToggle(false);
141
172
  setToggleVal(maskValue(incomingValue, resolvedMaskLength));
142
173
  setLoading(false);
143
174
  }, [ui_render_type, incomingValue, resolvedMaskLength]);
144
- var commitValue = function commitValue(nextValue, reveal) {
175
+ var commitValue = function commitValue(nextValue, flash) {
145
176
  var normalized = limitText(normalizeValue(nextValue), maxLength);
146
177
  emittedRef.current = normalized;
147
178
  editingRef.current = true;
148
179
  realValueRef.current = normalized;
149
180
  setRealValue(normalized);
150
- if (reveal) {
151
- showPlainText(normalized);
181
+ if (flash) {
182
+ flashLastChar(normalized);
183
+ } else {
184
+ resetReveal();
152
185
  }
153
186
  if (onChange) {
154
187
  onChange({
@@ -171,6 +204,7 @@ var InputPasswordDecrypt = function InputPasswordDecrypt(props) {
171
204
  hidePlainText();
172
205
  return _context.a(2);
173
206
  case 1:
207
+ resetReveal();
174
208
  if (!loading) {
175
209
  _context.n = 2;
176
210
  break;
@@ -275,7 +309,8 @@ var InputPasswordDecrypt = function InputPasswordDecrypt(props) {
275
309
  }
276
310
  if (event.key === "Backspace" || event.key === "Delete") {
277
311
  event.preventDefault();
278
- commitValue("", false);
312
+ var _nextValue = editingRef.current ? realValueRef.current.slice(0, -1) : "";
313
+ commitValue(_nextValue, false);
279
314
  return;
280
315
  }
281
316
  if (event.key.length !== 1) {
@@ -283,7 +318,7 @@ var InputPasswordDecrypt = function InputPasswordDecrypt(props) {
283
318
  }
284
319
  event.preventDefault();
285
320
  var nextValue = editingRef.current ? realValueRef.current + event.key : event.key;
286
- commitValue(nextValue, false);
321
+ commitValue(nextValue, true);
287
322
  };
288
323
  var onModifyPaste = function onModifyPaste(event) {
289
324
  if (onPaste) {
@@ -308,10 +343,33 @@ var InputPasswordDecrypt = function InputPasswordDecrypt(props) {
308
343
  }
309
344
  return;
310
345
  }
311
- commitValue(nextValue, true);
346
+ var normalized = limitText(normalizeValue(nextValue), maxLength);
347
+ emittedRef.current = normalized;
348
+ editingRef.current = true;
349
+ realValueRef.current = normalized;
350
+ setRealValue(normalized);
351
+ setToggleVal(normalized);
352
+ if (onChange) {
353
+ onChange({
354
+ target: {
355
+ value: normalized
356
+ }
357
+ });
358
+ }
312
359
  };
313
360
  if (ui_render_type === "modify") {
314
- var modifyDisplay = toggle ? toggleVal : maskValue(realValue, resolvedMaskLength);
361
+ var modifyDisplay;
362
+ if (toggle) {
363
+ modifyDisplay = toggleVal;
364
+ } else if (editingRef.current && realValue) {
365
+ modifyDisplay = realValue.split("").map(function (c, i) {
366
+ return i === revealIndex ? c : "*";
367
+ }).join("");
368
+ } else if (realValue) {
369
+ modifyDisplay = maskValue(realValue, resolvedMaskLength);
370
+ } else {
371
+ modifyDisplay = "";
372
+ }
315
373
  return /*#__PURE__*/_react["default"].createElement(_antd.Input, _extends({}, restInputProps, {
316
374
  value: modifyDisplay,
317
375
  placeholder: placeholder,
@@ -269,7 +269,8 @@ var FormForBrowse = /*#__PURE__*/function (_React$Component) {
269
269
  }
270
270
  var _this$props2 = _this.props,
271
271
  initialValues = _this$props2.initialValues,
272
- custom_form_items = _this$props2.custom_form_items;
272
+ custom_form_items = _this$props2.custom_form_items,
273
+ file_service_path = _this$props2.file_service_path;
273
274
  if (Array.isArray(initialValues)) {
274
275
  initialValues = initialValues[0] || {};
275
276
  }
@@ -332,7 +333,7 @@ var FormForBrowse = /*#__PURE__*/function (_React$Component) {
332
333
  ui_render_type: 'browse',
333
334
  initValue: initialValues[code],
334
335
  maskLength: _this.props.maskLength,
335
- requestUrl: _this.props.file_service_path,
336
+ requestUrl: _this.props.requestUrl || file_service_path,
336
337
  appconfig: _this.props.appconfig || _this.props.appConfig
337
338
  }));
338
339
  }
@@ -347,7 +347,7 @@ var FormForModify = /*#__PURE__*/function (_React$Component) {
347
347
  ui_render_type: 'browse',
348
348
  initValue: displayValue,
349
349
  maskLength: _this2.props.maskLength,
350
- requestUrl: _this2.props.file_service_path,
350
+ requestUrl: _this2.props.requestUrl || file_service_path,
351
351
  appconfig: _this2.props.appconfig || _this2.props.appConfig
352
352
  })) : /*#__PURE__*/_react["default"].createElement(_InputPasswordDecrypt["default"], _extends({
353
353
  key: form_id + "_" + code,
@@ -464,7 +464,8 @@ var GridForBrowse = /*#__PURE__*/function (_React$Component) {
464
464
  var _this$props3 = this.props,
465
465
  default_locked_fields = _this$props3.default_locked_fields,
466
466
  customCells = _this$props3.customCells,
467
- grid_info_record_of_data_check = _this$props3.grid_info_record_of_data_check;
467
+ grid_info_record_of_data_check = _this$props3.grid_info_record_of_data_check,
468
+ file_service_path = _this$props3.file_service_path;
468
469
  var ui_render_type = config.ui_render_type,
469
470
  code = config.code,
470
471
  display_code = config.display_code,
@@ -594,7 +595,7 @@ var GridForBrowse = /*#__PURE__*/function (_React$Component) {
594
595
  ui_render_type: 'browse',
595
596
  initValue: dataItem[code],
596
597
  maskLength: this.props.maskLength,
597
- requestUrl: this.props.requestUrl,
598
+ requestUrl: this.props.requestUrl || file_service_path,
598
599
  appconfig: this.props.appconfig || this.props.appConfig
599
600
  }) : null);
600
601
  } else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bchr/bchr-front-lib",
3
- "version": "0.6.3",
3
+ "version": "0.6.5",
4
4
  "description": "bchr front lib",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {
@@ -1,32 +1,266 @@
1
- import React, {Fragment, useState} from "react"
1
+ import React, {Fragment, useEffect, useRef, useState} from "react";
2
2
  import {Input} from "antd";
3
3
 
4
+ const MASK_LENGTH = 8;
5
+
6
+ const normalizeValue = (value) => {
7
+ if (value === null || value === undefined) {
8
+ return "";
9
+ }
10
+ return String(value);
11
+ };
12
+
13
+ const maskValue = (value, maskLength) => {
14
+ if (value === null || value === undefined || value === "") {
15
+ return "";
16
+ }
17
+ return "*".repeat(maskLength);
18
+ };
19
+
20
+ const limitText = (text, maxLength) => {
21
+ if (typeof maxLength === "number" && maxLength > 0 && text.length > maxLength) {
22
+ return text.slice(0, maxLength);
23
+ }
24
+ return text;
25
+ };
26
+
4
27
  const InputPassword = (props) => {
5
- let {ui_render_type, initValue} = props;
28
+ const {
29
+ ui_render_type,
30
+ initValue,
31
+ value,
32
+ onChange,
33
+ maskLength,
34
+ placeholder,
35
+ maxLength,
36
+ allowClear = true,
37
+ disabled = false,
38
+ onKeyDown,
39
+ onPaste,
40
+ ...restInputProps
41
+ } = props;
42
+ const resolvedMaskLength = Number.isInteger(maskLength) && maskLength > 0 ? maskLength : MASK_LENGTH;
43
+ const incomingValue = normalizeValue(value !== undefined ? value : initValue);
44
+ const [realValue, setRealValue] = useState(incomingValue);
6
45
  const [toggle, setToggle] = useState(false);
7
- const [toggleVal, setToggleVal] = useState(initValue && initValue.split('').map(item => '*').join(''))
46
+ const [toggleVal, setToggleVal] = useState(maskValue(initValue, resolvedMaskLength));
47
+ const [revealIndex, setRevealIndex] = useState(-1);
48
+ const timerRef = useRef(null);
49
+ const revealTimerRef = useRef(null);
50
+ const realValueRef = useRef(incomingValue);
51
+ const originalRef = useRef(incomingValue);
52
+ const emittedRef = useRef(incomingValue);
53
+ const editingRef = useRef(false);
54
+ const maskLengthRef = useRef(resolvedMaskLength);
55
+
56
+ realValueRef.current = ui_render_type === "modify" ? realValue : incomingValue;
57
+ maskLengthRef.current = resolvedMaskLength;
58
+
59
+ const clearHideTimer = () => {
60
+ if (timerRef.current) {
61
+ clearTimeout(timerRef.current);
62
+ timerRef.current = null;
63
+ }
64
+ };
65
+
66
+ const clearRevealTimer = () => {
67
+ if (revealTimerRef.current) {
68
+ clearTimeout(revealTimerRef.current);
69
+ revealTimerRef.current = null;
70
+ }
71
+ };
72
+
73
+ const resetReveal = () => {
74
+ clearRevealTimer();
75
+ setRevealIndex(-1);
76
+ };
77
+
78
+ const flashLastChar = (val) => {
79
+ clearRevealTimer();
80
+ if (!val) {
81
+ setRevealIndex(-1);
82
+ return;
83
+ }
84
+ setRevealIndex(val.length - 1);
85
+ revealTimerRef.current = setTimeout(() => {
86
+ revealTimerRef.current = null;
87
+ setRevealIndex(-1);
88
+ }, 500);
89
+ };
90
+
91
+ const hidePlainText = () => {
92
+ clearHideTimer();
93
+ resetReveal();
94
+ setToggleVal(maskValue(realValueRef.current, maskLengthRef.current));
95
+ setToggle(false);
96
+ };
97
+
98
+ const showPlainText = (plainText) => {
99
+ clearHideTimer();
100
+ resetReveal();
101
+ setToggleVal(plainText);
102
+ setToggle(true);
103
+ timerRef.current = setTimeout(() => {
104
+ timerRef.current = null;
105
+ setToggleVal(maskValue(realValueRef.current, maskLengthRef.current));
106
+ setToggle(false);
107
+ }, 5000);
108
+ };
109
+
110
+ useEffect(() => () => {
111
+ clearHideTimer();
112
+ clearRevealTimer();
113
+ }, []);
114
+
115
+ useEffect(() => {
116
+ if (ui_render_type === "modify") {
117
+ return undefined;
118
+ }
119
+ clearHideTimer();
120
+ resetReveal();
121
+ setToggle(false);
122
+ setToggleVal(maskValue(incomingValue, resolvedMaskLength));
123
+ return () => clearHideTimer();
124
+ }, [ui_render_type, incomingValue, resolvedMaskLength]);
125
+
126
+ useEffect(() => {
127
+ if (ui_render_type !== "modify" || incomingValue === emittedRef.current) {
128
+ return undefined;
129
+ }
130
+ emittedRef.current = incomingValue;
131
+ originalRef.current = incomingValue;
132
+ editingRef.current = false;
133
+ realValueRef.current = incomingValue;
134
+ setRealValue(incomingValue);
135
+ clearHideTimer();
136
+ resetReveal();
137
+ setToggle(false);
138
+ setToggleVal(maskValue(incomingValue, resolvedMaskLength));
139
+ }, [ui_render_type, incomingValue, resolvedMaskLength]);
140
+
141
+ const commitValue = (nextValue, flash) => {
142
+ const normalized = limitText(normalizeValue(nextValue), maxLength);
143
+ emittedRef.current = normalized;
144
+ editingRef.current = true;
145
+ realValueRef.current = normalized;
146
+ setRealValue(normalized);
147
+ if (flash) {
148
+ flashLastChar(normalized);
149
+ } else {
150
+ resetReveal();
151
+ }
152
+ if (onChange) {
153
+ onChange({target: {value: normalized}});
154
+ }
155
+ };
8
156
 
9
157
  const onToggleFull = () => {
158
+ if (disabled) {
159
+ return;
160
+ }
10
161
  if (toggle) {
11
- setToggleVal(initValue && initValue.split('').map(item => '*').join(''))
12
- setToggle(false);
162
+ hidePlainText();
163
+ return;
164
+ }
165
+ resetReveal();
166
+ const plainText = realValueRef.current;
167
+ if (ui_render_type === "modify" && (plainText === "" || editingRef.current || plainText !== originalRef.current)) {
168
+ if (plainText) {
169
+ showPlainText(plainText);
170
+ }
171
+ return;
172
+ }
173
+ if (plainText) {
174
+ showPlainText(plainText);
175
+ }
176
+ };
177
+
178
+ const onModifyKeyDown = (event) => {
179
+ if (onKeyDown) {
180
+ onKeyDown(event);
181
+ }
182
+ if (disabled || toggle || event.defaultPrevented || event.ctrlKey || event.metaKey || event.altKey || event.isComposing) {
183
+ return;
184
+ }
185
+ if (event.key === "Backspace" || event.key === "Delete") {
186
+ event.preventDefault();
187
+ commitValue(editingRef.current ? realValueRef.current.slice(0, -1) : "", false);
188
+ return;
189
+ }
190
+ if (event.key.length !== 1) {
191
+ return;
192
+ }
193
+ event.preventDefault();
194
+ commitValue(editingRef.current ? realValueRef.current + event.key : event.key, true);
195
+ };
196
+
197
+ const onModifyPaste = (event) => {
198
+ if (onPaste) {
199
+ onPaste(event);
200
+ }
201
+ if (disabled || toggle || event.defaultPrevented) {
202
+ return;
203
+ }
204
+ const text = event.clipboardData ? event.clipboardData.getData("text") : "";
205
+ event.preventDefault();
206
+ if (text) {
207
+ commitValue(editingRef.current ? realValueRef.current + text : text, false);
208
+ }
209
+ };
210
+
211
+ const onModifyChange = (event) => {
212
+ const nextValue = event.target.value;
213
+ if (!toggle) {
214
+ if (nextValue === "") {
215
+ commitValue("", false);
216
+ }
217
+ return;
218
+ }
219
+ const normalized = limitText(normalizeValue(nextValue), maxLength);
220
+ emittedRef.current = normalized;
221
+ editingRef.current = true;
222
+ realValueRef.current = normalized;
223
+ setRealValue(normalized);
224
+ setToggleVal(normalized);
225
+ if (onChange) {
226
+ onChange({target: {value: normalized}});
227
+ }
228
+ };
229
+
230
+ if (ui_render_type === "modify") {
231
+ let modifyDisplay;
232
+ if (toggle) {
233
+ modifyDisplay = toggleVal;
234
+ } else if (editingRef.current && realValue) {
235
+ modifyDisplay = realValue.split("").map((char, index) => index === revealIndex ? char : "*").join("");
236
+ } else if (realValue) {
237
+ modifyDisplay = maskValue(realValue, resolvedMaskLength);
13
238
  } else {
14
- setToggleVal(initValue)
15
- setToggle(true);
16
- setTimeout(() => {
17
- setToggleVal(initValue && initValue.split('').map(item => '*').join(''))
18
- setToggle(false);
19
- }, 5000)
239
+ modifyDisplay = "";
20
240
  }
241
+ return <Input
242
+ {...restInputProps}
243
+ value={modifyDisplay}
244
+ placeholder={placeholder}
245
+ disabled={disabled}
246
+ allowClear={allowClear && !disabled}
247
+ maxLength={toggle && typeof maxLength === "number" ? maxLength : undefined}
248
+ onChange={onModifyChange}
249
+ onKeyDown={onModifyKeyDown}
250
+ onPaste={onModifyPaste}
251
+ addonAfter={<span className={`iconfont ${toggle ? "icon-bchr-show" : "icon-bchr-hide"}`} style={{cursor: disabled ? "not-allowed" : "pointer"}} onClick={onToggleFull}/>}
252
+ />;
21
253
  }
22
254
 
23
- if (ui_render_type === 'modify') {
24
- return <Input defaultValue={toggleVal} addonAfter={<span className={`iconfont ${toggle ? 'icon-bchr-show' : 'icon-bchr-hide'}`}/>}/>
25
- } else if (ui_render_type === 'browse') {
255
+ if (ui_render_type === "browse") {
26
256
  return <Fragment>
27
- <span>{toggleVal}<span className={`iconfont ${toggle ? 'icon-bchr-show' : 'icon-bchr-hide'}`} style={{cursor: 'pointer', margin: '0 5px', color: '#337DB7', fontWeight: 'bolder'}} onClick={onToggleFull}/></span>
28
- </Fragment>
257
+ <span>{toggleVal}<span className={`iconfont ${toggle ? "icon-bchr-show" : "icon-bchr-hide"}`}
258
+ style={{cursor: disabled ? "not-allowed" : "pointer", margin: "0 5px", color: "#337DB7", fontWeight: "bolder"}}
259
+ onClick={onToggleFull}/></span>
260
+ </Fragment>;
29
261
  }
30
- }
262
+
263
+ return null;
264
+ };
31
265
 
32
266
  export default InputPassword;
@@ -48,8 +48,10 @@ const InputPasswordDecrypt = (props) => {
48
48
  const [realValue, setRealValue] = useState(incomingValue);
49
49
  const [toggle, setToggle] = useState(false);
50
50
  const [toggleVal, setToggleVal] = useState(maskValue(initValue, resolvedMaskLength));
51
+ const [revealIndex, setRevealIndex] = useState(-1);
51
52
  const [loading, setLoading] = useState(false);
52
53
  const timerRef = useRef(null);
54
+ const revealTimerRef = useRef(null);
53
55
  const requestSeqRef = useRef(0);
54
56
  const realValueRef = useRef(incomingValue);
55
57
  const originalRef = useRef(incomingValue);
@@ -67,14 +69,41 @@ const InputPasswordDecrypt = (props) => {
67
69
  }
68
70
  };
69
71
 
72
+ const clearRevealTimer = () => {
73
+ if (revealTimerRef.current) {
74
+ clearTimeout(revealTimerRef.current);
75
+ revealTimerRef.current = null;
76
+ }
77
+ };
78
+
79
+ const resetReveal = () => {
80
+ clearRevealTimer();
81
+ setRevealIndex(-1);
82
+ };
83
+
84
+ const flashLastChar = (val) => {
85
+ clearRevealTimer();
86
+ if (!val) {
87
+ setRevealIndex(-1);
88
+ return;
89
+ }
90
+ setRevealIndex(val.length - 1);
91
+ revealTimerRef.current = setTimeout(() => {
92
+ revealTimerRef.current = null;
93
+ setRevealIndex(-1);
94
+ }, 500);
95
+ };
96
+
70
97
  const hidePlainText = () => {
71
98
  clearHideTimer();
99
+ resetReveal();
72
100
  setToggleVal(maskValue(realValueRef.current, maskLengthRef.current));
73
101
  setToggle(false);
74
102
  };
75
103
 
76
104
  const showPlainText = (plainText) => {
77
105
  clearHideTimer();
106
+ resetReveal();
78
107
  setToggleVal(plainText);
79
108
  setToggle(true);
80
109
  timerRef.current = setTimeout(() => {
@@ -88,6 +117,7 @@ const InputPasswordDecrypt = (props) => {
88
117
  return () => {
89
118
  requestSeqRef.current += 1;
90
119
  clearHideTimer();
120
+ clearRevealTimer();
91
121
  };
92
122
  }, []);
93
123
 
@@ -115,19 +145,22 @@ const InputPasswordDecrypt = (props) => {
115
145
  setRealValue(incomingValue);
116
146
  requestSeqRef.current += 1;
117
147
  clearHideTimer();
148
+ resetReveal();
118
149
  setToggle(false);
119
150
  setToggleVal(maskValue(incomingValue, resolvedMaskLength));
120
151
  setLoading(false);
121
152
  }, [ui_render_type, incomingValue, resolvedMaskLength]);
122
153
 
123
- const commitValue = (nextValue, reveal) => {
154
+ const commitValue = (nextValue, flash) => {
124
155
  const normalized = limitText(normalizeValue(nextValue), maxLength);
125
156
  emittedRef.current = normalized;
126
157
  editingRef.current = true;
127
158
  realValueRef.current = normalized;
128
159
  setRealValue(normalized);
129
- if (reveal) {
130
- showPlainText(normalized);
160
+ if (flash) {
161
+ flashLastChar(normalized);
162
+ } else {
163
+ resetReveal();
131
164
  }
132
165
  if (onChange) {
133
166
  onChange({target: {value: normalized}});
@@ -139,6 +172,7 @@ const InputPasswordDecrypt = (props) => {
139
172
  hidePlainText();
140
173
  return;
141
174
  }
175
+ resetReveal();
142
176
  if (loading) {
143
177
  return;
144
178
  }
@@ -201,7 +235,8 @@ const InputPasswordDecrypt = (props) => {
201
235
  }
202
236
  if (event.key === "Backspace" || event.key === "Delete") {
203
237
  event.preventDefault();
204
- commitValue("", false);
238
+ const nextValue = editingRef.current ? realValueRef.current.slice(0, -1) : "";
239
+ commitValue(nextValue, false);
205
240
  return;
206
241
  }
207
242
  if (event.key.length !== 1) {
@@ -209,7 +244,7 @@ const InputPasswordDecrypt = (props) => {
209
244
  }
210
245
  event.preventDefault();
211
246
  const nextValue = editingRef.current ? realValueRef.current + event.key : event.key;
212
- commitValue(nextValue, false);
247
+ commitValue(nextValue, true);
213
248
  };
214
249
 
215
250
  const onModifyPaste = (event) => {
@@ -236,11 +271,28 @@ const InputPasswordDecrypt = (props) => {
236
271
  }
237
272
  return;
238
273
  }
239
- commitValue(nextValue, true);
274
+ const normalized = limitText(normalizeValue(nextValue), maxLength);
275
+ emittedRef.current = normalized;
276
+ editingRef.current = true;
277
+ realValueRef.current = normalized;
278
+ setRealValue(normalized);
279
+ setToggleVal(normalized);
280
+ if (onChange) {
281
+ onChange({target: {value: normalized}});
282
+ }
240
283
  };
241
284
 
242
285
  if (ui_render_type === "modify") {
243
- const modifyDisplay = toggle ? toggleVal : maskValue(realValue, resolvedMaskLength);
286
+ let modifyDisplay;
287
+ if (toggle) {
288
+ modifyDisplay = toggleVal;
289
+ } else if (editingRef.current && realValue) {
290
+ modifyDisplay = realValue.split("").map((c, i) => i === revealIndex ? c : "*").join("");
291
+ } else if (realValue) {
292
+ modifyDisplay = maskValue(realValue, resolvedMaskLength);
293
+ } else {
294
+ modifyDisplay = "";
295
+ }
244
296
  return <Input
245
297
  {...restInputProps}
246
298
  value={modifyDisplay}
@@ -299,7 +299,7 @@ class FormForBrowse extends React.Component{
299
299
  );
300
300
  }
301
301
 
302
- let {initialValues, custom_form_items} = this.props;
302
+ let {initialValues, custom_form_items, file_service_path} = this.props;
303
303
 
304
304
  if (Array.isArray(initialValues)) {
305
305
  initialValues = initialValues[0] || {};
@@ -370,7 +370,7 @@ class FormForBrowse extends React.Component{
370
370
  components = (
371
371
  <div>
372
372
  {
373
- initialValues[code] && <InputPasswordDecrypt ui_render_type={'browse'} initValue={initialValues[code]} maskLength={this.props.maskLength} requestUrl={this.props.file_service_path} appconfig={this.props.appconfig || this.props.appConfig}/>
373
+ initialValues[code] && <InputPasswordDecrypt ui_render_type={'browse'} initValue={initialValues[code]} maskLength={this.props.maskLength} requestUrl={this.props.requestUrl || file_service_path} appconfig={this.props.appconfig || this.props.appConfig}/>
374
374
  }
375
375
  </div>
376
376
  )
@@ -424,7 +424,7 @@ class FormForModify extends React.Component{
424
424
  case UI_RENDER_TYPE.INPUT_PASSWORD_DECRYPT:
425
425
  component = b_readonly || this.disabledExpression(dataItem, data) ? (
426
426
  <div>
427
- <InputPasswordDecrypt ui_render_type={'browse'} initValue={displayValue} maskLength={this.props.maskLength} requestUrl={this.props.file_service_path} appconfig={this.props.appconfig || this.props.appConfig}/>
427
+ <InputPasswordDecrypt ui_render_type={'browse'} initValue={displayValue} maskLength={this.props.maskLength} requestUrl={this.props.requestUrl || file_service_path} appconfig={this.props.appconfig || this.props.appConfig}/>
428
428
  </div>
429
429
  )
430
430
  :
@@ -301,7 +301,7 @@ class GridForBrowse extends React.Component{
301
301
 
302
302
  let allColumns = this.gridRef.current.columns;
303
303
  let {dataItem, columnIndex} = props;
304
- let {default_locked_fields, customCells, grid_info_record_of_data_check} = this.props;
304
+ let {default_locked_fields, customCells, grid_info_record_of_data_check, file_service_path} = this.props;
305
305
  let {ui_render_type, code, display_code, ref_pk, check_type} = config;
306
306
  let grid_default_locked = null;
307
307
  let column_locked_left = 0;
@@ -448,7 +448,7 @@ class GridForBrowse extends React.Component{
448
448
  {
449
449
  dataItem[code] ? (ui_render_type === UI_RENDER_TYPE.INPUT_PASSWORD
450
450
  ? <InputPassword ui_render_type={'browse'} initValue={dataItem[code]}/>
451
- : <InputPasswordDecrypt ui_render_type={'browse'} initValue={dataItem[code]} maskLength={this.props.maskLength} requestUrl={this.props.requestUrl} appconfig={this.props.appconfig || this.props.appConfig}/>)
451
+ : <InputPasswordDecrypt ui_render_type={'browse'} initValue={dataItem[code]} maskLength={this.props.maskLength} requestUrl={this.props.requestUrl || file_service_path} appconfig={this.props.appconfig || this.props.appConfig}/>)
452
452
  : null
453
453
  }
454
454
  </td>