react-hook-core 0.4.8 → 0.4.10
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.
- package/lib/common.js +30 -0
- package/lib/core.js +0 -118
- package/lib/formutil.js +0 -28
- package/lib/index.js +25 -0
- package/lib/input.js +57 -0
- package/lib/reflect.js +38 -48
- package/lib/route.js +11 -0
- package/lib/search.js +11 -28
- package/lib/update.js +56 -4
- package/lib/useEdit.js +38 -41
- package/lib/useSearch.js +44 -11
- package/lib/util.js +3 -3
- package/package.json +1 -1
- package/src/common.ts +28 -0
- package/src/core.ts +14 -134
- package/src/formutil.ts +0 -27
- package/src/index.ts +26 -0
- package/src/input.ts +55 -15
- package/src/reflect.ts +38 -56
- package/src/route.ts +19 -0
- package/src/search.ts +12 -33
- package/src/update.ts +56 -4
- package/src/useEdit.ts +28 -44
- package/src/useSearch.ts +43 -19
- package/src/util.ts +3 -3
- package/lib/error.js +0 -53
- package/lib/merge.js +0 -28
- package/src/error.ts +0 -55
- package/src/merge.ts +0 -26
package/lib/useEdit.js
CHANGED
|
@@ -16,11 +16,11 @@ var __assign =
|
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true })
|
|
17
17
|
var react_1 = require("react")
|
|
18
18
|
var react_router_1 = require("react-router")
|
|
19
|
+
var common_1 = require("./common")
|
|
19
20
|
var core_1 = require("./core")
|
|
20
21
|
var edit_1 = require("./edit")
|
|
21
|
-
var error_1 = require("./error")
|
|
22
22
|
var formutil_1 = require("./formutil")
|
|
23
|
-
var
|
|
23
|
+
var input_1 = require("./input")
|
|
24
24
|
var reflect_1 = require("./reflect")
|
|
25
25
|
var state_1 = require("./state")
|
|
26
26
|
var update_1 = require("./update")
|
|
@@ -119,7 +119,7 @@ exports.useEdit = function (refForm, initialState, service, p2, p) {
|
|
|
119
119
|
react_1.useEffect(function () {
|
|
120
120
|
if (refForm) {
|
|
121
121
|
var registerEvents = p2.ui ? p2.ui.registerEvents : undefined
|
|
122
|
-
|
|
122
|
+
input_1.initForm(baseProps.refForm.current, registerEvents)
|
|
123
123
|
}
|
|
124
124
|
var n = baseProps.getModelName(refForm.current)
|
|
125
125
|
var obj = {}
|
|
@@ -144,7 +144,7 @@ exports.useEdit = function (refForm, initialState, service, p2, p) {
|
|
|
144
144
|
baseProps.load(id, p ? p.callback : undefined)
|
|
145
145
|
} catch (error) {
|
|
146
146
|
p2.showError(error)
|
|
147
|
-
|
|
147
|
+
input_1.hideLoading(p2.loading)
|
|
148
148
|
}
|
|
149
149
|
}
|
|
150
150
|
}, [])
|
|
@@ -156,7 +156,7 @@ exports.useEditProps = function (props, refForm, initialState, service, p2, p) {
|
|
|
156
156
|
react_1.useEffect(function () {
|
|
157
157
|
if (refForm) {
|
|
158
158
|
var registerEvents = p2.ui ? p2.ui.registerEvents : undefined
|
|
159
|
-
|
|
159
|
+
input_1.initForm(baseProps.refForm.current, registerEvents)
|
|
160
160
|
}
|
|
161
161
|
var n = baseProps.getModelName(refForm.current)
|
|
162
162
|
var obj = {}
|
|
@@ -197,12 +197,12 @@ exports.useCoreEdit = function (refForm, initialState, service, p1, p, props) {
|
|
|
197
197
|
if (p && p.name && p.name.length > 0) {
|
|
198
198
|
return p.name
|
|
199
199
|
}
|
|
200
|
-
return
|
|
200
|
+
return update_1.getModelName(f)
|
|
201
201
|
}
|
|
202
202
|
var baseProps = update_1.useUpdate(initialState, getModelName, p1.getLocale)
|
|
203
203
|
var state = baseProps.state,
|
|
204
204
|
setState = baseProps.setState
|
|
205
|
-
var _b =
|
|
205
|
+
var _b = update_1.useMergeState({
|
|
206
206
|
newMode: false,
|
|
207
207
|
setBack: false,
|
|
208
208
|
readOnly: p ? p.readOnly : undefined,
|
|
@@ -225,16 +225,16 @@ exports.useCoreEdit = function (refForm, initialState, service, p1, p, props) {
|
|
|
225
225
|
showModel(model)
|
|
226
226
|
}
|
|
227
227
|
var _handleNotFound = function (form) {
|
|
228
|
-
var msg = error_1.message(p1.resource.value, "error_404", "error")
|
|
229
228
|
if (form) {
|
|
230
229
|
formutil_1.setReadOnly(form)
|
|
231
230
|
}
|
|
231
|
+
var resource = p1.resource.resource()
|
|
232
232
|
p1.showError(
|
|
233
|
-
|
|
233
|
+
resource.error_404,
|
|
234
234
|
function () {
|
|
235
235
|
return window.history.back
|
|
236
236
|
},
|
|
237
|
-
|
|
237
|
+
resource.error,
|
|
238
238
|
)
|
|
239
239
|
}
|
|
240
240
|
var handleNotFound = p && p.handleNotFound ? p.handleNotFound : _handleNotFound
|
|
@@ -260,15 +260,15 @@ exports.useCoreEdit = function (refForm, initialState, service, p1, p, props) {
|
|
|
260
260
|
if (objKeys.length === 0) {
|
|
261
261
|
navigate(-1)
|
|
262
262
|
} else {
|
|
263
|
-
var
|
|
263
|
+
var resource = p1.resource.resource()
|
|
264
264
|
p1.confirm(
|
|
265
|
-
|
|
265
|
+
resource.msg_confirm_back,
|
|
266
266
|
function () {
|
|
267
267
|
navigate(-1)
|
|
268
268
|
},
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
269
|
+
resource.confirm,
|
|
270
|
+
resource.no,
|
|
271
|
+
resource.yes,
|
|
272
272
|
)
|
|
273
273
|
}
|
|
274
274
|
}
|
|
@@ -296,13 +296,12 @@ exports.useCoreEdit = function (refForm, initialState, service, p1, p, props) {
|
|
|
296
296
|
}
|
|
297
297
|
}
|
|
298
298
|
var _onSave = function (isBack) {
|
|
299
|
+
var resource = p1.resource.resource()
|
|
299
300
|
if (p && p.readOnly) {
|
|
300
301
|
if (flag.newMode === true) {
|
|
301
|
-
|
|
302
|
-
p1.showError(m.message, undefined, m.title)
|
|
302
|
+
p1.showError(resource.error_permission_add, undefined, resource.error_permission)
|
|
303
303
|
} else {
|
|
304
|
-
|
|
305
|
-
p1.showError(msg.message, undefined, msg.title)
|
|
304
|
+
p1.showError(resource.error_permission_edit, undefined, resource.error_permission)
|
|
306
305
|
}
|
|
307
306
|
} else {
|
|
308
307
|
if (running === true) {
|
|
@@ -319,15 +318,14 @@ exports.useCoreEdit = function (refForm, initialState, service, p1, p, props) {
|
|
|
319
318
|
}
|
|
320
319
|
if (flag.newMode) {
|
|
321
320
|
validate(obj_1, function () {
|
|
322
|
-
var msg = error_1.message(p1.resource.value, "msg_confirm_save", "confirm", "yes", "no")
|
|
323
321
|
p1.confirm(
|
|
324
|
-
|
|
322
|
+
resource.msg_confirm_save,
|
|
325
323
|
function () {
|
|
326
324
|
doSave(obj_1, undefined, version_1, isBack)
|
|
327
325
|
},
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
326
|
+
resource.confirm,
|
|
327
|
+
resource.no,
|
|
328
|
+
resource.yes,
|
|
331
329
|
)
|
|
332
330
|
})
|
|
333
331
|
} else {
|
|
@@ -337,15 +335,14 @@ exports.useCoreEdit = function (refForm, initialState, service, p1, p, props) {
|
|
|
337
335
|
p1.showMessage(p1.resource.value("msg_no_change"))
|
|
338
336
|
} else {
|
|
339
337
|
validate(obj_1, function () {
|
|
340
|
-
var msg = error_1.message(p1.resource.value, "msg_confirm_save", "confirm", "yes", "no")
|
|
341
338
|
p1.confirm(
|
|
342
|
-
|
|
339
|
+
resource.msg_confirm_save,
|
|
343
340
|
function () {
|
|
344
341
|
doSave(obj_1, diffObj_1, version_1, isBack)
|
|
345
342
|
},
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
343
|
+
resource.confirm,
|
|
344
|
+
resource.no,
|
|
345
|
+
resource.yes,
|
|
349
346
|
)
|
|
350
347
|
})
|
|
351
348
|
}
|
|
@@ -414,7 +411,7 @@ exports.useCoreEdit = function (refForm, initialState, service, p1, p, props) {
|
|
|
414
411
|
var _handleError = function (err) {
|
|
415
412
|
if (err) {
|
|
416
413
|
setRunning(false)
|
|
417
|
-
|
|
414
|
+
input_1.hideLoading(p1.loading)
|
|
418
415
|
var errHeader = p1.resource.value("error")
|
|
419
416
|
var errMsg = p1.resource.value("error_internal")
|
|
420
417
|
var data = err && err.response ? err.response : err
|
|
@@ -429,7 +426,7 @@ exports.useCoreEdit = function (refForm, initialState, service, p1, p, props) {
|
|
|
429
426
|
var handleError = p && p.handleError ? p.handleError : _handleError
|
|
430
427
|
var _postSave = function (r, origin, version, isPatch, backOnSave) {
|
|
431
428
|
setRunning(false)
|
|
432
|
-
|
|
429
|
+
input_1.hideLoading(p1.loading)
|
|
433
430
|
var x = r
|
|
434
431
|
var successMsg = p1.resource.value("msg_save_success")
|
|
435
432
|
var newMod = flag.newMode
|
|
@@ -467,13 +464,13 @@ exports.useCoreEdit = function (refForm, initialState, service, p1, p, props) {
|
|
|
467
464
|
}
|
|
468
465
|
var postSave = p && p.postSave ? p.postSave : _postSave
|
|
469
466
|
var _handleDuplicateKey = function (result) {
|
|
470
|
-
var
|
|
471
|
-
p1.showError(
|
|
467
|
+
var resource = p1.resource.resource()
|
|
468
|
+
p1.showError(resource.error_duplicate_key, undefined, resource.error)
|
|
472
469
|
}
|
|
473
470
|
var handleDuplicateKey = p && p.handleDuplicateKey ? p.handleDuplicateKey : _handleDuplicateKey
|
|
474
471
|
var _doSave = function (obj, body, version, isBack) {
|
|
475
472
|
setRunning(true)
|
|
476
|
-
|
|
473
|
+
input_1.showLoading(p1.loading)
|
|
477
474
|
var isBackO = isBack != null && isBack !== undefined ? isBack : false
|
|
478
475
|
var patchable = p ? p.patchable : true
|
|
479
476
|
if (flag.newMode === false) {
|
|
@@ -506,7 +503,7 @@ exports.useCoreEdit = function (refForm, initialState, service, p1, p, props) {
|
|
|
506
503
|
var id = _id
|
|
507
504
|
if (id != null && id !== "") {
|
|
508
505
|
setRunning(true)
|
|
509
|
-
|
|
506
|
+
input_1.showLoading(p1.loading)
|
|
510
507
|
service
|
|
511
508
|
.load(id)
|
|
512
509
|
.then(function (obj) {
|
|
@@ -521,14 +518,14 @@ exports.useCoreEdit = function (refForm, initialState, service, p1, p, props) {
|
|
|
521
518
|
}
|
|
522
519
|
}
|
|
523
520
|
setRunning(false)
|
|
524
|
-
|
|
521
|
+
input_1.hideLoading(p1.loading)
|
|
525
522
|
})
|
|
526
523
|
.catch(function (err) {
|
|
527
524
|
var _a, _b, _c
|
|
528
525
|
var data = err && err.response ? err.response : err
|
|
529
|
-
var
|
|
530
|
-
var title =
|
|
531
|
-
var msg =
|
|
526
|
+
var resource = p1.resource.resource()
|
|
527
|
+
var title = resource.error
|
|
528
|
+
var msg = resource.error_internal
|
|
532
529
|
if (data && data.status === 422) {
|
|
533
530
|
fail((_a = err.response) === null || _a === void 0 ? void 0 : _a.data)
|
|
534
531
|
var obj = (_c = (_b = err.response) === null || _b === void 0 ? void 0 : _b.data) === null || _c === void 0 ? void 0 : _c.value
|
|
@@ -540,7 +537,7 @@ exports.useCoreEdit = function (refForm, initialState, service, p1, p, props) {
|
|
|
540
537
|
handleNotFound(refForm.current)
|
|
541
538
|
} else {
|
|
542
539
|
if (data.status && !isNaN(data.status)) {
|
|
543
|
-
msg =
|
|
540
|
+
msg = common_1.messageByHttpStatus(data.status, resource)
|
|
544
541
|
}
|
|
545
542
|
if (data && (data.status === 401 || data.status === 403)) {
|
|
546
543
|
formutil_1.setReadOnly(refForm.current)
|
|
@@ -549,7 +546,7 @@ exports.useCoreEdit = function (refForm, initialState, service, p1, p, props) {
|
|
|
549
546
|
}
|
|
550
547
|
}
|
|
551
548
|
setRunning(false)
|
|
552
|
-
|
|
549
|
+
input_1.hideLoading(p1.loading)
|
|
553
550
|
})
|
|
554
551
|
} else {
|
|
555
552
|
var obj = createModel()
|
package/lib/useSearch.js
CHANGED
|
@@ -22,14 +22,42 @@ var __spreadArrays =
|
|
|
22
22
|
}
|
|
23
23
|
Object.defineProperty(exports, "__esModule", { value: true })
|
|
24
24
|
var react_1 = require("react")
|
|
25
|
+
var common_1 = require("./common")
|
|
25
26
|
var core_1 = require("./core")
|
|
26
|
-
var
|
|
27
|
+
var input_1 = require("./input")
|
|
27
28
|
var merge_1 = require("./merge")
|
|
28
29
|
var reflect_1 = require("./reflect")
|
|
29
30
|
var route_1 = require("./route")
|
|
30
31
|
var search_1 = require("./search")
|
|
31
32
|
var state_1 = require("./state")
|
|
32
33
|
var update_1 = require("./update")
|
|
34
|
+
function showPaging(com, list, pageSize, total) {
|
|
35
|
+
com.total = total
|
|
36
|
+
var pageTotal = search_1.getPageTotal(pageSize, total)
|
|
37
|
+
com.pages = pageTotal
|
|
38
|
+
com.showPaging = !total || com.pages <= 1 || (list && list.length >= total) ? false : true
|
|
39
|
+
}
|
|
40
|
+
exports.showPaging = showPaging
|
|
41
|
+
function removeFormError(u, f) {
|
|
42
|
+
if (f && u && u.ui) {
|
|
43
|
+
u.ui.removeFormError(f)
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
exports.removeFormError = removeFormError
|
|
47
|
+
function getValidateForm(u, vf) {
|
|
48
|
+
if (vf) {
|
|
49
|
+
return vf
|
|
50
|
+
}
|
|
51
|
+
return u && u.ui ? u.ui.validateForm : undefined
|
|
52
|
+
}
|
|
53
|
+
exports.getValidateForm = getValidateForm
|
|
54
|
+
function getRemoveError(u, rmErr) {
|
|
55
|
+
if (rmErr) {
|
|
56
|
+
return rmErr
|
|
57
|
+
}
|
|
58
|
+
return u && u.ui ? u.ui.removeError : undefined
|
|
59
|
+
}
|
|
60
|
+
exports.getRemoveError = getRemoveError
|
|
33
61
|
function getModel(state, modelName, searchable, fields, excluding) {
|
|
34
62
|
var obj2 = getModelFromState(state, modelName)
|
|
35
63
|
var obj = obj2 ? obj2 : {}
|
|
@@ -259,7 +287,7 @@ exports.useSearch = function (refForm, initialState, service, p2, p) {
|
|
|
259
287
|
searchError = baseProps.searchError
|
|
260
288
|
if (refForm) {
|
|
261
289
|
var registerEvents = p2.ui ? p2.ui.registerEvents : undefined
|
|
262
|
-
|
|
290
|
+
input_1.initForm(refForm.current, registerEvents)
|
|
263
291
|
}
|
|
264
292
|
if (p && p.initialize) {
|
|
265
293
|
p.initialize(load, setState, component)
|
|
@@ -281,16 +309,20 @@ exports.useSearchOneProps = function (p) {
|
|
|
281
309
|
exports.useSearchOne = function (p) {
|
|
282
310
|
return exports.useCoreSearch(p.refForm, p.initialState, p.service, p, p)
|
|
283
311
|
}
|
|
312
|
+
function getName(d, n) {
|
|
313
|
+
return n && n.length > 0 ? n : d
|
|
314
|
+
}
|
|
315
|
+
exports.getName = getName
|
|
284
316
|
exports.useCoreSearch = function (refForm, initialState, service, p1, p2) {
|
|
285
317
|
var p = mergeParam(p2)
|
|
286
318
|
var _a = react_1.useState(),
|
|
287
319
|
running = _a[0],
|
|
288
320
|
setRunning = _a[1]
|
|
289
321
|
var _getModelName = function () {
|
|
290
|
-
return
|
|
322
|
+
return getName("filter", p && p.name ? p.name : undefined)
|
|
291
323
|
}
|
|
292
324
|
var getModelName = p && p.getModelName ? p.getModelName : _getModelName
|
|
293
|
-
var baseProps = update_1.useUpdate(initialState, getModelName, p1.getLocale,
|
|
325
|
+
var baseProps = update_1.useUpdate(initialState, getModelName, p1.getLocale, getRemoveError(p1))
|
|
294
326
|
var state = baseProps.state,
|
|
295
327
|
setState = baseProps.setState
|
|
296
328
|
var _b = react_1.useState(false),
|
|
@@ -359,7 +391,7 @@ exports.useCoreSearch = function (refForm, initialState, service, p1, p2) {
|
|
|
359
391
|
}
|
|
360
392
|
var load = p && p.load ? p.load : _load
|
|
361
393
|
var doSearch = function (se, isFirstLoad) {
|
|
362
|
-
|
|
394
|
+
removeFormError(p1, refForm.current)
|
|
363
395
|
var s = getFilter(se)
|
|
364
396
|
if (isFirstLoad) {
|
|
365
397
|
setState(state)
|
|
@@ -370,7 +402,7 @@ exports.useCoreSearch = function (refForm, initialState, service, p1, p2) {
|
|
|
370
402
|
return
|
|
371
403
|
}
|
|
372
404
|
setRunning(true)
|
|
373
|
-
|
|
405
|
+
input_1.showLoading(p1.loading)
|
|
374
406
|
if (p && !p.ignoreUrlParam) {
|
|
375
407
|
search_1.addParametersIntoUrl(s, isFirstLoad)
|
|
376
408
|
}
|
|
@@ -383,7 +415,7 @@ exports.useCoreSearch = function (refForm, initialState, service, p1, p2) {
|
|
|
383
415
|
})
|
|
384
416
|
}
|
|
385
417
|
var _validateSearch = function (se, callback) {
|
|
386
|
-
validate(se, callback, refForm.current, p1.getLocale ? p1.getLocale() : undefined,
|
|
418
|
+
validate(se, callback, refForm.current, p1.getLocale ? p1.getLocale() : undefined, getValidateForm(p1))
|
|
387
419
|
}
|
|
388
420
|
var validateSearch = p && p.validateSearch ? p.validateSearch : _validateSearch
|
|
389
421
|
var pageSizeChanged = function (event) {
|
|
@@ -472,8 +504,9 @@ exports.useCoreSearch = function (refForm, initialState, service, p1, p2) {
|
|
|
472
504
|
}
|
|
473
505
|
var searchError = function (err) {
|
|
474
506
|
setComponent({ page: component.tmpPageIndex })
|
|
475
|
-
|
|
476
|
-
|
|
507
|
+
var resource = p1.resource.resource()
|
|
508
|
+
common_1.error(err, resource, p1.showError)
|
|
509
|
+
input_1.hideLoading(p1.loading)
|
|
477
510
|
}
|
|
478
511
|
var appendList = p && p.appendList ? p.appendList : appendListOfState
|
|
479
512
|
var setList = p && p.setList ? p.setList : setListOfState
|
|
@@ -500,7 +533,7 @@ exports.useCoreSearch = function (refForm, initialState, service, p1, p2) {
|
|
|
500
533
|
setList(results, setState)
|
|
501
534
|
}
|
|
502
535
|
} else {
|
|
503
|
-
|
|
536
|
+
showPaging(component, sr.list, s.limit, sr.total)
|
|
504
537
|
setList(results, setState)
|
|
505
538
|
setComponent({ tmpPageIndex: s.page })
|
|
506
539
|
if (s.limit) {
|
|
@@ -509,7 +542,7 @@ exports.useCoreSearch = function (refForm, initialState, service, p1, p2) {
|
|
|
509
542
|
}
|
|
510
543
|
}
|
|
511
544
|
setRunning(false)
|
|
512
|
-
|
|
545
|
+
input_1.hideLoading(p1.loading)
|
|
513
546
|
if (component.triggerSearch) {
|
|
514
547
|
setComponent({ triggerSearch: false })
|
|
515
548
|
resetAndSearch()
|
package/lib/util.js
CHANGED
|
@@ -72,15 +72,15 @@ function getStringCurrency(value, datatype, locale, maxLength, isOnBlur) {
|
|
|
72
72
|
}
|
|
73
73
|
}
|
|
74
74
|
var dotPosition = value.indexOf(".")
|
|
75
|
-
var beforeDot = dotPosition >= 0 ? value.
|
|
75
|
+
var beforeDot = dotPosition >= 0 ? value.substring(0, dotPosition) : value
|
|
76
76
|
if (datatype === "string-currency" || isOnBlur) {
|
|
77
77
|
beforeDot = beforeDot.replace(new RegExp("\\B(?=(\\d{" + groupDigits + "})+(?!\\d))", "g"), groupSeparator)
|
|
78
78
|
}
|
|
79
79
|
var afterDot
|
|
80
80
|
if (dotPosition > 0) {
|
|
81
|
-
afterDot = value.
|
|
81
|
+
afterDot = value.substring(dotPosition + 1)
|
|
82
82
|
if (afterDot.length > decimalDigits) {
|
|
83
|
-
afterDot = afterDot.
|
|
83
|
+
afterDot = afterDot.substring(0, decimalDigits)
|
|
84
84
|
}
|
|
85
85
|
}
|
|
86
86
|
if (maxLength && beforeDot.length > maxLength - (decimalDigits + 1)) {
|
package/package.json
CHANGED
package/src/common.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { StringMap } from "./core"
|
|
2
|
+
|
|
3
|
+
export function messageByHttpStatus(status: number, resource: StringMap): string {
|
|
4
|
+
const k = "error_" + status
|
|
5
|
+
let msg = resource[k]
|
|
6
|
+
if (!msg || msg.length === 0) {
|
|
7
|
+
msg = resource.error_500
|
|
8
|
+
}
|
|
9
|
+
return msg
|
|
10
|
+
}
|
|
11
|
+
export function error(err: any, resource: StringMap, ae: (msg: string, callback?: () => void, header?: string) => void) {
|
|
12
|
+
const title = resource.error
|
|
13
|
+
let msg = resource.error_internal
|
|
14
|
+
if (!err) {
|
|
15
|
+
ae(msg, undefined, title)
|
|
16
|
+
return
|
|
17
|
+
}
|
|
18
|
+
const data = err && err.response ? err.response : err
|
|
19
|
+
if (data) {
|
|
20
|
+
const status = data.status
|
|
21
|
+
if (status && !isNaN(status)) {
|
|
22
|
+
msg = messageByHttpStatus(status, resource)
|
|
23
|
+
}
|
|
24
|
+
ae(msg, undefined, title)
|
|
25
|
+
} else {
|
|
26
|
+
ae(msg, undefined, title)
|
|
27
|
+
}
|
|
28
|
+
}
|
package/src/core.ts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { focusFirstElement } from "./formutil"
|
|
2
|
-
|
|
3
1
|
export interface LoadingService {
|
|
4
2
|
showLoading(firstTime?: boolean): void
|
|
5
3
|
hideLoading(): void
|
|
@@ -19,13 +17,6 @@ export class resources {
|
|
|
19
17
|
static pageMaxSize = 7
|
|
20
18
|
}
|
|
21
19
|
|
|
22
|
-
export function removePhoneFormat(phone: string): string {
|
|
23
|
-
if (phone) {
|
|
24
|
-
return phone.replace(resources.phone, "")
|
|
25
|
-
} else {
|
|
26
|
-
return phone
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
20
|
export interface StringMap {
|
|
30
21
|
[key: string]: string
|
|
31
22
|
}
|
|
@@ -55,7 +46,6 @@ export interface ErrorMessage {
|
|
|
55
46
|
}
|
|
56
47
|
export interface UIService {
|
|
57
48
|
getValue(el: HTMLInputElement, locale?: Locale, currencyCode?: string): string | number | boolean | null | undefined
|
|
58
|
-
// decodeFromForm(form: HTMLFormElement, locale?: Locale, currencyCode?: string | null): any;
|
|
59
49
|
|
|
60
50
|
validateForm(form?: HTMLFormElement, locale?: Locale, focusFirst?: boolean, scroll?: boolean): boolean
|
|
61
51
|
removeFormError(form: HTMLFormElement): void
|
|
@@ -66,7 +56,7 @@ export interface UIService {
|
|
|
66
56
|
registerEvents?(form: HTMLFormElement): void
|
|
67
57
|
}
|
|
68
58
|
|
|
69
|
-
export type
|
|
59
|
+
export type Type =
|
|
70
60
|
| "ObjectId"
|
|
71
61
|
| "date"
|
|
72
62
|
| "datetime"
|
|
@@ -88,9 +78,21 @@ export type DataType =
|
|
|
88
78
|
| "datetimes"
|
|
89
79
|
| "times"
|
|
90
80
|
|
|
81
|
+
export type Format = "currency" | "percentage" | "email" | "url" | "phone" | "fax" | "ipv4" | "ipv6"
|
|
82
|
+
|
|
83
|
+
export interface Attribute {
|
|
84
|
+
name?: string
|
|
85
|
+
type?: Type
|
|
86
|
+
format?: Format
|
|
87
|
+
key?: boolean
|
|
88
|
+
version?: boolean
|
|
89
|
+
typeof?: Attributes
|
|
90
|
+
scale?: number
|
|
91
|
+
noformat?: boolean
|
|
92
|
+
}
|
|
91
93
|
export interface Attribute {
|
|
92
94
|
name?: string
|
|
93
|
-
type?:
|
|
95
|
+
type?: Type
|
|
94
96
|
key?: boolean
|
|
95
97
|
version?: boolean
|
|
96
98
|
typeof?: Attributes
|
|
@@ -98,125 +100,3 @@ export interface Attribute {
|
|
|
98
100
|
export interface Attributes {
|
|
99
101
|
[key: string]: Attribute
|
|
100
102
|
}
|
|
101
|
-
|
|
102
|
-
export const datetimeToString = (inputDate: Date) => {
|
|
103
|
-
const date = new Date(inputDate)
|
|
104
|
-
const year = date.getFullYear()
|
|
105
|
-
const month = String(date.getMonth() + 1).padStart(2, "0")
|
|
106
|
-
const day = String(date.getDate()).padStart(2, "0")
|
|
107
|
-
const hours = String(date.getHours()).padStart(2, "0")
|
|
108
|
-
const minutes = String(date.getMinutes()).padStart(2, "0")
|
|
109
|
-
const seconds = String(date.getSeconds()).padStart(2, "0")
|
|
110
|
-
return `${year}-${month}-${day}T${hours}:${minutes}:${seconds}`
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
export const dateToString = (inputDate: Date) => {
|
|
114
|
-
const year = inputDate.getFullYear()
|
|
115
|
-
const month = String(inputDate.getMonth() + 1).padStart(2, "0")
|
|
116
|
-
const day = String(inputDate.getDate()).padStart(2, "0")
|
|
117
|
-
return `${year}-${month}-${day}`
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
export function initForm(form?: HTMLFormElement, initMat?: (f: HTMLFormElement) => void): HTMLFormElement | undefined {
|
|
121
|
-
if (form) {
|
|
122
|
-
setTimeout(() => {
|
|
123
|
-
if (initMat) {
|
|
124
|
-
initMat(form)
|
|
125
|
-
}
|
|
126
|
-
focusFirstElement(form)
|
|
127
|
-
}, 100)
|
|
128
|
-
}
|
|
129
|
-
return form
|
|
130
|
-
}
|
|
131
|
-
export function getName(d: string, n?: string): string {
|
|
132
|
-
return n && n.length > 0 ? n : d
|
|
133
|
-
}
|
|
134
|
-
export function getModelName(form?: HTMLFormElement | null, name?: string): string {
|
|
135
|
-
if (form) {
|
|
136
|
-
const a = form.getAttribute("model-name")
|
|
137
|
-
if (a && a.length > 0) {
|
|
138
|
-
return a
|
|
139
|
-
}
|
|
140
|
-
const b = form.name
|
|
141
|
-
if (b) {
|
|
142
|
-
if (b.endsWith("Form")) {
|
|
143
|
-
return b.substring(0, b.length - 4)
|
|
144
|
-
}
|
|
145
|
-
return b
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
if (name && name.length > 0) {
|
|
149
|
-
return name
|
|
150
|
-
}
|
|
151
|
-
return ""
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
export const scrollToFocus = (e: any, isUseTimeOut?: boolean) => {
|
|
155
|
-
try {
|
|
156
|
-
const element = e.target as HTMLInputElement
|
|
157
|
-
const form = element.form
|
|
158
|
-
if (form) {
|
|
159
|
-
const container = form.childNodes[1] as HTMLElement
|
|
160
|
-
const elementRect = element.getBoundingClientRect()
|
|
161
|
-
const absoluteElementTop = elementRect.top + window.pageYOffset
|
|
162
|
-
const middle = absoluteElementTop - window.innerHeight / 2
|
|
163
|
-
const scrollTop = container.scrollTop
|
|
164
|
-
const timeOut = isUseTimeOut ? 300 : 0
|
|
165
|
-
const isChrome = navigator.userAgent.search("Chrome") > 0
|
|
166
|
-
setTimeout(() => {
|
|
167
|
-
if (isChrome) {
|
|
168
|
-
const scrollPosition = scrollTop === 0 ? elementRect.top + 64 : scrollTop + middle
|
|
169
|
-
container.scrollTo(0, Math.abs(scrollPosition))
|
|
170
|
-
} else {
|
|
171
|
-
container.scrollTo(0, Math.abs(scrollTop + middle))
|
|
172
|
-
}
|
|
173
|
-
}, timeOut)
|
|
174
|
-
}
|
|
175
|
-
} catch (e) {
|
|
176
|
-
console.log(e)
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
export interface LoadingParameter {
|
|
180
|
-
loading?: LoadingService
|
|
181
|
-
}
|
|
182
|
-
export function showLoading(s?: LoadingService): void {
|
|
183
|
-
if (s) {
|
|
184
|
-
s.showLoading()
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
export function hideLoading(s?: LoadingService): void {
|
|
188
|
-
if (s) {
|
|
189
|
-
s.hideLoading()
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
export interface UIParameter {
|
|
193
|
-
ui?: UIService
|
|
194
|
-
}
|
|
195
|
-
export function getRemoveError(u?: UIParameter, rmErr?: (el: HTMLInputElement) => void): ((el: HTMLInputElement) => void) | undefined {
|
|
196
|
-
if (rmErr) {
|
|
197
|
-
return rmErr
|
|
198
|
-
}
|
|
199
|
-
return u && u.ui ? u.ui.removeError : undefined
|
|
200
|
-
}
|
|
201
|
-
export function removeFormError(u?: UIParameter, f?: HTMLFormElement): void {
|
|
202
|
-
if (f && u && u.ui) {
|
|
203
|
-
u.ui.removeFormError(f)
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
export function getValidateForm(
|
|
207
|
-
u?: UIParameter,
|
|
208
|
-
vf?: (form: HTMLFormElement, locale?: Locale, focusFirst?: boolean, scroll?: boolean) => boolean,
|
|
209
|
-
): ((form: HTMLFormElement, locale?: Locale, focusFirst?: boolean, scroll?: boolean) => boolean) | undefined {
|
|
210
|
-
if (vf) {
|
|
211
|
-
return vf
|
|
212
|
-
}
|
|
213
|
-
return u && u.ui ? u.ui.validateForm : undefined
|
|
214
|
-
}
|
|
215
|
-
/*
|
|
216
|
-
export function getDecodeFromForm(u?: UIParameter, d?: (form: HTMLFormElement, locale?: Locale, currencyCode?: string) => any): ((form: HTMLFormElement, locale?: Locale, currencyCode?: string) => any) | undefined {
|
|
217
|
-
if (d) {
|
|
218
|
-
return d;
|
|
219
|
-
}
|
|
220
|
-
return (u && u.ui ? u.ui.decodeFromForm : undefined);
|
|
221
|
-
}
|
|
222
|
-
*/
|
package/src/formutil.ts
CHANGED
|
@@ -40,33 +40,6 @@ export function setReadOnly(form?: HTMLFormElement | null, ...args: string[]): v
|
|
|
40
40
|
}
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
|
-
export function focusFirstElement(form: HTMLFormElement): void {
|
|
44
|
-
let i = 0
|
|
45
|
-
const len = form.length
|
|
46
|
-
for (i = 0; i < len; i++) {
|
|
47
|
-
const ctrl = form[i] as HTMLInputElement
|
|
48
|
-
if (!(ctrl.readOnly || ctrl.disabled)) {
|
|
49
|
-
let nodeName = ctrl.nodeName
|
|
50
|
-
const type = ctrl.getAttribute("type")
|
|
51
|
-
if (type) {
|
|
52
|
-
const t = type.toUpperCase()
|
|
53
|
-
if (t === "BUTTON" || t === "SUBMIT") {
|
|
54
|
-
ctrl.focus()
|
|
55
|
-
}
|
|
56
|
-
if (nodeName === "INPUT") {
|
|
57
|
-
nodeName = t
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
if (nodeName !== "BUTTON" && nodeName !== "RESET" && nodeName !== "SUBMIT" && nodeName !== "CHECKBOX" && nodeName !== "RADIO") {
|
|
61
|
-
ctrl.focus()
|
|
62
|
-
try {
|
|
63
|
-
ctrl.setSelectionRange(0, ctrl.value.length)
|
|
64
|
-
} catch (err) {}
|
|
65
|
-
return
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
43
|
export function focusFirstError(form?: HTMLFormElement | null, className?: string): void {
|
|
71
44
|
if (!form) {
|
|
72
45
|
return
|
package/src/index.ts
CHANGED
|
@@ -203,3 +203,29 @@ export function datetimeToString(date?: Date | string): string | undefined {
|
|
|
203
203
|
export function getNumber(event: ChangeEvent<HTMLSelectElement | HTMLInputElement>): number {
|
|
204
204
|
return parseInt(event.currentTarget.value, 10)
|
|
205
205
|
}
|
|
206
|
+
|
|
207
|
+
export const scrollToFocus = (e: any, isUseTimeOut?: boolean) => {
|
|
208
|
+
try {
|
|
209
|
+
const element = e.target as HTMLInputElement
|
|
210
|
+
const form = element.form
|
|
211
|
+
if (form) {
|
|
212
|
+
const container = form.childNodes[1] as HTMLElement
|
|
213
|
+
const elementRect = element.getBoundingClientRect()
|
|
214
|
+
const absoluteElementTop = elementRect.top + window.pageYOffset
|
|
215
|
+
const middle = absoluteElementTop - window.innerHeight / 2
|
|
216
|
+
const scrollTop = container.scrollTop
|
|
217
|
+
const timeOut = isUseTimeOut ? 300 : 0
|
|
218
|
+
const isChrome = navigator.userAgent.search("Chrome") > 0
|
|
219
|
+
setTimeout(() => {
|
|
220
|
+
if (isChrome) {
|
|
221
|
+
const scrollPosition = scrollTop === 0 ? elementRect.top + 64 : scrollTop + middle
|
|
222
|
+
container.scrollTo(0, Math.abs(scrollPosition))
|
|
223
|
+
} else {
|
|
224
|
+
container.scrollTo(0, Math.abs(scrollTop + middle))
|
|
225
|
+
}
|
|
226
|
+
}, timeOut)
|
|
227
|
+
}
|
|
228
|
+
} catch (e) {
|
|
229
|
+
console.log(e)
|
|
230
|
+
}
|
|
231
|
+
}
|