react-hook-core 0.1.18 → 0.1.19
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/index.js +16 -0
- package/lib/useEdit.js +7 -5
- package/package.json +1 -1
- package/src/index.ts +23 -0
- package/src/useEdit.ts +6 -4
package/lib/index.js
CHANGED
|
@@ -4,6 +4,7 @@ function __export(m) {
|
|
|
4
4
|
}
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
var React = require("react");
|
|
7
|
+
var react_1 = require("react");
|
|
7
8
|
__export(require("./formutil"));
|
|
8
9
|
__export(require("./util"));
|
|
9
10
|
__export(require("./core"));
|
|
@@ -20,6 +21,21 @@ __export(require("./components"));
|
|
|
20
21
|
__export(require("./search"));
|
|
21
22
|
__export(require("./reflect"));
|
|
22
23
|
__export(require("./com"));
|
|
24
|
+
exports.useCallbackState = function (initialValue) {
|
|
25
|
+
var _a = react_1.useState(initialValue), state = _a[0], _setState = _a[1];
|
|
26
|
+
var callbackQueue = react_1.useRef([]);
|
|
27
|
+
react_1.useEffect(function () {
|
|
28
|
+
callbackQueue.current.forEach(function (cb) { return cb(state); });
|
|
29
|
+
callbackQueue.current = [];
|
|
30
|
+
}, [state]);
|
|
31
|
+
var setState = function (newValue, callback) {
|
|
32
|
+
_setState(newValue);
|
|
33
|
+
if (callback && typeof callback === "function") {
|
|
34
|
+
callbackQueue.current.push(callback);
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
return [state, setState];
|
|
38
|
+
};
|
|
23
39
|
function checked(s, v) {
|
|
24
40
|
if (s) {
|
|
25
41
|
if (Array.isArray(s)) {
|
package/lib/useEdit.js
CHANGED
|
@@ -299,11 +299,13 @@ exports.useCoreEdit = function (refForm, initialState, service, p1, p, props) {
|
|
|
299
299
|
var unmappedErrors = u.showFormError(f, errors);
|
|
300
300
|
formutil_1.focusFirstError(f);
|
|
301
301
|
if (!result.message) {
|
|
302
|
-
if (
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
302
|
+
if (errors && errors.length > 0) {
|
|
303
|
+
if (p1.ui && p1.ui.buildErrorMessage) {
|
|
304
|
+
result.message = p1.ui.buildErrorMessage(unmappedErrors);
|
|
305
|
+
}
|
|
306
|
+
else {
|
|
307
|
+
result.message = errors[0].message;
|
|
308
|
+
}
|
|
307
309
|
}
|
|
308
310
|
}
|
|
309
311
|
if (result.message) {
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
|
+
import { useEffect, useRef, useState } from 'react';
|
|
2
3
|
export * from './formutil';
|
|
3
4
|
export * from './util';
|
|
4
5
|
export * from './core';
|
|
@@ -16,6 +17,28 @@ export * from './search';
|
|
|
16
17
|
export * from './reflect';
|
|
17
18
|
export * from './com';
|
|
18
19
|
|
|
20
|
+
type CallBackType<T> = (updatedValue: T) => void;
|
|
21
|
+
type SetStateType<T> = T | ((prev: T) => T);
|
|
22
|
+
type RetType = <T>(initialValue: T | (() => T)) => [T, (newValue: SetStateType<T>, callback?: CallBackType<T>) => void];
|
|
23
|
+
|
|
24
|
+
export const useCallbackState: RetType = <T>(initialValue: T | (() => T)) => {
|
|
25
|
+
const [state, _setState] = useState<T>(initialValue);
|
|
26
|
+
const callbackQueue = useRef<CallBackType<T>[]>([]);
|
|
27
|
+
|
|
28
|
+
useEffect(() => {
|
|
29
|
+
callbackQueue.current.forEach((cb) => cb(state));
|
|
30
|
+
callbackQueue.current = [];
|
|
31
|
+
}, [state]);
|
|
32
|
+
|
|
33
|
+
const setState = (newValue: SetStateType<T>, callback?: CallBackType<T>) => {
|
|
34
|
+
_setState(newValue);
|
|
35
|
+
if (callback && typeof callback === "function") {
|
|
36
|
+
callbackQueue.current.push(callback);
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
return [state, setState];
|
|
40
|
+
};
|
|
41
|
+
|
|
19
42
|
export function checked(s: string[]|string|undefined, v: string): boolean|undefined {
|
|
20
43
|
if (s) {
|
|
21
44
|
if (Array.isArray(s)) {
|
package/src/useEdit.ts
CHANGED
|
@@ -371,10 +371,12 @@ export const useCoreEdit = <T, ID, S, P>(
|
|
|
371
371
|
const unmappedErrors = u.showFormError(f, errors);
|
|
372
372
|
focusFirstError(f);
|
|
373
373
|
if (!result.message) {
|
|
374
|
-
if (
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
374
|
+
if (errors && errors.length > 0) {
|
|
375
|
+
if (p1.ui && p1.ui.buildErrorMessage) {
|
|
376
|
+
result.message = p1.ui.buildErrorMessage(unmappedErrors);
|
|
377
|
+
} else {
|
|
378
|
+
result.message = errors[0].message;
|
|
379
|
+
}
|
|
378
380
|
}
|
|
379
381
|
}
|
|
380
382
|
if (result.message) {
|