react-hook-core 0.1.2 → 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/components.ts CHANGED
@@ -1,17 +1,18 @@
1
1
  import * as React from 'react';
2
- import {RouteComponentProps} from 'react-router';
3
- import {clone, diff, makeDiff} from 'reflectx';
4
- import {addParametersIntoUrl, append, buildMessage, changePage, changePageSize, formatResultsByComponent, getFieldsFromForm, getModel, handleAppend, handleSortEvent, initFilter, mergeFilter as mergeFilter2, more, reset, Searchable, showPaging, validate} from 'search-core';
5
- import {BaseDiffState, createDiffStatus, createEditStatus, DiffApprService, DiffParameter, DiffState, DiffStatusConfig, hideLoading, showLoading} from './core';
6
- import {Attributes, buildId, EditStatusConfig, error, ErrorMessage, Filter, getCurrencyCode, getModelName as getModelName2, initForm, LoadingService, Locale, message, messageByHttpStatus, removePhoneFormat, ResourceService, SearchParameter, SearchResult, SearchService, SearchState, StringMap, UIService, ViewParameter, ViewService} from './core';
2
+ import {BaseDiffState, createDiffStatus, createEditStatus, DiffApprService, DiffParameter, DiffState, DiffStatusConfig, handleToggle, hideLoading, showLoading} from './core';
3
+ import {Attributes, EditStatusConfig, error, ErrorMessage, Filter, getCurrencyCode, getModelName as getModelName2, initForm, LoadingService, Locale, message, messageByHttpStatus, PageChange, pageSizes, removePhoneFormat, ResourceService, SearchParameter, SearchResult, SearchService, SearchState, StringMap, UIService, ViewParameter, ViewService} from './core';
7
4
  import {formatDiffModel, getDataFields} from './diff';
8
5
  import {build, createModel as createModel2, EditParameter, GenericService, handleStatus, handleVersion, initPropertyNullInModel, ResultInfo} from './edit';
9
6
  import {focusFirstError, readOnly} from './formutil';
10
7
  import {getAutoSearch, getConfirmFunc, getDiffStatusFunc, getEditStatusFunc, getErrorFunc, getLoadingFunc, getLocaleFunc, getMsgFunc, getResource, getUIService} from './input';
8
+ import {clone, diff, makeDiff} from './reflect';
11
9
  import {buildFromUrl} from './route';
10
+ import {addParametersIntoUrl, append, buildMessage, changePage, changePageSize, formatResults, getFieldsFromForm, getModel, handleAppend, handleSortEvent, initFilter, mergeFilter as mergeFilter2, more, Pagination, reset, showPaging, Sortable, validate} from './search';
12
11
  import {buildFlatState, buildState, enLocale, handleEvent, handleProps, localeOf} from './state';
13
12
 
14
- export class ViewComponent<T, ID, P extends RouteComponentProps, S> extends React.Component<P, S> {
13
+ interface Searchable extends Pagination, Sortable {
14
+ }
15
+ export class ViewComponent<T, ID, P, S> extends React.Component<P, S> {
15
16
  constructor(props: P, sv: ((id: ID, ctx?: any) => Promise<T>)|ViewService<T, ID>,
16
17
  param: ResourceService|ViewParameter,
17
18
  showError?: (msg: string, title?: string, detail?: string, callback?: () => void) => void,
@@ -40,7 +41,6 @@ export class ViewComponent<T, ID, P extends RouteComponentProps, S> extends Reac
40
41
  }
41
42
  }
42
43
  }
43
- this.back = this.back.bind(this);
44
44
  this.getModelName = this.getModelName.bind(this);
45
45
  this.load = this.load.bind(this);
46
46
  this.getModel = this.getModel.bind(this);
@@ -54,19 +54,13 @@ export class ViewComponent<T, ID, P extends RouteComponentProps, S> extends Reac
54
54
  loading?: LoadingService;
55
55
  showError: (msg: string, title?: string, detail?: string, callback?: () => void) => void;
56
56
  getLocale?: (profile?: string) => Locale;
57
- loadData?: (id: ID, ctx?: any) => Promise<T>;
57
+ loadData?: (id: ID) => Promise<T|null>;
58
58
  // protected service: ViewService<T, ID>;
59
59
  form?: HTMLFormElement;
60
60
  ref: any;
61
61
  keys?: string[];
62
62
  metadata?: Attributes;
63
63
 
64
- back(event: any) {
65
- if (event) {
66
- event.preventDefault();
67
- }
68
- this.props.history.goBack();
69
- }
70
64
  getModelName(): string {
71
65
  if (this.name && this.name.length > 0) {
72
66
  return this.name;
@@ -78,13 +72,6 @@ export class ViewComponent<T, ID, P extends RouteComponentProps, S> extends Reac
78
72
  return n;
79
73
  }
80
74
  }
81
- componentDidMount() {
82
- this.form = this.ref.current;
83
- const id = buildId<ID>(this.props, this.keys);
84
- if (id) {
85
- this.load(id);
86
- }
87
- }
88
75
  load(_id: ID, callback?: (m: T, showF: (model: T) => void) => void) {
89
76
  const id: any = _id;
90
77
  if (id != null && id !== '' && this.loadData) {
@@ -291,7 +278,7 @@ export class MessageComponent<S extends MessageOnlyState, P> extends BaseCompone
291
278
  this.setState({ message: '' });
292
279
  }
293
280
  }
294
- export class BaseSearchComponent<T, S extends Filter, P, I extends SearchState<T, S>> extends BaseComponent<P, I> implements Searchable {
281
+ export class BaseSearchComponent<T, F extends Filter, P, I extends SearchState<T, F>> extends BaseComponent<P, I> implements Searchable {
295
282
  constructor(props: P,
296
283
  protected resourceService: ResourceService,
297
284
  protected showMessage: (msg: string) => void,
@@ -301,6 +288,7 @@ export class BaseSearchComponent<T, S extends Filter, P, I extends SearchState<T
301
288
  protected listFormId?: string) {
302
289
  super(props, getLocale, (ui ? ui.removeError : undefined));
303
290
  this.resource = resourceService.resource();
291
+ this.hideFilter = true;
304
292
  this.getModelName = this.getModelName.bind(this);
305
293
  this.showMessage = this.showMessage.bind(this);
306
294
 
@@ -315,7 +303,7 @@ export class BaseSearchComponent<T, S extends Filter, P, I extends SearchState<T
315
303
 
316
304
  this.pageSizeChanged = this.pageSizeChanged.bind(this);
317
305
  this.clearQ = this.clearQ.bind(this);
318
- this.searchOnClick = this.searchOnClick.bind(this);
306
+ this.search = this.search.bind(this);
319
307
 
320
308
  this.resetAndSearch = this.resetAndSearch.bind(this);
321
309
  this.doSearch = this.doSearch.bind(this);
@@ -341,14 +329,14 @@ export class BaseSearchComponent<T, S extends Filter, P, I extends SearchState<T
341
329
  resource: StringMap;
342
330
  url: string;
343
331
 
344
- filter?: S;
332
+ filter?: F;
345
333
  // Pagination
346
- initPageSize = 20;
347
- pageSize = 20;
334
+ initPageSize = 24;
335
+ pageSize = 24;
348
336
  pageIndex?: number = 1;
349
337
  nextPageToken?: string;
350
- itemTotal = 0;
351
- pageTotal = 0;
338
+ total = 0;
339
+ pages = 0;
352
340
  showPaging?: boolean;
353
341
  append?: boolean;
354
342
  appendMode?: boolean;
@@ -368,7 +356,7 @@ export class BaseSearchComponent<T, S extends Filter, P, I extends SearchState<T
368
356
  tmpPageIndex?: number = 1;
369
357
 
370
358
  pageMaxSize = 7;
371
- pageSizes: number[] = [10, 20, 40, 60, 100, 200, 400, 800];
359
+ pageSizes: number[] = pageSizes;
372
360
 
373
361
  list?: T[];
374
362
  excluding?: string[]|number[];
@@ -388,10 +376,12 @@ export class BaseSearchComponent<T, S extends Filter, P, I extends SearchState<T
388
376
  return 'filter';
389
377
  }
390
378
 
391
- toggleFilter(event: any): void {
392
- this.hideFilter = !this.hideFilter;
379
+ toggleFilter(event: React.MouseEvent<HTMLButtonElement, MouseEvent>): void {
380
+ const x = !this.hideFilter;
381
+ handleToggle(event.target as HTMLInputElement, !x);
382
+ this.hideFilter = x;
393
383
  }
394
- load(s: S, autoSearch: boolean): void {
384
+ load(s: F, autoSearch: boolean): void {
395
385
  const obj2 = initFilter(s, this);
396
386
  this.setFilter(obj2);
397
387
  const com = this;
@@ -412,7 +402,7 @@ export class BaseSearchComponent<T, S extends Filter, P, I extends SearchState<T
412
402
  }
413
403
  return this.form;
414
404
  }
415
- setFilter(filter: S): void {
405
+ setFilter(filter: F): void {
416
406
  const modelName = this.getModelName();
417
407
  const objSet: any = {};
418
408
  objSet[modelName] = filter;
@@ -421,7 +411,7 @@ export class BaseSearchComponent<T, S extends Filter, P, I extends SearchState<T
421
411
  getCurrencyCode(): string|undefined {
422
412
  return getCurrencyCode(this.form);
423
413
  }
424
- getFilter(): S {
414
+ getFilter(): F {
425
415
  const name = this.getModelName();
426
416
  let lc: Locale|undefined;
427
417
  if (this.getLocale) {
@@ -435,7 +425,7 @@ export class BaseSearchComponent<T, S extends Filter, P, I extends SearchState<T
435
425
  const l = this.getList();
436
426
  const f = this.getSearchForm();
437
427
  const dc = (this.ui ? this.ui.decodeFromForm : undefined);
438
- const obj3 = getModel<T, S>(this.state, name, this, fields, this.excluding, this.keys, l, f, dc, lc, cc);
428
+ const obj3 = getModel<T, F>(this.state, name, this, fields, this.excluding, this.keys, l, f, dc, lc, cc);
439
429
  return obj3;
440
430
  }
441
431
  getFields(): string[]|undefined {
@@ -464,7 +454,7 @@ export class BaseSearchComponent<T, S extends Filter, P, I extends SearchState<T
464
454
  });
465
455
  }
466
456
  }
467
- searchOnClick(event: any): void {
457
+ search(event: any): void {
468
458
  if (event) {
469
459
  event.preventDefault();
470
460
  if (!this.getSearchForm()) {
@@ -508,26 +498,27 @@ export class BaseSearchComponent<T, S extends Filter, P, I extends SearchState<T
508
498
  });
509
499
  }
510
500
 
511
- call(s: S): void {
501
+ call(s: F): void {
512
502
 
513
503
  }
514
504
 
515
- validateSearch(se: S, callback: () => void): void {
505
+ validateSearch(se: F, callback: () => void): void {
516
506
  const u = this.ui;
517
507
  const vl = (u ? u.validateForm : undefined);
518
508
  validate(se, callback, this.getSearchForm(), localeOf(undefined, this.getLocale), vl);
519
509
  }
520
- showResults(s: S, sr: SearchResult<T>) {
510
+ showResults(s: F, sr: SearchResult<T>) {
521
511
  const com = this;
522
512
  const results = sr.list;
523
513
  if (results && results.length > 0) {
524
514
  const lc = localeOf(undefined, this.getLocale);
525
- formatResultsByComponent(results, com, lc);
515
+ // formatResultsByComponent(results, com, lc);
516
+ formatResults(results, com.pageIndex, com.pageSize, com.pageSize, com.sequenceNo, com.format, lc);
526
517
  }
527
518
  const am = com.appendMode;
528
519
  com.pageIndex = (s.page && s.page >= 1 ? s.page : 1);
529
520
  if (sr.total) {
530
- com.itemTotal = sr.total;
521
+ com.total = sr.total;
531
522
  }
532
523
  if (am) {
533
524
  let limit = s.limit;
@@ -609,13 +600,13 @@ export class BaseSearchComponent<T, S extends Filter, P, I extends SearchState<T
609
600
  this.doSearch();
610
601
  }
611
602
 
612
- pageChanged(data: any) {
613
- const { currentPage, itemsPerPage } = data;
614
- changePage(this, currentPage, itemsPerPage);
603
+ pageChanged(data: PageChange) {
604
+ const { page, size } = data;
605
+ changePage(this, page, size);
615
606
  this.doSearch();
616
607
  }
617
608
  }
618
- export class SearchComponent<T, S extends Filter, P extends RouteComponentProps, I extends SearchState<T, S>> extends BaseSearchComponent<T, S, P, I> {
609
+ export class SearchComponent<T, S extends Filter, P, I extends SearchState<T, S>> extends BaseSearchComponent<T, S, P, I> {
619
610
  constructor(props: P, sv: ((s: S, ctx?: any) => Promise<SearchResult<T>>) | SearchService<T, S>,
620
611
  param: ResourceService|SearchParameter,
621
612
  showMessage?: (msg: string, option?: string) => void,
@@ -629,15 +620,14 @@ export class SearchComponent<T, S extends Filter, P extends RouteComponentProps,
629
620
  if (sv) {
630
621
  if (typeof sv === 'function') {
631
622
  const x: any = sv;
632
- this.search = x;
623
+ this.service = x;
633
624
  } else {
634
- this.search = sv.search;
625
+ this.service = sv.search;
635
626
  if (sv.keys) {
636
627
  this.keys = sv.keys();
637
628
  }
638
629
  }
639
630
  }
640
- this.add = this.add.bind(this);
641
631
  this.call = this.call.bind(this);
642
632
  this.showError = getErrorFunc(param, showError);
643
633
  this.componentDidMount = this.componentDidMount.bind(this);
@@ -646,7 +636,7 @@ export class SearchComponent<T, S extends Filter, P extends RouteComponentProps,
646
636
  this.ref = React.createRef();
647
637
  }
648
638
  protected showError: (m: string, header?: string, detail?: string, callback?: () => void) => void;
649
- protected search?: (s: S, limit?: number, offset?: number|string, fields?: string[]) => Promise<SearchResult<T>>;
639
+ protected service?: (s: S, limit?: number, offset?: number|string, fields?: string[]) => Promise<SearchResult<T>>;
650
640
  // protected service: SearchService<T, S>;
651
641
  protected ref: any;
652
642
  protected autoSearch: boolean;
@@ -663,11 +653,6 @@ export class SearchComponent<T, S extends Filter, P extends RouteComponentProps,
663
653
  const s: any = {};
664
654
  return s;
665
655
  }
666
- add = (event: any) => {
667
- event.preventDefault();
668
- const url = this.url + '/add';
669
- this.props.history.push(url);
670
- }
671
656
  call(se: S) {
672
657
  this.running = true;
673
658
  const s = clone(se);
@@ -692,8 +677,8 @@ export class SearchComponent<T, S extends Filter, P extends RouteComponentProps,
692
677
  delete se['firstLimit'];
693
678
  showLoading(this.loading);
694
679
  const com = this;
695
- if (this.search) {
696
- this.search(s, limit, next, fields).then(sr => {
680
+ if (this.service) {
681
+ this.service(s, limit, next, fields).then(sr => {
697
682
  com.showResults(s, sr);
698
683
  com.running = undefined;
699
684
  hideLoading(com.loading);
@@ -707,7 +692,7 @@ export class SearchComponent<T, S extends Filter, P extends RouteComponentProps,
707
692
  }
708
693
  }
709
694
 
710
- export abstract class BaseEditComponent<T, P extends RouteComponentProps, S> extends BaseComponent<P, S> {
695
+ export abstract class BaseEditComponent<T, P, S> extends BaseComponent<P, S> {
711
696
  constructor(props: P,
712
697
  protected resourceService: ResourceService,
713
698
  protected showMessage: (msg: string) => void,
@@ -743,11 +728,11 @@ export abstract class BaseEditComponent<T, P extends RouteComponentProps, S> ext
743
728
  this.getModel = this.getModel.bind(this);
744
729
  this.createModel = this.createModel.bind(this);
745
730
 
746
- this.newOnClick = this.newOnClick.bind(this);
747
- this.saveOnClick = this.saveOnClick.bind(this);
731
+ this.create = this.create.bind(this);
732
+ this.save = this.save.bind(this);
748
733
  this.onSave = this.onSave.bind(this);
749
734
  this.validate = this.validate.bind(this);
750
- this.save = this.save.bind(this);
735
+ this.doSave = this.doSave.bind(this);
751
736
  this.succeed = this.succeed.bind(this);
752
737
  this.fail = this.fail.bind(this);
753
738
  this.postSave = this.postSave.bind(this);
@@ -775,7 +760,6 @@ export abstract class BaseEditComponent<T, P extends RouteComponentProps, S> ext
775
760
  if (event) {
776
761
  event.preventDefault();
777
762
  }
778
- this.props.history.goBack();
779
763
  }
780
764
  resetState(newMod: boolean, model: T, originalModel?: T) {
781
765
  this.newMode = newMod;
@@ -822,7 +806,7 @@ export abstract class BaseEditComponent<T, P extends RouteComponentProps, S> ext
822
806
  }
823
807
  }
824
808
 
825
- newOnClick = (event: any) => {
809
+ create = (event: any) => {
826
810
  if (event) {
827
811
  event.preventDefault();
828
812
  }
@@ -839,7 +823,7 @@ export abstract class BaseEditComponent<T, P extends RouteComponentProps, S> ext
839
823
  }, 100);
840
824
  }
841
825
  }
842
- saveOnClick = (event: any) => {
826
+ save = (event: any) => {
843
827
  event.preventDefault();
844
828
  (event as any).persist();
845
829
  if (!this.form && event && event.target) {
@@ -867,7 +851,7 @@ export abstract class BaseEditComponent<T, P extends RouteComponentProps, S> ext
867
851
  com.validate(obj, () => {
868
852
  const msg = message(r.value, 'msg_confirm_save', 'confirm', 'yes', 'no');
869
853
  this.confirm(msg.message, msg.title, () => {
870
- com.save(obj, obj, isBack);
854
+ com.doSave(obj, obj, isBack);
871
855
  }, msg.no, msg.yes);
872
856
  });
873
857
  } else {
@@ -879,7 +863,7 @@ export abstract class BaseEditComponent<T, P extends RouteComponentProps, S> ext
879
863
  com.validate(obj, () => {
880
864
  const msg = message(r.value, 'msg_confirm_save', 'confirm', 'yes', 'no');
881
865
  this.confirm(msg.message, msg.title, () => {
882
- com.save(obj, diffObj, isBack);
866
+ com.doSave(obj, diffObj, isBack);
883
867
  }, msg.no, msg.yes);
884
868
  });
885
869
  }
@@ -897,7 +881,7 @@ export abstract class BaseEditComponent<T, P extends RouteComponentProps, S> ext
897
881
  }
898
882
  }
899
883
 
900
- save(obj: T, dif?: T, isBack?: boolean) {
884
+ doSave(obj: T, dif?: T, isBack?: boolean) {
901
885
  }
902
886
 
903
887
  succeed(msg: string, isBack?: boolean, result?: ResultInfo<T>) {
@@ -982,7 +966,7 @@ export abstract class BaseEditComponent<T, P extends RouteComponentProps, S> ext
982
966
  this.showError(msg.message, msg.title);
983
967
  }
984
968
  }
985
- export class EditComponent<T, ID, P extends RouteComponentProps, S> extends BaseEditComponent<T, P, S> {
969
+ export class EditComponent<T, ID, P, S> extends BaseEditComponent<T, P, S> {
986
970
  constructor(props: P, protected service: GenericService<T, ID, number|ResultInfo<T>>,
987
971
  param: ResourceService|EditParameter,
988
972
  showMessage?: (msg: string, option?: string) => void,
@@ -1013,20 +997,11 @@ export class EditComponent<T, ID, P extends RouteComponentProps, S> extends Base
1013
997
  this.keys = [];
1014
998
  }
1015
999
  this.load = this.load.bind(this);
1016
- this.save = this.save.bind(this);
1017
- this.componentDidMount = this.componentDidMount.bind(this);
1000
+ this.doSave = this.doSave.bind(this);
1018
1001
  this.ref = React.createRef();
1019
1002
  }
1020
1003
  ref: any;
1021
- componentDidMount() {
1022
- const k = (this.ui ? this.ui.registerEvents : undefined);
1023
- this.form = initForm(this.ref.current, k);
1024
- const id = buildId<ID>(this.props, this.keys);
1025
- if (id) {
1026
- this.load(id);
1027
- }
1028
- }
1029
- load(_id: ID, callback?: (m: T, showM: (m2: T) => void) => void) {
1004
+ load(_id: ID|null, callback?: (m: T, showM: (m2: T) => void) => void) {
1030
1005
  const id: any = _id;
1031
1006
  if (id != null && id !== '') {
1032
1007
  const com = this;
@@ -1078,7 +1053,7 @@ export class EditComponent<T, ID, P extends RouteComponentProps, S> extends Base
1078
1053
  }
1079
1054
  }
1080
1055
  }
1081
- save(obj: T, body?: T, isBack?: boolean) {
1056
+ doSave(obj: T, body?: T, isBack?: boolean) {
1082
1057
  this.running = true;
1083
1058
  showLoading(this.loading);
1084
1059
  const isBackO = (isBack == null || isBack === undefined ? this.backOnSuccess : isBack);
@@ -1103,7 +1078,7 @@ export class EditComponent<T, ID, P extends RouteComponentProps, S> extends Base
1103
1078
  }
1104
1079
  }
1105
1080
 
1106
- export class BaseDiffApprComponent<T, ID, P extends RouteComponentProps, S extends BaseDiffState> extends React.Component<P, S & any> {
1081
+ export class BaseDiffApprComponent<T, ID, P, S extends BaseDiffState> extends React.Component<P, S & any> {
1107
1082
  constructor(props: P, protected keys: string[], protected resourceService: ResourceService,
1108
1083
  protected showMessage: (msg: string, option?: string) => void,
1109
1084
  protected showError: (m: string, title?: string, detail?: string, callback?: () => void) => void,
@@ -1116,7 +1091,6 @@ export class BaseDiffApprComponent<T, ID, P extends RouteComponentProps, S exten
1116
1091
  this.resource = resourceService.resource();
1117
1092
  this.showMessage = this.showMessage.bind(this);
1118
1093
  this.showError = this.showError.bind(this);
1119
- this.back = this.back.bind(this);
1120
1094
  this.initModel = this.initModel.bind(this);
1121
1095
  this.postApprove = this.postApprove.bind(this);
1122
1096
  this.postReject = this.postReject.bind(this);
@@ -1133,13 +1107,6 @@ export class BaseDiffApprComponent<T, ID, P extends RouteComponentProps, S exten
1133
1107
  running?: boolean;
1134
1108
  resource: StringMap;
1135
1109
 
1136
- back(event: any) {
1137
- if (event) {
1138
- event.preventDefault();
1139
- }
1140
- this.props.history.goBack();
1141
- }
1142
-
1143
1110
  initModel(): T {
1144
1111
  const x: any = {};
1145
1112
  return x;
@@ -1222,7 +1189,7 @@ export class BaseDiffApprComponent<T, ID, P extends RouteComponentProps, S exten
1222
1189
  this.showError(msg.message, msg.title);
1223
1190
  }
1224
1191
  }
1225
- export class DiffApprComponent<T, ID, P extends RouteComponentProps, S extends DiffState<T>> extends BaseDiffApprComponent<T, ID, P, S> {
1192
+ export class DiffApprComponent<T, ID, P, S extends DiffState<T>> extends BaseDiffApprComponent<T, ID, P, S> {
1226
1193
  constructor(props: P, protected service: DiffApprService<T, ID>,
1227
1194
  param: ResourceService|DiffParameter,
1228
1195
  showMessage?: (msg: string, option?: string) => void,
@@ -1242,14 +1209,6 @@ export class DiffApprComponent<T, ID, P extends RouteComponentProps, S extends D
1242
1209
  }
1243
1210
  ref: any;
1244
1211
 
1245
- componentDidMount() {
1246
- this.form = this.ref.current;
1247
- const id = buildId<ID>(this.props, this.keys);
1248
- if (id) {
1249
- this.load(id);
1250
- }
1251
- }
1252
-
1253
1212
  formatFields(value: T): T {
1254
1213
  return value;
1255
1214
  }