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/diff.ts CHANGED
@@ -1,52 +1,82 @@
1
- import {DiffModel} from './core';
2
- import {clone} from './reflect';
1
+ import { LoadingService, ResourceService } from "./core"
2
+ import { clone } from "./reflect"
3
+
4
+ export interface DiffParameter {
5
+ resource: ResourceService
6
+ showMessage: (msg: string, option?: string) => void
7
+ showError: (m: string, header?: string, detail?: string, callback?: () => void) => void
8
+ loading?: LoadingService
9
+ // status?: DiffStatusConfig;
10
+ }
11
+ export interface BaseDiffState {
12
+ disabled: boolean
13
+ }
14
+ export interface DiffModel<T, ID> {
15
+ id?: ID
16
+ origin?: T
17
+ value: T
18
+ }
19
+ export interface ApprService<ID> {
20
+ approve(id: ID, ctx?: any): Promise<number | string>
21
+ reject(id: ID, ctx?: any): Promise<number | string>
22
+ }
23
+ export interface DiffService<T, ID> {
24
+ keys(): string[]
25
+ diff(id: ID, ctx?: any): Promise<DiffModel<T, ID>>
26
+ }
27
+ export interface DiffApprService<T, ID> extends DiffService<T, ID>, ApprService<ID> {}
28
+ export interface DiffState<T> {
29
+ origin: T
30
+ value: T
31
+ disabled: boolean
32
+ }
3
33
 
4
34
  export function formatDiffModel<T, ID>(obj: DiffModel<T, ID>, formatFields?: (obj3: T) => T): DiffModel<T, ID> {
5
35
  if (!obj) {
6
- return obj;
36
+ return obj
7
37
  }
8
- const obj2 = clone(obj);
38
+ const obj2 = clone(obj)
9
39
  if (!obj2.origin) {
10
- obj2.origin = {};
40
+ obj2.origin = {}
11
41
  } else {
12
- if (typeof obj2.origin === 'string') {
13
- obj2.origin = JSON.parse(obj2.origin);
42
+ if (typeof obj2.origin === "string") {
43
+ obj2.origin = JSON.parse(obj2.origin)
14
44
  }
15
- if (formatFields && typeof obj2.origin === 'object' && !Array.isArray(obj2.origin)) {
16
- obj2.origin = formatFields(obj2.origin);
45
+ if (formatFields && typeof obj2.origin === "object" && !Array.isArray(obj2.origin)) {
46
+ obj2.origin = formatFields(obj2.origin)
17
47
  }
18
48
  }
19
49
  if (!obj2.value) {
20
- obj2.value = {};
50
+ obj2.value = {}
21
51
  } else {
22
- if (typeof obj2.value === 'string') {
23
- obj2.value = JSON.parse(obj2.value);
52
+ if (typeof obj2.value === "string") {
53
+ obj2.value = JSON.parse(obj2.value)
24
54
  }
25
- if (formatFields && typeof obj2.value === 'object' && !Array.isArray(obj2.value)) {
26
- obj2.value = formatFields(obj2.value);
55
+ if (formatFields && typeof obj2.value === "object" && !Array.isArray(obj2.value)) {
56
+ obj2.value = formatFields(obj2.value)
27
57
  }
28
58
  }
29
- return obj2;
59
+ return obj2
30
60
  }
31
61
 
32
- export function getDataFields(form?: HTMLElement|null): HTMLElement[] {
33
- let results: HTMLElement[] = [];
62
+ export function getDataFields(form?: HTMLElement | null): HTMLElement[] {
63
+ let results: HTMLElement[] = []
34
64
  if (!form) {
35
- return results;
65
+ return results
36
66
  }
37
- const attributeValue = form.getAttribute('data-field');
67
+ const attributeValue = form.getAttribute("data-field")
38
68
  if (attributeValue && attributeValue.length > 0) {
39
- results.push(form);
69
+ results.push(form)
40
70
  }
41
- const childNodes = form.childNodes;
71
+ const childNodes = form.childNodes
42
72
  if (childNodes.length > 0) {
43
73
  // tslint:disable-next-line:prefer-for-of
44
74
  for (let i = 0; i < childNodes.length; i++) {
45
- const childNode = childNodes[i] as HTMLElement;
75
+ const childNode = childNodes[i] as HTMLElement
46
76
  if (childNode.nodeType === Node.ELEMENT_NODE) {
47
- results = results.concat(getDataFields(childNode));
77
+ results = results.concat(getDataFields(childNode))
48
78
  }
49
79
  }
50
80
  }
51
- return results;
81
+ return results
52
82
  }
package/src/edit.ts CHANGED
@@ -1,121 +1,52 @@
1
- import { NavigateFunction } from 'react-router-dom';
2
- import {Attribute, Attributes, ErrorMessage, LoadingService, Locale, resources, ResourceService, StringMap, UIService, ViewService} from './core';
3
-
4
- export interface ResultInfo<T> {
5
- status: number|string;
6
- errors?: ErrorMessage[];
7
- value?: T;
8
- message?: string;
9
- }
10
- export interface EditParameter {
11
- resource: ResourceService;
12
- showMessage: (msg: string, option?: string) => void;
13
- showError: (m: string, callback?: () => void, header?: string) => void;
14
- confirm: (m2: string, yesCallback?: () => void, header?: string, btnLeftText?: string, btnRightText?: string, noCallback?: () => void) => void;
15
- ui?: UIService;
16
- getLocale?: (profile?: string) => Locale;
17
- loading?: LoadingService;
18
- // status?: EditStatusConfig;
19
- }
20
- export interface GenericService<T, ID, R> extends ViewService<T, ID> {
21
- patch?(obj: Partial<T>, ctx?: any): Promise<R>;
22
- create(obj: T, ctx?: any): Promise<R>;
23
- update(obj: T, ctx?: any): Promise<R>;
24
- delete?(id: ID, ctx?: any): Promise<number>;
25
- }
26
- export interface MetaModel {
27
- keys?: string[];
28
- version?: string;
29
- }
30
- export function build(attributes: Attributes, name?: string): MetaModel|undefined {
31
- if (!attributes) {
32
- return undefined;
33
- }
34
- if (resources.cache && name && name.length > 0) {
35
- let meta: MetaModel = resources._cache[name];
36
- if (!meta) {
37
- meta = buildMetaModel(attributes);
38
- resources._cache[name] = meta;
39
- }
40
- return meta;
41
- } else {
42
- return buildMetaModel(attributes);
43
- }
44
- }
45
-
46
- function buildMetaModel(attributes: Attributes): MetaModel {
47
- if (!attributes) {
48
- return {};
49
- }
50
- /*
51
- if (model && !model.source) {
52
- model.source = model.name;
53
- }
54
- */
55
- const md: MetaModel = {};
56
- const pks: string[] = new Array<string>();
57
- const keys: string[] = Object.keys(attributes);
58
- for (const key of keys) {
59
- const attr: Attribute = attributes[key];
60
- if (attr) {
61
- if (attr.version) {
62
- md.version = key;
63
- }
64
- if (attr.key === true) {
65
- pks.push(key);
66
- }
67
- }
68
- }
69
- md.keys = pks;
70
- return md;
71
- }
1
+ import { NavigateFunction } from "react-router-dom"
2
+ import { Attributes, ErrorMessage, StringMap } from "./core"
72
3
 
73
4
  export function createModel<T>(attributes?: Attributes): T {
74
- const obj: any = {};
5
+ const obj: any = {}
75
6
  if (!attributes) {
76
- return obj;
7
+ return obj
77
8
  }
78
- const attrs = Object.keys(attributes);
9
+ const attrs = Object.keys(attributes)
79
10
  for (const k of attrs) {
80
- const attr = attributes[k];
11
+ const attr = attributes[k]
81
12
  if (attr.name) {
82
13
  switch (attr.type) {
83
- case 'string':
84
- case 'text':
85
- obj[attr.name] = '';
86
- break;
87
- case 'integer':
88
- case 'number':
89
- obj[attr.name] = 0;
90
- break;
91
- case 'array':
92
- obj[attr.name] = [];
93
- break;
94
- case 'boolean':
95
- obj[attr.name] = false;
96
- break;
97
- case 'date':
98
- obj[attr.name] = new Date();
99
- break;
100
- case 'object':
14
+ case "string":
15
+ case "text":
16
+ obj[attr.name] = ""
17
+ break
18
+ case "integer":
19
+ case "number":
20
+ obj[attr.name] = 0
21
+ break
22
+ case "array":
23
+ obj[attr.name] = []
24
+ break
25
+ case "boolean":
26
+ obj[attr.name] = false
27
+ break
28
+ case "date":
29
+ obj[attr.name] = new Date()
30
+ break
31
+ case "object":
101
32
  if (attr.typeof) {
102
- const object = createModel(attr.typeof);
103
- obj[attr.name] = object;
104
- break;
33
+ const object = createModel(attr.typeof)
34
+ obj[attr.name] = object
35
+ break
105
36
  } else {
106
- obj[attr.name] = {};
107
- break;
37
+ obj[attr.name] = {}
38
+ break
108
39
  }
109
- case 'ObjectId':
110
- obj[attr.name] = null;
111
- break;
40
+ case "ObjectId":
41
+ obj[attr.name] = null
42
+ break
112
43
  default:
113
- obj[attr.name] = '';
114
- break;
44
+ obj[attr.name] = ""
45
+ break
115
46
  }
116
47
  }
117
48
  }
118
- return obj;
49
+ return obj
119
50
  }
120
51
  /*
121
52
  export function initPropertyNullInModel<T>(obj: T, m?: Attributes): T {
@@ -142,25 +73,15 @@ export function handleStatus(x: number|string, st: EditStatusConfig, gv: (k: str
142
73
  }
143
74
  }
144
75
  */
145
- export function handleVersion<T>(obj: T, version?: string): void {
146
- if (obj && version && version.length > 0) {
147
- const v = (obj as any)[version];
148
- if (v && typeof v === 'number') {
149
- (obj as any)[version] = v + 1;
150
- } else {
151
- (obj as any)[version] = 1;
152
- }
153
- }
154
- }
155
- export function isSuccessful<T>(x: number|T|ErrorMessage[]): boolean {
76
+ export function isSuccessful<T>(x: number | T | ErrorMessage[]): boolean {
156
77
  if (Array.isArray(x)) {
157
- return false;
158
- } else if (typeof x === 'object') {
159
- return true;
160
- } else if (typeof x === 'number' && x > 0) {
161
- return true;
78
+ return false
79
+ } else if (typeof x === "object") {
80
+ return true
81
+ } else if (typeof x === "number" && x > 0) {
82
+ return true
162
83
  }
163
- return false;
84
+ return false
164
85
  }
165
86
  type Result<T> = number | T | ErrorMessage[]
166
87
  export function afterSaved<T>(
package/src/error.ts ADDED
@@ -0,0 +1,55 @@
1
+ import { StringMap } from "./core"
2
+
3
+ export interface Message {
4
+ message: string
5
+ title: string
6
+ yes?: string
7
+ no?: string
8
+ }
9
+ export function getString(key: string, gv: StringMap | ((key: string) => string)): string {
10
+ if (typeof gv === "function") {
11
+ return gv(key)
12
+ } else {
13
+ return gv[key]
14
+ }
15
+ }
16
+ export function message(gv: StringMap | ((key: string) => string), msg: string, title?: string, yes?: string, no?: string): Message {
17
+ const m2 = msg && msg.length > 0 ? getString(msg, gv) : ""
18
+ const m: Message = { message: m2, title: "" }
19
+ if (title && title.length > 0) {
20
+ m.title = getString(title, gv)
21
+ }
22
+ if (yes && yes.length > 0) {
23
+ m.yes = getString(yes, gv)
24
+ }
25
+ if (no && no.length > 0) {
26
+ m.no = getString(no, gv)
27
+ }
28
+ return m
29
+ }
30
+ export function messageByHttpStatus(status: number, gv: StringMap | ((key: string) => string)): string {
31
+ const k = "error_" + status
32
+ let msg = getString(k, gv)
33
+ if (!msg || msg.length === 0) {
34
+ msg = getString("error_500", gv)
35
+ }
36
+ return msg
37
+ }
38
+ export function error(err: any, gv: StringMap | ((key: string) => string), ae: (msg: string, callback?: () => void, header?: string) => void) {
39
+ const title = getString("error", gv)
40
+ let msg = getString("error_internal", gv)
41
+ if (!err) {
42
+ ae(msg, undefined, title)
43
+ return
44
+ }
45
+ const data = err && err.response ? err.response : err
46
+ if (data) {
47
+ const status = data.status
48
+ if (status && !isNaN(status)) {
49
+ msg = messageByHttpStatus(status, gv)
50
+ }
51
+ ae(msg, undefined, title)
52
+ } else {
53
+ ae(msg, undefined, title)
54
+ }
55
+ }
package/src/formutil.ts CHANGED
@@ -1,106 +1,95 @@
1
- export function setReadOnly(form?: HTMLFormElement|null, ...args: string[]): void {
1
+ export function setReadOnly(form?: HTMLFormElement | null, ...args: string[]): void {
2
2
  if (!form) {
3
- return;
3
+ return
4
4
  }
5
- const len = form.length;
5
+ const len = form.length
6
6
  for (let i = 0; i < len; i++) {
7
- const ctrl = form[i] as HTMLInputElement;
8
- const name = ctrl.getAttribute('name');
9
- let skip = false;
10
- if (name != null && name.length > 0 && name !== 'btnBack') {
7
+ const ctrl = form[i] as HTMLInputElement
8
+ const name = ctrl.getAttribute("name")
9
+ let skip = false
10
+ if (name != null && name.length > 0 && name !== "btnBack") {
11
11
  if (arguments.length > 1) {
12
12
  for (let j = 1; j < arguments.length; j++) {
13
13
  if (arguments[j] === name) {
14
- skip = true;
14
+ skip = true
15
15
  // continue; has bugs => why?
16
16
  }
17
17
  }
18
18
  }
19
19
  if (skip === false) {
20
- let nodeName = ctrl.nodeName;
21
- const type = ctrl.getAttribute('type');
22
- if (nodeName === 'INPUT' && type !== null) {
23
- nodeName = type.toUpperCase();
20
+ let nodeName = ctrl.nodeName
21
+ const type = ctrl.getAttribute("type")
22
+ if (nodeName === "INPUT" && type !== null) {
23
+ nodeName = type.toUpperCase()
24
24
  }
25
- if (nodeName !== 'BUTTON'
26
- && nodeName !== 'RESET'
27
- && nodeName !== 'SUBMIT'
28
- && nodeName !== 'SELECT') {
25
+ if (nodeName !== "BUTTON" && nodeName !== "RESET" && nodeName !== "SUBMIT" && nodeName !== "SELECT") {
29
26
  switch (type) {
30
- case 'checkbox':
31
- ctrl.disabled = true;
32
- break;
33
- case 'radio':
34
- ctrl.disabled = true;
35
- break;
27
+ case "checkbox":
28
+ ctrl.disabled = true
29
+ break
30
+ case "radio":
31
+ ctrl.disabled = true
32
+ break
36
33
  default:
37
- ctrl.readOnly = true;
34
+ ctrl.readOnly = true
38
35
  }
39
36
  } else {
40
- ctrl.disabled = true;
37
+ ctrl.disabled = true
41
38
  }
42
39
  }
43
40
  }
44
41
  }
45
42
  }
46
43
  export function focusFirstElement(form: HTMLFormElement): void {
47
- let i = 0;
48
- const len = form.length;
44
+ let i = 0
45
+ const len = form.length
49
46
  for (i = 0; i < len; i++) {
50
- const ctrl = form[i] as HTMLInputElement;
47
+ const ctrl = form[i] as HTMLInputElement
51
48
  if (!(ctrl.readOnly || ctrl.disabled)) {
52
- let nodeName = ctrl.nodeName;
53
- const type = ctrl.getAttribute('type');
49
+ let nodeName = ctrl.nodeName
50
+ const type = ctrl.getAttribute("type")
54
51
  if (type) {
55
- const t = type.toUpperCase();
56
- if (t === 'BUTTON' || t === 'SUBMIT') {
57
- ctrl.focus();
52
+ const t = type.toUpperCase()
53
+ if (t === "BUTTON" || t === "SUBMIT") {
54
+ ctrl.focus()
58
55
  }
59
- if (nodeName === 'INPUT') {
60
- nodeName = t;
56
+ if (nodeName === "INPUT") {
57
+ nodeName = t
61
58
  }
62
59
  }
63
- if (nodeName !== 'BUTTON'
64
- && nodeName !== 'RESET'
65
- && nodeName !== 'SUBMIT'
66
- && nodeName !== 'CHECKBOX'
67
- && nodeName !== 'RADIO') {
68
- ctrl.focus();
60
+ if (nodeName !== "BUTTON" && nodeName !== "RESET" && nodeName !== "SUBMIT" && nodeName !== "CHECKBOX" && nodeName !== "RADIO") {
61
+ ctrl.focus()
69
62
  try {
70
- ctrl.setSelectionRange(0, ctrl.value.length);
71
- } catch (err) {
72
- }
73
- return;
63
+ ctrl.setSelectionRange(0, ctrl.value.length)
64
+ } catch (err) {}
65
+ return
74
66
  }
75
67
  }
76
68
  }
77
69
  }
78
- export function focusFirstError(form?: HTMLFormElement|null, className?: string): void {
70
+ export function focusFirstError(form?: HTMLFormElement | null, className?: string): void {
79
71
  if (!form) {
80
- return;
72
+ return
81
73
  }
82
- const len = form.length;
74
+ const len = form.length
83
75
  if (className && className.length > 0) {
84
76
  for (let i = 0; i < len; i++) {
85
- const ctrl = form[i] as HTMLInputElement;
86
- const parent = ctrl.parentElement;
87
- if (ctrl.classList.contains(className)
88
- || (parent && parent.classList.contains(className))) {
89
- ctrl.focus();
90
- ctrl.scrollIntoView();
91
- return;
77
+ const ctrl = form[i] as HTMLInputElement
78
+ const parent = ctrl.parentElement
79
+ if (ctrl.classList.contains(className) || (parent && parent.classList.contains(className))) {
80
+ ctrl.focus()
81
+ ctrl.scrollIntoView()
82
+ return
92
83
  }
93
84
  }
94
85
  } else {
95
86
  for (let i = 0; i < len; i++) {
96
- const ctrl = form[i] as HTMLInputElement;
97
- const parent = ctrl.parentElement;
98
- if (ctrl.classList.contains('invalid')
99
- || ctrl.classList.contains('.ng-invalid')
100
- || (parent && parent.classList.contains('invalid'))) {
101
- ctrl.focus();
102
- ctrl.scrollIntoView();
103
- return;
87
+ const ctrl = form[i] as HTMLInputElement
88
+ const parent = ctrl.parentElement
89
+ if (ctrl.classList.contains("invalid") || ctrl.classList.contains(".ng-invalid") || (parent && parent.classList.contains("invalid"))) {
90
+ ctrl.focus()
91
+ ctrl.scrollIntoView()
92
+ return
104
93
  }
105
94
  }
106
95
  }