@xhsreds/reds-rn-next 0.0.2-beta20240708190837 → 0.0.2
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/coverage/coverage-final.json +8 -5
- package/lib/cjs/_chunks/{Dx-JjEWA.js → CAyGJuk1.js} +27 -1
- package/lib/cjs/_chunks/{Dx-JjEWA.js.map → CAyGJuk1.js.map} +1 -1
- package/lib/cjs/_chunks/DiJl2cva.js +49 -0
- package/lib/cjs/_chunks/DiJl2cva.js.map +1 -0
- package/lib/cjs/components/Button/index.js +1 -1
- package/lib/cjs/components/Button/index.js.map +1 -1
- package/lib/cjs/components/Button/interface/index.js +12 -0
- package/lib/cjs/components/Button/interface/index.js.map +1 -1
- package/lib/cjs/components/ConfigProvider/ConfigProvider.js +15 -0
- package/lib/cjs/components/ConfigProvider/ConfigProvider.js.map +1 -0
- package/lib/cjs/components/ConfigProvider/hooks/ConfigCache/ConfigCache.js +147 -0
- package/lib/cjs/components/ConfigProvider/hooks/ConfigCache/ConfigCache.js.map +1 -0
- package/lib/cjs/components/ConfigProvider/hooks/ConfigCache/index.js +10 -0
- package/lib/cjs/components/ConfigProvider/hooks/ConfigCache/index.js.map +1 -0
- package/lib/cjs/components/ConfigProvider/hooks/ConfigCache/interface.js +3 -0
- package/lib/cjs/components/ConfigProvider/hooks/ConfigCache/interface.js.map +1 -0
- package/lib/cjs/components/ConfigProvider/hooks/ConfigContext/ConfigContext.js +10 -0
- package/lib/cjs/components/ConfigProvider/hooks/ConfigContext/ConfigContext.js.map +1 -0
- package/lib/cjs/components/ConfigProvider/hooks/themeToken/useThemeToken.js +24 -0
- package/lib/cjs/components/ConfigProvider/hooks/themeToken/useThemeToken.js.map +1 -0
- package/lib/cjs/components/ConfigProvider/index.js +17 -0
- package/lib/cjs/components/ConfigProvider/index.js.map +1 -0
- package/lib/cjs/components/ConfigProvider/interface/index.js +3 -0
- package/lib/cjs/components/ConfigProvider/interface/index.js.map +1 -0
- package/lib/cjs/components/Popover/component.js +1 -1
- package/lib/cjs/components/Popover/hook.js +1 -1
- package/lib/cjs/components/Popover/index.js +4 -4
- package/lib/cjs/components/Popover/index.js.map +1 -1
- package/lib/cjs/components/Switch/index.js +8 -6
- package/lib/cjs/components/Switch/index.js.map +1 -1
- package/lib/cjs/components/Switch/interface/index.js +2 -2
- package/lib/cjs/components/Switch/interface/index.js.map +1 -1
- package/lib/cjs/index.js +7 -1
- package/lib/cjs/index.js.map +1 -1
- package/lib/esm/_chunks/{Bm4Re-8r.js → BHmvJnIm.js} +25 -2
- package/lib/esm/_chunks/{Bm4Re-8r.js.map → BHmvJnIm.js.map} +1 -1
- package/lib/esm/_chunks/Br6Ked0G.js +46 -0
- package/lib/esm/_chunks/Br6Ked0G.js.map +1 -0
- package/lib/esm/components/Button/index.js +1 -1
- package/lib/esm/components/Button/index.js.map +1 -1
- package/lib/esm/components/Button/interface/index.js +11 -0
- package/lib/esm/components/Button/interface/index.js.map +1 -1
- package/lib/esm/components/ConfigProvider/ConfigProvider.js +7 -0
- package/lib/esm/components/ConfigProvider/ConfigProvider.js.map +1 -0
- package/lib/esm/components/ConfigProvider/hooks/ConfigCache/ConfigCache.js +145 -0
- package/lib/esm/components/ConfigProvider/hooks/ConfigCache/ConfigCache.js.map +1 -0
- package/lib/esm/components/ConfigProvider/hooks/ConfigCache/index.js +4 -0
- package/lib/esm/components/ConfigProvider/hooks/ConfigCache/index.js.map +1 -0
- package/lib/esm/components/ConfigProvider/hooks/ConfigCache/interface.js +2 -0
- package/lib/esm/components/ConfigProvider/hooks/ConfigCache/interface.js.map +1 -0
- package/lib/esm/components/ConfigProvider/hooks/ConfigContext/ConfigContext.js +8 -0
- package/lib/esm/components/ConfigProvider/hooks/ConfigContext/ConfigContext.js.map +1 -0
- package/lib/esm/components/ConfigProvider/hooks/themeToken/useThemeToken.js +22 -0
- package/lib/esm/components/ConfigProvider/hooks/themeToken/useThemeToken.js.map +1 -0
- package/lib/esm/components/ConfigProvider/index.js +12 -0
- package/lib/esm/components/ConfigProvider/index.js.map +1 -0
- package/lib/esm/components/ConfigProvider/interface/index.js +2 -0
- package/lib/esm/components/ConfigProvider/interface/index.js.map +1 -0
- package/lib/esm/components/Popover/component.js +1 -1
- package/lib/esm/components/Popover/hook.js +1 -1
- package/lib/esm/components/Popover/index.js +4 -4
- package/lib/esm/components/Popover/index.js.map +1 -1
- package/lib/esm/components/Switch/index.js +9 -7
- package/lib/esm/components/Switch/index.js.map +1 -1
- package/lib/esm/components/Switch/interface/index.js +3 -3
- package/lib/esm/components/Switch/interface/index.js.map +1 -1
- package/lib/esm/index.js +5 -1
- package/lib/esm/index.js.map +1 -1
- package/lib/types/src/components/Button/index.d.ts +4 -0
- package/lib/types/src/components/Button/interface/index.d.ts +11 -0
- package/lib/types/src/components/ConfigProvider/ConfigProvider.d.ts +4 -0
- package/lib/types/src/components/ConfigProvider/hooks/ConfigCache/ConfigCache.d.ts +22 -0
- package/lib/types/src/components/ConfigProvider/hooks/ConfigCache/index.d.ts +1 -0
- package/lib/types/src/components/ConfigProvider/hooks/ConfigCache/interface.d.ts +36 -0
- package/lib/types/src/components/ConfigProvider/hooks/ConfigContext/ConfigContext.d.ts +5 -0
- package/lib/types/src/components/ConfigProvider/hooks/themeToken/useThemeToken.d.ts +8 -0
- package/lib/types/src/components/ConfigProvider/index.d.ts +3 -0
- package/lib/types/src/components/ConfigProvider/interface/index.d.ts +27 -0
- package/lib/types/src/components/Popover/component.d.ts +13 -0
- package/lib/types/src/components/Popover/hook.d.ts +66 -0
- package/lib/types/src/components/Popover/index.d.ts +4 -0
- package/lib/types/src/components/Popover/interface/index.d.ts +13 -0
- package/lib/types/src/components/Popover/manifest.d.ts +3 -0
- package/lib/types/src/components/Switch/index.d.ts +7 -0
- package/lib/types/src/components/Switch/interface/index.d.ts +11 -0
- package/lib/types/src/index.d.ts +5 -0
- package/package.json +8 -4
- package/rollup.config.js +25 -25
- package/src/components/Button/demo/index.jsx +50 -0
- package/src/components/Button/demo/index.tsx +50 -0
- package/src/components/Button/doc/index.mdx +18 -0
- package/src/components/Button/index.tsx +0 -1
- package/src/components/Button/interface/index.ts +17 -29
- package/src/components/Button/test/index.bench.tsx +18 -0
- package/src/components/Button/test/index.test.tsx +9 -0
- package/src/components/ConfigProvider/ConfigProvider.tsx +48 -0
- package/src/components/ConfigProvider/demo/base.jsx +114 -0
- package/src/components/ConfigProvider/demo/index.jsx +50 -0
- package/src/components/ConfigProvider/demo/index.tsx +50 -0
- package/src/components/ConfigProvider/demo/token.jsx +8 -0
- package/src/components/ConfigProvider/doc/index.mdx +22 -0
- package/src/components/ConfigProvider/doc/token.mdx +7 -0
- package/src/components/ConfigProvider/hooks/ConfigCache/ConfigCache.ts +115 -0
- package/src/components/ConfigProvider/hooks/ConfigCache/index.ts +1 -0
- package/src/components/ConfigProvider/hooks/ConfigCache/interface.ts +38 -0
- package/src/components/ConfigProvider/hooks/ConfigContext/ConfigContext.ts +9 -0
- package/src/components/ConfigProvider/hooks/themeToken/useThemeToken.ts +33 -0
- package/src/components/ConfigProvider/index.ts +4 -0
- package/src/components/ConfigProvider/interface/index.ts +28 -0
- package/src/components/ConfigProvider/test/index.bench.tsx +18 -0
- package/src/components/ConfigProvider/test/index.test.tsx +9 -0
- package/src/components/Popover/demo/{base.jsx → index.tsx} +2 -2
- package/src/components/Popover/doc/index.mdx +21 -15
- package/src/components/Popover/index.tsx +3 -3
- package/src/components/Switch/demo/{base.jsx → index.tsx} +9 -6
- package/src/components/Switch/doc/index.mdx +14 -12
- package/src/components/Switch/index.tsx +9 -6
- package/src/components/Switch/interface/index.ts +3 -3
- package/src/components/Switch/test/index.bench.tsx +2 -5
- package/src/components/Switch/test/index.test.tsx +11 -23
- package/src/index.ts +2 -1
- package/tsconfig.json +2 -2
- package/lib/cjs/components/Button/common/index.js +0 -11
- package/lib/cjs/components/Button/common/index.js.map +0 -1
- package/lib/cjs/components/Button/rn/index.js +0 -25
- package/lib/cjs/components/Button/rn/index.js.map +0 -1
- package/lib/cjs/index.d.ts +0 -37
- package/lib/cjs/index.d.ts.map +0 -1
- package/lib/cjs/utils/hook.js +0 -3
- package/lib/cjs/utils/hook.js.map +0 -1
- package/lib/esm/components/Button/common/index.js +0 -9
- package/lib/esm/components/Button/common/index.js.map +0 -1
- package/lib/esm/components/Button/rn/index.js +0 -21
- package/lib/esm/components/Button/rn/index.js.map +0 -1
- package/lib/esm/utils/hook.js +0 -2
- package/lib/esm/utils/hook.js.map +0 -1
- package/src/components/Button/common/index.ts +0 -6
- package/src/components/Button/rn/index.tsx +0 -20
- package/src/utils/hook.ts +0 -0
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { alwaysColor, lightColor, darkColor } from "@xhs/reds-token-next";
|
|
2
|
+
import { IConfigCache, Component, LightToken, DarkToken } from "./interface";
|
|
3
|
+
|
|
4
|
+
class ConfigCache {
|
|
5
|
+
public configCache: Record<string, IConfigCache> = {};
|
|
6
|
+
public multipleConfigurations: boolean = false;
|
|
7
|
+
|
|
8
|
+
constructor({ id, themeCache }: IConfigCache) {
|
|
9
|
+
const rootThemeCache = {
|
|
10
|
+
colorMode: themeCache.colorMode,
|
|
11
|
+
token: {
|
|
12
|
+
lightColor: {
|
|
13
|
+
...lightColor,
|
|
14
|
+
},
|
|
15
|
+
darkColor: {
|
|
16
|
+
...darkColor,
|
|
17
|
+
},
|
|
18
|
+
alwaysColor: {
|
|
19
|
+
...alwaysColor,
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
this.addNewConfigCache({ id, themeCache: rootThemeCache, parentId: id });
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
addNewConfigCache({ id, themeCache, parentId }: IConfigCache) {
|
|
27
|
+
if (Object.keys(this.configCache).includes(id)) {
|
|
28
|
+
this.configCache[id].parentId = parentId;
|
|
29
|
+
this.configCache[id].themeCache = {
|
|
30
|
+
colorMode: themeCache.colorMode,
|
|
31
|
+
token: {
|
|
32
|
+
lightColor: {
|
|
33
|
+
...this.configCache[parentId].themeCache.token?.lightColor,
|
|
34
|
+
...themeCache.token?.lightColor,
|
|
35
|
+
},
|
|
36
|
+
darkColor: {
|
|
37
|
+
...this.configCache[parentId].themeCache.token?.darkColor,
|
|
38
|
+
...themeCache.token?.darkColor,
|
|
39
|
+
},
|
|
40
|
+
alwaysColor: {
|
|
41
|
+
...this.configCache[parentId].themeCache.token?.alwaysColor,
|
|
42
|
+
...themeCache.token?.alwaysColor,
|
|
43
|
+
},
|
|
44
|
+
components: {
|
|
45
|
+
...this.configCache[parentId].themeCache.token?.components,
|
|
46
|
+
...themeCache.token?.components,
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
} else {
|
|
51
|
+
this.configCache[id] = { id, themeCache, parentId };
|
|
52
|
+
if (Object.keys(this.configCache).length > 1) {
|
|
53
|
+
this.multipleConfigurations = true;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
updateColorMode({ id, colorMode }: { id: string; colorMode: "light" | "dark" }) {
|
|
59
|
+
this.configCache[id].themeCache.colorMode = colorMode;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
getColorModeById(id: string) {
|
|
63
|
+
return this.configCache[id]?.themeCache?.colorMode;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
getTokenById(id: string, tokenType: "lightColor" | "darkColor" | "alwaysColor", key: LightToken | DarkToken): any {
|
|
67
|
+
if (this.configCache[id]?.id === id) {
|
|
68
|
+
return this.configCache[id].themeCache.token?.[tokenType][key];
|
|
69
|
+
} else {
|
|
70
|
+
return this.getTokenById(this.configCache[id]?.parentId || "root", tokenType, key);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
getLightTokenById(id: string, key: LightToken | DarkToken) {
|
|
75
|
+
return this.getTokenById(id, "lightColor", key);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
getDarkTokenById(id: string, key: LightToken | DarkToken) {
|
|
79
|
+
return this.getTokenById(id, "darkColor", key);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
getAlwaysTokenById(id: string, key: LightToken | DarkToken) {
|
|
83
|
+
return this.getTokenById(id, "alwaysColor", key);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
getTokenByIdAndComponent(
|
|
87
|
+
id: string,
|
|
88
|
+
tokenType: "lightColor" | "darkColor" | "alwaysColor",
|
|
89
|
+
component: Component,
|
|
90
|
+
): any {
|
|
91
|
+
if (this.configCache[id]?.id === id) {
|
|
92
|
+
const componentToken = {
|
|
93
|
+
...this.configCache[id].themeCache.token?.[tokenType],
|
|
94
|
+
...this.configCache[id].themeCache.token?.components?.[component]?.[tokenType],
|
|
95
|
+
};
|
|
96
|
+
return componentToken;
|
|
97
|
+
} else {
|
|
98
|
+
return this.getTokenByIdAndComponent(this.configCache[id]?.parentId || "root", tokenType, component);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
getLightTokenByIdAndComponent(id: string, component: Component) {
|
|
103
|
+
return this.getTokenByIdAndComponent(id, "lightColor", component);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
getDarkTokenByIdAndComponent(id: string, component: Component) {
|
|
107
|
+
return this.getTokenByIdAndComponent(id, "darkColor", component);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
getAlwaysTokenByIdAndComponent(id: string, component: Component) {
|
|
111
|
+
return this.getTokenByIdAndComponent(id, "alwaysColor", component);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export const configCache = new ConfigCache({ id: "root", themeCache: { colorMode: "light" }, parentId: "root" });
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./ConfigCache";
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { lightColor, darkColor, typography } from "@xhs/reds-token-next";
|
|
2
|
+
|
|
3
|
+
type ValueOf<T> = T[keyof T];
|
|
4
|
+
|
|
5
|
+
export type LightToken = keyof typeof lightColor;
|
|
6
|
+
export type DarkToken = keyof typeof darkColor;
|
|
7
|
+
export type TypographyToken = keyof typeof typography;
|
|
8
|
+
|
|
9
|
+
export type ThemeColor = { [key in LightToken | DarkToken]: ValueOf<typeof lightColor | typeof darkColor> };
|
|
10
|
+
|
|
11
|
+
export type ColorMode = "light" | "dark";
|
|
12
|
+
export type Component = "Button" | "Popover";
|
|
13
|
+
export type ComponentColor = { [key in Component]: ThemeColor };
|
|
14
|
+
|
|
15
|
+
type ComponentsToken = {
|
|
16
|
+
[K in Component]?: {
|
|
17
|
+
lightColor?: Record<string, string>;
|
|
18
|
+
darkColor?: Record<string, string>;
|
|
19
|
+
alwaysColor?: Record<string, string>;
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export interface IThemeCache {
|
|
24
|
+
token?: {
|
|
25
|
+
lightColor: Record<string, string>;
|
|
26
|
+
darkColor: Record<string, string>;
|
|
27
|
+
alwaysColor: Record<string, string>;
|
|
28
|
+
components?: ComponentsToken;
|
|
29
|
+
};
|
|
30
|
+
colorMode: ColorMode;
|
|
31
|
+
inherit?: boolean;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface IConfigCache {
|
|
35
|
+
id: string;
|
|
36
|
+
themeCache: IThemeCache;
|
|
37
|
+
parentId: string;
|
|
38
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { useContext } from "react";
|
|
2
|
+
import { configCache } from "../ConfigCache";
|
|
3
|
+
import { ConfigProviderContext } from "../ConfigContext/ConfigContext";
|
|
4
|
+
import { ThemeColor, ColorMode, LightToken, DarkToken, Component } from "../ConfigCache/interface";
|
|
5
|
+
|
|
6
|
+
export interface IThemeInstance {
|
|
7
|
+
colorMode: ColorMode;
|
|
8
|
+
}
|
|
9
|
+
export interface MyProxyConstructor {
|
|
10
|
+
new <T, H extends Record<string, unknown>>(target: T, handler: ProxyHandler<H>): H;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const useThemeColor = (component?: Component) => {
|
|
14
|
+
const id = useContext(ConfigProviderContext).id;
|
|
15
|
+
const themeColor: ThemeColor = new (Proxy as MyProxyConstructor)(
|
|
16
|
+
{},
|
|
17
|
+
{
|
|
18
|
+
get(this: IThemeInstance, _, key: LightToken | DarkToken) {
|
|
19
|
+
if (configCache.getColorModeById(id) === "dark") {
|
|
20
|
+
return component
|
|
21
|
+
? configCache.getDarkTokenByIdAndComponent(id, component)
|
|
22
|
+
: configCache.getDarkTokenById(id, key);
|
|
23
|
+
} else {
|
|
24
|
+
return component
|
|
25
|
+
? configCache.getLightTokenByIdAndComponent(id, component)
|
|
26
|
+
: configCache.getLightTokenById(id, key);
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
return themeColor;
|
|
33
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
type ColorMode = "light" | "dark";
|
|
2
|
+
type Component = "Button" | "Popover";
|
|
3
|
+
|
|
4
|
+
type ComponentsToken = {
|
|
5
|
+
[K in Component]?: {
|
|
6
|
+
lightColor?: Record<string, string>;
|
|
7
|
+
darkColor?: Record<string, string>;
|
|
8
|
+
alwaysColor?: Record<string, string>;
|
|
9
|
+
};
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
interface IThemeConfig {
|
|
13
|
+
token?: {
|
|
14
|
+
lightColor?: Record<string, string>;
|
|
15
|
+
darkColor?: Record<string, string>;
|
|
16
|
+
alwaysColor?: Record<string, string>;
|
|
17
|
+
components?: ComponentsToken;
|
|
18
|
+
};
|
|
19
|
+
inherit?: boolean;
|
|
20
|
+
colorMode?: ColorMode;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface ConfigProviderProps {
|
|
24
|
+
id?: string;
|
|
25
|
+
children: React.ReactNode;
|
|
26
|
+
theme?: IThemeConfig;
|
|
27
|
+
parentId?: string;
|
|
28
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { render, fireEvent, userEvent } from "@testing-library/react-native";
|
|
3
|
+
import "@testing-library/react-native/extend-expect";
|
|
4
|
+
import { Pressable, Text, View } from "react-native";
|
|
5
|
+
import ConfigProvider from "../index";
|
|
6
|
+
|
|
7
|
+
describe("ConfigProvider", () => {
|
|
8
|
+
it(":base", async () => {
|
|
9
|
+
let isOn = false;
|
|
10
|
+
const wrapper = render(<ConfigProvider onChange={(v) => (isOn = v)}></ConfigProvider>);
|
|
11
|
+
expect(wrapper.UNSAFE_root.props.defaultValue).toBe(false);
|
|
12
|
+
await fireEvent(wrapper.UNSAFE_root.children[0], "press");
|
|
13
|
+
expect(isOn).toBe(true);
|
|
14
|
+
|
|
15
|
+
await fireEvent(wrapper.UNSAFE_root.children[0], "press");
|
|
16
|
+
expect(isOn).toBe(false);
|
|
17
|
+
});
|
|
18
|
+
});
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { render, fireEvent, userEvent } from "@testing-library/react-native";
|
|
3
|
+
import "@testing-library/react-native/extend-expect";
|
|
4
|
+
import { Pressable, Text, View } from "react-native";
|
|
5
|
+
import ConfigProvider from "../index";
|
|
6
|
+
|
|
7
|
+
describe("ConfigProvider", () => {
|
|
8
|
+
it(":base", async () => {});
|
|
9
|
+
});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import React, { useRef, useEffect, useState } from "react";
|
|
1
|
+
import React, { useRef, useEffect, useState, forwardRef } from "react";
|
|
2
2
|
import { View, Text, StyleSheet, FlatList, ScrollView } from "react-native";
|
|
3
3
|
import { Popover, Button } from "@xhs/reds-rn-next";
|
|
4
4
|
|
|
@@ -17,7 +17,7 @@ const styles = StyleSheet.create({
|
|
|
17
17
|
backgroundColor: "blue",
|
|
18
18
|
},
|
|
19
19
|
});
|
|
20
|
-
const Br = (props) => <View style={{ height: props.height || 20 }}></View>;
|
|
20
|
+
const Br = (props: { height?: any }) => <View style={{ height: props.height || 20 }}></View>;
|
|
21
21
|
|
|
22
22
|
export default function App() {
|
|
23
23
|
const placementList = ["top-start", "top", "top-end", "bottom-start", "bottom", "bottom-end"];
|
|
@@ -1,25 +1,31 @@
|
|
|
1
|
-
import BaseDemo from "!!raw-loader!../demo/
|
|
1
|
+
import BaseDemo from "!!raw-loader!../demo/index.tsx";
|
|
2
2
|
|
|
3
3
|
## 类型
|
|
4
4
|
|
|
5
5
|
::BaseDemo::
|
|
6
6
|
|
|
7
|
+
### 类型
|
|
8
|
+
|
|
9
|
+
::BaseDemo::
|
|
10
|
+
|
|
7
11
|
## API
|
|
8
12
|
|
|
9
13
|
### Props
|
|
10
|
-
|
|
11
|
-
|
|
|
12
|
-
|
|
|
13
|
-
|
|
|
14
|
-
|
|
|
15
|
-
|
|
|
16
|
-
|
|
|
17
|
-
|
|
|
18
|
-
|
|
|
19
|
-
|
|
|
20
|
-
|
|
|
14
|
+
|
|
15
|
+
| 属性 | 描述 | 类型 | 默认值 |
|
|
16
|
+
| -------------------- | ---------------------- | ---------------------- | ------ |
|
|
17
|
+
| visible | 是否显示气泡 | boolean | false |
|
|
18
|
+
| placement | 气泡方向 | "top" | "bottom" | top |
|
|
19
|
+
| content | 弹出内容 | string | |
|
|
20
|
+
| opacity | 遮罩透明度 | number | 0 |
|
|
21
|
+
| mode | 主题色 | "light" | "dark" | light |
|
|
22
|
+
| trigger | 触发方式 | "click" | "touch" | click |
|
|
23
|
+
| z-index | 层级 | number | 1000 |
|
|
24
|
+
| close-on-click-again | 再次点击关闭 | boolean | true |
|
|
25
|
+
| children | popover 作用的目标节点 | ReactNode | |
|
|
21
26
|
|
|
22
27
|
### Events
|
|
23
|
-
|
|
24
|
-
|
|
|
25
|
-
|
|
|
28
|
+
|
|
29
|
+
| 事件 | 描述 | 类型 | 默认值 |
|
|
30
|
+
| ---------- | ---------------- | ---------------------- | ------ |
|
|
31
|
+
| click-mask | 点击遮罩层时触发 | (value: Event) => void | |
|
|
@@ -110,9 +110,9 @@ export default ({
|
|
|
110
110
|
>
|
|
111
111
|
<Text
|
|
112
112
|
style={{
|
|
113
|
-
fontWeight: typography.
|
|
114
|
-
fontSize: typography.
|
|
115
|
-
lineHeight: typography.
|
|
113
|
+
fontWeight: typography.B2LooseFontWeight,
|
|
114
|
+
fontSize: typography.B2LooseFontSize,
|
|
115
|
+
lineHeight: typography.B2LooseLineHeight,
|
|
116
116
|
color: popoverStyle.color,
|
|
117
117
|
}}
|
|
118
118
|
>
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import React, { useRef, useEffect, useState } from "react";
|
|
2
2
|
import { View, Text, StyleSheet, FlatList, ScrollView } from "react-native";
|
|
3
|
-
import { Switch
|
|
3
|
+
import { Switch } from "@xhs/reds-rn-next";
|
|
4
4
|
|
|
5
5
|
const styles = StyleSheet.create({
|
|
6
6
|
container: {
|
|
@@ -26,7 +26,10 @@ export default function App() {
|
|
|
26
26
|
const [value, setValue] = useState(false);
|
|
27
27
|
const [valueStr, setValueStr] = useState(value.toString());
|
|
28
28
|
const onClick = (e) => setValue((v) => !v);
|
|
29
|
-
const onChange = (v) =>
|
|
29
|
+
const onChange = (v) => {
|
|
30
|
+
console.log(v);
|
|
31
|
+
setValueStr(v.toString());
|
|
32
|
+
};
|
|
30
33
|
return (
|
|
31
34
|
<View style={styles.container}>
|
|
32
35
|
<Desc>默认用法</Desc>
|
|
@@ -37,14 +40,14 @@ export default function App() {
|
|
|
37
40
|
<Switch defaultValue={true}></Switch>
|
|
38
41
|
<Br />
|
|
39
42
|
|
|
43
|
+
<Desc>修改开关颜色</Desc>
|
|
44
|
+
<Switch trackOnColor={"#1b8bcd"} trackOffColor={"#da36c9"}></Switch>
|
|
45
|
+
<Br />
|
|
46
|
+
|
|
40
47
|
<Desc>受控用法</Desc>
|
|
41
48
|
<Text>当前值:{valueStr}</Text>
|
|
42
49
|
<Switch value={value} onClick={onClick} onChange={onChange}></Switch>
|
|
43
50
|
<Br />
|
|
44
|
-
|
|
45
|
-
<Desc>修改开关颜色</Desc>
|
|
46
|
-
<Switch trackOnColor={"#1b8bcd"} trackOffColor={"#da36c9"}></Switch>
|
|
47
|
-
<Br />
|
|
48
51
|
</View>
|
|
49
52
|
);
|
|
50
53
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import BaseDemo from "!!raw-loader!../demo/
|
|
1
|
+
import BaseDemo from "!!raw-loader!../demo/index.tsx";
|
|
2
2
|
|
|
3
3
|
## 类型
|
|
4
4
|
|
|
@@ -7,16 +7,18 @@ import BaseDemo from "!!raw-loader!../demo/base.jsx";
|
|
|
7
7
|
## API
|
|
8
8
|
|
|
9
9
|
### Props
|
|
10
|
-
|
|
11
|
-
|
|
|
12
|
-
|
|
|
13
|
-
|
|
|
14
|
-
|
|
|
15
|
-
|
|
|
16
|
-
|
|
|
10
|
+
|
|
11
|
+
| 属性 | 描述 | 类型 | 默认值 |
|
|
12
|
+
| --------------- | ---------------- | ------- | --------------------- |
|
|
13
|
+
| value | 当前的状态 | boolean | |
|
|
14
|
+
| default-value | 默认状态 | boolean | false |
|
|
15
|
+
| disabled | 是否显示气泡 | boolean | false |
|
|
16
|
+
| track-on-color | 开关开启时的颜色 | string | rgba(255, 36, 66, 1) |
|
|
17
|
+
| track-off-color | 开关关闭时的颜色 | string | rgba(48, 48, 52, 0.1) |
|
|
17
18
|
|
|
18
19
|
### Events
|
|
19
|
-
|
|
20
|
-
|
|
|
21
|
-
|
|
|
22
|
-
|
|
|
20
|
+
|
|
21
|
+
| 事件 | 描述 | 类型 | 默认值 |
|
|
22
|
+
| ------ | ------------------ | ------------------------ | ------ |
|
|
23
|
+
| change | 当值发生变化时触发 | (value: boolean) => void | |
|
|
24
|
+
| click | 被点击时触发 | (value: Event) => void | |
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import React, { useState, useRef, useEffect, useMemo } from "react";
|
|
2
2
|
import { View, TouchableWithoutFeedback, StyleSheet, Animated, GestureResponderEvent } from "react-native";
|
|
3
3
|
import { RedsSwitch, defaultProps } from "./interface/index";
|
|
4
|
-
import {
|
|
4
|
+
import { alwaysColor } from "@xhs/reds-token-next";
|
|
5
5
|
|
|
6
|
-
const Switch = ({ value:
|
|
7
|
-
const isControlled =
|
|
6
|
+
const Switch = ({ value: isOn, defaultValue, onChange, onClick, trackOnColor, trackOffColor }: RedsSwitch) => {
|
|
7
|
+
const isControlled = isOn !== undefined;
|
|
8
8
|
const [value, setValue] = useState(defaultValue);
|
|
9
9
|
|
|
10
10
|
const animatedValue = useRef(new Animated.Value(defaultValue ? 18 : 2)).current;
|
|
@@ -12,14 +12,17 @@ const Switch = ({ value: cValue, defaultValue, onChange, onClick, trackOnColor,
|
|
|
12
12
|
const thumbTranslateX = useMemo(() => animatedValue.interpolate({ inputRange: [2, 18], outputRange: [2, 18] }), []);
|
|
13
13
|
|
|
14
14
|
useEffect(() => {
|
|
15
|
-
isControlled && setValue(cValue);
|
|
16
15
|
onChange?.(!!value);
|
|
17
16
|
Animated.timing(animatedValue, {
|
|
18
17
|
toValue: value ? 18 : 2,
|
|
19
18
|
duration: 200,
|
|
20
19
|
useNativeDriver: true,
|
|
21
20
|
}).start();
|
|
22
|
-
}, [value
|
|
21
|
+
}, [value]);
|
|
22
|
+
|
|
23
|
+
useEffect(() => {
|
|
24
|
+
isControlled && setValue(isOn);
|
|
25
|
+
}, [isOn]);
|
|
23
26
|
|
|
24
27
|
const toggleSwitch = (e: GestureResponderEvent) => {
|
|
25
28
|
onClick?.(e);
|
|
@@ -49,7 +52,7 @@ const styles = StyleSheet.create({
|
|
|
49
52
|
width: 20,
|
|
50
53
|
height: 20,
|
|
51
54
|
borderRadius: 10,
|
|
52
|
-
backgroundColor:
|
|
55
|
+
backgroundColor: alwaysColor.White,
|
|
53
56
|
justifyContent: "center",
|
|
54
57
|
alignItems: "center",
|
|
55
58
|
position: "absolute",
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { GestureResponderEvent } from "react-native";
|
|
2
|
-
import {
|
|
2
|
+
import { lightColor } from "@xhs/reds-token-next";
|
|
3
3
|
|
|
4
4
|
export interface RedsSwitch {
|
|
5
5
|
value?: boolean;
|
|
@@ -16,6 +16,6 @@ export const defaultProps: RedsSwitch = {
|
|
|
16
16
|
disabled: false,
|
|
17
17
|
onClick: (event: GestureResponderEvent) => {},
|
|
18
18
|
onChange: (value: boolean) => {},
|
|
19
|
-
trackOnColor:
|
|
20
|
-
trackOffColor:
|
|
19
|
+
trackOnColor: lightColor.Primary,
|
|
20
|
+
trackOffColor: lightColor.Fill2,
|
|
21
21
|
};
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import React from "react";
|
|
1
2
|
import { describe } from "vitest";
|
|
2
3
|
import { redsBench } from "~root/utils/redsBench";
|
|
3
4
|
import { Switch } from "@xhs/reds-rn-next";
|
|
@@ -6,10 +7,6 @@ import { Text } from "react-native";
|
|
|
6
7
|
|
|
7
8
|
describe("render", () => {
|
|
8
9
|
redsBench("base", async () => {
|
|
9
|
-
render(
|
|
10
|
-
<Switch content="hello Switch">
|
|
11
|
-
<Text>{"button"}</Text>
|
|
12
|
-
</Switch>,
|
|
13
|
-
);
|
|
10
|
+
render(<Switch></Switch>);
|
|
14
11
|
});
|
|
15
12
|
});
|
|
@@ -1,30 +1,18 @@
|
|
|
1
1
|
import { describe, it, expect } from "vitest";
|
|
2
|
-
import { render, fireEvent } from "@testing-library/react-native";
|
|
2
|
+
import { render, fireEvent, userEvent } from "@testing-library/react-native";
|
|
3
3
|
import "@testing-library/react-native/extend-expect";
|
|
4
|
-
import { Pressable, Text } from "react-native";
|
|
4
|
+
import { Pressable, Text, View } from "react-native";
|
|
5
5
|
import Switch from "../index";
|
|
6
6
|
|
|
7
7
|
describe("Switch", () => {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
// }
|
|
18
|
-
// }}
|
|
19
|
-
// >
|
|
20
|
-
// <Text>{"button"}</Text>
|
|
21
|
-
// </Switch>,
|
|
22
|
-
// );
|
|
23
|
-
// expect(wrapper.queryByText("hello Switch")).not.toBeOnTheScreen();
|
|
24
|
-
// await fireEvent(wrapper.queryByText("button"), "press");
|
|
25
|
-
// expect(wrapper.queryByText("hello Switch")).toBeOnTheScreen();
|
|
26
|
-
// await fireEvent(wrapper.queryByText("button"), "press");
|
|
27
|
-
// expect(wrapper.queryByText("hello Switch")).not.toBeOnTheScreen();
|
|
28
|
-
});
|
|
8
|
+
it(":base", async () => {
|
|
9
|
+
let isOn = false;
|
|
10
|
+
const wrapper = render(<Switch onChange={(v) => (isOn = v)}></Switch>);
|
|
11
|
+
expect(wrapper.UNSAFE_root.props.defaultValue).toBe(false);
|
|
12
|
+
await fireEvent(wrapper.UNSAFE_root.children[0], "press");
|
|
13
|
+
expect(isOn).toBe(true);
|
|
14
|
+
|
|
15
|
+
await fireEvent(wrapper.UNSAFE_root.children[0], "press");
|
|
16
|
+
expect(isOn).toBe(false);
|
|
29
17
|
});
|
|
30
18
|
});
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import Button from "./components/Button";
|
|
2
2
|
import Popover from "./components/Popover";
|
|
3
3
|
import Switch from "./components/Switch";
|
|
4
|
-
|
|
4
|
+
import ConfigProvider, { useThemeColor } from "./components/ConfigProvider";
|
|
5
|
+
export { Button, Popover, Switch, ConfigProvider, useThemeColor };
|
package/tsconfig.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"extends": "../../tsconfig.json",
|
|
3
3
|
"compilerOptions": {
|
|
4
|
-
"outDir": "./
|
|
4
|
+
"outDir": "./lib",
|
|
5
5
|
"rootDir": "./"
|
|
6
6
|
},
|
|
7
7
|
"include": ["src"],
|
|
8
|
-
"exclude": ["node_modules", "lib"]
|
|
8
|
+
"exclude": ["node_modules", "lib", "src/**/demo/**", "src/**/test/**"]
|
|
9
9
|
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../../../../../src/components/Button/common/index.ts"],"sourcesContent":["import dayjs from \"dayjs\";\n\nexport const getTime = () => {\n const time = dayjs().format(\"YYYY-MM-DD HH:mm:ss\");\n return time;\n};\n"],"names":["getTime","time","dayjs","format"],"mappings":";;;;IAEaA,UAAU,SAAVA,UAAgB;EAC3B,IAAMC,IAAO,GAAAC,KAAA,EAAQ,CAAAC,MAAA,CAAO,qBAAqB,CAAA,CAAA;AAC1C,EAAA,OAAAF,IAAA,CAAA;AACT;;;;"}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
var _rollupPluginBabelHelpers = require('../../../_chunks/Dx-JjEWA.js');
|
|
6
|
-
var React = require('react');
|
|
7
|
-
var reactNative = require('react-native');
|
|
8
|
-
var components_Button_common_index = require('../common/index.js');
|
|
9
|
-
require('dayjs');
|
|
10
|
-
|
|
11
|
-
var index = (function (props) {
|
|
12
|
-
var _useState = React.useState(""),
|
|
13
|
-
_useState2 = _rollupPluginBabelHelpers._slicedToArray(_useState, 2),
|
|
14
|
-
time = _useState2[0],
|
|
15
|
-
setTime = _useState2[1];
|
|
16
|
-
return /* @__PURE__ */React.createElement(reactNative.View, null, /* @__PURE__ */React.createElement(reactNative.Button, {
|
|
17
|
-
title: props.name || "",
|
|
18
|
-
onPress: function onPress() {
|
|
19
|
-
setTime(components_Button_common_index.getTime());
|
|
20
|
-
}
|
|
21
|
-
}), /* @__PURE__ */React.createElement(reactNative.Text, null, time));
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
exports.default = index;
|
|
25
|
-
//# sourceMappingURL=index.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../../../../../src/components/Button/rn/index.tsx"],"sourcesContent":["import React, { useState } from \"react\";\nimport { Button, Text, View } from \"react-native\";\nimport { getTime } from \"../common/index\";\n\nimport { RedsButton } from \"../interface/index\";\n\nexport default (props: RedsButton) => {\n const [time, setTime] = useState(\"\");\n return (\n <View>\n <Button\n title={props.name || \"\"}\n onPress={() => {\n setTime(getTime());\n }}\n ></Button>\n <Text>{time}</Text>\n </View>\n );\n};\n"],"names":["props","_useState","useState","_useState2","_slicedToArray","time","setTime","View","React","createElement","Button","title","name","onPress","getTime","Text"],"mappings":";;;;;;;;;;AAMA,YAAe,CAAA,UAACA,KAAsB,EAAA;AACpC,EAAA,IAAAC,SAAA,GAAwBC,eAAS,EAAE,CAAA;IAAAC,UAAA,GAAAC,wCAAA,CAAAH,SAAA,EAAA,CAAA,CAAA;AAA5BI,IAAAA,IAAA,GAAAF,UAAA,CAAA,CAAA,CAAA;AAAMG,IAAAA,OAAO,GAAAH,UAAA,CAAA,CAAA,CAAA,CAAA;AACpB,EAAA,0CACGI,gBACC,EAAA,IAAA,iBAAAC,KAAA,CAAAC,aAAA,CAACC,kBAAA,EAAA;AACCC,IAAAA,KAAA,EAAOX,MAAMY,IAAQ,IAAA,EAAA;IACrBC,SAAS,SAAAA,OAAAA,GAAM;AACbP,MAAAA,OAAA,CAAQQ,wCAAS,CAAA,CAAA;AACnB,KAAA;AAAA,GACD,CACD,iBAAAN,KAAA,CAAAC,aAAA,CAACM,gBAAM,EAAA,IAAA,EAAAV,IAAK,CACd,CAAA,CAAA;AAEJ,CAAA;;;;"}
|
package/lib/cjs/index.d.ts
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import React, { ReactNode } from 'react';
|
|
2
|
-
import { View, GestureResponderEvent } from 'react-native';
|
|
3
|
-
|
|
4
|
-
declare const _default$1: React.ForwardRefExoticComponent<React.RefAttributes<View>>;
|
|
5
|
-
|
|
6
|
-
interface RedsPopover {
|
|
7
|
-
visible?: boolean;
|
|
8
|
-
placement?: "top" | "bottom";
|
|
9
|
-
content?: string;
|
|
10
|
-
opacity?: number;
|
|
11
|
-
mode?: "light" | "dark";
|
|
12
|
-
children?: ReactNode;
|
|
13
|
-
closeOnClickAgain?: boolean;
|
|
14
|
-
trigger?: "click" | "touch";
|
|
15
|
-
zIndex?: number;
|
|
16
|
-
onVisibleChange?: (v: boolean) => void;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
declare const _default: ({ mode, opacity, content, children, placement, closeOnClickAgain, trigger, zIndex, visible: defaultVisible, onVisibleChange, }: RedsPopover) => React.JSX.Element;
|
|
20
|
-
|
|
21
|
-
interface RedsSwitch {
|
|
22
|
-
value?: boolean;
|
|
23
|
-
defaultValue?: boolean;
|
|
24
|
-
disabled?: boolean;
|
|
25
|
-
onClick?: (event: GestureResponderEvent) => void;
|
|
26
|
-
onChange?: (value: boolean) => void;
|
|
27
|
-
trackOnColor?: string;
|
|
28
|
-
trackOffColor?: string;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
declare const Switch: {
|
|
32
|
-
({ value: cValue, defaultValue, onChange, onClick, trackOnColor, trackOffColor }: RedsSwitch): React.JSX.Element;
|
|
33
|
-
defaultProps: RedsSwitch;
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
export { _default$1 as Button, _default as Popover, Switch };
|
|
37
|
-
//# sourceMappingURL=index.d.ts.map
|
package/lib/cjs/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sources":[],"sourcesContent":[],"names":[],"mappings":""}
|