react-hook-core 0.4.5 → 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/lib/com.js +33 -11
- package/lib/core.js +103 -157
- package/lib/diff.js +28 -30
- package/lib/edit.js +54 -112
- package/lib/error.js +53 -0
- package/lib/formutil.js +58 -72
- package/lib/index.js +110 -95
- package/lib/input.js +31 -32
- package/lib/merge.js +26 -16
- package/lib/reflect.js +140 -147
- package/lib/route.js +45 -53
- package/lib/search.js +233 -475
- package/lib/state.js +159 -168
- package/lib/update.js +69 -64
- package/lib/useEdit.js +421 -348
- package/lib/useMessage.js +20 -20
- package/lib/useSearch.js +437 -246
- package/lib/useView.js +127 -120
- package/lib/util.js +74 -85
- package/package.json +1 -1
- package/src/com.ts +44 -30
- package/src/core.ts +147 -321
- package/src/diff.ts +54 -24
- package/src/edit.ts +42 -121
- package/src/error.ts +55 -0
- package/src/formutil.ts +51 -62
- package/src/index.ts +129 -114
- package/src/input.ts +44 -38
- package/src/merge.ts +16 -13
- package/src/reflect.ts +100 -101
- package/src/route.ts +40 -40
- package/src/search.ts +130 -316
- package/src/state.ts +135 -124
- package/src/update.ts +41 -41
- package/src/useEdit.ts +450 -319
- package/src/useMessage.ts +21 -21
- package/src/useSearch.ts +493 -277
- package/src/useView.ts +122 -97
- package/src/util.ts +67 -66
package/src/index.ts
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
import * as React from
|
|
2
|
-
import { ChangeEvent, ChangeEventHandler, FocusEvent, useEffect, useState } from
|
|
3
|
-
export * from
|
|
4
|
-
export * from
|
|
5
|
-
export * from
|
|
6
|
-
export * from
|
|
7
|
-
export * from
|
|
8
|
-
export * from
|
|
9
|
-
export * from
|
|
10
|
-
export * from
|
|
11
|
-
export * from
|
|
12
|
-
export * from
|
|
13
|
-
export * from
|
|
14
|
-
export * from
|
|
15
|
-
export * from
|
|
16
|
-
export * from
|
|
17
|
-
export * from
|
|
1
|
+
import * as React from "react"
|
|
2
|
+
import { ChangeEvent, ChangeEventHandler, FocusEvent, useEffect, useState } from "react"
|
|
3
|
+
export * from "./com"
|
|
4
|
+
export * from "./core"
|
|
5
|
+
export * from "./diff"
|
|
6
|
+
export * from "./edit"
|
|
7
|
+
export * from "./formutil"
|
|
8
|
+
export * from "./merge"
|
|
9
|
+
export * from "./reflect"
|
|
10
|
+
export * from "./route"
|
|
11
|
+
export * from "./search"
|
|
12
|
+
export * from "./state"
|
|
13
|
+
export * from "./update"
|
|
14
|
+
export * from "./useEdit"
|
|
15
|
+
export * from "./useMessage"
|
|
16
|
+
export * from "./useSearch"
|
|
17
|
+
export * from "./util"
|
|
18
18
|
/*
|
|
19
19
|
type CallBackType<T> = (updatedValue: T) => void;
|
|
20
20
|
type SetStateType<T> = T | ((prev: T) => T);
|
|
@@ -38,34 +38,38 @@ export const useCallbackState: RetType = <T>(initialValue: T | (() => T)) => {
|
|
|
38
38
|
return [state, setState];
|
|
39
39
|
};
|
|
40
40
|
*/
|
|
41
|
-
export function checked(s: string[]|string|undefined, v: string): boolean|undefined {
|
|
41
|
+
export function checked(s: string[] | string | undefined, v: string): boolean | undefined {
|
|
42
42
|
if (s) {
|
|
43
43
|
if (Array.isArray(s)) {
|
|
44
|
-
return s.includes(v)
|
|
44
|
+
return s.includes(v)
|
|
45
45
|
} else {
|
|
46
|
-
return s === v
|
|
46
|
+
return s === v
|
|
47
47
|
}
|
|
48
48
|
}
|
|
49
|
-
return false
|
|
49
|
+
return false
|
|
50
50
|
}
|
|
51
51
|
export function value<T>(obj?: T): T {
|
|
52
|
-
return
|
|
52
|
+
return obj ? obj : ({} as any)
|
|
53
53
|
}
|
|
54
54
|
export interface LoadingProps {
|
|
55
|
-
error?: any
|
|
55
|
+
error?: any
|
|
56
56
|
}
|
|
57
57
|
export const Loading = (props: LoadingProps) => {
|
|
58
58
|
const loadingStyle = {
|
|
59
|
-
top:
|
|
60
|
-
backgroundColor:
|
|
61
|
-
border:
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
}
|
|
59
|
+
top: "30%",
|
|
60
|
+
backgroundColor: "white",
|
|
61
|
+
border: "none",
|
|
62
|
+
WebkitBoxShadow: "none",
|
|
63
|
+
boxShadow: "none",
|
|
64
|
+
}
|
|
65
65
|
if (props.error) {
|
|
66
|
-
return React.createElement(
|
|
66
|
+
return React.createElement("div", null, "Error Load Module!") // return (<div>Error Load Module!</div>);
|
|
67
67
|
} else {
|
|
68
|
-
return
|
|
68
|
+
return React.createElement(
|
|
69
|
+
"div",
|
|
70
|
+
{ className: "loader-wrapper" },
|
|
71
|
+
React.createElement("div", { className: "loader-sign", style: loadingStyle }, React.createElement("div", { className: "loader" })),
|
|
72
|
+
)
|
|
69
73
|
/*
|
|
70
74
|
return (
|
|
71
75
|
<div className='loader-wrapper'>
|
|
@@ -75,116 +79,127 @@ export const Loading = (props: LoadingProps) => {
|
|
|
75
79
|
</div>
|
|
76
80
|
);*/
|
|
77
81
|
}
|
|
78
|
-
}
|
|
82
|
+
}
|
|
79
83
|
interface Locale {
|
|
80
|
-
decimalSeparator: string
|
|
81
|
-
groupSeparator: string
|
|
82
|
-
currencyCode: string
|
|
83
|
-
currencySymbol: string
|
|
84
|
-
currencyPattern: number
|
|
84
|
+
decimalSeparator: string
|
|
85
|
+
groupSeparator: string
|
|
86
|
+
currencyCode: string
|
|
87
|
+
currencySymbol: string
|
|
88
|
+
currencyPattern: number
|
|
85
89
|
}
|
|
86
90
|
interface InputProps {
|
|
87
|
-
name?: string
|
|
88
|
-
className?: string
|
|
89
|
-
value?: string
|
|
90
|
-
[
|
|
91
|
-
defaultValue?: string
|
|
92
|
-
onChangeNumber?: (value: number) => void
|
|
93
|
-
onChange?: ChangeEventHandler<HTMLInputElement
|
|
94
|
-
currencyOnBlur?: (event: Event|any, locale: Locale, currencyCode?: string, includingCurrencySymbol?: boolean) => void
|
|
95
|
-
currencyCode?: string
|
|
96
|
-
symbol?: boolean
|
|
97
|
-
readOnly?: boolean
|
|
98
|
-
locale?: Locale
|
|
99
|
-
type?: string
|
|
100
|
-
disabled?: boolean
|
|
101
|
-
required?: boolean
|
|
102
|
-
typeOutput?: string
|
|
103
|
-
min?: string|number
|
|
104
|
-
max?: string|number
|
|
105
|
-
allowZero?: boolean
|
|
91
|
+
name?: string
|
|
92
|
+
className?: string
|
|
93
|
+
value?: string
|
|
94
|
+
["data-field"]?: string
|
|
95
|
+
defaultValue?: string
|
|
96
|
+
onChangeNumber?: (value: number) => void
|
|
97
|
+
onChange?: ChangeEventHandler<HTMLInputElement>
|
|
98
|
+
currencyOnBlur?: (event: Event | any, locale: Locale, currencyCode?: string, includingCurrencySymbol?: boolean) => void
|
|
99
|
+
currencyCode?: string
|
|
100
|
+
symbol?: boolean
|
|
101
|
+
readOnly?: boolean
|
|
102
|
+
locale?: Locale
|
|
103
|
+
type?: string
|
|
104
|
+
disabled?: boolean
|
|
105
|
+
required?: boolean
|
|
106
|
+
typeOutput?: string
|
|
107
|
+
min?: string | number
|
|
108
|
+
max?: string | number
|
|
109
|
+
allowZero?: boolean
|
|
106
110
|
}
|
|
107
111
|
export const CurrencyInput = (props: InputProps) => {
|
|
108
|
-
const [state, setState] = useState<string|undefined>(undefined)
|
|
112
|
+
const [state, setState] = useState<string | undefined>(undefined)
|
|
109
113
|
useEffect(() => {
|
|
110
|
-
setState(props.value)
|
|
111
|
-
}, [props.value])
|
|
114
|
+
setState(props.value)
|
|
115
|
+
}, [props.value])
|
|
112
116
|
const onChange = (e: ChangeEvent<HTMLInputElement>) => {
|
|
113
|
-
const v1 = e.target.value
|
|
114
|
-
setState(v1)
|
|
117
|
+
const v1 = e.target.value
|
|
118
|
+
setState(v1)
|
|
115
119
|
if (props.onChange) {
|
|
116
|
-
props.onChange(e)
|
|
120
|
+
props.onChange(e)
|
|
117
121
|
}
|
|
118
122
|
if (props.onChangeNumber) {
|
|
119
|
-
props.onChangeNumber(parseFloat(v1))
|
|
123
|
+
props.onChangeNumber(parseFloat(v1))
|
|
120
124
|
}
|
|
121
|
-
}
|
|
125
|
+
}
|
|
122
126
|
const onBlur = (e: FocusEvent<HTMLInputElement>) => {
|
|
123
|
-
if (props.allowZero && e.target.value ===
|
|
124
|
-
setState(
|
|
125
|
-
return
|
|
127
|
+
if (props.allowZero && e.target.value === "0") {
|
|
128
|
+
setState("0")
|
|
129
|
+
return
|
|
126
130
|
}
|
|
127
131
|
if (props.locale && props.currencyOnBlur) {
|
|
128
|
-
props.currencyOnBlur(e, props.locale, props.currencyCode, props.symbol)
|
|
132
|
+
props.currencyOnBlur(e, props.locale, props.currencyCode, props.symbol)
|
|
129
133
|
}
|
|
130
134
|
setTimeout(() => {
|
|
131
|
-
const v2 = e.target.value
|
|
132
|
-
setState(v2)
|
|
133
|
-
}, 50)
|
|
135
|
+
const v2 = e.target.value
|
|
136
|
+
setState(v2)
|
|
137
|
+
}, 50)
|
|
134
138
|
}
|
|
135
139
|
// return <input className={props.className} onBlur={onBlur} type={props.type} name={props.name} onChange={props.onChange ? props.onChange : onChange} disabled={props.disabled} data-field={props['data-field']} min={props.min} max={props.max} value={state} />;
|
|
136
|
-
return React.createElement("input", {
|
|
137
|
-
|
|
138
|
-
|
|
140
|
+
return React.createElement("input", {
|
|
141
|
+
className: props.className,
|
|
142
|
+
onBlur: onBlur,
|
|
143
|
+
type: props.type,
|
|
144
|
+
name: props.name,
|
|
145
|
+
onChange: props.onChange ? props.onChange : onChange,
|
|
146
|
+
disabled: props.disabled,
|
|
147
|
+
"data-field": props["data-field"],
|
|
148
|
+
min: props.min,
|
|
149
|
+
max: props.max,
|
|
150
|
+
value: state,
|
|
151
|
+
})
|
|
152
|
+
}
|
|
153
|
+
export type OnClick = React.MouseEvent<HTMLElement, MouseEvent>
|
|
139
154
|
export function getParam(url: string, i?: number): string {
|
|
140
|
-
const ps = url.split(
|
|
155
|
+
const ps = url.split("/")
|
|
141
156
|
if (!i || i < 0) {
|
|
142
|
-
i = 0
|
|
157
|
+
i = 0
|
|
143
158
|
}
|
|
144
|
-
return ps[ps.length - 1 - i]
|
|
159
|
+
return ps[ps.length - 1 - i]
|
|
145
160
|
}
|
|
146
161
|
export function formatDate(date: Date | null | undefined, format: string): string {
|
|
147
162
|
if (!date) {
|
|
148
|
-
return
|
|
163
|
+
return ""
|
|
149
164
|
}
|
|
150
165
|
const opts: any = {
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
}
|
|
159
|
-
const d2 = new Date(date).toLocaleString("en-US", opts)
|
|
160
|
-
let od = format.replace("YYYY", d2.slice(6, 10))
|
|
161
|
-
od = od.replace("MM", d2.slice(0, 2))
|
|
162
|
-
od = od.replace("DD", d2.slice(3, 5))
|
|
163
|
-
od = od.replace("HH", d2.slice(12, 14))
|
|
164
|
-
od = od.replace("mm", d2.slice(15, 17))
|
|
165
|
-
od = od.replace("ss", d2.slice(18, 20))
|
|
166
|
-
return od
|
|
167
|
-
}
|
|
166
|
+
year: "numeric",
|
|
167
|
+
month: "2-digit",
|
|
168
|
+
day: "2-digit",
|
|
169
|
+
hour: "numeric",
|
|
170
|
+
minute: "numeric",
|
|
171
|
+
second: "numeric",
|
|
172
|
+
hour12: false,
|
|
173
|
+
}
|
|
174
|
+
const d2 = new Date(date).toLocaleString("en-US", opts)
|
|
175
|
+
let od = format.replace("YYYY", d2.slice(6, 10))
|
|
176
|
+
od = od.replace("MM", d2.slice(0, 2))
|
|
177
|
+
od = od.replace("DD", d2.slice(3, 5))
|
|
178
|
+
od = od.replace("HH", d2.slice(12, 14))
|
|
179
|
+
od = od.replace("mm", d2.slice(15, 17))
|
|
180
|
+
od = od.replace("ss", d2.slice(18, 20))
|
|
181
|
+
return od
|
|
182
|
+
}
|
|
168
183
|
export function dateToString(date: Date | string): string {
|
|
169
|
-
const d2 = typeof date !== "string" ? date : new Date(date)
|
|
170
|
-
const year = d2.getFullYear()
|
|
171
|
-
const month = String(d2.getMonth() + 1).padStart(2, "0")
|
|
172
|
-
const day = String(d2.getDate()).padStart(2, "0")
|
|
173
|
-
return `${year}-${month}-${day}
|
|
174
|
-
}
|
|
175
|
-
export function datetimeToString(date?: Date | string): string|undefined {
|
|
176
|
-
if (!date || date ===
|
|
177
|
-
return undefined
|
|
184
|
+
const d2 = typeof date !== "string" ? date : new Date(date)
|
|
185
|
+
const year = d2.getFullYear()
|
|
186
|
+
const month = String(d2.getMonth() + 1).padStart(2, "0")
|
|
187
|
+
const day = String(d2.getDate()).padStart(2, "0")
|
|
188
|
+
return `${year}-${month}-${day}`
|
|
189
|
+
}
|
|
190
|
+
export function datetimeToString(date?: Date | string): string | undefined {
|
|
191
|
+
if (!date || date === "") {
|
|
192
|
+
return undefined
|
|
178
193
|
}
|
|
179
|
-
const d2 = typeof date !== "string" ? date : new Date(date)
|
|
180
|
-
const year = d2.getFullYear()
|
|
181
|
-
const month = String(d2.getMonth() + 1).padStart(2, "0")
|
|
182
|
-
const day = String(d2.getDate()).padStart(2, "0")
|
|
183
|
-
const hours = String(d2.getHours()).padStart(2, "0")
|
|
184
|
-
const minutes = String(d2.getMinutes()).padStart(2, "0")
|
|
185
|
-
const seconds = String(d2.getSeconds()).padStart(2, "0")
|
|
186
|
-
return `${year}-${month}-${day}T${hours}:${minutes}:${seconds}
|
|
187
|
-
}
|
|
188
|
-
export function getNumber(event: ChangeEvent<HTMLSelectElement|HTMLInputElement>): number {
|
|
194
|
+
const d2 = typeof date !== "string" ? date : new Date(date)
|
|
195
|
+
const year = d2.getFullYear()
|
|
196
|
+
const month = String(d2.getMonth() + 1).padStart(2, "0")
|
|
197
|
+
const day = String(d2.getDate()).padStart(2, "0")
|
|
198
|
+
const hours = String(d2.getHours()).padStart(2, "0")
|
|
199
|
+
const minutes = String(d2.getMinutes()).padStart(2, "0")
|
|
200
|
+
const seconds = String(d2.getSeconds()).padStart(2, "0")
|
|
201
|
+
return `${year}-${month}-${day}T${hours}:${minutes}:${seconds}`
|
|
202
|
+
}
|
|
203
|
+
export function getNumber(event: ChangeEvent<HTMLSelectElement | HTMLInputElement>): number {
|
|
189
204
|
return parseInt(event.currentTarget.value, 10)
|
|
190
205
|
}
|
package/src/input.ts
CHANGED
|
@@ -1,79 +1,85 @@
|
|
|
1
|
-
import {LoadingService, Locale, ResourceService, UIService} from
|
|
1
|
+
import { LoadingService, Locale, ResourceService, UIService } from "./core"
|
|
2
2
|
|
|
3
3
|
interface ResourceInput {
|
|
4
|
-
resource: ResourceService
|
|
4
|
+
resource: ResourceService
|
|
5
5
|
}
|
|
6
|
-
export function getResource(p: ResourceService|ResourceInput): ResourceService {
|
|
7
|
-
const x: any = p
|
|
8
|
-
if (x.value && x.format && typeof x.value ===
|
|
9
|
-
return x
|
|
6
|
+
export function getResource(p: ResourceService | ResourceInput): ResourceService {
|
|
7
|
+
const x: any = p
|
|
8
|
+
if (x.value && x.format && typeof x.value === "function") {
|
|
9
|
+
return x
|
|
10
10
|
} else {
|
|
11
|
-
return x.resource
|
|
11
|
+
return x.resource
|
|
12
12
|
}
|
|
13
13
|
}
|
|
14
14
|
interface ShortSearchParameter {
|
|
15
|
-
auto?: boolean
|
|
15
|
+
auto?: boolean
|
|
16
16
|
}
|
|
17
|
-
export function getAutoSearch(p: ResourceService|ShortSearchParameter): boolean {
|
|
18
|
-
const x: any = p
|
|
19
|
-
if (x.value && x.format && typeof x.value ===
|
|
20
|
-
return true
|
|
17
|
+
export function getAutoSearch(p: ResourceService | ShortSearchParameter): boolean {
|
|
18
|
+
const x: any = p
|
|
19
|
+
if (x.value && x.format && typeof x.value === "function") {
|
|
20
|
+
return true
|
|
21
21
|
}
|
|
22
|
-
return x.auto
|
|
22
|
+
return x.auto
|
|
23
23
|
}
|
|
24
24
|
interface UIInput {
|
|
25
|
-
ui?: UIService
|
|
25
|
+
ui?: UIService
|
|
26
26
|
}
|
|
27
|
-
export function getUIService(p: ResourceService|UIInput, ui0?: UIService): UIService {
|
|
27
|
+
export function getUIService(p: ResourceService | UIInput, ui0?: UIService): UIService {
|
|
28
28
|
if (ui0) {
|
|
29
|
-
return ui0
|
|
29
|
+
return ui0
|
|
30
30
|
}
|
|
31
|
-
return (p as any).ui
|
|
31
|
+
return (p as any).ui
|
|
32
32
|
}
|
|
33
33
|
interface LoadingInput {
|
|
34
|
-
loading?: LoadingService
|
|
34
|
+
loading?: LoadingService
|
|
35
35
|
}
|
|
36
|
-
export function getLoadingFunc(p: ResourceService|LoadingInput, ui0?: LoadingService): LoadingService {
|
|
36
|
+
export function getLoadingFunc(p: ResourceService | LoadingInput, ui0?: LoadingService): LoadingService {
|
|
37
37
|
if (ui0) {
|
|
38
|
-
return ui0
|
|
38
|
+
return ui0
|
|
39
39
|
}
|
|
40
|
-
return (p as any).loading
|
|
40
|
+
return (p as any).loading
|
|
41
41
|
}
|
|
42
42
|
interface ShowMessageInput {
|
|
43
|
-
showMessage: (msg: string, option?: string) => void
|
|
43
|
+
showMessage: (msg: string, option?: string) => void
|
|
44
44
|
}
|
|
45
|
-
export function getMsgFunc(p: ResourceService|ShowMessageInput, showMsg?: (msg: string, option?: string) => void): (msg: string) => void {
|
|
45
|
+
export function getMsgFunc(p: ResourceService | ShowMessageInput, showMsg?: (msg: string, option?: string) => void): (msg: string) => void {
|
|
46
46
|
if (showMsg) {
|
|
47
|
-
return showMsg
|
|
47
|
+
return showMsg
|
|
48
48
|
}
|
|
49
|
-
return (p as any).showMessage
|
|
49
|
+
return (p as any).showMessage
|
|
50
50
|
}
|
|
51
51
|
interface ConfirmInput {
|
|
52
|
-
confirm: (m2: string, yesCallback?: () => void, header?: string, btnLeftText?: string, btnRightText?: string, noCallback?: () => void) => void
|
|
52
|
+
confirm: (m2: string, yesCallback?: () => void, header?: string, btnLeftText?: string, btnRightText?: string, noCallback?: () => void) => void
|
|
53
53
|
}
|
|
54
|
-
export function getConfirmFunc(
|
|
54
|
+
export function getConfirmFunc(
|
|
55
|
+
p: ResourceService | ConfirmInput,
|
|
56
|
+
cf?: (m2: string, yesCallback?: () => void, header?: string, btnLeftText?: string, btnRightText?: string, noCallback?: () => void) => void,
|
|
57
|
+
): (m2: string, yesCallback?: () => void, header?: string, btnLeftText?: string, btnRightText?: string, noCallback?: () => void) => void {
|
|
55
58
|
if (cf) {
|
|
56
|
-
return cf
|
|
59
|
+
return cf
|
|
57
60
|
}
|
|
58
|
-
return (p as any).confirm
|
|
61
|
+
return (p as any).confirm
|
|
59
62
|
}
|
|
60
63
|
interface GetLocaleInput {
|
|
61
|
-
getLocale?: (profile?: string) => Locale
|
|
64
|
+
getLocale?: (profile?: string) => Locale
|
|
62
65
|
}
|
|
63
|
-
export function getLocaleFunc(p: ResourceService|GetLocaleInput, getLoc?: () => Locale): () => Locale {
|
|
66
|
+
export function getLocaleFunc(p: ResourceService | GetLocaleInput, getLoc?: () => Locale): () => Locale {
|
|
64
67
|
if (getLoc) {
|
|
65
|
-
return getLoc
|
|
68
|
+
return getLoc
|
|
66
69
|
}
|
|
67
|
-
return (p as any).getLocale
|
|
70
|
+
return (p as any).getLocale
|
|
68
71
|
}
|
|
69
72
|
interface ShowErrorInput {
|
|
70
|
-
showError: (m: string, callback?: () => void, header?: string) => void
|
|
73
|
+
showError: (m: string, callback?: () => void, header?: string) => void
|
|
71
74
|
}
|
|
72
|
-
export function getErrorFunc(
|
|
75
|
+
export function getErrorFunc(
|
|
76
|
+
p: ResourceService | ShowErrorInput,
|
|
77
|
+
showErr?: (m: string, callback?: () => void, header?: string) => void,
|
|
78
|
+
): (m: string, callback?: () => void, header?: string) => void {
|
|
73
79
|
if (showErr) {
|
|
74
|
-
return showErr
|
|
80
|
+
return showErr
|
|
75
81
|
}
|
|
76
|
-
return (p as any).showError
|
|
82
|
+
return (p as any).showError
|
|
77
83
|
}
|
|
78
84
|
/*
|
|
79
85
|
export interface EditStatusParameter {
|
|
@@ -94,4 +100,4 @@ export function getDiffStatusFunc(p: ResourceService|DiffStatusParameter, status
|
|
|
94
100
|
}
|
|
95
101
|
return (p as any).status;
|
|
96
102
|
}
|
|
97
|
-
*/
|
|
103
|
+
*/
|
package/src/merge.ts
CHANGED
|
@@ -1,23 +1,26 @@
|
|
|
1
|
-
import {useCallback, useEffect, useRef, useState} from
|
|
1
|
+
import { useCallback, useEffect, useRef, useState } from "react"
|
|
2
2
|
|
|
3
|
-
export type Callback<T> = (value?: T) => void
|
|
4
|
-
export type DispatchWithCallback<T> = (value: T, callback?: Callback<T>) => void
|
|
3
|
+
export type Callback<T> = (value?: T) => void
|
|
4
|
+
export type DispatchWithCallback<T> = (value: T, callback?: Callback<T>) => void
|
|
5
5
|
|
|
6
6
|
export function useMergeState<T>(initialState?: T | (() => T)): [T, DispatchWithCallback<Partial<T>>] {
|
|
7
|
-
const [state, _setState] = useState(initialState ? initialState : {} as any)
|
|
7
|
+
const [state, _setState] = useState(initialState ? initialState : ({} as any))
|
|
8
8
|
|
|
9
|
-
const callbackRef = useRef<Callback<T>>()
|
|
9
|
+
const callbackRef = useRef<Callback<T>>()
|
|
10
10
|
|
|
11
|
-
const setState = useCallback(
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
const setState = useCallback(
|
|
12
|
+
(newState: Partial<T>, callback?: Callback<T>): void => {
|
|
13
|
+
callbackRef.current = callback
|
|
14
|
+
_setState((prevState: any) => Object.assign({}, prevState, newState))
|
|
15
|
+
},
|
|
16
|
+
[state],
|
|
17
|
+
)
|
|
15
18
|
|
|
16
|
-
useEffect(
|
|
19
|
+
useEffect(() => {
|
|
17
20
|
if (callbackRef.current) {
|
|
18
|
-
callbackRef.current(state)
|
|
21
|
+
callbackRef.current(state)
|
|
19
22
|
}
|
|
20
|
-
}, [state])
|
|
23
|
+
}, [state])
|
|
21
24
|
|
|
22
|
-
return [state, setState]
|
|
25
|
+
return [state, setState]
|
|
23
26
|
}
|