react-hook-core 0.4.6 → 0.4.7

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/src/core.ts CHANGED
@@ -1,265 +1,103 @@
1
- import { Params } from 'react-router';
2
- import { focusFirstElement } from './formutil';
1
+ import { Params } from "react-router"
2
+ import { focusFirstElement } from "./formutil"
3
3
 
4
- export const pageSizes = [12, 24, 60, 100, 120, 180, 300, 600];
4
+ export interface LoadingService {
5
+ showLoading(firstTime?: boolean): void
6
+ hideLoading(): void
7
+ }
8
+ export const pageSizes = [12, 24, 60, 100, 120, 180, 300, 600]
5
9
  // tslint:disable-next-line:class-name
6
10
  export class resources {
7
- static phone = / |-|\.|\(|\)/g;
8
- static _cache: any = {};
9
- static cache = true;
10
- static fields = "fields";
11
- static page = "page";
12
- static limit = "limit";
13
- static defaultLimit = 24;
14
- static limits = pageSizes;
15
- static pageMaxSize = 7;
16
- }
17
- export const size = pageSizes;
18
- export interface ModelMap {
19
- [key: string]: any;
20
- }
21
- export interface PageChange {
22
- page: number; // currentPage
23
- size: number; // itemsPerPage
24
- }
25
- export interface ModelProps {
26
- setGlobalState?: (m: ModelMap) => void;
27
- shouldBeCustomized?: boolean;
28
- }
29
- export interface EditPermission {
30
- addable?: boolean;
31
- readOnly?: boolean;
32
- deletable?: boolean;
33
- }
34
- export interface SearchPermission {
35
- viewable?: boolean;
36
- addable?: boolean;
37
- editable?: boolean;
38
- deletable?: boolean;
39
- approvable?: boolean;
40
- }
41
- export interface SearchParameter {
42
- resource: ResourceService;
43
- showMessage: (msg: string, option?: string) => void;
44
- showError: (m: string, callback?: () => void, h?: string) => void;
45
- ui?: UIService;
46
- getLocale?: (profile?: string) => Locale;
47
- loading?: LoadingService;
48
- auto?: boolean;
49
- }
50
- /*
51
- export interface EditStatusConfig {
52
- duplicate_key: number | string;
53
- not_found: number | string;
54
- success: number | string;
55
- version_error: number | string;
56
- error?: number | string;
57
- data_corrupt?: number | string;
58
- }
59
- export function createEditStatus(status?: EditStatusConfig): EditStatusConfig {
60
- if (status) {
61
- return status;
62
- }
63
- const s: EditStatusConfig = {
64
- duplicate_key: 0,
65
- not_found: 0,
66
- success: 1,
67
- version_error: -1,
68
- error: 2,
69
- data_corrupt: 4
70
- };
71
- return s;
72
- }
73
- export interface DiffStatusConfig {
74
- not_found: number | string;
75
- success: number | string;
76
- version_error: number | string;
77
- error?: number | string;
78
- }
79
- export function createDiffStatus(status?: DiffStatusConfig): DiffStatusConfig {
80
- if (status) {
81
- return status;
82
- }
83
- const s: DiffStatusConfig = {
84
- not_found: 0,
85
- success: 1,
86
- version_error: 2,
87
- error: 4
88
- };
89
- return s;
90
- }
91
- */
92
- export interface Filter {
93
- q?: string;
94
- page?: number;
95
- limit: number;
96
- firstLimit?: number;
97
- fields?: string[];
98
- sort?: string;
99
- }
100
- export interface SearchResult<T> {
101
- total?: number;
102
- list: T[];
103
- next?: string;
104
- last?: boolean;
105
- }
106
- export interface SearchState<T, S extends Filter> {
107
- model?: S;
108
- q?: string;
109
- list?: T[];
110
- }
111
- export interface SearchService<T, S extends Filter> {
112
- keys?(): string[];
113
- search(s: S, limit?: number, offset?: number | string, fields?: string[]): Promise<SearchResult<T>>;
114
- }
115
- export interface ViewParameter {
116
- resource: ResourceService;
117
- showError: (m: string, header?: string, detail?: string, callback?: () => void) => void;
118
- getLocale?: (profile?: string) => Locale;
119
- loading?: LoadingService;
120
- }
121
- export interface ViewService<T, ID> {
122
- metadata?(): Attributes | undefined;
123
- keys?(): string[];
124
- load(id: ID, ctx?: any): Promise<T | null>;
125
- }
126
-
127
- export interface DiffParameter {
128
- resource: ResourceService;
129
- showMessage: (msg: string, option?: string) => void;
130
- showError: (m: string, header?: string, detail?: string, callback?: () => void) => void;
131
- loading?: LoadingService;
132
- // status?: DiffStatusConfig;
133
- }
134
- export interface BaseDiffState {
135
- disabled: boolean;
136
- }
137
- export interface DiffModel<T, ID> {
138
- id?: ID;
139
- origin?: T;
140
- value: T;
141
- }
142
- export interface DiffModel<T, ID> {
143
- id?: ID;
144
- origin?: T;
145
- value: T;
146
- }
147
- export interface ApprService<ID> {
148
- approve(id: ID, ctx?: any): Promise<number | string>;
149
- reject(id: ID, ctx?: any): Promise<number | string>;
150
- }
151
- export interface DiffService<T, ID> {
152
- keys(): string[];
153
- diff(id: ID, ctx?: any): Promise<DiffModel<T, ID>>;
154
- }
155
- export interface DiffApprService<T, ID> extends DiffService<T, ID>, ApprService<ID> {
156
- }
157
- export interface DiffState<T> {
158
- origin: T;
159
- value: T;
160
- disabled: boolean;
161
- }
11
+ static phone = / |-|\.|\(|\)/g
12
+ static _cache: any = {}
13
+ static cache = true
14
+ static fields = "fields"
15
+ static page = "page"
16
+ static limit = "limit"
17
+ static defaultLimit = 24
18
+ static limits = pageSizes
19
+ static pageMaxSize = 7
20
+ }
21
+ export const size = pageSizes
162
22
 
163
23
  export function getCurrencyCode(form?: HTMLFormElement | null): string | undefined {
164
24
  if (form) {
165
- const x = form.getAttribute('currency-code');
25
+ const x = form.getAttribute("currency-code")
166
26
  if (x) {
167
- return x;
27
+ return x
168
28
  }
169
29
  }
170
- return undefined;
30
+ return undefined
171
31
  }
172
32
  export function removePhoneFormat(phone: string): string {
173
33
  if (phone) {
174
- return phone.replace(resources.phone, '');
34
+ return phone.replace(resources.phone, "")
175
35
  } else {
176
- return phone;
36
+ return phone
177
37
  }
178
38
  }
179
39
  export interface StringMap {
180
- [key: string]: string;
40
+ [key: string]: string
181
41
  }
182
42
  export interface ResourceService {
183
- resource(): StringMap;
184
- value(key: string, param?: any): string;
185
- format(f: string, ...args: any[]): string;
186
- }
187
- export interface Message {
188
- message: string;
189
- title: string;
190
- yes?: string;
191
- no?: string;
192
- }
193
- export function getString(key: string, gv: StringMap | ((key: string) => string)): string {
194
- if (typeof gv === 'function') {
195
- return gv(key);
196
- } else {
197
- return gv[key];
198
- }
199
- }
200
- export function message(gv: StringMap | ((key: string) => string), msg: string, title?: string, yes?: string, no?: string): Message {
201
- const m2 = (msg && msg.length > 0 ? getString(msg, gv) : '');
202
- const m: Message = { message: m2, title: '' };
203
- if (title && title.length > 0) {
204
- m.title = getString(title, gv);
205
- }
206
- if (yes && yes.length > 0) {
207
- m.yes = getString(yes, gv);
208
- }
209
- if (no && no.length > 0) {
210
- m.no = getString(no, gv);
211
- }
212
- return m;
213
- }
214
- export function messageByHttpStatus(status: number, gv: StringMap | ((key: string) => string)): string {
215
- const k = 'error_' + status;
216
- let msg = getString(k, gv);
217
- if (!msg || msg.length === 0) {
218
- msg = getString('error_500', gv);
219
- }
220
- return msg;
43
+ resource(): StringMap
44
+ value(key: string, param?: any): string
45
+ format(f: string, ...args: any[]): string
221
46
  }
222
47
 
223
48
  export interface Locale {
224
- id?: string;
225
- countryCode: string;
226
- dateFormat: string;
227
- firstDayOfWeek: number;
228
- decimalSeparator: string;
229
- groupSeparator: string;
230
- decimalDigits: number;
231
- currencyCode: string;
232
- currencySymbol: string;
233
- currencyPattern: number;
234
- currencySample?: string;
235
- }
236
- export interface LoadingService {
237
- showLoading(firstTime?: boolean): void;
238
- hideLoading(): void;
49
+ id?: string
50
+ countryCode: string
51
+ dateFormat: string
52
+ firstDayOfWeek: number
53
+ decimalSeparator: string
54
+ groupSeparator: string
55
+ decimalDigits: number
56
+ currencyCode: string
57
+ currencySymbol: string
58
+ currencyPattern: number
59
+ currencySample?: string
239
60
  }
240
61
  export interface ErrorMessage {
241
- field: string;
242
- code: string;
243
- param?: string | number | Date;
244
- message?: string;
62
+ field: string
63
+ code: string
64
+ param?: string | number | Date
65
+ message?: string
245
66
  }
246
67
  export interface UIService {
247
- getValue(el: HTMLInputElement, locale?: Locale, currencyCode?: string): string | number | boolean | null | undefined;
68
+ getValue(el: HTMLInputElement, locale?: Locale, currencyCode?: string): string | number | boolean | null | undefined
248
69
  // decodeFromForm(form: HTMLFormElement, locale?: Locale, currencyCode?: string | null): any;
249
70
 
250
- validateForm(form?: HTMLFormElement, locale?: Locale, focusFirst?: boolean, scroll?: boolean): boolean;
251
- removeFormError(form: HTMLFormElement): void;
252
- removeError(el: HTMLInputElement): void;
253
- showFormError(form?: HTMLFormElement, errors?: ErrorMessage[], focusFirst?: boolean): ErrorMessage[];
254
- buildErrorMessage(errors: ErrorMessage[]): string;
71
+ validateForm(form?: HTMLFormElement, locale?: Locale, focusFirst?: boolean, scroll?: boolean): boolean
72
+ removeFormError(form: HTMLFormElement): void
73
+ removeError(el: HTMLInputElement): void
74
+ showFormError(form?: HTMLFormElement, errors?: ErrorMessage[], focusFirst?: boolean): ErrorMessage[]
75
+ buildErrorMessage(errors: ErrorMessage[]): string
255
76
 
256
- registerEvents?(form: HTMLFormElement): void;
77
+ registerEvents?(form: HTMLFormElement): void
257
78
  }
258
79
 
259
- export type DataType = 'ObjectId' | 'date' | 'datetime' | 'time'
260
- | 'boolean' | 'number' | 'integer' | 'string' | 'text'
261
- | 'object' | 'array' | 'binary'
262
- | 'primitives' | 'booleans' | 'numbers' | 'integers' | 'strings' | 'dates' | 'datetimes' | 'times';
80
+ export type DataType =
81
+ | "ObjectId"
82
+ | "date"
83
+ | "datetime"
84
+ | "time"
85
+ | "boolean"
86
+ | "number"
87
+ | "integer"
88
+ | "string"
89
+ | "text"
90
+ | "object"
91
+ | "array"
92
+ | "binary"
93
+ | "primitives"
94
+ | "booleans"
95
+ | "numbers"
96
+ | "integers"
97
+ | "strings"
98
+ | "dates"
99
+ | "datetimes"
100
+ | "times"
263
101
  /*
264
102
  export interface Metadata {
265
103
  name?: string;
@@ -268,72 +106,72 @@ export interface Metadata {
268
106
  }
269
107
  */
270
108
  export interface Attribute {
271
- name?: string;
272
- type?: DataType;
273
- key?: boolean;
274
- version?: boolean;
275
- typeof?: Attributes;
109
+ name?: string
110
+ type?: DataType
111
+ key?: boolean
112
+ version?: boolean
113
+ typeof?: Attributes
276
114
  }
277
115
  export interface Attributes {
278
- [key: string]: Attribute;
116
+ [key: string]: Attribute
279
117
  }
280
118
 
281
119
  export function buildKeys(attributes: Attributes): string[] {
282
120
  if (!attributes) {
283
- return [];
121
+ return []
284
122
  }
285
- const ks = Object.keys(attributes);
286
- const ps = [];
123
+ const ks = Object.keys(attributes)
124
+ const ps = []
287
125
  for (const k of ks) {
288
- const attr: Attribute = attributes[k];
126
+ const attr: Attribute = attributes[k]
289
127
  if (attr.key === true) {
290
- ps.push(k);
128
+ ps.push(k)
291
129
  }
292
130
  }
293
- return ps;
131
+ return ps
294
132
  }
295
133
  type Readonly<T> = {
296
- readonly [P in keyof T]: T[P];
297
- };
134
+ readonly [P in keyof T]: T[P]
135
+ }
298
136
  export function buildId<ID>(p: Readonly<Params<string>>, keys?: string[]): ID | null {
299
137
  if (!keys || keys.length === 0 || keys.length === 1) {
300
138
  if (keys && keys.length === 1) {
301
139
  if (p[keys[0]]) {
302
- return p[keys[0]] as any;
140
+ return p[keys[0]] as any
303
141
  }
304
142
  }
305
- return p['id'] as any;
143
+ return p["id"] as any
306
144
  }
307
- const id: any = {};
145
+ const id: any = {}
308
146
  for (const key of keys) {
309
- let v = p[key];
147
+ let v = p[key]
310
148
  if (!v) {
311
- v = p[key];
149
+ v = p[key]
312
150
  if (!v) {
313
- return null;
151
+ return null
314
152
  }
315
153
  }
316
- id[key] = v;
154
+ id[key] = v
317
155
  }
318
- return id;
156
+ return id
319
157
  }
320
158
  export const datetimeToString = (inputDate: Date) => {
321
- const date = new Date(inputDate);
322
- const year = date.getFullYear();
323
- const month = String(date.getMonth() + 1).padStart(2, '0');
324
- const day = String(date.getDate()).padStart(2, '0');
325
- const hours = String(date.getHours()).padStart(2, '0');
326
- const minutes = String(date.getMinutes()).padStart(2, '0');
327
- const seconds = String(date.getSeconds()).padStart(2, '0');
328
- return `${year}-${month}-${day}T${hours}:${minutes}:${seconds}`;
329
- };
159
+ const date = new Date(inputDate)
160
+ const year = date.getFullYear()
161
+ const month = String(date.getMonth() + 1).padStart(2, "0")
162
+ const day = String(date.getDate()).padStart(2, "0")
163
+ const hours = String(date.getHours()).padStart(2, "0")
164
+ const minutes = String(date.getMinutes()).padStart(2, "0")
165
+ const seconds = String(date.getSeconds()).padStart(2, "0")
166
+ return `${year}-${month}-${day}T${hours}:${minutes}:${seconds}`
167
+ }
330
168
 
331
169
  export const dateToString = (inputDate: Date) => {
332
- const year = inputDate.getFullYear();
333
- const month = String(inputDate.getMonth() + 1).padStart(2, '0');
334
- const day = String(inputDate.getDate()).padStart(2, '0');
335
- return `${year}-${month}-${day}`;
336
- };
170
+ const year = inputDate.getFullYear()
171
+ const month = String(inputDate.getMonth() + 1).padStart(2, "0")
172
+ const day = String(inputDate.getDate()).padStart(2, "0")
173
+ return `${year}-${month}-${day}`
174
+ }
337
175
  /*
338
176
  export function formatFax(value: string) {
339
177
  return formatter.formatFax(value);
@@ -394,111 +232,96 @@ export function initForm(form?: HTMLFormElement, initMat?: (f: HTMLFormElement)
394
232
  if (form) {
395
233
  setTimeout(() => {
396
234
  if (initMat) {
397
- initMat(form);
235
+ initMat(form)
398
236
  }
399
- focusFirstElement(form);
400
- }, 100);
401
- }
402
- return form;
403
- }
404
- export function error(err: any, gv: StringMap | ((key: string) => string), ae: (msg: string, callback?: () => void, header?: string) => void) {
405
- const title = getString('error', gv);
406
- let msg = getString('error_internal', gv);
407
- if (!err) {
408
- ae(msg, undefined, title);
409
- return;
410
- }
411
- const data = err && err.response ? err.response : err;
412
- if (data) {
413
- const status = data.status;
414
- if (status && !isNaN(status)) {
415
- msg = messageByHttpStatus(status, gv);
416
- }
417
- ae(msg, undefined, title);
418
- } else {
419
- ae(msg, undefined, title);
237
+ focusFirstElement(form)
238
+ }, 100)
420
239
  }
240
+ return form
421
241
  }
422
242
  export function getName(d: string, n?: string): string {
423
- return (n && n.length > 0 ? n : d);
243
+ return n && n.length > 0 ? n : d
424
244
  }
425
245
  export function getModelName(form?: HTMLFormElement | null, name?: string): string {
426
246
  if (form) {
427
- const a = form.getAttribute('model-name');
247
+ const a = form.getAttribute("model-name")
428
248
  if (a && a.length > 0) {
429
- return a;
249
+ return a
430
250
  }
431
- const b = form.name;
251
+ const b = form.name
432
252
  if (b) {
433
- if (b.endsWith('Form')) {
434
- return b.substr(0, b.length - 4);
253
+ if (b.endsWith("Form")) {
254
+ return b.substr(0, b.length - 4)
435
255
  }
436
- return b;
256
+ return b
437
257
  }
438
258
  }
439
259
  if (name && name.length > 0) {
440
- return name;
260
+ return name
441
261
  }
442
- return '';
262
+ return ""
443
263
  }
444
264
 
445
265
  export const scrollToFocus = (e: any, isUseTimeOut?: boolean) => {
446
266
  try {
447
- const element = e.target as HTMLInputElement;
448
- const form = element.form;
267
+ const element = e.target as HTMLInputElement
268
+ const form = element.form
449
269
  if (form) {
450
- const container = form.childNodes[1] as HTMLElement;
451
- const elementRect = element.getBoundingClientRect();
452
- const absoluteElementTop = elementRect.top + window.pageYOffset;
453
- const middle = absoluteElementTop - (window.innerHeight / 2);
454
- const scrollTop = container.scrollTop;
455
- const timeOut = isUseTimeOut ? 300 : 0;
456
- const isChrome = navigator.userAgent.search('Chrome') > 0;
270
+ const container = form.childNodes[1] as HTMLElement
271
+ const elementRect = element.getBoundingClientRect()
272
+ const absoluteElementTop = elementRect.top + window.pageYOffset
273
+ const middle = absoluteElementTop - window.innerHeight / 2
274
+ const scrollTop = container.scrollTop
275
+ const timeOut = isUseTimeOut ? 300 : 0
276
+ const isChrome = navigator.userAgent.search("Chrome") > 0
457
277
  setTimeout(() => {
458
278
  if (isChrome) {
459
- const scrollPosition = scrollTop === 0 ? (elementRect.top + 64) : (scrollTop + middle);
460
- container.scrollTo(0, Math.abs(scrollPosition));
279
+ const scrollPosition = scrollTop === 0 ? elementRect.top + 64 : scrollTop + middle
280
+ container.scrollTo(0, Math.abs(scrollPosition))
461
281
  } else {
462
- container.scrollTo(0, Math.abs(scrollTop + middle));
282
+ container.scrollTo(0, Math.abs(scrollTop + middle))
463
283
  }
464
- }, timeOut);
284
+ }, timeOut)
465
285
  }
466
286
  } catch (e) {
467
- console.log(e);
287
+ console.log(e)
468
288
  }
469
- };
289
+ }
470
290
  export interface LoadingParameter {
471
- loading?: LoadingService;
291
+ loading?: LoadingService
472
292
  }
473
293
  export function showLoading(s?: LoadingService): void {
474
294
  if (s) {
475
- s.showLoading();
295
+ s.showLoading()
476
296
  }
477
297
  }
478
298
  export function hideLoading(s?: LoadingService): void {
479
299
  if (s) {
480
- s.hideLoading();
300
+ s.hideLoading()
481
301
  }
482
302
  }
483
303
  export interface UIParameter {
484
- ui?: UIService;
304
+ ui?: UIService
485
305
  }
486
306
  export function getRemoveError(u?: UIParameter, rmErr?: (el: HTMLInputElement) => void): ((el: HTMLInputElement) => void) | undefined {
487
307
  if (rmErr) {
488
- return rmErr;
308
+ return rmErr
489
309
  }
490
- return (u && u.ui ? u.ui.removeError : undefined);
310
+ return u && u.ui ? u.ui.removeError : undefined
491
311
  }
492
312
  export function removeFormError(u?: UIParameter, f?: HTMLFormElement): void {
493
313
  if (f && u && u.ui) {
494
- u.ui.removeFormError(f);
314
+ u.ui.removeFormError(f)
495
315
  }
496
316
  }
497
- export function getValidateForm(u?: UIParameter, vf?: (form: HTMLFormElement, locale?: Locale, focusFirst?: boolean, scroll?: boolean) => boolean): ((form: HTMLFormElement, locale?: Locale, focusFirst?: boolean, scroll?: boolean) => boolean) | undefined {
317
+ export function getValidateForm(
318
+ u?: UIParameter,
319
+ vf?: (form: HTMLFormElement, locale?: Locale, focusFirst?: boolean, scroll?: boolean) => boolean,
320
+ ): ((form: HTMLFormElement, locale?: Locale, focusFirst?: boolean, scroll?: boolean) => boolean) | undefined {
498
321
  if (vf) {
499
- return vf;
322
+ return vf
500
323
  }
501
- return (u && u.ui ? u.ui.validateForm : undefined);
324
+ return u && u.ui ? u.ui.validateForm : undefined
502
325
  }
503
326
  /*
504
327
  export function getDecodeFromForm(u?: UIParameter, d?: (form: HTMLFormElement, locale?: Locale, currencyCode?: string) => any): ((form: HTMLFormElement, locale?: Locale, currencyCode?: string) => any) | undefined {