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