react-solidlike 2.2.1 → 2.2.3
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/README.en.md +10 -0
- package/README.md +10 -0
- package/dist/For.d.ts +10 -2
- package/dist/Repeat.d.ts +10 -2
- package/dist/index.js +16 -9
- package/package.json +2 -2
package/README.en.md
CHANGED
|
@@ -58,6 +58,11 @@ import { For } from "react-solidlike";
|
|
|
58
58
|
<For each={items} fallback={<EmptyState />}>
|
|
59
59
|
{(item, index) => <ListItem item={item} index={index} />}
|
|
60
60
|
</For>
|
|
61
|
+
|
|
62
|
+
// With wrapper element
|
|
63
|
+
<For each={items} wrapper={<ul className="list" />}>
|
|
64
|
+
{(item) => <li>{item.name}</li>}
|
|
65
|
+
</For>
|
|
61
66
|
```
|
|
62
67
|
|
|
63
68
|
### `<Switch>` / `<Match>` / `<Default>` - Multi-branch Rendering
|
|
@@ -126,6 +131,11 @@ import { Repeat } from "react-solidlike";
|
|
|
126
131
|
<Repeat times={3}>
|
|
127
132
|
{(i) => <SkeletonCard key={i} />}
|
|
128
133
|
</Repeat>
|
|
134
|
+
|
|
135
|
+
// With wrapper element
|
|
136
|
+
<Repeat times={5} wrapper={<div className="stars" />}>
|
|
137
|
+
{(i) => <Star key={i} />}
|
|
138
|
+
</Repeat>
|
|
129
139
|
```
|
|
130
140
|
|
|
131
141
|
### `<Dynamic>` - Dynamic Component
|
package/README.md
CHANGED
|
@@ -58,6 +58,11 @@ import { For } from "react-solidlike";
|
|
|
58
58
|
<For each={items} fallback={<EmptyState />}>
|
|
59
59
|
{(item, index) => <ListItem item={item} index={index} />}
|
|
60
60
|
</For>
|
|
61
|
+
|
|
62
|
+
// 使用 wrapper 包装元素
|
|
63
|
+
<For each={items} wrapper={<ul className="list" />}>
|
|
64
|
+
{(item) => <li>{item.name}</li>}
|
|
65
|
+
</For>
|
|
61
66
|
```
|
|
62
67
|
|
|
63
68
|
### `<Switch>` / `<Match>` / `<Default>` - 多分支渲染
|
|
@@ -126,6 +131,11 @@ import { Repeat } from "react-solidlike";
|
|
|
126
131
|
<Repeat times={3}>
|
|
127
132
|
{(i) => <SkeletonCard key={i} />}
|
|
128
133
|
</Repeat>
|
|
134
|
+
|
|
135
|
+
// 使用 wrapper 包装元素
|
|
136
|
+
<Repeat times={5} wrapper={<div className="stars" />}>
|
|
137
|
+
{(i) => <Star key={i} />}
|
|
138
|
+
</Repeat>
|
|
129
139
|
```
|
|
130
140
|
|
|
131
141
|
### `<Dynamic>` - 动态组件
|
package/dist/For.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type Key, type ReactNode } from "react";
|
|
1
|
+
import { type Key, type ReactElement, type ReactNode } from "react";
|
|
2
2
|
export interface ForProps<T> {
|
|
3
3
|
/** Array to iterate over | 要遍历的数组 */
|
|
4
4
|
each: T[] | readonly T[] | null | undefined;
|
|
@@ -8,6 +8,8 @@ export interface ForProps<T> {
|
|
|
8
8
|
keyExtractor?: (item: T, index: number) => Key;
|
|
9
9
|
/** Fallback content when array is empty | 数组为空时渲染的备选内容 */
|
|
10
10
|
fallback?: ReactNode;
|
|
11
|
+
/** Wrapper element for all rendered elements | 包装所有渲染元素的元素 */
|
|
12
|
+
wrapper?: ReactElement;
|
|
11
13
|
}
|
|
12
14
|
/**
|
|
13
15
|
* List rendering component, replaces array.map() in JSX
|
|
@@ -31,5 +33,11 @@ export interface ForProps<T> {
|
|
|
31
33
|
* <For each={items} fallback={<EmptyState />}>
|
|
32
34
|
* {(item, index) => <ListItem key={item.id} item={item} index={index} />}
|
|
33
35
|
* </For>
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* // With wrapper element | 使用包装元素
|
|
39
|
+
* <For each={items} wrapper={<ul className="list" />}>
|
|
40
|
+
* {(item) => <li>{item.name}</li>}
|
|
41
|
+
* </For>
|
|
34
42
|
*/
|
|
35
|
-
export declare function For<T>({ each, children, keyExtractor, fallback }: ForProps<T>): ReactNode;
|
|
43
|
+
export declare function For<T>({ each, children, keyExtractor, fallback, wrapper }: ForProps<T>): ReactNode;
|
package/dist/Repeat.d.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import { type ReactNode } from "react";
|
|
1
|
+
import { type ReactElement, type ReactNode } from "react";
|
|
2
2
|
export interface RepeatProps {
|
|
3
3
|
/** Number of times to repeat | 重复次数 */
|
|
4
4
|
times: number;
|
|
5
5
|
/** Render function, receives current index (starting from 0) | 渲染函数,接收当前索引(从 0 开始) */
|
|
6
6
|
children: (index: number) => ReactNode;
|
|
7
|
+
/** Wrapper element for all rendered elements | 包装所有渲染元素的元素 */
|
|
8
|
+
wrapper?: ReactElement;
|
|
7
9
|
}
|
|
8
10
|
/**
|
|
9
11
|
* Repeat rendering component, replaces Array.from({ length: n }).map()
|
|
@@ -27,5 +29,11 @@ export interface RepeatProps {
|
|
|
27
29
|
* <Repeat times={rating}>
|
|
28
30
|
* {(i) => <FilledStar key={i} />}
|
|
29
31
|
* </Repeat>
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* // With wrapper element | 使用包装元素
|
|
35
|
+
* <Repeat times={5} wrapper={<div className="stars" />}>
|
|
36
|
+
* {(i) => <Star key={i} />}
|
|
37
|
+
* </Repeat>
|
|
30
38
|
*/
|
|
31
|
-
export declare function Repeat({ times, children }: RepeatProps): ReactNode;
|
|
39
|
+
export declare function Repeat({ times, children, wrapper }: RepeatProps): ReactNode;
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Children, Component, Fragment, createElement, useEffect, useState } from "react";
|
|
1
|
+
import { Children, Component, Fragment, cloneElement, createElement, isValidElement, useEffect, useState } from "react";
|
|
2
2
|
import { jsx } from "react/jsx-runtime";
|
|
3
3
|
|
|
4
4
|
function Await({ promise, loading = null, error = null, children }) {
|
|
@@ -51,6 +51,9 @@ function Dynamic({ component, fallback = null, ...props }) {
|
|
|
51
51
|
var ErrorBoundary = class extends Component {
|
|
52
52
|
constructor(props) {
|
|
53
53
|
super(props);
|
|
54
|
+
this.reset = () => {
|
|
55
|
+
this.setState({ error: null });
|
|
56
|
+
};
|
|
54
57
|
this.state = { error: null };
|
|
55
58
|
}
|
|
56
59
|
static getDerivedStateFromError(error) {
|
|
@@ -62,9 +65,6 @@ var ErrorBoundary = class extends Component {
|
|
|
62
65
|
componentDidUpdate(prevProps) {
|
|
63
66
|
if (this.state.error && prevProps.resetKey !== this.props.resetKey) this.reset();
|
|
64
67
|
}
|
|
65
|
-
reset = () => {
|
|
66
|
-
this.setState({ error: null });
|
|
67
|
-
};
|
|
68
68
|
render() {
|
|
69
69
|
const { error } = this.state;
|
|
70
70
|
const { fallback, children } = this.props;
|
|
@@ -76,12 +76,13 @@ var ErrorBoundary = class extends Component {
|
|
|
76
76
|
}
|
|
77
77
|
};
|
|
78
78
|
|
|
79
|
-
function For({ each, children, keyExtractor, fallback = null }) {
|
|
79
|
+
function For({ each, children, keyExtractor, fallback = null, wrapper }) {
|
|
80
80
|
if (!each || each.length === 0) return fallback;
|
|
81
|
-
|
|
81
|
+
const elements = each.map((item, index) => {
|
|
82
82
|
const key = keyExtractor ? keyExtractor(item, index) : index;
|
|
83
83
|
return /* @__PURE__ */ jsx(Fragment, { children: children(item, index) }, key);
|
|
84
84
|
});
|
|
85
|
+
return wrapper && isValidElement(wrapper) ? cloneElement(wrapper, {}, elements) : elements;
|
|
85
86
|
}
|
|
86
87
|
|
|
87
88
|
function defaultIsEmpty(data) {
|
|
@@ -101,18 +102,24 @@ function QueryBoundary({ query, loading = null, error = null, empty = null, chil
|
|
|
101
102
|
return children;
|
|
102
103
|
}
|
|
103
104
|
|
|
104
|
-
function Repeat({ times, children }) {
|
|
105
|
+
function Repeat({ times, children, wrapper }) {
|
|
105
106
|
if (times <= 0) return null;
|
|
106
107
|
const elements = [];
|
|
107
108
|
for (let i = 0; i < times; i++) elements.push(/* @__PURE__ */ jsx(Fragment, { children: children(i) }, i));
|
|
108
|
-
return elements;
|
|
109
|
+
return wrapper && isValidElement(wrapper) ? cloneElement(wrapper, {}, elements) : elements;
|
|
109
110
|
}
|
|
110
111
|
|
|
111
112
|
function Show({ when, children, fallback = null }) {
|
|
112
|
-
if (!when) return fallback;
|
|
113
|
+
if (!when || isEmpty(when)) return fallback;
|
|
113
114
|
if (typeof children === "function") return children(when);
|
|
114
115
|
return children;
|
|
115
116
|
}
|
|
117
|
+
function isEmpty(value) {
|
|
118
|
+
if (Array.isArray(value)) return value.length === 0;
|
|
119
|
+
if (value instanceof Map || value instanceof Set) return value.size === 0;
|
|
120
|
+
if (typeof value === "object" && value !== null && Object.getPrototypeOf(value) === Object.prototype) return Object.keys(value).length === 0;
|
|
121
|
+
return false;
|
|
122
|
+
}
|
|
116
123
|
|
|
117
124
|
function Match(_props) {
|
|
118
125
|
return null;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-solidlike",
|
|
3
|
-
"version": "2.2.
|
|
3
|
+
"version": "2.2.3",
|
|
4
4
|
"description": "Declarative React control flow components inspired by Solid.js, replacing ternary expressions and array.map() in JSX",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -50,7 +50,7 @@
|
|
|
50
50
|
"@types/react-dom": "^19.2.3",
|
|
51
51
|
"react": "^19.2.3",
|
|
52
52
|
"react-dom": "^19.2.3",
|
|
53
|
-
"rolldown": "^1.0.0-
|
|
53
|
+
"rolldown": "^1.0.0-rc.2",
|
|
54
54
|
"typescript": "^5.9.3"
|
|
55
55
|
}
|
|
56
56
|
}
|