react-hook-core 0.4.15 → 0.4.16

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 CHANGED
@@ -28,3 +28,60 @@ function error(err, resource, ae) {
28
28
  }
29
29
  }
30
30
  exports.error = error
31
+ function showLoading(loading) {
32
+ if (loading) {
33
+ if (typeof loading === "function") {
34
+ loading()
35
+ } else {
36
+ loading.showLoading()
37
+ }
38
+ }
39
+ }
40
+ exports.showLoading = showLoading
41
+ function hideLoading(loading) {
42
+ if (loading) {
43
+ if (typeof loading === "function") {
44
+ loading()
45
+ } else {
46
+ loading.hideLoading()
47
+ }
48
+ }
49
+ }
50
+ exports.hideLoading = hideLoading
51
+ function initForm(form, initMat) {
52
+ if (form) {
53
+ setTimeout(function () {
54
+ if (initMat) {
55
+ initMat(form)
56
+ }
57
+ focusFirstElement(form)
58
+ }, 100)
59
+ }
60
+ return form
61
+ }
62
+ exports.initForm = initForm
63
+ function focusFirstElement(form) {
64
+ var i = 0
65
+ var len = form.length
66
+ for (i = 0; i < len; i++) {
67
+ var ctrl = form[i]
68
+ if (!(ctrl.readOnly || ctrl.disabled)) {
69
+ var nodeName = ctrl.nodeName
70
+ var type = ctrl.getAttribute("type")
71
+ if (type) {
72
+ var t = type.toUpperCase()
73
+ if (t === "BUTTON" || t === "SUBMIT") {
74
+ ctrl.focus()
75
+ }
76
+ if (nodeName === "INPUT") {
77
+ nodeName = t
78
+ }
79
+ }
80
+ if (nodeName !== "BUTTON" && nodeName !== "RESET" && nodeName !== "SUBMIT" && nodeName !== "CHECKBOX" && nodeName !== "RADIO") {
81
+ ctrl.focus()
82
+ return
83
+ }
84
+ }
85
+ }
86
+ }
87
+ exports.focusFirstElement = focusFirstElement
package/lib/index.js CHANGED
@@ -10,7 +10,6 @@ __export(require("./core"))
10
10
  __export(require("./diff"))
11
11
  __export(require("./edit"))
12
12
  __export(require("./formutil"))
13
- __export(require("./input"))
14
13
  __export(require("./reflect"))
15
14
  __export(require("./route"))
16
15
  __export(require("./search"))
package/lib/useEdit.js CHANGED
@@ -20,7 +20,6 @@ var common_1 = require("./common")
20
20
  var core_1 = require("./core")
21
21
  var edit_1 = require("./edit")
22
22
  var formutil_1 = require("./formutil")
23
- var input_1 = require("./input")
24
23
  var reflect_1 = require("./reflect")
25
24
  var state_1 = require("./state")
26
25
  var update_1 = require("./update")
@@ -113,13 +112,13 @@ function handleVersion(obj, version) {
113
112
  }
114
113
  }
115
114
  exports.handleVersion = handleVersion
116
- exports.useEdit = function (refForm, initialState, service, p2, p) {
115
+ exports.useEdit = function (refForm, initialState, service, resource, p2, p) {
117
116
  var params = react_router_1.useParams()
118
- var baseProps = exports.useCoreEdit(refForm, initialState, service, p2, p)
117
+ var baseProps = exports.useCoreEdit(refForm, initialState, service, resource, p2, p)
119
118
  react_1.useEffect(function () {
120
119
  if (refForm) {
121
120
  var registerEvents = p2.ui ? p2.ui.registerEvents : undefined
122
- input_1.initForm(baseProps.refForm.current, registerEvents)
121
+ common_1.initForm(baseProps.refForm.current, registerEvents)
123
122
  }
124
123
  var n = baseProps.getModelName(refForm.current)
125
124
  var obj = {}
@@ -144,19 +143,19 @@ exports.useEdit = function (refForm, initialState, service, p2, p) {
144
143
  baseProps.load(id, p ? p.callback : undefined)
145
144
  } catch (error) {
146
145
  p2.showError(error)
147
- input_1.hideLoading(p2.loading)
146
+ common_1.hideLoading(p2.loading)
148
147
  }
149
148
  }
150
149
  }, [])
151
150
  return __assign({}, baseProps)
152
151
  }
153
- exports.useEditProps = function (props, refForm, initialState, service, p2, p) {
152
+ exports.useEditProps = function (props, refForm, initialState, service, resource, p2, p) {
154
153
  var params = react_router_1.useParams()
155
- var baseProps = exports.useCoreEdit(refForm, initialState, service, p2, p, props)
154
+ var baseProps = exports.useCoreEdit(refForm, initialState, service, resource, p2, p, props)
156
155
  react_1.useEffect(function () {
157
156
  if (refForm) {
158
157
  var registerEvents = p2.ui ? p2.ui.registerEvents : undefined
159
- input_1.initForm(baseProps.refForm.current, registerEvents)
158
+ common_1.initForm(baseProps.refForm.current, registerEvents)
160
159
  }
161
160
  var n = baseProps.getModelName(refForm.current)
162
161
  var obj = {}
@@ -182,13 +181,13 @@ exports.useEditProps = function (props, refForm, initialState, service, p2, p) {
182
181
  }, [])
183
182
  return __assign({}, baseProps)
184
183
  }
185
- exports.useEditOneProps = function (p) {
186
- return exports.useEditProps(p.props, p.refForm, p.initialState, p.service, p, p)
184
+ exports.useEditOneProps = function (resource, p) {
185
+ return exports.useEditProps(p.props, p.refForm, p.initialState, p.service, resource, p, p)
187
186
  }
188
- exports.useEditOne = function (p) {
189
- return exports.useEdit(p.refForm, p.initialState, p.service, p, p)
187
+ exports.useEditOne = function (resource, p) {
188
+ return exports.useEdit(p.refForm, p.initialState, p.service, resource, p, p)
190
189
  }
191
- exports.useCoreEdit = function (refForm, initialState, service, p1, p, props) {
190
+ exports.useCoreEdit = function (refForm, initialState, service, resource, p1, p, props) {
192
191
  var navigate = react_router_1.useNavigate()
193
192
  var _a = react_1.useState(),
194
193
  running = _a[0],
@@ -228,7 +227,6 @@ exports.useCoreEdit = function (refForm, initialState, service, p1, p, props) {
228
227
  if (form) {
229
228
  formutil_1.setReadOnly(form)
230
229
  }
231
- var resource = p1.resource.resource()
232
230
  p1.showError(
233
231
  resource.error_404,
234
232
  function () {
@@ -260,7 +258,6 @@ exports.useCoreEdit = function (refForm, initialState, service, p1, p, props) {
260
258
  if (objKeys.length === 0) {
261
259
  navigate(-1)
262
260
  } else {
263
- var resource = p1.resource.resource()
264
261
  p1.confirm(
265
262
  resource.msg_confirm_back,
266
263
  function () {
@@ -296,7 +293,6 @@ exports.useCoreEdit = function (refForm, initialState, service, p1, p, props) {
296
293
  }
297
294
  }
298
295
  var _onSave = function (isBack) {
299
- var resource = p1.resource.resource()
300
296
  if (p && p.readOnly) {
301
297
  if (flag.newMode === true) {
302
298
  p1.showError(resource.error_permission_add, undefined, resource.error_permission)
@@ -384,7 +380,6 @@ exports.useCoreEdit = function (refForm, initialState, service, p1, p, props) {
384
380
  }
385
381
  var succeed = p && p.succeed ? p.succeed : _succeed
386
382
  var _fail = function (result) {
387
- var resource = p1.resource.resource()
388
383
  var f = refForm.current
389
384
  var u = p1.ui
390
385
  if (u && f) {
@@ -411,9 +406,8 @@ exports.useCoreEdit = function (refForm, initialState, service, p1, p, props) {
411
406
  var fail = p && p.fail ? p.fail : _fail
412
407
  var _handleError = function (err) {
413
408
  if (err) {
414
- var resource = p1.resource.resource()
415
409
  setRunning(false)
416
- input_1.hideLoading(p1.loading)
410
+ common_1.hideLoading(p1.loading)
417
411
  var errHeader = resource.error
418
412
  var errMsg = resource.error_internal
419
413
  var data = err && err.response ? err.response : err
@@ -428,8 +422,7 @@ exports.useCoreEdit = function (refForm, initialState, service, p1, p, props) {
428
422
  var handleError = p && p.handleError ? p.handleError : _handleError
429
423
  var _postSave = function (r, origin, version, isPatch, backOnSave) {
430
424
  setRunning(false)
431
- var resource = p1.resource.resource()
432
- input_1.hideLoading(p1.loading)
425
+ common_1.hideLoading(p1.loading)
433
426
  var x = r
434
427
  var successMsg = resource.msg_save_success
435
428
  var newMod = flag.newMode
@@ -467,13 +460,12 @@ exports.useCoreEdit = function (refForm, initialState, service, p1, p, props) {
467
460
  }
468
461
  var postSave = p && p.postSave ? p.postSave : _postSave
469
462
  var _handleDuplicateKey = function (result) {
470
- var resource = p1.resource.resource()
471
463
  p1.showError(resource.error_duplicate_key, undefined, resource.error)
472
464
  }
473
465
  var handleDuplicateKey = p && p.handleDuplicateKey ? p.handleDuplicateKey : _handleDuplicateKey
474
466
  var _doSave = function (obj, body, version, isBack) {
475
467
  setRunning(true)
476
- input_1.showLoading(p1.loading)
468
+ common_1.showLoading(p1.loading)
477
469
  var isBackO = isBack != null && isBack !== undefined ? isBack : false
478
470
  var patchable = p ? p.patchable : true
479
471
  if (flag.newMode === false) {
@@ -506,7 +498,7 @@ exports.useCoreEdit = function (refForm, initialState, service, p1, p, props) {
506
498
  var id = _id
507
499
  if (id != null && id !== "") {
508
500
  setRunning(true)
509
- input_1.showLoading(p1.loading)
501
+ common_1.showLoading(p1.loading)
510
502
  service
511
503
  .load(id)
512
504
  .then(function (obj) {
@@ -521,12 +513,11 @@ exports.useCoreEdit = function (refForm, initialState, service, p1, p, props) {
521
513
  }
522
514
  }
523
515
  setRunning(false)
524
- input_1.hideLoading(p1.loading)
516
+ common_1.hideLoading(p1.loading)
525
517
  })
526
518
  .catch(function (err) {
527
519
  var _a, _b, _c
528
520
  var data = err && err.response ? err.response : err
529
- var resource = p1.resource.resource()
530
521
  var title = resource.error
531
522
  var msg = resource.error_internal
532
523
  if (data && data.status === 422) {
@@ -549,7 +540,7 @@ exports.useCoreEdit = function (refForm, initialState, service, p1, p, props) {
549
540
  }
550
541
  }
551
542
  setRunning(false)
552
- input_1.hideLoading(p1.loading)
543
+ common_1.hideLoading(p1.loading)
553
544
  })
554
545
  } else {
555
546
  var obj = createModel()
@@ -566,7 +557,6 @@ exports.useCoreEdit = function (refForm, initialState, service, p1, p, props) {
566
557
  back: back,
567
558
  refForm: refForm,
568
559
  ui: p1.ui,
569
- resource: p1.resource.resource(),
570
560
  flag: flag,
571
561
  running: running,
572
562
  setRunning: setRunning,
package/lib/useSearch.js CHANGED
@@ -24,7 +24,6 @@ Object.defineProperty(exports, "__esModule", { value: true })
24
24
  var react_1 = require("react")
25
25
  var common_1 = require("./common")
26
26
  var core_1 = require("./core")
27
- var input_1 = require("./input")
28
27
  var reflect_1 = require("./reflect")
29
28
  var route_1 = require("./route")
30
29
  var search_1 = require("./search")
@@ -277,8 +276,8 @@ function mergeParam(p) {
277
276
  }
278
277
  }
279
278
  exports.mergeParam = mergeParam
280
- exports.useSearch = function (refForm, initialState, service, p2, p) {
281
- var baseProps = exports.useCoreSearch(refForm, initialState, service, p2, p)
279
+ exports.useSearch = function (refForm, initialState, service, resource, p2, p) {
280
+ var baseProps = exports.useCoreSearch(refForm, initialState, service, resource, p2, p)
282
281
  react_1.useEffect(function () {
283
282
  var load = baseProps.load,
284
283
  setState = baseProps.setState,
@@ -286,7 +285,7 @@ exports.useSearch = function (refForm, initialState, service, p2, p) {
286
285
  searchError = baseProps.searchError
287
286
  if (refForm) {
288
287
  var registerEvents = p2.ui ? p2.ui.registerEvents : undefined
289
- input_1.initForm(refForm.current, registerEvents)
288
+ common_1.initForm(refForm.current, registerEvents)
290
289
  }
291
290
  if (p && p.initialize) {
292
291
  p.initialize(load, setState, component)
@@ -302,17 +301,17 @@ exports.useSearch = function (refForm, initialState, service, p2, p) {
302
301
  }, [])
303
302
  return __assign({}, baseProps)
304
303
  }
305
- exports.useSearchOneProps = function (p) {
306
- return exports.useSearch(p.refForm, p.initialState, p.service, p, p)
304
+ exports.useSearchOneProps = function (resource, p) {
305
+ return exports.useSearch(p.refForm, p.initialState, p.service, resource, p, p)
307
306
  }
308
- exports.useSearchOne = function (p) {
309
- return exports.useCoreSearch(p.refForm, p.initialState, p.service, p, p)
307
+ exports.useSearchOne = function (resource, p) {
308
+ return exports.useCoreSearch(p.refForm, p.initialState, p.service, resource, p, p)
310
309
  }
311
310
  function getName(d, n) {
312
311
  return n && n.length > 0 ? n : d
313
312
  }
314
313
  exports.getName = getName
315
- exports.useCoreSearch = function (refForm, initialState, service, p1, p2) {
314
+ exports.useCoreSearch = function (refForm, initialState, service, resource, p1, p2) {
316
315
  var p = mergeParam(p2)
317
316
  var _a = react_1.useState(),
318
317
  running = _a[0],
@@ -401,7 +400,7 @@ exports.useCoreSearch = function (refForm, initialState, service, p1, p2) {
401
400
  return
402
401
  }
403
402
  setRunning(true)
404
- input_1.showLoading(p1.loading)
403
+ common_1.showLoading(p1.loading)
405
404
  if (p && !p.ignoreUrlParam) {
406
405
  search_1.addParametersIntoUrl(s, isFirstLoad)
407
406
  }
@@ -503,9 +502,8 @@ exports.useCoreSearch = function (refForm, initialState, service, p1, p2) {
503
502
  }
504
503
  var searchError = function (err) {
505
504
  setComponent({ page: component.tmpPageIndex })
506
- var resource = p1.resource.resource()
507
505
  common_1.error(err, resource, p1.showError)
508
- input_1.hideLoading(p1.loading)
506
+ common_1.hideLoading(p1.loading)
509
507
  }
510
508
  var appendList = p && p.appendList ? p.appendList : appendListOfState
511
509
  var setList = p && p.setList ? p.setList : setListOfState
@@ -536,12 +534,12 @@ exports.useCoreSearch = function (refForm, initialState, service, p1, p2) {
536
534
  setList(results, setState)
537
535
  setComponent({ tmpPageIndex: s.page })
538
536
  if (s.limit) {
539
- var m1 = search_1.buildMessage(p1.resource.resource(), sr.list, s.limit, s.page, sr.total)
537
+ var m1 = search_1.buildMessage(resource, sr.list, s.limit, s.page, sr.total)
540
538
  p1.showMessage(m1)
541
539
  }
542
540
  }
543
541
  setRunning(false)
544
- input_1.hideLoading(p1.loading)
542
+ common_1.hideLoading(p1.loading)
545
543
  if (component.triggerSearch) {
546
544
  setComponent({ triggerSearch: false })
547
545
  resetAndSearch()
@@ -571,7 +569,6 @@ exports.useCoreSearch = function (refForm, initialState, service, p1, p2) {
571
569
  running: running,
572
570
  setRunning: setRunning,
573
571
  getCurrencyCode: getCurrencyCode,
574
- resource: p1.resource.resource(),
575
572
  setComponent: setComponent,
576
573
  component: component,
577
574
  showMessage: p1.showMessage,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-hook-core",
3
- "version": "0.4.15",
3
+ "version": "0.4.16",
4
4
  "description": "react",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./src/index.ts",
package/src/common.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { StringMap } from "./core"
1
+ import { LoadingService, StringMap } from "./core"
2
2
 
3
3
  export function messageByHttpStatus(status: number, resource: StringMap): string {
4
4
  const k = "error_" + status
@@ -26,3 +26,63 @@ export function error(err: any, resource: StringMap, ae: (msg: string, callback?
26
26
  ae(msg, undefined, title)
27
27
  }
28
28
  }
29
+
30
+ export function showLoading(loading?: LoadingService | ((firstTime?: boolean) => void)): void {
31
+ if (loading) {
32
+ if (typeof loading === "function") {
33
+ loading()
34
+ } else {
35
+ loading.showLoading()
36
+ }
37
+ }
38
+ }
39
+ export function hideLoading(loading?: LoadingService | (() => void)): void {
40
+ if (loading) {
41
+ if (typeof loading === "function") {
42
+ loading()
43
+ } else {
44
+ loading.hideLoading()
45
+ }
46
+ }
47
+ }
48
+
49
+ export function initForm(form?: HTMLFormElement, initMat?: (f: HTMLFormElement) => void): HTMLFormElement | undefined {
50
+ if (form) {
51
+ setTimeout(() => {
52
+ if (initMat) {
53
+ initMat(form)
54
+ }
55
+ focusFirstElement(form)
56
+ }, 100)
57
+ }
58
+ return form
59
+ }
60
+ export function focusFirstElement(form: HTMLFormElement): void {
61
+ let i = 0
62
+ const len = form.length
63
+ for (i = 0; i < len; i++) {
64
+ const ctrl = form[i] as HTMLInputElement
65
+ if (!(ctrl.readOnly || ctrl.disabled)) {
66
+ let nodeName = ctrl.nodeName
67
+ const type = ctrl.getAttribute("type")
68
+ if (type) {
69
+ const t = type.toUpperCase()
70
+ if (t === "BUTTON" || t === "SUBMIT") {
71
+ ctrl.focus()
72
+ }
73
+ if (nodeName === "INPUT") {
74
+ nodeName = t
75
+ }
76
+ }
77
+ if (nodeName !== "BUTTON" && nodeName !== "RESET" && nodeName !== "SUBMIT" && nodeName !== "CHECKBOX" && nodeName !== "RADIO") {
78
+ ctrl.focus()
79
+ /*
80
+ try {
81
+ ctrl.setSelectionRange(0, ctrl.value.length)
82
+ } catch (err) {}
83
+ */
84
+ return
85
+ }
86
+ }
87
+ }
88
+ }
package/src/core.ts CHANGED
@@ -20,10 +20,6 @@ export class resources {
20
20
  export interface StringMap {
21
21
  [key: string]: string
22
22
  }
23
- export interface ResourceService {
24
- resource(): StringMap
25
- }
26
-
27
23
  export interface Locale {
28
24
  id?: string
29
25
  countryCode: string
package/src/diff.ts CHANGED
@@ -1,12 +1,10 @@
1
- import { LoadingService, ResourceService } from "./core"
1
+ import { LoadingService } from "./core"
2
2
  import { clone } from "./reflect"
3
3
 
4
4
  export interface DiffParameter {
5
- resource: ResourceService
6
5
  showMessage: (msg: string, option?: string) => void
7
6
  showError: (m: string, header?: string, detail?: string, callback?: () => void) => void
8
7
  loading?: LoadingService
9
- // status?: DiffStatusConfig;
10
8
  }
11
9
  export interface BaseDiffState {
12
10
  disabled: boolean
package/src/index.ts CHANGED
@@ -5,7 +5,6 @@ export * from "./core"
5
5
  export * from "./diff"
6
6
  export * from "./edit"
7
7
  export * from "./formutil"
8
- export * from "./input"
9
8
  export * from "./reflect"
10
9
  export * from "./route"
11
10
  export * from "./search"
package/src/useEdit.ts CHANGED
@@ -1,10 +1,9 @@
1
1
  import { useEffect, useState } from "react"
2
2
  import { Params, useNavigate, useParams } from "react-router"
3
- import { messageByHttpStatus } from "./common"
4
- import { Attribute, Attributes, ErrorMessage, LoadingService, Locale, resources, ResourceService, UIService } from "./core"
3
+ import { hideLoading, initForm, messageByHttpStatus, showLoading } from "./common"
4
+ import { Attribute, Attributes, ErrorMessage, LoadingService, Locale, resources, StringMap, UIService } from "./core"
5
5
  import { createModel as createModel2 } from "./edit"
6
6
  import { focusFirstError, setReadOnly } from "./formutil"
7
- import { hideLoading, initForm, showLoading } from "./input"
8
7
  import { clone, makeDiff } from "./reflect"
9
8
  import { localeOf } from "./state"
10
9
  import { DispatchWithCallback, getModelName as getModelName2, useMergeState, useUpdate } from "./update"
@@ -50,14 +49,12 @@ export function buildId<ID>(p: Readonly<Params<string>>, keys?: string[]): ID |
50
49
  }
51
50
 
52
51
  export interface EditParameter {
53
- resource: ResourceService
54
52
  showMessage: (msg: string, option?: string) => void
55
53
  showError: (m: string, callback?: () => void, header?: string) => void
56
54
  confirm: (m2: string, yesCallback?: () => void, header?: string, btnLeftText?: string, btnRightText?: string, noCallback?: () => void) => void
57
55
  ui?: UIService
58
56
  getLocale?: (profile?: string) => Locale
59
57
  loading?: LoadingService
60
- // status?: EditStatusConfig;
61
58
  }
62
59
  export interface GenericService<T, ID, R> {
63
60
  metadata?(): Attributes | undefined
@@ -162,7 +159,7 @@ export interface HookBaseEditParameter<T, ID, S> extends BaseEditComponentParam<
162
159
  refForm: any
163
160
  initialState: S
164
161
  service: GenericService<T, ID, number | T | ErrorMessage[]>
165
- resource: ResourceService
162
+ // resource: ResourceService
166
163
  showMessage: (msg: string) => void
167
164
  showError: (m: string, callback?: () => void, header?: string) => void
168
165
  getLocale?: () => Locale
@@ -196,12 +193,12 @@ export const useEdit = <T, ID, S>(
196
193
  refForm: any,
197
194
  initialState: S,
198
195
  service: GenericService<T, ID, number | T | ErrorMessage[]>,
199
-
196
+ resource: StringMap,
200
197
  p2: EditParameter,
201
198
  p?: EditComponentParam<T, ID, S>,
202
199
  ) => {
203
200
  const params = useParams()
204
- const baseProps = useCoreEdit(refForm, initialState, service, p2, p)
201
+ const baseProps = useCoreEdit(refForm, initialState, service, resource, p2, p)
205
202
  useEffect(() => {
206
203
  if (refForm) {
207
204
  const registerEvents = p2.ui ? p2.ui.registerEvents : undefined
@@ -242,11 +239,12 @@ export const useEditProps = <T, ID, S, P>(
242
239
  refForm: any,
243
240
  initialState: S,
244
241
  service: GenericService<T, ID, number | T | ErrorMessage[]>,
242
+ resource: StringMap,
245
243
  p2: EditParameter,
246
244
  p?: EditComponentParam<T, ID, S>,
247
245
  ) => {
248
246
  const params = useParams()
249
- const baseProps = useCoreEdit<T, ID, S, P>(refForm, initialState, service, p2, p, props)
247
+ const baseProps = useCoreEdit<T, ID, S, P>(refForm, initialState, service, resource, p2, p, props)
250
248
  useEffect(() => {
251
249
  if (refForm) {
252
250
  const registerEvents = p2.ui ? p2.ui.registerEvents : undefined
@@ -277,16 +275,17 @@ export const useEditProps = <T, ID, S, P>(
277
275
  }, [])
278
276
  return { ...baseProps }
279
277
  }
280
- export const useEditOneProps = <T, ID, S, P>(p: HookPropsEditParameter<T, ID, S, P>) => {
281
- return useEditProps(p.props, p.refForm, p.initialState, p.service, p, p)
278
+ export const useEditOneProps = <T, ID, S, P>(resource: StringMap, p: HookPropsEditParameter<T, ID, S, P>) => {
279
+ return useEditProps(p.props, p.refForm, p.initialState, p.service, resource, p, p)
282
280
  }
283
- export const useEditOne = <T, ID, S>(p: HookBaseEditParameter<T, ID, S>) => {
284
- return useEdit(p.refForm, p.initialState, p.service, p, p)
281
+ export const useEditOne = <T, ID, S>(resource: StringMap, p: HookBaseEditParameter<T, ID, S>) => {
282
+ return useEdit(p.refForm, p.initialState, p.service, resource, p, p)
285
283
  }
286
284
  export const useCoreEdit = <T, ID, S, P>(
287
285
  refForm: any,
288
286
  initialState: S,
289
287
  service: GenericService<T, ID, number | T | ErrorMessage[]>,
288
+ resource: StringMap,
290
289
  p1: EditParameter,
291
290
  p?: BaseEditComponentParam<T, ID>,
292
291
  props?: P,
@@ -339,7 +338,6 @@ export const useCoreEdit = <T, ID, S, P>(
339
338
  if (form) {
340
339
  setReadOnly(form)
341
340
  }
342
- const resource = p1.resource.resource()
343
341
  p1.showError(resource.error_404, () => window.history.back, resource.error)
344
342
  }
345
343
  const handleNotFound = p && p.handleNotFound ? p.handleNotFound : _handleNotFound
@@ -367,7 +365,6 @@ export const useCoreEdit = <T, ID, S, P>(
367
365
  if (objKeys.length === 0) {
368
366
  navigate(-1)
369
367
  } else {
370
- const resource = p1.resource.resource()
371
368
  p1.confirm(
372
369
  resource.msg_confirm_back,
373
370
  () => {
@@ -405,7 +402,6 @@ export const useCoreEdit = <T, ID, S, P>(
405
402
  }
406
403
 
407
404
  const _onSave = (isBack?: boolean) => {
408
- const resource = p1.resource.resource()
409
405
  if (p && p.readOnly) {
410
406
  if (flag.newMode === true) {
411
407
  p1.showError(resource.error_permission_add, undefined, resource.error_permission)
@@ -497,7 +493,6 @@ export const useCoreEdit = <T, ID, S, P>(
497
493
  const succeed = p && p.succeed ? p.succeed : _succeed
498
494
 
499
495
  const _fail = (result: ErrorMessage[]) => {
500
- const resource = p1.resource.resource()
501
496
  const f = refForm.current
502
497
  const u = p1.ui
503
498
  if (u && f) {
@@ -525,7 +520,6 @@ export const useCoreEdit = <T, ID, S, P>(
525
520
 
526
521
  const _handleError = function (err: any) {
527
522
  if (err) {
528
- const resource = p1.resource.resource()
529
523
  setRunning(false)
530
524
  hideLoading(p1.loading)
531
525
  const errHeader = resource.error
@@ -543,7 +537,6 @@ export const useCoreEdit = <T, ID, S, P>(
543
537
 
544
538
  const _postSave = (r: number | T | ErrorMessage[], origin: T, version?: string, isPatch?: boolean, backOnSave?: boolean) => {
545
539
  setRunning(false)
546
- const resource = p1.resource.resource()
547
540
  hideLoading(p1.loading)
548
541
  const x: any = r
549
542
  const successMsg = resource.msg_save_success
@@ -583,7 +576,6 @@ export const useCoreEdit = <T, ID, S, P>(
583
576
  const postSave = p && p.postSave ? p.postSave : _postSave
584
577
 
585
578
  const _handleDuplicateKey = (result?: T) => {
586
- const resource = p1.resource.resource()
587
579
  p1.showError(resource.error_duplicate_key, undefined, resource.error)
588
580
  }
589
581
  const handleDuplicateKey = p && p.handleDuplicateKey ? p.handleDuplicateKey : _handleDuplicateKey
@@ -640,7 +632,6 @@ export const useCoreEdit = <T, ID, S, P>(
640
632
  })
641
633
  .catch((err: any) => {
642
634
  const data = err && err.response ? err.response : err
643
- const resource = p1.resource.resource()
644
635
  const title = resource.error
645
636
  let msg = resource.error_internal
646
637
  if (data && data.status === 422) {
@@ -682,7 +673,6 @@ export const useCoreEdit = <T, ID, S, P>(
682
673
  back,
683
674
  refForm,
684
675
  ui: p1.ui,
685
- resource: p1.resource.resource(),
686
676
  flag,
687
677
  running,
688
678
  setRunning,
package/src/useMessage.ts CHANGED
@@ -1,11 +1,5 @@
1
1
  import * as React from "react"
2
-
3
- interface ErrorMessage {
4
- field: string
5
- code: string
6
- param?: string | number | Date
7
- message?: string
8
- }
2
+ import { ErrorMessage } from "./core"
9
3
 
10
4
  export interface MessageState {
11
5
  message?: string
package/src/useSearch.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  import { useEffect, useState } from "react"
2
- import { error } from "./common"
3
- import { LoadingService, Locale, pageSizes, resources, ResourceService, UIService } from "./core"
4
- import { hideLoading, initForm, showLoading } from "./input"
2
+ import { error, hideLoading, initForm, showLoading } from "./common"
3
+ import { LoadingService, Locale, pageSizes, resources, StringMap, UIService } from "./core"
5
4
  import { clone } from "./reflect"
6
5
  import { buildFromUrl } from "./route"
7
6
  import {
@@ -36,7 +35,6 @@ interface Searchable extends Pagination, Sortable {
36
35
  excluding?: string[] | number[]
37
36
  }
38
37
  export interface SearchParameter {
39
- resource: ResourceService
40
38
  showMessage: (msg: string, option?: string) => void
41
39
  showError: (m: string, callback?: () => void, h?: string) => void
42
40
  ui?: UIService
@@ -325,7 +323,7 @@ export interface HookBaseSearchParameter<T, S extends Filter, ST extends SearchC
325
323
  refForm: any
326
324
  initialState: ST
327
325
  service: ((s: S, limit?: number, offset?: number | string, fields?: string[]) => Promise<SearchResult<T>>) | SearchService<T, S>
328
- resource: ResourceService
326
+ // resource: ResourceService
329
327
  showMessage: (msg: string) => void
330
328
  showError: (m: string, callback?: () => void, header?: string) => void
331
329
  getLocale?: () => Locale
@@ -411,10 +409,11 @@ export const useSearch = <T, S extends Filter, ST extends SearchComponentState<T
411
409
  refForm: any,
412
410
  initialState: ST,
413
411
  service: ((s: S, limit?: number, offset?: number | string, fields?: string[]) => Promise<SearchResult<T>>) | SearchService<T, S>,
412
+ resource: StringMap,
414
413
  p2: SearchParameter,
415
414
  p?: InitSearchComponentParam<T, S, ST>,
416
415
  ) => {
417
- const baseProps = useCoreSearch(refForm, initialState, service, p2, p)
416
+ const baseProps = useCoreSearch(refForm, initialState, service, resource, p2, p)
418
417
 
419
418
  useEffect(() => {
420
419
  const { load, setState, component, searchError } = baseProps
@@ -437,11 +436,14 @@ export const useSearch = <T, S extends Filter, ST extends SearchComponentState<T
437
436
  }, [])
438
437
  return { ...baseProps }
439
438
  }
440
- export const useSearchOneProps = <T, S extends Filter, ST extends SearchComponentState<T, S>, P>(p: HookPropsSearchParameter<T, S, ST, P>) => {
441
- return useSearch(p.refForm, p.initialState, p.service, p, p)
439
+ export const useSearchOneProps = <T, S extends Filter, ST extends SearchComponentState<T, S>, P>(
440
+ resource: StringMap,
441
+ p: HookPropsSearchParameter<T, S, ST, P>,
442
+ ) => {
443
+ return useSearch(p.refForm, p.initialState, p.service, resource, p, p)
442
444
  }
443
- export const useSearchOne = <T, S extends Filter, ST extends SearchComponentState<T, S>>(p: HookBaseSearchParameter<T, S, ST>) => {
444
- return useCoreSearch(p.refForm, p.initialState, p.service, p, p)
445
+ export const useSearchOne = <T, S extends Filter, ST extends SearchComponentState<T, S>>(resource: StringMap, p: HookBaseSearchParameter<T, S, ST>) => {
446
+ return useCoreSearch(p.refForm, p.initialState, p.service, resource, p, p)
445
447
  }
446
448
 
447
449
  export function getName(d: string, n?: string): string {
@@ -451,6 +453,7 @@ export const useCoreSearch = <T, S extends Filter, ST>(
451
453
  refForm: any,
452
454
  initialState: ST,
453
455
  service: ((s: S, limit?: number, offset?: number | string, fields?: string[]) => Promise<SearchResult<T>>) | SearchService<T, S>,
456
+ resource: StringMap,
454
457
  p1: SearchParameter,
455
458
  p2?: SearchComponentParam<T, S>,
456
459
  ) => {
@@ -655,7 +658,6 @@ export const useCoreSearch = <T, S extends Filter, ST>(
655
658
 
656
659
  const searchError = (err: any): void => {
657
660
  setComponent({ page: component.tmpPageIndex })
658
- const resource = p1.resource.resource()
659
661
  error(err, resource, p1.showError)
660
662
  hideLoading(p1.loading)
661
663
  }
@@ -688,7 +690,7 @@ export const useCoreSearch = <T, S extends Filter, ST>(
688
690
  setList(results, setState as any)
689
691
  setComponent({ tmpPageIndex: s.page })
690
692
  if (s.limit) {
691
- const m1 = buildMessage(p1.resource.resource(), sr.list, s.limit, s.page, sr.total)
693
+ const m1 = buildMessage(resource, sr.list, s.limit, s.page, sr.total)
692
694
  p1.showMessage(m1)
693
695
  }
694
696
  }
@@ -726,7 +728,6 @@ export const useCoreSearch = <T, S extends Filter, ST>(
726
728
  running,
727
729
  setRunning,
728
730
  getCurrencyCode,
729
- resource: p1.resource.resource(),
730
731
  setComponent,
731
732
  component,
732
733
  showMessage: p1.showMessage,
package/tsconfig.json CHANGED
@@ -13,18 +13,8 @@
13
13
  "declaration": false,
14
14
  "experimentalDecorators": false,
15
15
  "removeComments": true,
16
- "lib": [
17
- "dom",
18
- "es6",
19
- "es7",
20
- "es2017",
21
- "es2022"
22
- ]
16
+ "lib": ["dom", "es6", "es7", "es2017"]
23
17
  },
24
- "include": [
25
- "src/**/*.ts"
26
- ],
27
- "exclude": [
28
- "node_modules"
29
- ]
18
+ "include": ["src/**/*.ts"],
19
+ "exclude": ["node_modules"]
30
20
  }
package/lib/input.js DELETED
@@ -1,118 +0,0 @@
1
- "use strict"
2
- Object.defineProperty(exports, "__esModule", { value: true })
3
- function getResource(p) {
4
- var x = p
5
- if (x.value && x.format && typeof x.value === "function") {
6
- return x
7
- } else {
8
- return x.resource
9
- }
10
- }
11
- exports.getResource = getResource
12
- function getAutoSearch(p) {
13
- var x = p
14
- if (x.value && x.format && typeof x.value === "function") {
15
- return true
16
- }
17
- return x.auto
18
- }
19
- exports.getAutoSearch = getAutoSearch
20
- function getUIService(p, ui0) {
21
- if (ui0) {
22
- return ui0
23
- }
24
- return p.ui
25
- }
26
- exports.getUIService = getUIService
27
- function getLoadingFunc(p, ui0) {
28
- if (ui0) {
29
- return ui0
30
- }
31
- return p.loading
32
- }
33
- exports.getLoadingFunc = getLoadingFunc
34
- function getMsgFunc(p, showMsg) {
35
- if (showMsg) {
36
- return showMsg
37
- }
38
- return p.showMessage
39
- }
40
- exports.getMsgFunc = getMsgFunc
41
- function getConfirmFunc(p, cf) {
42
- if (cf) {
43
- return cf
44
- }
45
- return p.confirm
46
- }
47
- exports.getConfirmFunc = getConfirmFunc
48
- function getLocaleFunc(p, getLoc) {
49
- if (getLoc) {
50
- return getLoc
51
- }
52
- return p.getLocale
53
- }
54
- exports.getLocaleFunc = getLocaleFunc
55
- function getErrorFunc(p, showErr) {
56
- if (showErr) {
57
- return showErr
58
- }
59
- return p.showError
60
- }
61
- exports.getErrorFunc = getErrorFunc
62
- function showLoading(loading) {
63
- if (loading) {
64
- if (typeof loading === "function") {
65
- loading()
66
- } else {
67
- loading.showLoading()
68
- }
69
- }
70
- }
71
- exports.showLoading = showLoading
72
- function hideLoading(loading) {
73
- if (loading) {
74
- if (typeof loading === "function") {
75
- loading()
76
- } else {
77
- loading.hideLoading()
78
- }
79
- }
80
- }
81
- exports.hideLoading = hideLoading
82
- function initForm(form, initMat) {
83
- if (form) {
84
- setTimeout(function () {
85
- if (initMat) {
86
- initMat(form)
87
- }
88
- focusFirstElement(form)
89
- }, 100)
90
- }
91
- return form
92
- }
93
- exports.initForm = initForm
94
- function focusFirstElement(form) {
95
- var i = 0
96
- var len = form.length
97
- for (i = 0; i < len; i++) {
98
- var ctrl = form[i]
99
- if (!(ctrl.readOnly || ctrl.disabled)) {
100
- var nodeName = ctrl.nodeName
101
- var type = ctrl.getAttribute("type")
102
- if (type) {
103
- var t = type.toUpperCase()
104
- if (t === "BUTTON" || t === "SUBMIT") {
105
- ctrl.focus()
106
- }
107
- if (nodeName === "INPUT") {
108
- nodeName = t
109
- }
110
- }
111
- if (nodeName !== "BUTTON" && nodeName !== "RESET" && nodeName !== "SUBMIT" && nodeName !== "CHECKBOX" && nodeName !== "RADIO") {
112
- ctrl.focus()
113
- return
114
- }
115
- }
116
- }
117
- }
118
- exports.focusFirstElement = focusFirstElement
package/src/input.ts DELETED
@@ -1,143 +0,0 @@
1
- import { LoadingService, Locale, ResourceService, UIService } from "./core"
2
-
3
- interface ResourceInput {
4
- resource: ResourceService
5
- }
6
- export function getResource(p: ResourceService | ResourceInput): ResourceService {
7
- const x: any = p
8
- if (x.value && x.format && typeof x.value === "function") {
9
- return x
10
- } else {
11
- return x.resource
12
- }
13
- }
14
- interface ShortSearchParameter {
15
- auto?: boolean
16
- }
17
- export function getAutoSearch(p: ResourceService | ShortSearchParameter): boolean {
18
- const x: any = p
19
- if (x.value && x.format && typeof x.value === "function") {
20
- return true
21
- }
22
- return x.auto
23
- }
24
- interface UIInput {
25
- ui?: UIService
26
- }
27
- export function getUIService(p: ResourceService | UIInput, ui0?: UIService): UIService {
28
- if (ui0) {
29
- return ui0
30
- }
31
- return (p as any).ui
32
- }
33
- interface LoadingInput {
34
- loading?: LoadingService
35
- }
36
- export function getLoadingFunc(p: ResourceService | LoadingInput, ui0?: LoadingService): LoadingService {
37
- if (ui0) {
38
- return ui0
39
- }
40
- return (p as any).loading
41
- }
42
- interface ShowMessageInput {
43
- showMessage: (msg: string, option?: string) => void
44
- }
45
- export function getMsgFunc(p: ResourceService | ShowMessageInput, showMsg?: (msg: string, option?: string) => void): (msg: string) => void {
46
- if (showMsg) {
47
- return showMsg
48
- }
49
- return (p as any).showMessage
50
- }
51
- interface ConfirmInput {
52
- confirm: (m2: string, yesCallback?: () => void, header?: string, btnLeftText?: string, btnRightText?: string, noCallback?: () => void) => void
53
- }
54
- export function getConfirmFunc(
55
- p: ResourceService | ConfirmInput,
56
- cf?: (m2: string, yesCallback?: () => void, header?: string, btnLeftText?: string, btnRightText?: string, noCallback?: () => void) => void,
57
- ): (m2: string, yesCallback?: () => void, header?: string, btnLeftText?: string, btnRightText?: string, noCallback?: () => void) => void {
58
- if (cf) {
59
- return cf
60
- }
61
- return (p as any).confirm
62
- }
63
- interface GetLocaleInput {
64
- getLocale?: (profile?: string) => Locale
65
- }
66
- export function getLocaleFunc(p: ResourceService | GetLocaleInput, getLoc?: () => Locale): () => Locale {
67
- if (getLoc) {
68
- return getLoc
69
- }
70
- return (p as any).getLocale
71
- }
72
- interface ShowErrorInput {
73
- showError: (m: string, callback?: () => void, header?: string) => void
74
- }
75
- export function getErrorFunc(
76
- p: ResourceService | ShowErrorInput,
77
- showErr?: (m: string, callback?: () => void, header?: string) => void,
78
- ): (m: string, callback?: () => void, header?: string) => void {
79
- if (showErr) {
80
- return showErr
81
- }
82
- return (p as any).showError
83
- }
84
-
85
- export function showLoading(loading?: LoadingService | ((firstTime?: boolean) => void)): void {
86
- if (loading) {
87
- if (typeof loading === "function") {
88
- loading()
89
- } else {
90
- loading.showLoading()
91
- }
92
- }
93
- }
94
- export function hideLoading(loading?: LoadingService | (() => void)): void {
95
- if (loading) {
96
- if (typeof loading === "function") {
97
- loading()
98
- } else {
99
- loading.hideLoading()
100
- }
101
- }
102
- }
103
-
104
- export function initForm(form?: HTMLFormElement, initMat?: (f: HTMLFormElement) => void): HTMLFormElement | undefined {
105
- if (form) {
106
- setTimeout(() => {
107
- if (initMat) {
108
- initMat(form)
109
- }
110
- focusFirstElement(form)
111
- }, 100)
112
- }
113
- return form
114
- }
115
- export function focusFirstElement(form: HTMLFormElement): void {
116
- let i = 0
117
- const len = form.length
118
- for (i = 0; i < len; i++) {
119
- const ctrl = form[i] as HTMLInputElement
120
- if (!(ctrl.readOnly || ctrl.disabled)) {
121
- let nodeName = ctrl.nodeName
122
- const type = ctrl.getAttribute("type")
123
- if (type) {
124
- const t = type.toUpperCase()
125
- if (t === "BUTTON" || t === "SUBMIT") {
126
- ctrl.focus()
127
- }
128
- if (nodeName === "INPUT") {
129
- nodeName = t
130
- }
131
- }
132
- if (nodeName !== "BUTTON" && nodeName !== "RESET" && nodeName !== "SUBMIT" && nodeName !== "CHECKBOX" && nodeName !== "RADIO") {
133
- ctrl.focus()
134
- /*
135
- try {
136
- ctrl.setSelectionRange(0, ctrl.value.length)
137
- } catch (err) {}
138
- */
139
- return
140
- }
141
- }
142
- }
143
- }