react-hook-core 0.4.7 → 0.4.9
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/lib/core.js +1 -169
- package/lib/formutil.js +0 -28
- package/lib/index.js +25 -0
- package/lib/input.js +49 -0
- package/lib/reflect.js +0 -10
- package/lib/route.js +11 -0
- package/lib/search.js +14 -31
- package/lib/update.js +30 -2
- package/lib/useEdit.js +57 -16
- package/lib/useSearch.js +41 -9
- package/lib/util.js +3 -3
- package/package.json +1 -1
- package/src/core.ts +2 -244
- package/src/edit.ts +2 -25
- package/src/formutil.ts +0 -27
- package/src/index.ts +26 -0
- package/src/input.ts +47 -15
- package/src/reflect.ts +0 -19
- package/src/route.ts +19 -0
- package/src/search.ts +13 -31
- package/src/update.ts +29 -2
- package/src/useEdit.ts +44 -17
- package/src/useSearch.ts +40 -17
- package/src/util.ts +3 -3
- package/lib/useView.js +0 -133
- package/src/useView.ts +0 -183
package/lib/useSearch.js
CHANGED
|
@@ -24,12 +24,40 @@ Object.defineProperty(exports, "__esModule", { value: true })
|
|
|
24
24
|
var react_1 = require("react")
|
|
25
25
|
var core_1 = require("./core")
|
|
26
26
|
var error_1 = require("./error")
|
|
27
|
+
var input_1 = require("./input")
|
|
27
28
|
var merge_1 = require("./merge")
|
|
28
29
|
var reflect_1 = require("./reflect")
|
|
29
30
|
var route_1 = require("./route")
|
|
30
31
|
var search_1 = require("./search")
|
|
31
32
|
var state_1 = require("./state")
|
|
32
33
|
var update_1 = require("./update")
|
|
34
|
+
function showPaging(com, list, pageSize, total) {
|
|
35
|
+
com.total = total
|
|
36
|
+
var pageTotal = search_1.getPageTotal(pageSize, total)
|
|
37
|
+
com.pages = pageTotal
|
|
38
|
+
com.showPaging = !total || com.pages <= 1 || (list && list.length >= total) ? false : true
|
|
39
|
+
}
|
|
40
|
+
exports.showPaging = showPaging
|
|
41
|
+
function removeFormError(u, f) {
|
|
42
|
+
if (f && u && u.ui) {
|
|
43
|
+
u.ui.removeFormError(f)
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
exports.removeFormError = removeFormError
|
|
47
|
+
function getValidateForm(u, vf) {
|
|
48
|
+
if (vf) {
|
|
49
|
+
return vf
|
|
50
|
+
}
|
|
51
|
+
return u && u.ui ? u.ui.validateForm : undefined
|
|
52
|
+
}
|
|
53
|
+
exports.getValidateForm = getValidateForm
|
|
54
|
+
function getRemoveError(u, rmErr) {
|
|
55
|
+
if (rmErr) {
|
|
56
|
+
return rmErr
|
|
57
|
+
}
|
|
58
|
+
return u && u.ui ? u.ui.removeError : undefined
|
|
59
|
+
}
|
|
60
|
+
exports.getRemoveError = getRemoveError
|
|
33
61
|
function getModel(state, modelName, searchable, fields, excluding) {
|
|
34
62
|
var obj2 = getModelFromState(state, modelName)
|
|
35
63
|
var obj = obj2 ? obj2 : {}
|
|
@@ -259,7 +287,7 @@ exports.useSearch = function (refForm, initialState, service, p2, p) {
|
|
|
259
287
|
searchError = baseProps.searchError
|
|
260
288
|
if (refForm) {
|
|
261
289
|
var registerEvents = p2.ui ? p2.ui.registerEvents : undefined
|
|
262
|
-
|
|
290
|
+
input_1.initForm(refForm.current, registerEvents)
|
|
263
291
|
}
|
|
264
292
|
if (p && p.initialize) {
|
|
265
293
|
p.initialize(load, setState, component)
|
|
@@ -281,16 +309,20 @@ exports.useSearchOneProps = function (p) {
|
|
|
281
309
|
exports.useSearchOne = function (p) {
|
|
282
310
|
return exports.useCoreSearch(p.refForm, p.initialState, p.service, p, p)
|
|
283
311
|
}
|
|
312
|
+
function getName(d, n) {
|
|
313
|
+
return n && n.length > 0 ? n : d
|
|
314
|
+
}
|
|
315
|
+
exports.getName = getName
|
|
284
316
|
exports.useCoreSearch = function (refForm, initialState, service, p1, p2) {
|
|
285
317
|
var p = mergeParam(p2)
|
|
286
318
|
var _a = react_1.useState(),
|
|
287
319
|
running = _a[0],
|
|
288
320
|
setRunning = _a[1]
|
|
289
321
|
var _getModelName = function () {
|
|
290
|
-
return
|
|
322
|
+
return getName("filter", p && p.name ? p.name : undefined)
|
|
291
323
|
}
|
|
292
324
|
var getModelName = p && p.getModelName ? p.getModelName : _getModelName
|
|
293
|
-
var baseProps = update_1.useUpdate(initialState, getModelName, p1.getLocale,
|
|
325
|
+
var baseProps = update_1.useUpdate(initialState, getModelName, p1.getLocale, getRemoveError(p1))
|
|
294
326
|
var state = baseProps.state,
|
|
295
327
|
setState = baseProps.setState
|
|
296
328
|
var _b = react_1.useState(false),
|
|
@@ -359,7 +391,7 @@ exports.useCoreSearch = function (refForm, initialState, service, p1, p2) {
|
|
|
359
391
|
}
|
|
360
392
|
var load = p && p.load ? p.load : _load
|
|
361
393
|
var doSearch = function (se, isFirstLoad) {
|
|
362
|
-
|
|
394
|
+
removeFormError(p1, refForm.current)
|
|
363
395
|
var s = getFilter(se)
|
|
364
396
|
if (isFirstLoad) {
|
|
365
397
|
setState(state)
|
|
@@ -370,7 +402,7 @@ exports.useCoreSearch = function (refForm, initialState, service, p1, p2) {
|
|
|
370
402
|
return
|
|
371
403
|
}
|
|
372
404
|
setRunning(true)
|
|
373
|
-
|
|
405
|
+
input_1.showLoading(p1.loading)
|
|
374
406
|
if (p && !p.ignoreUrlParam) {
|
|
375
407
|
search_1.addParametersIntoUrl(s, isFirstLoad)
|
|
376
408
|
}
|
|
@@ -383,7 +415,7 @@ exports.useCoreSearch = function (refForm, initialState, service, p1, p2) {
|
|
|
383
415
|
})
|
|
384
416
|
}
|
|
385
417
|
var _validateSearch = function (se, callback) {
|
|
386
|
-
validate(se, callback, refForm.current, p1.getLocale ? p1.getLocale() : undefined,
|
|
418
|
+
validate(se, callback, refForm.current, p1.getLocale ? p1.getLocale() : undefined, getValidateForm(p1))
|
|
387
419
|
}
|
|
388
420
|
var validateSearch = p && p.validateSearch ? p.validateSearch : _validateSearch
|
|
389
421
|
var pageSizeChanged = function (event) {
|
|
@@ -473,7 +505,7 @@ exports.useCoreSearch = function (refForm, initialState, service, p1, p2) {
|
|
|
473
505
|
var searchError = function (err) {
|
|
474
506
|
setComponent({ page: component.tmpPageIndex })
|
|
475
507
|
error_1.error(err, p1.resource.value, p1.showError)
|
|
476
|
-
|
|
508
|
+
input_1.hideLoading(p1.loading)
|
|
477
509
|
}
|
|
478
510
|
var appendList = p && p.appendList ? p.appendList : appendListOfState
|
|
479
511
|
var setList = p && p.setList ? p.setList : setListOfState
|
|
@@ -500,7 +532,7 @@ exports.useCoreSearch = function (refForm, initialState, service, p1, p2) {
|
|
|
500
532
|
setList(results, setState)
|
|
501
533
|
}
|
|
502
534
|
} else {
|
|
503
|
-
|
|
535
|
+
showPaging(component, sr.list, s.limit, sr.total)
|
|
504
536
|
setList(results, setState)
|
|
505
537
|
setComponent({ tmpPageIndex: s.page })
|
|
506
538
|
if (s.limit) {
|
|
@@ -509,7 +541,7 @@ exports.useCoreSearch = function (refForm, initialState, service, p1, p2) {
|
|
|
509
541
|
}
|
|
510
542
|
}
|
|
511
543
|
setRunning(false)
|
|
512
|
-
|
|
544
|
+
input_1.hideLoading(p1.loading)
|
|
513
545
|
if (component.triggerSearch) {
|
|
514
546
|
setComponent({ triggerSearch: false })
|
|
515
547
|
resetAndSearch()
|
package/lib/util.js
CHANGED
|
@@ -72,15 +72,15 @@ function getStringCurrency(value, datatype, locale, maxLength, isOnBlur) {
|
|
|
72
72
|
}
|
|
73
73
|
}
|
|
74
74
|
var dotPosition = value.indexOf(".")
|
|
75
|
-
var beforeDot = dotPosition >= 0 ? value.
|
|
75
|
+
var beforeDot = dotPosition >= 0 ? value.substring(0, dotPosition) : value
|
|
76
76
|
if (datatype === "string-currency" || isOnBlur) {
|
|
77
77
|
beforeDot = beforeDot.replace(new RegExp("\\B(?=(\\d{" + groupDigits + "})+(?!\\d))", "g"), groupSeparator)
|
|
78
78
|
}
|
|
79
79
|
var afterDot
|
|
80
80
|
if (dotPosition > 0) {
|
|
81
|
-
afterDot = value.
|
|
81
|
+
afterDot = value.substring(dotPosition + 1)
|
|
82
82
|
if (afterDot.length > decimalDigits) {
|
|
83
|
-
afterDot = afterDot.
|
|
83
|
+
afterDot = afterDot.substring(0, decimalDigits)
|
|
84
84
|
}
|
|
85
85
|
}
|
|
86
86
|
if (maxLength && beforeDot.length > maxLength - (decimalDigits + 1)) {
|
package/package.json
CHANGED
package/src/core.ts
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
import { Params } from "react-router"
|
|
2
|
-
import { focusFirstElement } from "./formutil"
|
|
3
|
-
|
|
4
1
|
export interface LoadingService {
|
|
5
2
|
showLoading(firstTime?: boolean): void
|
|
6
3
|
hideLoading(): void
|
|
7
4
|
}
|
|
8
5
|
export const pageSizes = [12, 24, 60, 100, 120, 180, 300, 600]
|
|
6
|
+
export const sizes = pageSizes
|
|
9
7
|
// tslint:disable-next-line:class-name
|
|
10
8
|
export class resources {
|
|
11
9
|
static phone = / |-|\.|\(|\)/g
|
|
@@ -18,31 +16,13 @@ export class resources {
|
|
|
18
16
|
static limits = pageSizes
|
|
19
17
|
static pageMaxSize = 7
|
|
20
18
|
}
|
|
21
|
-
export const size = pageSizes
|
|
22
19
|
|
|
23
|
-
export function getCurrencyCode(form?: HTMLFormElement | null): string | undefined {
|
|
24
|
-
if (form) {
|
|
25
|
-
const x = form.getAttribute("currency-code")
|
|
26
|
-
if (x) {
|
|
27
|
-
return x
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
return undefined
|
|
31
|
-
}
|
|
32
|
-
export function removePhoneFormat(phone: string): string {
|
|
33
|
-
if (phone) {
|
|
34
|
-
return phone.replace(resources.phone, "")
|
|
35
|
-
} else {
|
|
36
|
-
return phone
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
20
|
export interface StringMap {
|
|
40
21
|
[key: string]: string
|
|
41
22
|
}
|
|
42
23
|
export interface ResourceService {
|
|
43
24
|
resource(): StringMap
|
|
44
25
|
value(key: string, param?: any): string
|
|
45
|
-
format(f: string, ...args: any[]): string
|
|
46
26
|
}
|
|
47
27
|
|
|
48
28
|
export interface Locale {
|
|
@@ -98,13 +78,7 @@ export type DataType =
|
|
|
98
78
|
| "dates"
|
|
99
79
|
| "datetimes"
|
|
100
80
|
| "times"
|
|
101
|
-
|
|
102
|
-
export interface Metadata {
|
|
103
|
-
name?: string;
|
|
104
|
-
attributes: Attributes;
|
|
105
|
-
source?: string;
|
|
106
|
-
}
|
|
107
|
-
*/
|
|
81
|
+
|
|
108
82
|
export interface Attribute {
|
|
109
83
|
name?: string
|
|
110
84
|
type?: DataType
|
|
@@ -115,219 +89,3 @@ export interface Attribute {
|
|
|
115
89
|
export interface Attributes {
|
|
116
90
|
[key: string]: Attribute
|
|
117
91
|
}
|
|
118
|
-
|
|
119
|
-
export function buildKeys(attributes: Attributes): string[] {
|
|
120
|
-
if (!attributes) {
|
|
121
|
-
return []
|
|
122
|
-
}
|
|
123
|
-
const ks = Object.keys(attributes)
|
|
124
|
-
const ps = []
|
|
125
|
-
for (const k of ks) {
|
|
126
|
-
const attr: Attribute = attributes[k]
|
|
127
|
-
if (attr.key === true) {
|
|
128
|
-
ps.push(k)
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
return ps
|
|
132
|
-
}
|
|
133
|
-
type Readonly<T> = {
|
|
134
|
-
readonly [P in keyof T]: T[P]
|
|
135
|
-
}
|
|
136
|
-
export function buildId<ID>(p: Readonly<Params<string>>, keys?: string[]): ID | null {
|
|
137
|
-
if (!keys || keys.length === 0 || keys.length === 1) {
|
|
138
|
-
if (keys && keys.length === 1) {
|
|
139
|
-
if (p[keys[0]]) {
|
|
140
|
-
return p[keys[0]] as any
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
return p["id"] as any
|
|
144
|
-
}
|
|
145
|
-
const id: any = {}
|
|
146
|
-
for (const key of keys) {
|
|
147
|
-
let v = p[key]
|
|
148
|
-
if (!v) {
|
|
149
|
-
v = p[key]
|
|
150
|
-
if (!v) {
|
|
151
|
-
return null
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
id[key] = v
|
|
155
|
-
}
|
|
156
|
-
return id
|
|
157
|
-
}
|
|
158
|
-
export const datetimeToString = (inputDate: Date) => {
|
|
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
|
-
}
|
|
168
|
-
|
|
169
|
-
export const dateToString = (inputDate: Date) => {
|
|
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
|
-
}
|
|
175
|
-
/*
|
|
176
|
-
export function formatFax(value: string) {
|
|
177
|
-
return formatter.formatFax(value);
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
export function formatPhone(value: string) {
|
|
181
|
-
return formatter.formatPhone(value);
|
|
182
|
-
}
|
|
183
|
-
export function formatNumber(num: string|number, scale?: number, locale?: Locale): string {
|
|
184
|
-
if (!scale) {
|
|
185
|
-
scale = 2;
|
|
186
|
-
}
|
|
187
|
-
if (!locale) {
|
|
188
|
-
locale = storage.getLocale();
|
|
189
|
-
}
|
|
190
|
-
let c: number;
|
|
191
|
-
if (!num) {
|
|
192
|
-
return '';
|
|
193
|
-
} else if (typeof num === 'number') {
|
|
194
|
-
c = num;
|
|
195
|
-
} else {
|
|
196
|
-
const x: any = num;
|
|
197
|
-
if (isNaN(x)) {
|
|
198
|
-
return '';
|
|
199
|
-
} else {
|
|
200
|
-
c = parseFloat(x);
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
return storage.locale().formatNumber(c, scale, locale);
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
export function formatCurrency(currency: string|number, locale?: Locale, currencyCode?: string) {
|
|
207
|
-
if (!currencyCode) {
|
|
208
|
-
currencyCode = 'USD';
|
|
209
|
-
}
|
|
210
|
-
if (!locale) {
|
|
211
|
-
locale = storage.getLocale();
|
|
212
|
-
}
|
|
213
|
-
let c: number;
|
|
214
|
-
if (!currency) {
|
|
215
|
-
return '';
|
|
216
|
-
} else if (typeof currency === 'number') {
|
|
217
|
-
c = currency;
|
|
218
|
-
} else {
|
|
219
|
-
let x: any = currency;
|
|
220
|
-
x = x.replace(locale.decimalSeparator, '.');
|
|
221
|
-
if (isNaN(x)) {
|
|
222
|
-
return '';
|
|
223
|
-
} else {
|
|
224
|
-
c = parseFloat(x);
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
return storage.locale().formatCurrency(c, currencyCode, locale);
|
|
228
|
-
}
|
|
229
|
-
*/
|
|
230
|
-
|
|
231
|
-
export function initForm(form?: HTMLFormElement, initMat?: (f: HTMLFormElement) => void): HTMLFormElement | undefined {
|
|
232
|
-
if (form) {
|
|
233
|
-
setTimeout(() => {
|
|
234
|
-
if (initMat) {
|
|
235
|
-
initMat(form)
|
|
236
|
-
}
|
|
237
|
-
focusFirstElement(form)
|
|
238
|
-
}, 100)
|
|
239
|
-
}
|
|
240
|
-
return form
|
|
241
|
-
}
|
|
242
|
-
export function getName(d: string, n?: string): string {
|
|
243
|
-
return n && n.length > 0 ? n : d
|
|
244
|
-
}
|
|
245
|
-
export function getModelName(form?: HTMLFormElement | null, name?: string): string {
|
|
246
|
-
if (form) {
|
|
247
|
-
const a = form.getAttribute("model-name")
|
|
248
|
-
if (a && a.length > 0) {
|
|
249
|
-
return a
|
|
250
|
-
}
|
|
251
|
-
const b = form.name
|
|
252
|
-
if (b) {
|
|
253
|
-
if (b.endsWith("Form")) {
|
|
254
|
-
return b.substr(0, b.length - 4)
|
|
255
|
-
}
|
|
256
|
-
return b
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
if (name && name.length > 0) {
|
|
260
|
-
return name
|
|
261
|
-
}
|
|
262
|
-
return ""
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
export const scrollToFocus = (e: any, isUseTimeOut?: boolean) => {
|
|
266
|
-
try {
|
|
267
|
-
const element = e.target as HTMLInputElement
|
|
268
|
-
const form = element.form
|
|
269
|
-
if (form) {
|
|
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
|
|
277
|
-
setTimeout(() => {
|
|
278
|
-
if (isChrome) {
|
|
279
|
-
const scrollPosition = scrollTop === 0 ? elementRect.top + 64 : scrollTop + middle
|
|
280
|
-
container.scrollTo(0, Math.abs(scrollPosition))
|
|
281
|
-
} else {
|
|
282
|
-
container.scrollTo(0, Math.abs(scrollTop + middle))
|
|
283
|
-
}
|
|
284
|
-
}, timeOut)
|
|
285
|
-
}
|
|
286
|
-
} catch (e) {
|
|
287
|
-
console.log(e)
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
export interface LoadingParameter {
|
|
291
|
-
loading?: LoadingService
|
|
292
|
-
}
|
|
293
|
-
export function showLoading(s?: LoadingService): void {
|
|
294
|
-
if (s) {
|
|
295
|
-
s.showLoading()
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
export function hideLoading(s?: LoadingService): void {
|
|
299
|
-
if (s) {
|
|
300
|
-
s.hideLoading()
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
export interface UIParameter {
|
|
304
|
-
ui?: UIService
|
|
305
|
-
}
|
|
306
|
-
export function getRemoveError(u?: UIParameter, rmErr?: (el: HTMLInputElement) => void): ((el: HTMLInputElement) => void) | undefined {
|
|
307
|
-
if (rmErr) {
|
|
308
|
-
return rmErr
|
|
309
|
-
}
|
|
310
|
-
return u && u.ui ? u.ui.removeError : undefined
|
|
311
|
-
}
|
|
312
|
-
export function removeFormError(u?: UIParameter, f?: HTMLFormElement): void {
|
|
313
|
-
if (f && u && u.ui) {
|
|
314
|
-
u.ui.removeFormError(f)
|
|
315
|
-
}
|
|
316
|
-
}
|
|
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 {
|
|
321
|
-
if (vf) {
|
|
322
|
-
return vf
|
|
323
|
-
}
|
|
324
|
-
return u && u.ui ? u.ui.validateForm : undefined
|
|
325
|
-
}
|
|
326
|
-
/*
|
|
327
|
-
export function getDecodeFromForm(u?: UIParameter, d?: (form: HTMLFormElement, locale?: Locale, currencyCode?: string) => any): ((form: HTMLFormElement, locale?: Locale, currencyCode?: string) => any) | undefined {
|
|
328
|
-
if (d) {
|
|
329
|
-
return d;
|
|
330
|
-
}
|
|
331
|
-
return (u && u.ui ? u.ui.decodeFromForm : undefined);
|
|
332
|
-
}
|
|
333
|
-
*/
|
package/src/edit.ts
CHANGED
|
@@ -48,31 +48,7 @@ export function createModel<T>(attributes?: Attributes): T {
|
|
|
48
48
|
}
|
|
49
49
|
return obj
|
|
50
50
|
}
|
|
51
|
-
|
|
52
|
-
export function initPropertyNullInModel<T>(obj: T, m?: Attributes): T {
|
|
53
|
-
if (!m) {
|
|
54
|
-
const x: any = {};
|
|
55
|
-
return x;
|
|
56
|
-
}
|
|
57
|
-
const model = createModel(m);
|
|
58
|
-
for (const key of Object.keys(model as any)) {
|
|
59
|
-
if (obj && !(obj as any).hasOwnProperty(key)) {
|
|
60
|
-
(obj as any)[key] = (model as any)[key];
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
return obj;
|
|
64
|
-
}
|
|
65
|
-
export function handleStatus(x: number|string, st: EditStatusConfig, gv: (k: string, p?: any) => string, se: (m: string, title?: string, detail?: string, callback?: () => void) => void): void {
|
|
66
|
-
const title = gv('error');
|
|
67
|
-
if (x === st.version_error) {
|
|
68
|
-
se(gv('error_version'), title);
|
|
69
|
-
} else if (x === st.data_corrupt) {
|
|
70
|
-
se(gv('error_data_corrupt'), title);
|
|
71
|
-
} else {
|
|
72
|
-
se(gv('error_internal'), title);
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
*/
|
|
51
|
+
|
|
76
52
|
export function isSuccessful<T>(x: number | T | ErrorMessage[]): boolean {
|
|
77
53
|
if (Array.isArray(x)) {
|
|
78
54
|
return false
|
|
@@ -83,6 +59,7 @@ export function isSuccessful<T>(x: number | T | ErrorMessage[]): boolean {
|
|
|
83
59
|
}
|
|
84
60
|
return false
|
|
85
61
|
}
|
|
62
|
+
|
|
86
63
|
type Result<T> = number | T | ErrorMessage[]
|
|
87
64
|
export function afterSaved<T>(
|
|
88
65
|
res: Result<T>,
|
package/src/formutil.ts
CHANGED
|
@@ -40,33 +40,6 @@ export function setReadOnly(form?: HTMLFormElement | null, ...args: string[]): v
|
|
|
40
40
|
}
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
|
-
export function focusFirstElement(form: HTMLFormElement): void {
|
|
44
|
-
let i = 0
|
|
45
|
-
const len = form.length
|
|
46
|
-
for (i = 0; i < len; i++) {
|
|
47
|
-
const ctrl = form[i] as HTMLInputElement
|
|
48
|
-
if (!(ctrl.readOnly || ctrl.disabled)) {
|
|
49
|
-
let nodeName = ctrl.nodeName
|
|
50
|
-
const type = ctrl.getAttribute("type")
|
|
51
|
-
if (type) {
|
|
52
|
-
const t = type.toUpperCase()
|
|
53
|
-
if (t === "BUTTON" || t === "SUBMIT") {
|
|
54
|
-
ctrl.focus()
|
|
55
|
-
}
|
|
56
|
-
if (nodeName === "INPUT") {
|
|
57
|
-
nodeName = t
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
if (nodeName !== "BUTTON" && nodeName !== "RESET" && nodeName !== "SUBMIT" && nodeName !== "CHECKBOX" && nodeName !== "RADIO") {
|
|
61
|
-
ctrl.focus()
|
|
62
|
-
try {
|
|
63
|
-
ctrl.setSelectionRange(0, ctrl.value.length)
|
|
64
|
-
} catch (err) {}
|
|
65
|
-
return
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
43
|
export function focusFirstError(form?: HTMLFormElement | null, className?: string): void {
|
|
71
44
|
if (!form) {
|
|
72
45
|
return
|
package/src/index.ts
CHANGED
|
@@ -203,3 +203,29 @@ export function datetimeToString(date?: Date | string): string | undefined {
|
|
|
203
203
|
export function getNumber(event: ChangeEvent<HTMLSelectElement | HTMLInputElement>): number {
|
|
204
204
|
return parseInt(event.currentTarget.value, 10)
|
|
205
205
|
}
|
|
206
|
+
|
|
207
|
+
export const scrollToFocus = (e: any, isUseTimeOut?: boolean) => {
|
|
208
|
+
try {
|
|
209
|
+
const element = e.target as HTMLInputElement
|
|
210
|
+
const form = element.form
|
|
211
|
+
if (form) {
|
|
212
|
+
const container = form.childNodes[1] as HTMLElement
|
|
213
|
+
const elementRect = element.getBoundingClientRect()
|
|
214
|
+
const absoluteElementTop = elementRect.top + window.pageYOffset
|
|
215
|
+
const middle = absoluteElementTop - window.innerHeight / 2
|
|
216
|
+
const scrollTop = container.scrollTop
|
|
217
|
+
const timeOut = isUseTimeOut ? 300 : 0
|
|
218
|
+
const isChrome = navigator.userAgent.search("Chrome") > 0
|
|
219
|
+
setTimeout(() => {
|
|
220
|
+
if (isChrome) {
|
|
221
|
+
const scrollPosition = scrollTop === 0 ? elementRect.top + 64 : scrollTop + middle
|
|
222
|
+
container.scrollTo(0, Math.abs(scrollPosition))
|
|
223
|
+
} else {
|
|
224
|
+
container.scrollTo(0, Math.abs(scrollTop + middle))
|
|
225
|
+
}
|
|
226
|
+
}, timeOut)
|
|
227
|
+
}
|
|
228
|
+
} catch (e) {
|
|
229
|
+
console.log(e)
|
|
230
|
+
}
|
|
231
|
+
}
|
package/src/input.ts
CHANGED
|
@@ -81,23 +81,55 @@ export function getErrorFunc(
|
|
|
81
81
|
}
|
|
82
82
|
return (p as any).showError
|
|
83
83
|
}
|
|
84
|
-
|
|
85
|
-
export
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
export function getEditStatusFunc(p: ResourceService|EditStatusParameter, status?: EditStatusConfig): EditStatusConfig {
|
|
89
|
-
if (status) {
|
|
90
|
-
return status;
|
|
84
|
+
|
|
85
|
+
export function showLoading(s?: LoadingService): void {
|
|
86
|
+
if (s) {
|
|
87
|
+
s.showLoading()
|
|
91
88
|
}
|
|
92
|
-
return (p as any).status;
|
|
93
89
|
}
|
|
94
|
-
export
|
|
95
|
-
|
|
90
|
+
export function hideLoading(s?: LoadingService): void {
|
|
91
|
+
if (s) {
|
|
92
|
+
s.hideLoading()
|
|
93
|
+
}
|
|
96
94
|
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
95
|
+
|
|
96
|
+
export function initForm(form?: HTMLFormElement, initMat?: (f: HTMLFormElement) => void): HTMLFormElement | undefined {
|
|
97
|
+
if (form) {
|
|
98
|
+
setTimeout(() => {
|
|
99
|
+
if (initMat) {
|
|
100
|
+
initMat(form)
|
|
101
|
+
}
|
|
102
|
+
focusFirstElement(form)
|
|
103
|
+
}, 100)
|
|
104
|
+
}
|
|
105
|
+
return form
|
|
106
|
+
}
|
|
107
|
+
export function focusFirstElement(form: HTMLFormElement): void {
|
|
108
|
+
let i = 0
|
|
109
|
+
const len = form.length
|
|
110
|
+
for (i = 0; i < len; i++) {
|
|
111
|
+
const ctrl = form[i] as HTMLInputElement
|
|
112
|
+
if (!(ctrl.readOnly || ctrl.disabled)) {
|
|
113
|
+
let nodeName = ctrl.nodeName
|
|
114
|
+
const type = ctrl.getAttribute("type")
|
|
115
|
+
if (type) {
|
|
116
|
+
const t = type.toUpperCase()
|
|
117
|
+
if (t === "BUTTON" || t === "SUBMIT") {
|
|
118
|
+
ctrl.focus()
|
|
119
|
+
}
|
|
120
|
+
if (nodeName === "INPUT") {
|
|
121
|
+
nodeName = t
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
if (nodeName !== "BUTTON" && nodeName !== "RESET" && nodeName !== "SUBMIT" && nodeName !== "CHECKBOX" && nodeName !== "RADIO") {
|
|
125
|
+
ctrl.focus()
|
|
126
|
+
/*
|
|
127
|
+
try {
|
|
128
|
+
ctrl.setSelectionRange(0, ctrl.value.length)
|
|
129
|
+
} catch (err) {}
|
|
130
|
+
*/
|
|
131
|
+
return
|
|
132
|
+
}
|
|
133
|
+
}
|
|
100
134
|
}
|
|
101
|
-
return (p as any).status;
|
|
102
135
|
}
|
|
103
|
-
*/
|
package/src/reflect.ts
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
import { NavigateFunction } from "react-router-dom"
|
|
2
|
-
import { StringMap } from "./core"
|
|
3
|
-
|
|
4
1
|
export function clone(obj: any): any {
|
|
5
2
|
if (!obj) {
|
|
6
3
|
return obj
|
|
@@ -143,22 +140,6 @@ export function hasDiff<T>(o1: T, o2: T, keys?: string[], version?: string): boo
|
|
|
143
140
|
return !isEmptyObject(diff)
|
|
144
141
|
}
|
|
145
142
|
|
|
146
|
-
export function goBack<T>(
|
|
147
|
-
navigate: NavigateFunction,
|
|
148
|
-
confirm: (msg: string, yesCallback?: () => void) => void,
|
|
149
|
-
resource: StringMap,
|
|
150
|
-
o1: T,
|
|
151
|
-
o2: T,
|
|
152
|
-
keys?: string[],
|
|
153
|
-
version?: string,
|
|
154
|
-
) {
|
|
155
|
-
if (!hasDiff(o1, o2, keys, version)) {
|
|
156
|
-
navigate(-1)
|
|
157
|
-
} else {
|
|
158
|
-
confirm(resource.msg_confirm_back, () => navigate(-1))
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
|
|
162
143
|
export function notIn(s1: string[], s2: string[]): string[] {
|
|
163
144
|
const r = []
|
|
164
145
|
for (const s of s2) {
|
package/src/route.ts
CHANGED
|
@@ -1,6 +1,25 @@
|
|
|
1
1
|
import * as qs from "query-string"
|
|
2
|
+
import { NavigateFunction } from "react-router-dom"
|
|
3
|
+
import { StringMap } from "./core"
|
|
4
|
+
import { hasDiff } from "./reflect"
|
|
2
5
|
import { Filter } from "./search"
|
|
3
6
|
|
|
7
|
+
export function goBack<T>(
|
|
8
|
+
navigate: NavigateFunction,
|
|
9
|
+
confirm: (msg: string, yesCallback?: () => void) => void,
|
|
10
|
+
resource: StringMap,
|
|
11
|
+
o1: T,
|
|
12
|
+
o2: T,
|
|
13
|
+
keys?: string[],
|
|
14
|
+
version?: string,
|
|
15
|
+
) {
|
|
16
|
+
if (!hasDiff(o1, o2, keys, version)) {
|
|
17
|
+
navigate(-1)
|
|
18
|
+
} else {
|
|
19
|
+
confirm(resource.msg_confirm_back, () => navigate(-1))
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
4
23
|
export function buildFromUrl<S extends Filter>(modelT?: S): S {
|
|
5
24
|
return buildParameters<S>(window.location.search, modelT)
|
|
6
25
|
}
|