react-hook-core 0.1.4 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/core.ts CHANGED
@@ -1,5 +1,5 @@
1
- import {RouteComponentProps} from 'react-router';
2
- import {focusFirstElement} from './formutil';
1
+ import { Params } from 'react-router';
2
+ import { focusFirstElement } from './formutil';
3
3
 
4
4
  export const pageSizes = [12, 24, 60, 100, 120, 180, 300, 600];
5
5
  export interface ModelMap {
@@ -35,12 +35,12 @@ export interface SearchParameter {
35
35
  auto?: boolean;
36
36
  }
37
37
  export interface EditStatusConfig {
38
- duplicate_key: number|string;
39
- not_found: number|string;
40
- success: number|string;
41
- version_error: number|string;
42
- error?: number|string;
43
- data_corrupt?: number|string;
38
+ duplicate_key: number | string;
39
+ not_found: number | string;
40
+ success: number | string;
41
+ version_error: number | string;
42
+ error?: number | string;
43
+ data_corrupt?: number | string;
44
44
  }
45
45
  export function createEditStatus(status?: EditStatusConfig): EditStatusConfig {
46
46
  if (status) {
@@ -57,10 +57,10 @@ export function createEditStatus(status?: EditStatusConfig): EditStatusConfig {
57
57
  return s;
58
58
  }
59
59
  export interface DiffStatusConfig {
60
- not_found: number|string;
61
- success: number|string;
62
- version_error: number|string;
63
- error?: number|string;
60
+ not_found: number | string;
61
+ success: number | string;
62
+ version_error: number | string;
63
+ error?: number | string;
64
64
  }
65
65
  export function createDiffStatus(status?: DiffStatusConfig): DiffStatusConfig {
66
66
  if (status) {
@@ -96,7 +96,7 @@ export interface SearchState<T, S extends Filter> {
96
96
  }
97
97
  export interface SearchService<T, S extends Filter> {
98
98
  keys?(): string[];
99
- search(s: S, limit?: number, offset?: number|string, fields?: string[]): Promise<SearchResult<T>>;
99
+ search(s: S, limit?: number, offset?: number | string, fields?: string[]): Promise<SearchResult<T>>;
100
100
  }
101
101
  export interface ViewParameter {
102
102
  resource: ResourceService;
@@ -105,9 +105,9 @@ export interface ViewParameter {
105
105
  loading?: LoadingService;
106
106
  }
107
107
  export interface ViewService<T, ID> {
108
- metadata?(): Attributes|undefined;
108
+ metadata?(): Attributes | undefined;
109
109
  keys?(): string[];
110
- load(id: ID, ctx?: any): Promise<T|null>;
110
+ load(id: ID, ctx?: any): Promise<T | null>;
111
111
  }
112
112
 
113
113
  export interface DiffParameter {
@@ -131,8 +131,8 @@ export interface DiffModel<T, ID> {
131
131
  value: T;
132
132
  }
133
133
  export interface ApprService<ID> {
134
- approve(id: ID, ctx?: any): Promise<number|string>;
135
- reject(id: ID, ctx?: any): Promise<number|string>;
134
+ approve(id: ID, ctx?: any): Promise<number | string>;
135
+ reject(id: ID, ctx?: any): Promise<number | string>;
136
136
  }
137
137
  export interface DiffService<T, ID> {
138
138
  keys(): string[];
@@ -148,11 +148,11 @@ export interface DiffState<T> {
148
148
 
149
149
  // tslint:disable-next-line:class-name
150
150
  export class resource {
151
- static phone = / |\-|\.|\(|\)/g;
151
+ static phone = / |-|\.|\(|\)/g;
152
152
  static _cache: any = {};
153
153
  static cache = true;
154
154
  }
155
- export function getCurrencyCode(form?: HTMLFormElement|null): string|undefined {
155
+ export function getCurrencyCode(form?: HTMLFormElement | null): string | undefined {
156
156
  if (form) {
157
157
  const x = form.getAttribute('currency-code');
158
158
  if (x) {
@@ -182,34 +182,34 @@ export interface Message {
182
182
  yes?: string;
183
183
  no?: string;
184
184
  }
185
- export function getString(key: string, gv: StringMap|((key: string) => string)): string {
185
+ export function getString(key: string, gv: StringMap | ((key: string) => string)): string {
186
186
  if (typeof gv === 'function') {
187
187
  return gv(key);
188
188
  } else {
189
189
  return gv[key];
190
190
  }
191
191
  }
192
- export function message(gv: StringMap|((key: string) => string), msg: string, title?: string, yes?: string, no?: string): Message {
192
+ export function message(gv: StringMap | ((key: string) => string), msg: string, title?: string, yes?: string, no?: string): Message {
193
193
  const m2 = (msg && msg.length > 0 ? getString(msg, gv) : '');
194
- const m: Message = { message: m2, title: '' };
195
- if (title && title.length > 0) {
196
- m.title = getString(title, gv);
197
- }
198
- if (yes && yes.length > 0) {
199
- m.yes = getString(yes, gv);
200
- }
201
- if (no && no.length > 0) {
202
- m.no = getString(no, gv);
203
- }
204
- return m;
194
+ const m: Message = { message: m2, title: '' };
195
+ if (title && title.length > 0) {
196
+ m.title = getString(title, gv);
197
+ }
198
+ if (yes && yes.length > 0) {
199
+ m.yes = getString(yes, gv);
200
+ }
201
+ if (no && no.length > 0) {
202
+ m.no = getString(no, gv);
203
+ }
204
+ return m;
205
205
  }
206
- export function messageByHttpStatus(status: number, gv: StringMap|((key: string) => string)): string {
206
+ export function messageByHttpStatus(status: number, gv: StringMap | ((key: string) => string)): string {
207
207
  const k = 'status_' + status;
208
208
  let msg = getString(k, gv);
209
- if (!msg || msg.length === 0) {
210
- msg = getString('error_internal', gv);
211
- }
212
- return msg;
209
+ if (!msg || msg.length === 0) {
210
+ msg = getString('error_internal', gv);
211
+ }
212
+ return msg;
213
213
  }
214
214
 
215
215
  export interface Locale {
@@ -232,12 +232,12 @@ export interface LoadingService {
232
232
  export interface ErrorMessage {
233
233
  field: string;
234
234
  code: string;
235
- param?: string|number|Date;
235
+ param?: string | number | Date;
236
236
  message?: string;
237
237
  }
238
238
  export interface UIService {
239
- getValue(el: HTMLInputElement, locale?: Locale, currencyCode?: string): string|number|boolean|null|undefined;
240
- decodeFromForm(form: HTMLFormElement, locale?: Locale, currencyCode?: string|null): any;
239
+ getValue(el: HTMLInputElement, locale?: Locale, currencyCode?: string): string | number | boolean | null | undefined;
240
+ decodeFromForm(form: HTMLFormElement, locale?: Locale, currencyCode?: string | null): any;
241
241
 
242
242
  validateForm(form?: HTMLFormElement, locale?: Locale, focusFirst?: boolean, scroll?: boolean): boolean;
243
243
  removeFormError(form: HTMLFormElement): void;
@@ -284,26 +284,23 @@ export function buildKeys(attributes: Attributes): string[] {
284
284
  }
285
285
  return ps;
286
286
  }
287
-
288
- export function buildId<ID>(props: RouteComponentProps, keys?: string[]): ID|null {
289
- if (!props) {
290
- return null;
291
- }
292
- const sp: any = ((props as any).match ? props : (props as any)['props']);
287
+ type Readonly<T> = {
288
+ readonly [P in keyof T]: T[P];
289
+ };
290
+ export function buildId<ID>(p: Readonly<Params<string>>, keys?: string[]): ID | null {
293
291
  if (!keys || keys.length === 0 || keys.length === 1) {
294
292
  if (keys && keys.length === 1) {
295
- const x = sp.match.params[keys[0]];
296
- if (x && x !== '') {
297
- return x;
293
+ if (p[keys[0]]) {
294
+ return p[keys[0]] as any;
298
295
  }
299
296
  }
300
- return sp.match.params['id'];
297
+ return p['id'] as any;
301
298
  }
302
299
  const id: any = {};
303
300
  for (const key of keys) {
304
- let v = sp.match.params[key];
301
+ let v = p[key];
305
302
  if (!v) {
306
- v = sp[key];
303
+ v = p[key];
307
304
  if (!v) {
308
305
  return null;
309
306
  }
@@ -312,8 +309,6 @@ export function buildId<ID>(props: RouteComponentProps, keys?: string[]): ID|nul
312
309
  }
313
310
  return id;
314
311
  }
315
-
316
-
317
312
  export function dateToDefaultString(date: Date): string {
318
313
  return '' + date.getFullYear() + '-' + addZero(date.getMonth() + 1, 2) + '-' + addZero(date.getDate(), 2); // DateUtil.formatDate(date, 'YYYY-MM-DD');
319
314
  }
@@ -380,7 +375,7 @@ export function formatCurrency(currency: string|number, locale?: Locale, currenc
380
375
  }
381
376
  */
382
377
 
383
- export function initForm(form?: HTMLFormElement, initMat?: (f: HTMLFormElement) => void): HTMLFormElement|undefined {
378
+ export function initForm(form?: HTMLFormElement, initMat?: (f: HTMLFormElement) => void): HTMLFormElement | undefined {
384
379
  if (form) {
385
380
  setTimeout(() => {
386
381
  if (initMat) {
@@ -412,7 +407,7 @@ export function error(err: any, gv: (key: string) => string, ae: (msg: string, h
412
407
  export function getName(d: string, n?: string): string {
413
408
  return (n && n.length > 0 ? n : d);
414
409
  }
415
- export function getModelName(form?: HTMLFormElement|null, name?: string): string {
410
+ export function getModelName(form?: HTMLFormElement | null, name?: string): string {
416
411
  if (form) {
417
412
  const a = form.getAttribute('model-name');
418
413
  if (a && a.length > 0) {
@@ -473,7 +468,7 @@ export function hideLoading(s?: LoadingService): void {
473
468
  export interface UIParameter {
474
469
  ui?: UIService;
475
470
  }
476
- export function getRemoveError(u?: UIParameter, rmErr?: (el: HTMLInputElement) => void): ((el: HTMLInputElement) => void)|undefined {
471
+ export function getRemoveError(u?: UIParameter, rmErr?: (el: HTMLInputElement) => void): ((el: HTMLInputElement) => void) | undefined {
477
472
  if (rmErr) {
478
473
  return rmErr;
479
474
  }
@@ -484,13 +479,13 @@ export function removeFormError(u?: UIParameter, f?: HTMLFormElement): void {
484
479
  u.ui.removeFormError(f);
485
480
  }
486
481
  }
487
- 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 {
482
+ 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 {
488
483
  if (vf) {
489
484
  return vf;
490
485
  }
491
486
  return (u && u.ui ? u.ui.validateForm : undefined);
492
487
  }
493
- export function getDecodeFromForm(u?: UIParameter, d?: (form: HTMLFormElement, locale?: Locale, currencyCode?: string) => any): ((form: HTMLFormElement, locale?: Locale, currencyCode?: string) => any)|undefined {
488
+ export function getDecodeFromForm(u?: UIParameter, d?: (form: HTMLFormElement, locale?: Locale, currencyCode?: string) => any): ((form: HTMLFormElement, locale?: Locale, currencyCode?: string) => any) | undefined {
494
489
  if (d) {
495
490
  return d;
496
491
  }
package/src/diff.ts CHANGED
@@ -1,5 +1,5 @@
1
- import {clone} from 'reflectx';
2
1
  import {DiffModel} from './core';
2
+ import {clone} from './reflect';
3
3
 
4
4
  export function formatDiffModel<T, ID>(obj: DiffModel<T, ID>, formatFields?: (obj3: T) => T): DiffModel<T, ID> {
5
5
  if (!obj) {
package/src/formutil.ts CHANGED
@@ -74,7 +74,7 @@ export function focusFirstError(form?: HTMLFormElement|null, className?: string)
74
74
  const ctrl = form[i] as HTMLInputElement;
75
75
  const parent = ctrl.parentElement;
76
76
  if (ctrl.classList.contains(className)
77
- || parent && parent.classList.contains(className)) {
77
+ || (parent && parent.classList.contains(className))) {
78
78
  ctrl.focus();
79
79
  ctrl.scrollIntoView();
80
80
  return;
@@ -86,7 +86,7 @@ export function focusFirstError(form?: HTMLFormElement|null, className?: string)
86
86
  const parent = ctrl.parentElement;
87
87
  if (ctrl.classList.contains('invalid')
88
88
  || ctrl.classList.contains('.ng-invalid')
89
- || parent && parent.classList.contains('invalid')) {
89
+ || (parent && parent.classList.contains('invalid'))) {
90
90
  ctrl.focus();
91
91
  ctrl.scrollIntoView();
92
92
  return;
package/src/index.ts CHANGED
@@ -1,20 +1,21 @@
1
1
  import * as React from 'react';
2
- import { RouteComponentProps } from 'react-router';
3
2
  export * from './formutil';
4
3
  export * from './util';
5
4
  export * from './core';
6
5
  export * from './state';
7
6
  export * from './edit';
8
7
  export * from './route';
9
- export * from './components';
10
8
  export * from './diff';
11
- export * from './router';
12
9
  export * from './merge';
13
10
  export * from './update';
14
- export * from './useView';
15
- export * from './useEdit';
16
11
  export * from './useSearch';
17
12
  export * from './useMessage';
13
+ export * from './useEdit';
14
+ export * from './components';
15
+ export * from './search';
16
+ export * from './reflect';
17
+
18
+ import {pageSizes} from './core';
18
19
 
19
20
  export function checked(s: string[]|string|undefined, v: string): boolean|undefined {
20
21
  if (s) {
@@ -29,10 +30,6 @@ export function checked(s: string[]|string|undefined, v: string): boolean|undefi
29
30
  export function value<T>(obj?: T): T {
30
31
  return (obj ? obj : {} as any);
31
32
  }
32
- export const withDefaultProps = (Component: any) => (props: RouteComponentProps) => {
33
- // return <Component props={props} history={props.history} />;
34
- return React.createElement(Component, { props, history: props.history });
35
- };
36
33
  export interface LoadingProps {
37
34
  error?: any;
38
35
  }
@@ -58,3 +55,17 @@ export const Loading = (props: LoadingProps) => {
58
55
  );*/
59
56
  }
60
57
  };
58
+ export interface Props {
59
+ id?: string;
60
+ name?: string;
61
+ size?: number;
62
+ sizes?: number[];
63
+ onChange?: React.ChangeEventHandler<HTMLSelectElement>;
64
+ }
65
+ export function PageSizeSelect(p: Props) {
66
+ const g = p.sizes;
67
+ const s = (!g || g.length === 0 ? pageSizes : g);
68
+ const opts = s.map(pgSize => React.createElement('option', { key: pgSize, value: pgSize }, pgSize));
69
+ return React.createElement('select', { id: p.id, name: p.name, defaultValue: p.size, onChange: p.onChange }, opts);
70
+ }
71
+ export default PageSizeSelect;
package/src/reflect.ts ADDED
@@ -0,0 +1,244 @@
1
+ export function clone(obj: any): any {
2
+ if (!obj) {
3
+ return obj;
4
+ }
5
+ if (obj instanceof Date) {
6
+ return new Date(obj.getTime());
7
+ }
8
+ if (typeof obj !== 'object') {
9
+ return obj;
10
+ }
11
+ if (Array.isArray(obj)) {
12
+ const arr = [];
13
+ for (const sub of obj) {
14
+ const c = clone(sub);
15
+ arr.push(c);
16
+ }
17
+ return arr;
18
+ }
19
+ const x: any = {};
20
+ const keys = Object.keys(obj);
21
+ for (const k of keys) {
22
+ const v = obj[k];
23
+ if (v instanceof Date) {
24
+ x[k] = new Date(v.getTime());
25
+ } else {
26
+ switch (typeof v) {
27
+ case 'object':
28
+ x[k] = clone(v);
29
+ break;
30
+ default:
31
+ x[k] = v;
32
+ break;
33
+ }
34
+ }
35
+ }
36
+ return x;
37
+ }
38
+
39
+ export function getDirectValue(obj: any, key: string): any {
40
+ if (obj && obj.hasOwnProperty(key)) {
41
+ return obj[key];
42
+ }
43
+ return null;
44
+ }
45
+
46
+ export function setValue(obj: any, key: string, value: any): any {
47
+ let replaceKey = key.replace(/\[/g, '.[').replace(/\.\./g, '.');
48
+ if (replaceKey.indexOf('.') === 0) {
49
+ replaceKey = replaceKey.slice(1, replaceKey.length);
50
+ }
51
+ const keys = replaceKey.split('.');
52
+ const firstKey = keys.shift();
53
+ if (!firstKey) {
54
+ return;
55
+ }
56
+ const isArrayKey = /\[([0-9]+)\]/.test(firstKey);
57
+ if (keys.length > 0) {
58
+ const firstKeyValue = obj[firstKey] || {};
59
+ const returnValue = setValue(firstKeyValue, keys.join('.'), value);
60
+ return setKey(obj, isArrayKey, firstKey, returnValue);
61
+ }
62
+ return setKey(obj, isArrayKey, firstKey, value);
63
+ }
64
+ const setKey = (_object: any, _isArrayKey: boolean, _key: string, _nextValue: any) => {
65
+ if (_isArrayKey) {
66
+ if (_object.length > _key) {
67
+ _object[_key] = _nextValue;
68
+ } else {
69
+ _object.push(_nextValue);
70
+ }
71
+ } else {
72
+ _object[_key] = _nextValue;
73
+ }
74
+ return _object;
75
+ };
76
+
77
+ export function diff(obj1: any, obj2: any): string[] {
78
+ const fields = [];
79
+ const key1s = Object.keys(obj1);
80
+ for (const k of key1s) {
81
+ const v1 = obj1[k];
82
+ const v2 = obj2[k];
83
+ if (v1 == null) {
84
+ if (v2 != null) {
85
+ fields.push(k);
86
+ }
87
+ } else {
88
+ if (typeof v1 !== 'object') {
89
+ if (v1 !== v2) {
90
+ fields.push(k);
91
+ }
92
+ } else {
93
+ const e = equal(v1, v2);
94
+ if (!e) {
95
+ fields.push(k);
96
+ }
97
+ }
98
+ }
99
+ }
100
+ const key2s = Object.keys(obj2);
101
+ const ni = notIn(key1s, key2s);
102
+ for (const n of ni) {
103
+ fields.push(n);
104
+ }
105
+ return fields;
106
+ }
107
+
108
+ export function makeDiff(obj1: any, obj2: any, keys?: string[], version?: string): any {
109
+ const obj3: any = {};
110
+ const s = diff(obj1, obj2);
111
+ if (s.length === 0) {
112
+ return obj3;
113
+ }
114
+ for (const d of s) {
115
+ obj3[d] = obj2[d];
116
+ }
117
+ if (keys && keys.length > 0) {
118
+ for (const x of keys) {
119
+ if (x.length > 0) {
120
+ obj3[x] = obj1[x];
121
+ }
122
+ }
123
+ }
124
+ if (version && version.length > 0) {
125
+ obj3[version] = obj1[version];
126
+ }
127
+ return obj3;
128
+ }
129
+
130
+ export function notIn(s1: string[], s2: string[]): string[] {
131
+ const r = [];
132
+ for (const s of s2) {
133
+ if (s1.indexOf(s) < 0) {
134
+ r.push(s);
135
+ }
136
+ }
137
+ return r;
138
+ }
139
+
140
+ export function equal(obj1: any, obj2: any): boolean {
141
+ if (obj1 == null && obj2 == null) {
142
+ return true;
143
+ }
144
+ if (obj1 == null || obj2 == null) {
145
+ return false;
146
+ }
147
+ if ((typeof obj1) !== (typeof obj2)) {
148
+ return false;
149
+ }
150
+ if (typeof obj1 !== 'object') {
151
+ return obj1 === obj2;
152
+ }
153
+ if (obj1 instanceof Date) {
154
+ if (!(obj2 instanceof Date)) {
155
+ return false;
156
+ }
157
+ return (obj1.getTime() === obj2.getTime());
158
+ }
159
+ if ((Array.isArray(obj1) && !Array.isArray(obj2))
160
+ || (!Array.isArray(obj1) && Array.isArray(obj2))) {
161
+ return false;
162
+ }
163
+ if (!Array.isArray(obj1) && !Array.isArray(obj2)) {
164
+ const key1s = Object.keys(obj1);
165
+ const key2s = Object.keys(obj2);
166
+ if (key1s.length !== key2s.length) {
167
+ return false;
168
+ }
169
+ for (const k of key1s) {
170
+ const v = obj1[k];
171
+ if (typeof v !== 'object') {
172
+ if (v !== obj2[k]) {
173
+ return false;
174
+ }
175
+ } else {
176
+ const e = equal(v, obj2[k]);
177
+ if (e === false) {
178
+ return false;
179
+ }
180
+ }
181
+ }
182
+ return true;
183
+ }
184
+ return equalArrays(obj1, obj2);
185
+ }
186
+
187
+ export function equalArrays<T>(ar1: T[], ar2: T[]): boolean {
188
+ if (ar1 == null && ar2 == null) {
189
+ return true;
190
+ }
191
+ if (ar1 == null || ar2 == null) {
192
+ return false;
193
+ }
194
+ if (ar1.length !== ar2.length) {
195
+ return false;
196
+ }
197
+ for (let i = 0; i < ar1.length; i++) {
198
+ const e = equal(ar1[i], ar2[i]);
199
+ if (e === false) {
200
+ return false;
201
+ }
202
+ }
203
+ return true;
204
+ }
205
+
206
+ export function getArray<T>(list: T[]|undefined|null, name: string, v: boolean|string|number): T[] {
207
+ const arrs = [];
208
+ if (list) {
209
+ for (const obj of list) {
210
+ if ((obj as any)[name] === v) {
211
+ arrs.push(obj);
212
+ }
213
+ }
214
+ }
215
+ return arrs;
216
+ }
217
+ export function getDiffArray<T>(list: T[]|undefined|null, name: string, v: boolean|string|number): T[] {
218
+ const arrs = [];
219
+ if (list) {
220
+ for (const obj of list) {
221
+ if ((obj as any)[name] !== true) {
222
+ arrs.push(obj);
223
+ }
224
+ }
225
+ }
226
+ return arrs;
227
+ }
228
+ export function setAll<T>(list: T[]|undefined|null, name: string, v: boolean|string|number): void {
229
+ if (list) {
230
+ for (const obj of list) {
231
+ (obj as any)[name] = v;
232
+ }
233
+ }
234
+ }
235
+ export function equalAll<T>(list: T[]|undefined|null, name: string, v: boolean|string|number): boolean {
236
+ if (list) {
237
+ for (const obj of list) {
238
+ if ((obj as any)[name] !== v) {
239
+ return false;
240
+ }
241
+ }
242
+ }
243
+ return true;
244
+ }