@vitus-labs/attrs 2.0.0-alpha.9 → 2.0.0-beta.1
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.d.ts +8 -8
- package/lib/index.js +32 -21
- package/package.json +12 -10
- package/LICENSE +0 -21
package/lib/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ComponentType,
|
|
1
|
+
import { ComponentType, ReactElement } from "react";
|
|
2
2
|
|
|
3
3
|
//#region src/types/attrs.d.ts
|
|
4
4
|
/** Callback form of `.attrs()` — receives current props and returns partial overrides. */
|
|
@@ -11,7 +11,7 @@ type TFn = (...args: any) => any;
|
|
|
11
11
|
* A React component type that accepts additional static properties.
|
|
12
12
|
* Supports both class/function components and forwardRef components.
|
|
13
13
|
*/
|
|
14
|
-
type ElementType<T extends TObj | unknown = any> =
|
|
14
|
+
type ElementType<T extends TObj | unknown = any> = ComponentType<T> & Partial<Record<string, any>>;
|
|
15
15
|
/**
|
|
16
16
|
* Forces TypeScript to expand/flatten a type for better IDE display.
|
|
17
17
|
* Short-circuits for `any` — the mapped type would turn `any` into
|
|
@@ -37,8 +37,8 @@ type Spread<A extends readonly [...any]> = A extends [infer L, ...infer R] ? Spr
|
|
|
37
37
|
/** Recursively checks whether any element in the tuple is `any`. */
|
|
38
38
|
type _HasAny<A> = A extends [infer L, ...infer R] ? 0 extends 1 & L ? true : _HasAny<R> : false;
|
|
39
39
|
type MergeTypes<A extends readonly [...any]> = _HasAny<A> extends true ? any : ExtractNullableKeys<Spread<A>>;
|
|
40
|
-
/** Extracts the props type from a React component type (class
|
|
41
|
-
type ExtractProps<TComponentOrTProps> = TComponentOrTProps extends ComponentType<infer TProps> ? TProps : TComponentOrTProps
|
|
40
|
+
/** Extracts the props type from a React component type (class or function). */
|
|
41
|
+
type ExtractProps<TComponentOrTProps> = TComponentOrTProps extends ComponentType<infer TProps> ? TProps : TComponentOrTProps;
|
|
42
42
|
//#endregion
|
|
43
43
|
//#region src/types/config.d.ts
|
|
44
44
|
/** A component that has been enhanced by attrs — identified by the `IS_ATTRS` marker. */
|
|
@@ -62,9 +62,6 @@ type GenericHoc = (component: ElementType) => ElementType;
|
|
|
62
62
|
type ComposeParam = Record<string, GenericHoc | null | undefined | false>;
|
|
63
63
|
//#endregion
|
|
64
64
|
//#region src/types/AttrsComponent.d.ts
|
|
65
|
-
type ExoticComponent<P extends Record<string, unknown> = {}> = ForwardRefExoticComponent<PropsWithoutRef<P> & RefAttributes<any>> & {
|
|
66
|
-
readonly $$typeof: symbol;
|
|
67
|
-
};
|
|
68
65
|
/**
|
|
69
66
|
* @param OA Origin component props params.
|
|
70
67
|
* @param EA Extended prop types
|
|
@@ -72,7 +69,10 @@ type ExoticComponent<P extends Record<string, unknown> = {}> = ForwardRefExoticC
|
|
|
72
69
|
* @param HOC High-order components
|
|
73
70
|
* @param DFP Calculated final component props
|
|
74
71
|
*/
|
|
75
|
-
interface AttrsComponent<C extends ElementType = ElementType, OA extends TObj = {}, EA extends TObj = {}, S extends TObj = {}, HOC extends TObj = {}, DFP extends Record<string, any> = MergeTypes<[OA, EA]>>
|
|
72
|
+
interface AttrsComponent<C extends ElementType = ElementType, OA extends TObj = {}, EA extends TObj = {}, S extends TObj = {}, HOC extends TObj = {}, DFP extends Record<string, any> = MergeTypes<[OA, EA]>> {
|
|
73
|
+
(props: DFP & {
|
|
74
|
+
ref?: any;
|
|
75
|
+
}): ReactElement | null;
|
|
76
76
|
/**
|
|
77
77
|
* A chaining method to define default component theme
|
|
78
78
|
* @param param _object_
|
package/lib/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { compose, hoistNonReactStatics, isEmpty, omit, pick } from "@vitus-labs/core";
|
|
2
|
-
import {
|
|
2
|
+
import { useImperativeHandle, useMemo, useRef } from "react";
|
|
3
3
|
import { jsx } from "react/jsx-runtime";
|
|
4
4
|
|
|
5
5
|
//#region src/utils/attrs.ts
|
|
@@ -31,22 +31,33 @@ const calculateChainOptions = (options) => (args) => {
|
|
|
31
31
|
const createAttrsHOC = ({ attrs, priorityAttrs }) => {
|
|
32
32
|
const calculateAttrs = calculateChainOptions(attrs);
|
|
33
33
|
const calculatePriorityAttrs = calculateChainOptions(priorityAttrs);
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
34
|
+
const hasAttrs = (attrs?.length ?? 0) > 0;
|
|
35
|
+
const hasPriorityAttrs = (priorityAttrs?.length ?? 0) > 0;
|
|
36
|
+
const hasAnyChain = hasAttrs || hasPriorityAttrs;
|
|
37
|
+
const attrsHoc = (WrappedComponent) => {
|
|
38
|
+
const HOC = (allProps) => {
|
|
39
|
+
const { ref, ...props } = allProps;
|
|
40
|
+
const filteredProps = useMemo(() => removeUndefinedProps(props), [allProps]);
|
|
41
|
+
return /* @__PURE__ */ jsx(WrappedComponent, { ...useMemo(() => {
|
|
42
|
+
if (!hasAnyChain) return {
|
|
43
|
+
$attrsRef: ref,
|
|
44
|
+
...filteredProps
|
|
45
|
+
};
|
|
46
|
+
const prioritizedAttrs = hasPriorityAttrs ? calculatePriorityAttrs([filteredProps]) : null;
|
|
47
|
+
const finalAttrs = hasAttrs ? calculateAttrs([prioritizedAttrs ? {
|
|
48
|
+
...prioritizedAttrs,
|
|
49
|
+
...filteredProps
|
|
50
|
+
} : filteredProps]) : null;
|
|
51
|
+
return {
|
|
52
|
+
$attrsRef: ref,
|
|
53
|
+
...prioritizedAttrs,
|
|
54
|
+
...finalAttrs,
|
|
55
|
+
...filteredProps
|
|
56
|
+
};
|
|
57
|
+
}, [filteredProps, ref]) });
|
|
58
|
+
};
|
|
59
|
+
return HOC;
|
|
60
|
+
};
|
|
50
61
|
return attrsHoc;
|
|
51
62
|
};
|
|
52
63
|
|
|
@@ -109,7 +120,7 @@ const cloneAndEnhance = (defaultOpts, opts) => attrsComponent({
|
|
|
109
120
|
/**
|
|
110
121
|
* Core factory that builds an attrs-enhanced React component.
|
|
111
122
|
*
|
|
112
|
-
* Creates a
|
|
123
|
+
* Creates a component that:
|
|
113
124
|
* 1. Wraps the original with attrsHoc (default props) + user HOCs from `.compose()`.
|
|
114
125
|
* 2. Manages ref forwarding through the HOC chain via `$attrsRef`.
|
|
115
126
|
* 3. Filters out internal props listed in `filterAttrs`.
|
|
@@ -122,7 +133,7 @@ const attrsComponent = (options) => {
|
|
|
122
133
|
const componentName = options.name ?? options.component.displayName ?? options.component.name;
|
|
123
134
|
const RenderComponent = options.component;
|
|
124
135
|
const hocsFuncs = [createAttrsHOC(options), ...calculateHocsFuncs(options.compose)];
|
|
125
|
-
const EnhancedComponent =
|
|
136
|
+
const EnhancedComponent = ({ $attrsRef, ref, ...props }) => {
|
|
126
137
|
const internalRef = useAttrsStyleRef({
|
|
127
138
|
$attrsRef,
|
|
128
139
|
ref
|
|
@@ -138,7 +149,7 @@ const attrsComponent = (options) => {
|
|
|
138
149
|
...filteredProps,
|
|
139
150
|
"data-attrs": componentName
|
|
140
151
|
} : filteredProps });
|
|
141
|
-
}
|
|
152
|
+
};
|
|
142
153
|
const AttrsComponent = compose(...hocsFuncs)(EnhancedComponent);
|
|
143
154
|
AttrsComponent.IS_ATTRS = true;
|
|
144
155
|
AttrsComponent.displayName = componentName;
|
|
@@ -193,7 +204,7 @@ const attrs = ({ name, component }) => {
|
|
|
193
204
|
//#region src/isAttrsComponent.tsx
|
|
194
205
|
/** Runtime type guard — checks if a component was created by `attrs()`. */
|
|
195
206
|
const isAttrsComponent = (component) => {
|
|
196
|
-
if (component && typeof component === "object"
|
|
207
|
+
if (component && (typeof component === "object" || typeof component === "function") && Object.hasOwn(component, "IS_ATTRS")) return true;
|
|
197
208
|
return false;
|
|
198
209
|
};
|
|
199
210
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vitus-labs/attrs",
|
|
3
|
-
"version": "2.0.0-
|
|
3
|
+
"version": "2.0.0-beta.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "Vit Bokisch <vit@bokisch.cz>",
|
|
6
6
|
"maintainers": [
|
|
@@ -13,6 +13,8 @@
|
|
|
13
13
|
"import": "./lib/index.js",
|
|
14
14
|
"types": "./lib/index.d.ts"
|
|
15
15
|
},
|
|
16
|
+
"types": "./lib/index.d.ts",
|
|
17
|
+
"main": "./lib/index.js",
|
|
16
18
|
"files": [
|
|
17
19
|
"lib",
|
|
18
20
|
"!lib/**/*.map",
|
|
@@ -53,18 +55,18 @@
|
|
|
53
55
|
"test:coverage": "vitest run --coverage",
|
|
54
56
|
"test:watch": "vitest",
|
|
55
57
|
"cover": "coveralls < .coverage/lcov.info",
|
|
56
|
-
"typecheck": "tsc --noEmit"
|
|
58
|
+
"typecheck": "tsc --noEmit",
|
|
59
|
+
"version": "node ../../scripts/sync-peer-deps.mjs"
|
|
57
60
|
},
|
|
58
61
|
"peerDependencies": {
|
|
59
|
-
"@vitus-labs/core": "
|
|
62
|
+
"@vitus-labs/core": "2.0.0-beta.1",
|
|
60
63
|
"react": ">= 19"
|
|
61
64
|
},
|
|
62
65
|
"devDependencies": {
|
|
63
|
-
"@vitus-labs/core": "
|
|
64
|
-
"@vitus-labs/elements": "
|
|
65
|
-
"@vitus-labs/tools-rolldown": "
|
|
66
|
-
"@vitus-labs/tools-storybook": "
|
|
67
|
-
"@vitus-labs/tools-typescript": "
|
|
68
|
-
}
|
|
69
|
-
"gitHead": "f2f47db887d6a846ee5f9e96f290c59cdde4772a"
|
|
66
|
+
"@vitus-labs/core": "workspace:*",
|
|
67
|
+
"@vitus-labs/elements": "workspace:*",
|
|
68
|
+
"@vitus-labs/tools-rolldown": "2.2.0",
|
|
69
|
+
"@vitus-labs/tools-storybook": "2.2.0",
|
|
70
|
+
"@vitus-labs/tools-typescript": "2.1.0"
|
|
71
|
+
}
|
|
70
72
|
}
|
package/LICENSE
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2023-present Vit Bokisch
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|