@rc-component/util 1.3.1 → 1.5.0
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/es/Dom/focus.d.ts +7 -0
- package/es/Dom/focus.js +26 -0
- package/es/index.d.ts +1 -1
- package/es/index.js +1 -1
- package/es/utils/set.d.ts +13 -1
- package/es/utils/set.js +21 -3
- package/lib/Dom/focus.d.ts +7 -0
- package/lib/Dom/focus.js +27 -0
- package/lib/index.d.ts +1 -1
- package/lib/index.js +6 -0
- package/lib/utils/set.d.ts +13 -1
- package/lib/utils/set.js +22 -3
- package/package.json +2 -2
package/es/Dom/focus.d.ts
CHANGED
|
@@ -6,3 +6,10 @@ export declare function clearLastFocusNode(): void;
|
|
|
6
6
|
/** @deprecated Do not use since this may failed when used in async */
|
|
7
7
|
export declare function backLastFocusNode(): void;
|
|
8
8
|
export declare function limitTabRange(node: HTMLElement, e: KeyboardEvent): void;
|
|
9
|
+
export interface InputFocusOptions extends FocusOptions {
|
|
10
|
+
cursor?: 'start' | 'end' | 'all';
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Focus element and set cursor position for input/textarea elements.
|
|
14
|
+
*/
|
|
15
|
+
export declare function triggerFocus(element?: HTMLElement, option?: InputFocusOptions): void;
|
package/es/Dom/focus.js
CHANGED
|
@@ -76,4 +76,30 @@ export function limitTabRange(node, e) {
|
|
|
76
76
|
e.preventDefault();
|
|
77
77
|
}
|
|
78
78
|
}
|
|
79
|
+
}
|
|
80
|
+
// Used for `rc-input` `rc-textarea` `rc-input-number`
|
|
81
|
+
/**
|
|
82
|
+
* Focus element and set cursor position for input/textarea elements.
|
|
83
|
+
*/
|
|
84
|
+
export function triggerFocus(element, option) {
|
|
85
|
+
if (!element) return;
|
|
86
|
+
element.focus(option);
|
|
87
|
+
|
|
88
|
+
// Selection content
|
|
89
|
+
const {
|
|
90
|
+
cursor
|
|
91
|
+
} = option || {};
|
|
92
|
+
if (cursor && (element instanceof HTMLInputElement || element instanceof HTMLTextAreaElement)) {
|
|
93
|
+
const len = element.value.length;
|
|
94
|
+
switch (cursor) {
|
|
95
|
+
case 'start':
|
|
96
|
+
element.setSelectionRange(0, 0);
|
|
97
|
+
break;
|
|
98
|
+
case 'end':
|
|
99
|
+
element.setSelectionRange(len, len);
|
|
100
|
+
break;
|
|
101
|
+
default:
|
|
102
|
+
element.setSelectionRange(0, len);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
79
105
|
}
|
package/es/index.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ export { default as useMergedState } from './hooks/useMergedState';
|
|
|
3
3
|
export { default as useControlledState } from './hooks/useControlledState';
|
|
4
4
|
export { supportNodeRef, supportRef, useComposeRef } from './ref';
|
|
5
5
|
export { default as get } from './utils/get';
|
|
6
|
-
export { default as set, merge } from './utils/set';
|
|
6
|
+
export { default as set, merge, mergeWith } from './utils/set';
|
|
7
7
|
export { default as warning, noteOnce } from './warning';
|
|
8
8
|
export { default as omit } from './omit';
|
|
9
9
|
export { default as toArray } from './Children/toArray';
|
package/es/index.js
CHANGED
|
@@ -3,7 +3,7 @@ export { default as useMergedState } from "./hooks/useMergedState";
|
|
|
3
3
|
export { default as useControlledState } from "./hooks/useControlledState";
|
|
4
4
|
export { supportNodeRef, supportRef, useComposeRef } from "./ref";
|
|
5
5
|
export { default as get } from "./utils/get";
|
|
6
|
-
export { default as set, merge } from "./utils/set";
|
|
6
|
+
export { default as set, merge, mergeWith } from "./utils/set";
|
|
7
7
|
export { default as warning, noteOnce } from "./warning";
|
|
8
8
|
export { default as omit } from "./omit";
|
|
9
9
|
export { default as toArray } from "./Children/toArray";
|
package/es/utils/set.d.ts
CHANGED
|
@@ -1,6 +1,18 @@
|
|
|
1
1
|
export type Path = (string | number | symbol)[];
|
|
2
2
|
export default function set<Entity = any, Output = Entity, Value = any>(entity: Entity, paths: Path, value: Value, removeIfUndefined?: boolean): Output;
|
|
3
|
+
export type MergeFn = (current: any, next: any) => any;
|
|
3
4
|
/**
|
|
4
|
-
* Merge objects
|
|
5
|
+
* Merge multiple objects. Support custom merge logic.
|
|
6
|
+
* @param sources object sources
|
|
7
|
+
* @param config.prepareArray Customize array prepare function.
|
|
8
|
+
* It will return empty [] by default.
|
|
9
|
+
* So when match array, it will auto be override with next array in sources.
|
|
10
|
+
*/
|
|
11
|
+
export declare function mergeWith<T extends object>(sources: T[], config?: {
|
|
12
|
+
prepareArray?: MergeFn;
|
|
13
|
+
}): T;
|
|
14
|
+
/**
|
|
15
|
+
* Merge multiple objects into a new single object.
|
|
16
|
+
* Arrays will be replaced by default.
|
|
5
17
|
*/
|
|
6
18
|
export declare function merge<T extends object>(...sources: T[]): T;
|
package/es/utils/set.js
CHANGED
|
@@ -38,10 +38,20 @@ function createEmpty(source) {
|
|
|
38
38
|
}
|
|
39
39
|
const keys = typeof Reflect === 'undefined' ? Object.keys : Reflect.ownKeys;
|
|
40
40
|
|
|
41
|
+
// ================================ Merge ================================
|
|
42
|
+
|
|
41
43
|
/**
|
|
42
|
-
* Merge objects
|
|
44
|
+
* Merge multiple objects. Support custom merge logic.
|
|
45
|
+
* @param sources object sources
|
|
46
|
+
* @param config.prepareArray Customize array prepare function.
|
|
47
|
+
* It will return empty [] by default.
|
|
48
|
+
* So when match array, it will auto be override with next array in sources.
|
|
43
49
|
*/
|
|
44
|
-
export function
|
|
50
|
+
export function mergeWith(sources, config = {}) {
|
|
51
|
+
const {
|
|
52
|
+
prepareArray
|
|
53
|
+
} = config;
|
|
54
|
+
const finalPrepareArray = prepareArray || (() => []);
|
|
45
55
|
let clone = createEmpty(sources[0]);
|
|
46
56
|
sources.forEach(src => {
|
|
47
57
|
function internalMerge(path, parentLoopSet) {
|
|
@@ -55,7 +65,7 @@ export function merge(...sources) {
|
|
|
55
65
|
const originValue = get(clone, path);
|
|
56
66
|
if (isArr) {
|
|
57
67
|
// Array will always be override
|
|
58
|
-
clone = set(clone, path,
|
|
68
|
+
clone = set(clone, path, finalPrepareArray(originValue, value));
|
|
59
69
|
} else if (!originValue || typeof originValue !== 'object') {
|
|
60
70
|
// Init container if not exist
|
|
61
71
|
clone = set(clone, path, createEmpty(value));
|
|
@@ -71,4 +81,12 @@ export function merge(...sources) {
|
|
|
71
81
|
internalMerge([]);
|
|
72
82
|
});
|
|
73
83
|
return clone;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Merge multiple objects into a new single object.
|
|
88
|
+
* Arrays will be replaced by default.
|
|
89
|
+
*/
|
|
90
|
+
export function merge(...sources) {
|
|
91
|
+
return mergeWith(sources);
|
|
74
92
|
}
|
package/lib/Dom/focus.d.ts
CHANGED
|
@@ -6,3 +6,10 @@ export declare function clearLastFocusNode(): void;
|
|
|
6
6
|
/** @deprecated Do not use since this may failed when used in async */
|
|
7
7
|
export declare function backLastFocusNode(): void;
|
|
8
8
|
export declare function limitTabRange(node: HTMLElement, e: KeyboardEvent): void;
|
|
9
|
+
export interface InputFocusOptions extends FocusOptions {
|
|
10
|
+
cursor?: 'start' | 'end' | 'all';
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Focus element and set cursor position for input/textarea elements.
|
|
14
|
+
*/
|
|
15
|
+
export declare function triggerFocus(element?: HTMLElement, option?: InputFocusOptions): void;
|
package/lib/Dom/focus.js
CHANGED
|
@@ -8,6 +8,7 @@ exports.clearLastFocusNode = clearLastFocusNode;
|
|
|
8
8
|
exports.getFocusNodeList = getFocusNodeList;
|
|
9
9
|
exports.limitTabRange = limitTabRange;
|
|
10
10
|
exports.saveLastFocusNode = saveLastFocusNode;
|
|
11
|
+
exports.triggerFocus = triggerFocus;
|
|
11
12
|
var _isVisible = _interopRequireDefault(require("./isVisible"));
|
|
12
13
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
13
14
|
function focusable(node, includePositive = false) {
|
|
@@ -87,4 +88,30 @@ function limitTabRange(node, e) {
|
|
|
87
88
|
e.preventDefault();
|
|
88
89
|
}
|
|
89
90
|
}
|
|
91
|
+
}
|
|
92
|
+
// Used for `rc-input` `rc-textarea` `rc-input-number`
|
|
93
|
+
/**
|
|
94
|
+
* Focus element and set cursor position for input/textarea elements.
|
|
95
|
+
*/
|
|
96
|
+
function triggerFocus(element, option) {
|
|
97
|
+
if (!element) return;
|
|
98
|
+
element.focus(option);
|
|
99
|
+
|
|
100
|
+
// Selection content
|
|
101
|
+
const {
|
|
102
|
+
cursor
|
|
103
|
+
} = option || {};
|
|
104
|
+
if (cursor && (element instanceof HTMLInputElement || element instanceof HTMLTextAreaElement)) {
|
|
105
|
+
const len = element.value.length;
|
|
106
|
+
switch (cursor) {
|
|
107
|
+
case 'start':
|
|
108
|
+
element.setSelectionRange(0, 0);
|
|
109
|
+
break;
|
|
110
|
+
case 'end':
|
|
111
|
+
element.setSelectionRange(len, len);
|
|
112
|
+
break;
|
|
113
|
+
default:
|
|
114
|
+
element.setSelectionRange(0, len);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
90
117
|
}
|
package/lib/index.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ export { default as useMergedState } from './hooks/useMergedState';
|
|
|
3
3
|
export { default as useControlledState } from './hooks/useControlledState';
|
|
4
4
|
export { supportNodeRef, supportRef, useComposeRef } from './ref';
|
|
5
5
|
export { default as get } from './utils/get';
|
|
6
|
-
export { default as set, merge } from './utils/set';
|
|
6
|
+
export { default as set, merge, mergeWith } from './utils/set';
|
|
7
7
|
export { default as warning, noteOnce } from './warning';
|
|
8
8
|
export { default as omit } from './omit';
|
|
9
9
|
export { default as toArray } from './Children/toArray';
|
package/lib/index.js
CHANGED
|
@@ -15,6 +15,12 @@ Object.defineProperty(exports, "merge", {
|
|
|
15
15
|
return _set.merge;
|
|
16
16
|
}
|
|
17
17
|
});
|
|
18
|
+
Object.defineProperty(exports, "mergeWith", {
|
|
19
|
+
enumerable: true,
|
|
20
|
+
get: function () {
|
|
21
|
+
return _set.mergeWith;
|
|
22
|
+
}
|
|
23
|
+
});
|
|
18
24
|
Object.defineProperty(exports, "noteOnce", {
|
|
19
25
|
enumerable: true,
|
|
20
26
|
get: function () {
|
package/lib/utils/set.d.ts
CHANGED
|
@@ -1,6 +1,18 @@
|
|
|
1
1
|
export type Path = (string | number | symbol)[];
|
|
2
2
|
export default function set<Entity = any, Output = Entity, Value = any>(entity: Entity, paths: Path, value: Value, removeIfUndefined?: boolean): Output;
|
|
3
|
+
export type MergeFn = (current: any, next: any) => any;
|
|
3
4
|
/**
|
|
4
|
-
* Merge objects
|
|
5
|
+
* Merge multiple objects. Support custom merge logic.
|
|
6
|
+
* @param sources object sources
|
|
7
|
+
* @param config.prepareArray Customize array prepare function.
|
|
8
|
+
* It will return empty [] by default.
|
|
9
|
+
* So when match array, it will auto be override with next array in sources.
|
|
10
|
+
*/
|
|
11
|
+
export declare function mergeWith<T extends object>(sources: T[], config?: {
|
|
12
|
+
prepareArray?: MergeFn;
|
|
13
|
+
}): T;
|
|
14
|
+
/**
|
|
15
|
+
* Merge multiple objects into a new single object.
|
|
16
|
+
* Arrays will be replaced by default.
|
|
5
17
|
*/
|
|
6
18
|
export declare function merge<T extends object>(...sources: T[]): T;
|
package/lib/utils/set.js
CHANGED
|
@@ -5,6 +5,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.default = set;
|
|
7
7
|
exports.merge = merge;
|
|
8
|
+
exports.mergeWith = mergeWith;
|
|
8
9
|
var _get = _interopRequireDefault(require("./get"));
|
|
9
10
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
10
11
|
function internalSet(entity, paths, value, removeIfUndefined) {
|
|
@@ -46,10 +47,20 @@ function createEmpty(source) {
|
|
|
46
47
|
}
|
|
47
48
|
const keys = typeof Reflect === 'undefined' ? Object.keys : Reflect.ownKeys;
|
|
48
49
|
|
|
50
|
+
// ================================ Merge ================================
|
|
51
|
+
|
|
49
52
|
/**
|
|
50
|
-
* Merge objects
|
|
53
|
+
* Merge multiple objects. Support custom merge logic.
|
|
54
|
+
* @param sources object sources
|
|
55
|
+
* @param config.prepareArray Customize array prepare function.
|
|
56
|
+
* It will return empty [] by default.
|
|
57
|
+
* So when match array, it will auto be override with next array in sources.
|
|
51
58
|
*/
|
|
52
|
-
function
|
|
59
|
+
function mergeWith(sources, config = {}) {
|
|
60
|
+
const {
|
|
61
|
+
prepareArray
|
|
62
|
+
} = config;
|
|
63
|
+
const finalPrepareArray = prepareArray || (() => []);
|
|
53
64
|
let clone = createEmpty(sources[0]);
|
|
54
65
|
sources.forEach(src => {
|
|
55
66
|
function internalMerge(path, parentLoopSet) {
|
|
@@ -63,7 +74,7 @@ function merge(...sources) {
|
|
|
63
74
|
const originValue = (0, _get.default)(clone, path);
|
|
64
75
|
if (isArr) {
|
|
65
76
|
// Array will always be override
|
|
66
|
-
clone = set(clone, path,
|
|
77
|
+
clone = set(clone, path, finalPrepareArray(originValue, value));
|
|
67
78
|
} else if (!originValue || typeof originValue !== 'object') {
|
|
68
79
|
// Init container if not exist
|
|
69
80
|
clone = set(clone, path, createEmpty(value));
|
|
@@ -79,4 +90,12 @@ function merge(...sources) {
|
|
|
79
90
|
internalMerge([]);
|
|
80
91
|
});
|
|
81
92
|
return clone;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Merge multiple objects into a new single object.
|
|
97
|
+
* Arrays will be replaced by default.
|
|
98
|
+
*/
|
|
99
|
+
function merge(...sources) {
|
|
100
|
+
return mergeWith(sources);
|
|
82
101
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rc-component/util",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.0",
|
|
4
4
|
"description": "Common Utils For React Component",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"react",
|
|
@@ -56,7 +56,7 @@
|
|
|
56
56
|
"cross-env": "^7.0.2",
|
|
57
57
|
"dumi": "^2.1.3",
|
|
58
58
|
"eslint": "^8.54.0",
|
|
59
|
-
"eslint-plugin-jest": "^
|
|
59
|
+
"eslint-plugin-jest": "^29.0.1",
|
|
60
60
|
"eslint-plugin-unicorn": "^56.0.1",
|
|
61
61
|
"father": "^4.1.3",
|
|
62
62
|
"glob": "^11.0.3",
|