react-hook-core 0.5.0 → 0.5.2
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/edit.js +41 -27
- package/lib/index.js +0 -4
- package/lib/reflect.js +18 -39
- package/lib/route.js +5 -0
- package/lib/search.js +8 -8
- package/lib/state.js +0 -185
- package/lib/util.js +13 -2
- package/package.json +1 -1
- package/src/com.ts +1 -1
- package/src/core.ts +0 -9
- package/src/diff.ts +3 -11
- package/src/edit.ts +58 -36
- package/src/index.ts +1 -5
- package/src/route.ts +15 -0
- package/src/search.ts +9 -9
- package/src/state.ts +4 -195
- package/src/util.ts +14 -2
- package/tsconfig.json +1 -1
- package/lib/common.js +0 -87
- package/lib/formutil.js +0 -76
- package/lib/update.js +0 -143
- package/lib/useEdit.js +0 -582
- package/lib/useSearch.js +0 -591
- package/src/common.ts +0 -88
- package/src/formutil.ts +0 -69
- package/src/update.ts +0 -130
- package/src/useEdit.ts +0 -699
- package/src/useSearch.ts +0 -750
package/src/formutil.ts
DELETED
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
export function setReadOnly(form?: HTMLFormElement | null, ...args: string[]): void {
|
|
2
|
-
if (!form) {
|
|
3
|
-
return
|
|
4
|
-
}
|
|
5
|
-
const len = form.length
|
|
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") {
|
|
11
|
-
if (arguments.length > 1) {
|
|
12
|
-
for (let j = 1; j < arguments.length; j++) {
|
|
13
|
-
if (arguments[j] === name) {
|
|
14
|
-
skip = true
|
|
15
|
-
// continue; has bugs => why?
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
}
|
|
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()
|
|
24
|
-
}
|
|
25
|
-
if (nodeName !== "BUTTON" && nodeName !== "RESET" && nodeName !== "SUBMIT" && nodeName !== "SELECT") {
|
|
26
|
-
switch (type) {
|
|
27
|
-
case "checkbox":
|
|
28
|
-
ctrl.disabled = true
|
|
29
|
-
break
|
|
30
|
-
case "radio":
|
|
31
|
-
ctrl.disabled = true
|
|
32
|
-
break
|
|
33
|
-
default:
|
|
34
|
-
ctrl.readOnly = true
|
|
35
|
-
}
|
|
36
|
-
} else {
|
|
37
|
-
ctrl.disabled = true
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
export function focusFirstError(form?: HTMLFormElement | null, className?: string): void {
|
|
44
|
-
if (!form) {
|
|
45
|
-
return
|
|
46
|
-
}
|
|
47
|
-
const len = form.length
|
|
48
|
-
if (className && className.length > 0) {
|
|
49
|
-
for (let i = 0; i < len; i++) {
|
|
50
|
-
const ctrl = form[i] as HTMLInputElement
|
|
51
|
-
const parent = ctrl.parentElement
|
|
52
|
-
if (ctrl.classList.contains(className) || (parent && parent.classList.contains(className))) {
|
|
53
|
-
ctrl.focus()
|
|
54
|
-
ctrl.scrollIntoView()
|
|
55
|
-
return
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
} else {
|
|
59
|
-
for (let i = 0; i < len; i++) {
|
|
60
|
-
const ctrl = form[i] as HTMLInputElement
|
|
61
|
-
const parent = ctrl.parentElement
|
|
62
|
-
if (ctrl.classList.contains("invalid") || ctrl.classList.contains(".ng-invalid") || (parent && parent.classList.contains("invalid"))) {
|
|
63
|
-
ctrl.focus()
|
|
64
|
-
ctrl.scrollIntoView()
|
|
65
|
-
return
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
}
|
package/src/update.ts
DELETED
|
@@ -1,130 +0,0 @@
|
|
|
1
|
-
import { useCallback, useEffect, useRef, useState } from "react"
|
|
2
|
-
import { Locale, removePhoneFormat } from "./core"
|
|
3
|
-
import { buildFlatState, buildState, handleEvent, localeOf } from "./state"
|
|
4
|
-
|
|
5
|
-
export function getModelName(form?: HTMLFormElement | null, name?: string): string {
|
|
6
|
-
if (form) {
|
|
7
|
-
const a = form.getAttribute("model-name")
|
|
8
|
-
if (a && a.length > 0) {
|
|
9
|
-
return a
|
|
10
|
-
}
|
|
11
|
-
const b = form.name
|
|
12
|
-
if (b) {
|
|
13
|
-
if (b.endsWith("Form")) {
|
|
14
|
-
return b.substring(0, b.length - 4)
|
|
15
|
-
}
|
|
16
|
-
return b
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
if (name && name.length > 0) {
|
|
20
|
-
return name
|
|
21
|
-
}
|
|
22
|
-
return ""
|
|
23
|
-
}
|
|
24
|
-
const m = "model"
|
|
25
|
-
const _getModelName = (f2?: HTMLFormElement | null): string => {
|
|
26
|
-
return getModelName(f2, m)
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export type Callback<T> = (value?: T) => void
|
|
30
|
-
export type DispatchWithCallback<T> = (value: T, callback?: Callback<T>) => void
|
|
31
|
-
|
|
32
|
-
export function useMergeState<T>(initialState?: T | (() => T)): [T, DispatchWithCallback<Partial<T>>] {
|
|
33
|
-
const [state, _setState] = useState(initialState ? initialState : ({} as any))
|
|
34
|
-
|
|
35
|
-
const callbackRef = useRef<Callback<T>>(null)
|
|
36
|
-
|
|
37
|
-
const setState = useCallback(
|
|
38
|
-
(newState: Partial<T>, callback?: Callback<T>): void => {
|
|
39
|
-
;(callbackRef as any).current = callback
|
|
40
|
-
_setState((prevState: any) => Object.assign({}, prevState, newState))
|
|
41
|
-
},
|
|
42
|
-
[state],
|
|
43
|
-
)
|
|
44
|
-
|
|
45
|
-
useEffect(() => {
|
|
46
|
-
if (callbackRef.current) {
|
|
47
|
-
callbackRef.current(state)
|
|
48
|
-
}
|
|
49
|
-
}, [state])
|
|
50
|
-
|
|
51
|
-
return [state, setState]
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export const useUpdate = <T>(
|
|
55
|
-
initialState: T,
|
|
56
|
-
getName?: ((f?: HTMLFormElement | null) => string) | string,
|
|
57
|
-
getLocale?: (() => Locale) | Locale,
|
|
58
|
-
removeErr?: (ctrl: HTMLInputElement) => void,
|
|
59
|
-
) => {
|
|
60
|
-
const [state, setState] = useMergeState<T>(initialState)
|
|
61
|
-
const [rerender, setRerender] = useState(false)
|
|
62
|
-
|
|
63
|
-
// trigger re-render page when change state in useSearch
|
|
64
|
-
useEffect(() => {
|
|
65
|
-
setRerender(!rerender)
|
|
66
|
-
}, [state])
|
|
67
|
-
|
|
68
|
-
const updatePhoneState = (event: any) => {
|
|
69
|
-
const re = /^[0-9\b]+$/
|
|
70
|
-
const target = event.currentTarget as HTMLInputElement
|
|
71
|
-
const value = removePhoneFormat(target.value)
|
|
72
|
-
if (re.test(value) || !value) {
|
|
73
|
-
updateState(event)
|
|
74
|
-
} else {
|
|
75
|
-
const splitArr = value.split("")
|
|
76
|
-
let responseStr = ""
|
|
77
|
-
splitArr.forEach((element) => {
|
|
78
|
-
if (re.test(element)) {
|
|
79
|
-
responseStr += element
|
|
80
|
-
}
|
|
81
|
-
})
|
|
82
|
-
target.value = responseStr
|
|
83
|
-
updateState(event)
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
const getModelName: (f2?: HTMLFormElement | null) => string = typeof getName === "function" ? getName : _getModelName
|
|
87
|
-
|
|
88
|
-
const updateState = (e: any, callback?: (prev: any) => void, lc?: Locale) => {
|
|
89
|
-
const ctrl = e.currentTarget as HTMLInputElement
|
|
90
|
-
let mn: string = m
|
|
91
|
-
if (getName) {
|
|
92
|
-
if (typeof getName === "string") {
|
|
93
|
-
mn = getName
|
|
94
|
-
} else {
|
|
95
|
-
mn = getName(ctrl.form)
|
|
96
|
-
}
|
|
97
|
-
} else {
|
|
98
|
-
mn = _getModelName(ctrl.form)
|
|
99
|
-
}
|
|
100
|
-
const l = localeOf(lc, getLocale)
|
|
101
|
-
handleEvent(e, removeErr)
|
|
102
|
-
const objSet = buildState<T, any>(e, state, ctrl, mn, l)
|
|
103
|
-
if (objSet) {
|
|
104
|
-
if (callback) {
|
|
105
|
-
setState({ ...objSet }, callback)
|
|
106
|
-
} else {
|
|
107
|
-
setState(objSet)
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
const updateFlatState = (e: any, callback?: () => void, lc?: Locale) => {
|
|
112
|
-
const objSet = buildFlatState<T, any>(e, state, lc)
|
|
113
|
-
if (objSet) {
|
|
114
|
-
if (callback) {
|
|
115
|
-
setState(objSet, callback)
|
|
116
|
-
} else {
|
|
117
|
-
setState(objSet)
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
return {
|
|
122
|
-
getModelName,
|
|
123
|
-
updateState,
|
|
124
|
-
updatePhoneState,
|
|
125
|
-
updateFlatState,
|
|
126
|
-
getLocale,
|
|
127
|
-
setState,
|
|
128
|
-
state,
|
|
129
|
-
}
|
|
130
|
-
}
|