react-native-directional-toggle 0.1.1 → 0.1.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 +236 -38
- package/README.md +236 -37
- package/lib/commonjs/AnimatedSwitch.js +211 -0
- package/lib/commonjs/AnimatedSwitch.js.map +1 -0
- package/lib/commonjs/index.js +15 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/commonjs/package.json +1 -0
- package/lib/module/AnimatedSwitch.js +150 -134
- package/lib/module/AnimatedSwitch.js.map +1 -1
- package/lib/module/index.js +1 -0
- package/lib/module/index.js.map +1 -1
- package/lib/typescript/commonjs/package.json +1 -0
- package/lib/typescript/commonjs/src/AnimatedSwitch.d.ts +48 -0
- package/lib/typescript/commonjs/src/AnimatedSwitch.d.ts.map +1 -0
- package/lib/typescript/commonjs/src/index.d.ts +4 -0
- package/lib/typescript/commonjs/src/index.d.ts.map +1 -0
- package/lib/typescript/module/src/AnimatedSwitch.d.ts +48 -0
- package/lib/typescript/module/src/AnimatedSwitch.d.ts.map +1 -0
- package/lib/typescript/module/src/index.d.ts +4 -0
- package/lib/typescript/module/src/index.d.ts.map +1 -0
- package/package.json +43 -19
- package/src/AnimatedSwitch.tsx +243 -169
- package/src/index.tsx +2 -1
- package/lib/typescript/src/AnimatedSwitch.d.ts +0 -25
- package/lib/typescript/src/AnimatedSwitch.d.ts.map +0 -1
- package/lib/typescript/src/index.d.ts +0 -3
- package/lib/typescript/src/index.d.ts.map +0 -1
- /package/lib/typescript/{package.json → module/package.json} +0 -0
package/README.en.md
CHANGED
|
@@ -1,67 +1,265 @@
|
|
|
1
|
-
#
|
|
1
|
+
# react-native-directional-toggle
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
<p align="center">
|
|
4
|
+
<a href="https://www.npmjs.com/package/react-native-directional-toggle">
|
|
5
|
+
<img src="https://img.shields.io/npm/v/react-native-directional-toggle.svg?style=for-the-badge" alt="NPM Version" />
|
|
6
|
+
</a>
|
|
7
|
+
<a href="https://github.com/alansuhe/react-native-directional-toggle/actions/workflows/ci.yml">
|
|
8
|
+
<img src="https://img.shields.io/github/actions/workflow/status/alansuhe/react-native-directional-toggle/ci.yml?branch=main&style=for-the-badge" alt="CI Status" />
|
|
9
|
+
</a>
|
|
10
|
+
<a href="https://github.com/alansuhe/react-native-directional-toggle/blob/main/LICENSE">
|
|
11
|
+
<img src="https://img.shields.io/npm/l/react-native-directional-toggle.svg?style=for-the-badge" alt="License" />
|
|
12
|
+
</a>
|
|
13
|
+
<a href="https://www.npmjs.com/package/react-native-directional-toggle">
|
|
14
|
+
<img src="https://img.shields.io/npm/dm/react-native-directional-toggle.svg?style=for-the-badge" alt="NPM Downloads" />
|
|
15
|
+
</a>
|
|
16
|
+
</p>
|
|
4
17
|
|
|
5
|
-
|
|
18
|
+
<p align="center">
|
|
19
|
+
<b>Multi-Element Toggle Component</b><br/>
|
|
20
|
+
A multi-option toggle switch component for React Native and Expo with support for horizontal and vertical layouts and smooth animations.
|
|
21
|
+
</p>
|
|
6
22
|
|
|
7
|
-
|
|
23
|
+
<p align="center">
|
|
24
|
+
English | <a href="./README.md">中文</a>
|
|
25
|
+
</p>
|
|
8
26
|
|
|
9
|
-
|
|
27
|
+
---
|
|
28
|
+
|
|
29
|
+
## ✨ Features
|
|
30
|
+
|
|
31
|
+
- 🎯 **Multi-Option Support** - Toggle between two or more options
|
|
32
|
+
- 📱 **Bidirectional Layout** - Support for horizontal and vertical layouts
|
|
33
|
+
- 🎨 **Highly Customizable** - Customize styles, colors, and text styles
|
|
34
|
+
- ⚡ **Smooth Animations** - High-performance animations powered by Reanimated
|
|
35
|
+
- 👆 **Gesture Support** - Support for tap and swipe gestures
|
|
36
|
+
- 🔧 **TypeScript** - Complete TypeScript type support
|
|
37
|
+
- 📦 **Lightweight** - Zero dependencies (except peer dependencies)
|
|
38
|
+
- 🚀 **Expo Compatible** - Works with Expo projects
|
|
39
|
+
|
|
40
|
+
## 📦 Installation
|
|
41
|
+
|
|
42
|
+
### Using npm
|
|
10
43
|
|
|
11
44
|
```bash
|
|
12
|
-
|
|
45
|
+
npm install react-native-directional-toggle
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### Using yarn
|
|
13
49
|
|
|
50
|
+
```bash
|
|
14
51
|
yarn add react-native-directional-toggle
|
|
15
52
|
```
|
|
16
53
|
|
|
17
|
-
|
|
54
|
+
### Using pnpm
|
|
18
55
|
|
|
19
56
|
```bash
|
|
20
|
-
pnpm add react-native-
|
|
57
|
+
pnpm add react-native-directional-toggle
|
|
21
58
|
```
|
|
22
59
|
|
|
23
|
-
Dependencies
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
60
|
+
### Install Peer Dependencies
|
|
61
|
+
|
|
62
|
+
This component relies on the following peer dependencies. Please ensure they are installed:
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
# npm
|
|
66
|
+
npm install react-native-gesture-handler react-native-reanimated react-native-worklets
|
|
67
|
+
|
|
68
|
+
# yarn
|
|
69
|
+
yarn add react-native-gesture-handler react-native-reanimated react-native-worklets
|
|
27
70
|
|
|
28
|
-
|
|
71
|
+
# pnpm
|
|
72
|
+
pnpm add react-native-gesture-handler react-native-reanimated react-native-worklets
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
**Note:**
|
|
29
76
|
|
|
30
|
-
|
|
77
|
+
- Expo projects require `expo prebuild` and `expo run`, not supported in Expo Go
|
|
78
|
+
- You need to wrap your app with `GestureHandlerRootView`
|
|
31
79
|
|
|
32
|
-
|
|
80
|
+
## 🚀 Usage
|
|
33
81
|
|
|
34
|
-
###
|
|
82
|
+
### Basic Usage
|
|
35
83
|
|
|
36
84
|
```tsx
|
|
37
85
|
import { GestureHandlerRootView } from 'react-native-gesture-handler';
|
|
38
86
|
import Switcher from 'react-native-directional-toggle';
|
|
39
87
|
|
|
40
88
|
const options = [
|
|
41
|
-
{
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
},
|
|
45
|
-
{
|
|
46
|
-
label: 'Option 2',
|
|
47
|
-
value: 'Option 2',
|
|
48
|
-
},
|
|
49
|
-
{
|
|
50
|
-
label: 'Option 3',
|
|
51
|
-
value: 'Option 3',
|
|
52
|
-
},
|
|
89
|
+
{ label: 'Option 1', value: 'Option 1' },
|
|
90
|
+
{ label: 'Option 2', value: 'Option 2' },
|
|
91
|
+
{ label: 'Option 3', value: 'Option 3' },
|
|
53
92
|
];
|
|
54
93
|
|
|
55
|
-
|
|
94
|
+
function App() {
|
|
95
|
+
const [value, setValue] = useState('Option 2');
|
|
96
|
+
|
|
97
|
+
return (
|
|
98
|
+
<GestureHandlerRootView style={{ flex: 1 }}>
|
|
99
|
+
{/* Horizontal mode: specify width and height */}
|
|
100
|
+
<Switcher
|
|
101
|
+
options={options}
|
|
102
|
+
value={value}
|
|
103
|
+
onChange={setValue}
|
|
104
|
+
style={{ width: 300, height: 40, backgroundColor: '#f2f2f2' }}
|
|
105
|
+
thumbStyle={{ backgroundColor: '#fff', elevation: 3 }}
|
|
106
|
+
/>
|
|
107
|
+
</GestureHandlerRootView>
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### Vertical Mode
|
|
113
|
+
|
|
114
|
+
```tsx
|
|
115
|
+
import { View } from 'react-native';
|
|
116
|
+
|
|
117
|
+
// Vertical mode: adapts to parent container height
|
|
118
|
+
<View style={{ height: 200, width: 60 }}>
|
|
119
|
+
<Switcher
|
|
120
|
+
vertical
|
|
121
|
+
options={options}
|
|
122
|
+
value={value}
|
|
123
|
+
onChange={setValue}
|
|
124
|
+
style={{ flex: 1 }}
|
|
125
|
+
/>
|
|
126
|
+
</View>;
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
### Custom Styles
|
|
130
|
+
|
|
131
|
+
```tsx
|
|
132
|
+
<Switcher
|
|
133
|
+
options={[
|
|
134
|
+
{ label: 'Day', value: 'day' },
|
|
135
|
+
{ label: 'Week', value: 'week' },
|
|
136
|
+
{ label: 'Month', value: 'month' },
|
|
137
|
+
]}
|
|
138
|
+
value={value}
|
|
139
|
+
onChange={setValue}
|
|
140
|
+
style={{
|
|
141
|
+
width: 240,
|
|
142
|
+
height: 44,
|
|
143
|
+
backgroundColor: '#e8e8e8',
|
|
144
|
+
borderRadius: 22,
|
|
145
|
+
padding: 3,
|
|
146
|
+
}}
|
|
147
|
+
thumbStyle={{
|
|
148
|
+
backgroundColor: '#fff',
|
|
149
|
+
borderRadius: 20,
|
|
150
|
+
shadowColor: '#000',
|
|
151
|
+
shadowOffset: { width: 0, height: 2 },
|
|
152
|
+
shadowOpacity: 0.1,
|
|
153
|
+
shadowRadius: 4,
|
|
154
|
+
elevation: 3,
|
|
155
|
+
}}
|
|
156
|
+
textStyle={{
|
|
157
|
+
fontSize: 14,
|
|
158
|
+
fontWeight: '600',
|
|
159
|
+
}}
|
|
160
|
+
activeTextStyle={{
|
|
161
|
+
color: '#007AFF',
|
|
162
|
+
}}
|
|
163
|
+
inactiveTextStyle={{
|
|
164
|
+
color: '#666',
|
|
165
|
+
}}
|
|
166
|
+
/>
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
## 📖 API Documentation
|
|
170
|
+
|
|
171
|
+
### Props
|
|
172
|
+
|
|
173
|
+
| Prop | Type | Required | Default | Description |
|
|
174
|
+
| ------------------- | -------------------------------------- | -------- | ------------------- | ------------------------------------------------------ |
|
|
175
|
+
| `options` | `Option[]` | ✅ | - | List of options, each with `label` and `value` |
|
|
176
|
+
| `value` | `string \| number` | ✅ | - | Currently selected value |
|
|
177
|
+
| `onChange` | `(value: string \| number) => void` | ✅ | - | Callback when value changes |
|
|
178
|
+
| `vertical` | `boolean` | ❌ | `false` | Whether to use vertical layout |
|
|
179
|
+
| `style` | `StyleProp<ViewStyle>` | ❌ | - | Container style for width, height, background, etc. |
|
|
180
|
+
| `thumbStyle` | `StyleProp<ViewStyle>` | ❌ | - | Style for the sliding thumb (selected item background) |
|
|
181
|
+
| `textStyle` | `StyleProp<TextStyle>` | ❌ | - | Default style for option text |
|
|
182
|
+
| `activeTextStyle` | `StyleProp<TextStyle>` | ❌ | - | Text style when active/selected |
|
|
183
|
+
| `inactiveTextStyle` | `StyleProp<TextStyle>` | ❌ | - | Text style when inactive |
|
|
184
|
+
| `animationConfig` | `WithTimingConfig \| WithSpringConfig` | ❌ | `{ duration: 150 }` | Animation configuration |
|
|
185
|
+
|
|
186
|
+
### Type Definitions
|
|
187
|
+
|
|
188
|
+
```typescript
|
|
189
|
+
type Option = {
|
|
190
|
+
label: string;
|
|
191
|
+
value: string | number;
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
type AnimatedSwitchProps = {
|
|
195
|
+
options: Option[];
|
|
196
|
+
value: string | number;
|
|
197
|
+
onChange: (value: string | number) => void;
|
|
198
|
+
vertical?: boolean;
|
|
199
|
+
style?: StyleProp<ViewStyle>;
|
|
200
|
+
thumbStyle?: StyleProp<ViewStyle>;
|
|
201
|
+
textStyle?: StyleProp<TextStyle>;
|
|
202
|
+
activeTextStyle?: StyleProp<TextStyle>;
|
|
203
|
+
inactiveTextStyle?: StyleProp<TextStyle>;
|
|
204
|
+
animationConfig?: WithTimingConfig | WithSpringConfig;
|
|
205
|
+
};
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
### Animation Configuration
|
|
209
|
+
|
|
210
|
+
Customize animations via `animationConfig`:
|
|
211
|
+
|
|
212
|
+
```tsx
|
|
213
|
+
// Using timing animation
|
|
214
|
+
<Switcher
|
|
215
|
+
animationConfig={{
|
|
216
|
+
duration: 200, // Animation duration in milliseconds
|
|
217
|
+
}}
|
|
218
|
+
/>
|
|
219
|
+
|
|
220
|
+
// Using spring animation
|
|
221
|
+
<Switcher
|
|
222
|
+
animationConfig={{
|
|
223
|
+
damping: 15, // Damping coefficient
|
|
224
|
+
stiffness: 150, // Stiffness
|
|
225
|
+
mass: 1, // Mass
|
|
226
|
+
}}
|
|
227
|
+
/>
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
## 📱 Example
|
|
231
|
+
|
|
232
|
+
Check the [example](example/) directory for a complete example project.
|
|
233
|
+
|
|
234
|
+
```bash
|
|
235
|
+
# Clone the repository
|
|
236
|
+
git clone https://github.com/alansuhe/react-native-directional-toggle.git
|
|
56
237
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
options={options}
|
|
60
|
-
value={'Option 2'}
|
|
61
|
-
height={36}
|
|
62
|
-
onChange={value => console.log(value)}
|
|
63
|
-
/>
|
|
64
|
-
</GestureHandlerRootView>
|
|
238
|
+
# Navigate to the example directory
|
|
239
|
+
cd react-native-directional-toggle/example
|
|
65
240
|
|
|
241
|
+
# Install dependencies
|
|
242
|
+
pnpm install
|
|
243
|
+
|
|
244
|
+
# Run on iOS
|
|
245
|
+
pnpm ios
|
|
246
|
+
|
|
247
|
+
# Run on Android
|
|
248
|
+
pnpm android
|
|
66
249
|
```
|
|
67
|
-
|
|
250
|
+
|
|
251
|
+
## 🤝 Contributing
|
|
252
|
+
|
|
253
|
+
Issues and Pull Requests are welcome!
|
|
254
|
+
|
|
255
|
+
Please read our [Contributing Guide](./CONTRIBUTING.md) for more information.
|
|
256
|
+
|
|
257
|
+
## 📄 License
|
|
258
|
+
|
|
259
|
+
[MIT](./LICENSE) © [Alan Suhe](https://github.com/alansuhe)
|
|
260
|
+
|
|
261
|
+
---
|
|
262
|
+
|
|
263
|
+
<p align="center">
|
|
264
|
+
If this project helped you, please give it a ⭐️
|
|
265
|
+
</p>
|
package/README.md
CHANGED
|
@@ -1,66 +1,265 @@
|
|
|
1
|
-
#
|
|
1
|
+
# react-native-directional-toggle
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
<p align="center">
|
|
4
|
+
<a href="https://www.npmjs.com/package/react-native-directional-toggle">
|
|
5
|
+
<img src="https://img.shields.io/npm/v/react-native-directional-toggle.svg?style=for-the-badge" alt="NPM Version" />
|
|
6
|
+
</a>
|
|
7
|
+
<a href="https://github.com/alansuhe/react-native-directional-toggle/actions/workflows/ci.yml">
|
|
8
|
+
<img src="https://img.shields.io/github/actions/workflow/status/alansuhe/react-native-directional-toggle/ci.yml?branch=main&style=for-the-badge" alt="CI Status" />
|
|
9
|
+
</a>
|
|
10
|
+
<a href="https://github.com/alansuhe/react-native-directional-toggle/blob/main/LICENSE">
|
|
11
|
+
<img src="https://img.shields.io/npm/l/react-native-directional-toggle.svg?style=for-the-badge" alt="License" />
|
|
12
|
+
</a>
|
|
13
|
+
<a href="https://www.npmjs.com/package/react-native-directional-toggle">
|
|
14
|
+
<img src="https://img.shields.io/npm/dm/react-native-directional-toggle.svg?style=for-the-badge" alt="NPM Downloads" />
|
|
15
|
+
</a>
|
|
16
|
+
</p>
|
|
4
17
|
|
|
5
|
-
|
|
18
|
+
<p align="center">
|
|
19
|
+
<b>多元素切换组件</b><br/>
|
|
20
|
+
支持 React Native 和 Expo 的多选项切换开关组件,支持横向和纵向布局及流畅动画。
|
|
21
|
+
</p>
|
|
6
22
|
|
|
7
|
-
|
|
23
|
+
<p align="center">
|
|
24
|
+
<a href="./README.en.md">English</a> | 中文
|
|
25
|
+
</p>
|
|
8
26
|
|
|
9
|
-
|
|
27
|
+
---
|
|
28
|
+
|
|
29
|
+
## ✨ 特性
|
|
30
|
+
|
|
31
|
+
- 🎯 **多选项支持** - 支持两个或更多选项的切换
|
|
32
|
+
- 📱 **双方向布局** - 支持横向(horizontal)和纵向(vertical)布局
|
|
33
|
+
- 🎨 **高度可定制** - 自定义样式、颜色、文字样式
|
|
34
|
+
- ⚡ **流畅动画** - 基于 Reanimated 的高性能动画
|
|
35
|
+
- 👆 **手势支持** - 支持点击和滑动手势
|
|
36
|
+
- 🔧 **TypeScript** - 完整的 TypeScript 类型支持
|
|
37
|
+
- 📦 **轻量级** - 零依赖(peer dependencies 除外)
|
|
38
|
+
- 🚀 **Expo 支持** - 兼容 Expo 项目
|
|
39
|
+
|
|
40
|
+
## 📦 安装
|
|
41
|
+
|
|
42
|
+
### 使用 npm
|
|
10
43
|
|
|
11
44
|
```bash
|
|
12
|
-
|
|
45
|
+
npm install react-native-directional-toggle
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### 使用 yarn
|
|
13
49
|
|
|
50
|
+
```bash
|
|
14
51
|
yarn add react-native-directional-toggle
|
|
15
52
|
```
|
|
16
53
|
|
|
17
|
-
|
|
54
|
+
### 使用 pnpm
|
|
18
55
|
|
|
19
56
|
```bash
|
|
20
|
-
pnpm add react-native-
|
|
57
|
+
pnpm add react-native-directional-toggle
|
|
21
58
|
```
|
|
22
59
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
60
|
+
### 安装 peer dependencies
|
|
61
|
+
|
|
62
|
+
本组件依赖以下 peer dependencies,请确保已安装:
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
# npm
|
|
66
|
+
npm install react-native-gesture-handler react-native-reanimated react-native-worklets
|
|
67
|
+
|
|
68
|
+
# yarn
|
|
69
|
+
yarn add react-native-gesture-handler react-native-reanimated react-native-worklets
|
|
27
70
|
|
|
71
|
+
# pnpm
|
|
72
|
+
pnpm add react-native-gesture-handler react-native-reanimated react-native-worklets
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
**注意:**
|
|
28
76
|
|
|
29
|
-
|
|
77
|
+
- Expo 项目需要使用 `expo prebuild` 后通过 `expo run` 运行,不支持 Expo Go
|
|
78
|
+
- 需要在应用外层包裹 `GestureHandlerRootView`
|
|
30
79
|
|
|
31
|
-
|
|
80
|
+
## 🚀 使用
|
|
32
81
|
|
|
33
|
-
###
|
|
82
|
+
### 基础用法
|
|
34
83
|
|
|
35
84
|
```tsx
|
|
36
85
|
import { GestureHandlerRootView } from 'react-native-gesture-handler';
|
|
37
86
|
import Switcher from 'react-native-directional-toggle';
|
|
38
87
|
|
|
39
88
|
const options = [
|
|
40
|
-
{
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
},
|
|
44
|
-
{
|
|
45
|
-
label: 'Option 2',
|
|
46
|
-
value: 'Option 2',
|
|
47
|
-
},
|
|
48
|
-
{
|
|
49
|
-
label: 'Option 3',
|
|
50
|
-
value: 'Option 3',
|
|
51
|
-
},
|
|
89
|
+
{ label: 'Option 1', value: 'Option 1' },
|
|
90
|
+
{ label: 'Option 2', value: 'Option 2' },
|
|
91
|
+
{ label: 'Option 3', value: 'Option 3' },
|
|
52
92
|
];
|
|
53
93
|
|
|
54
|
-
|
|
94
|
+
function App() {
|
|
95
|
+
const [value, setValue] = useState('Option 2');
|
|
96
|
+
|
|
97
|
+
return (
|
|
98
|
+
<GestureHandlerRootView style={{ flex: 1 }}>
|
|
99
|
+
{/* 横向模式:指定宽度和高度 */}
|
|
100
|
+
<Switcher
|
|
101
|
+
options={options}
|
|
102
|
+
value={value}
|
|
103
|
+
onChange={setValue}
|
|
104
|
+
style={{ width: 300, height: 40, backgroundColor: '#f2f2f2' }}
|
|
105
|
+
thumbStyle={{ backgroundColor: '#fff', elevation: 3 }}
|
|
106
|
+
/>
|
|
107
|
+
</GestureHandlerRootView>
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### 纵向模式
|
|
113
|
+
|
|
114
|
+
```tsx
|
|
115
|
+
import { View } from 'react-native';
|
|
116
|
+
|
|
117
|
+
// 纵向模式:自适应父容器高度
|
|
118
|
+
<View style={{ height: 200, width: 60 }}>
|
|
119
|
+
<Switcher
|
|
120
|
+
vertical
|
|
121
|
+
options={options}
|
|
122
|
+
value={value}
|
|
123
|
+
onChange={setValue}
|
|
124
|
+
style={{ flex: 1 }}
|
|
125
|
+
/>
|
|
126
|
+
</View>;
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
### 自定义样式
|
|
130
|
+
|
|
131
|
+
```tsx
|
|
132
|
+
<Switcher
|
|
133
|
+
options={[
|
|
134
|
+
{ label: '日', value: 'day' },
|
|
135
|
+
{ label: '周', value: 'week' },
|
|
136
|
+
{ label: '月', value: 'month' },
|
|
137
|
+
]}
|
|
138
|
+
value={value}
|
|
139
|
+
onChange={setValue}
|
|
140
|
+
style={{
|
|
141
|
+
width: 240,
|
|
142
|
+
height: 44,
|
|
143
|
+
backgroundColor: '#e8e8e8',
|
|
144
|
+
borderRadius: 22,
|
|
145
|
+
padding: 3,
|
|
146
|
+
}}
|
|
147
|
+
thumbStyle={{
|
|
148
|
+
backgroundColor: '#fff',
|
|
149
|
+
borderRadius: 20,
|
|
150
|
+
shadowColor: '#000',
|
|
151
|
+
shadowOffset: { width: 0, height: 2 },
|
|
152
|
+
shadowOpacity: 0.1,
|
|
153
|
+
shadowRadius: 4,
|
|
154
|
+
elevation: 3,
|
|
155
|
+
}}
|
|
156
|
+
textStyle={{
|
|
157
|
+
fontSize: 14,
|
|
158
|
+
fontWeight: '600',
|
|
159
|
+
}}
|
|
160
|
+
activeTextStyle={{
|
|
161
|
+
color: '#007AFF',
|
|
162
|
+
}}
|
|
163
|
+
inactiveTextStyle={{
|
|
164
|
+
color: '#666',
|
|
165
|
+
}}
|
|
166
|
+
/>
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
## 📖 API 文档
|
|
170
|
+
|
|
171
|
+
### Props
|
|
172
|
+
|
|
173
|
+
| 属性 | 类型 | 必填 | 默认值 | 描述 |
|
|
174
|
+
| ------------------- | -------------------------------------- | ---- | ------------------- | ----------------------------------------- |
|
|
175
|
+
| `options` | `Option[]` | ✅ | - | 选项列表,每个选项包含 `label` 和 `value` |
|
|
176
|
+
| `value` | `string \| number` | ✅ | - | 当前选中的值 |
|
|
177
|
+
| `onChange` | `(value: string \| number) => void` | ✅ | - | 值改变时的回调函数 |
|
|
178
|
+
| `vertical` | `boolean` | ❌ | `false` | 是否为纵向布局 |
|
|
179
|
+
| `style` | `StyleProp<ViewStyle>` | ❌ | - | 容器样式,用于设置宽度、高度、背景色等 |
|
|
180
|
+
| `thumbStyle` | `StyleProp<ViewStyle>` | ❌ | - | 滑块(选中项背景)样式 |
|
|
181
|
+
| `textStyle` | `StyleProp<TextStyle>` | ❌ | - | 选项文字的默认样式 |
|
|
182
|
+
| `activeTextStyle` | `StyleProp<TextStyle>` | ❌ | - | 选中状态的文字样式 |
|
|
183
|
+
| `inactiveTextStyle` | `StyleProp<TextStyle>` | ❌ | - | 未选中状态的文字样式 |
|
|
184
|
+
| `animationConfig` | `WithTimingConfig \| WithSpringConfig` | ❌ | `{ duration: 150 }` | 动画配置 |
|
|
185
|
+
|
|
186
|
+
### 类型定义
|
|
187
|
+
|
|
188
|
+
```typescript
|
|
189
|
+
type Option = {
|
|
190
|
+
label: string;
|
|
191
|
+
value: string | number;
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
type AnimatedSwitchProps = {
|
|
195
|
+
options: Option[];
|
|
196
|
+
value: string | number;
|
|
197
|
+
onChange: (value: string | number) => void;
|
|
198
|
+
vertical?: boolean;
|
|
199
|
+
style?: StyleProp<ViewStyle>;
|
|
200
|
+
thumbStyle?: StyleProp<ViewStyle>;
|
|
201
|
+
textStyle?: StyleProp<TextStyle>;
|
|
202
|
+
activeTextStyle?: StyleProp<TextStyle>;
|
|
203
|
+
inactiveTextStyle?: StyleProp<TextStyle>;
|
|
204
|
+
animationConfig?: WithTimingConfig | WithSpringConfig;
|
|
205
|
+
};
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
### 动画配置
|
|
209
|
+
|
|
210
|
+
可以通过 `animationConfig` 自定义动画效果:
|
|
211
|
+
|
|
212
|
+
```tsx
|
|
213
|
+
// 使用 timing 动画
|
|
214
|
+
<Switcher
|
|
215
|
+
animationConfig={{
|
|
216
|
+
duration: 200, // 动画时长(毫秒)
|
|
217
|
+
}}
|
|
218
|
+
/>
|
|
219
|
+
|
|
220
|
+
// 使用 spring 动画
|
|
221
|
+
<Switcher
|
|
222
|
+
animationConfig={{
|
|
223
|
+
damping: 15, // 阻尼系数
|
|
224
|
+
stiffness: 150, // 刚度
|
|
225
|
+
mass: 1, // 质量
|
|
226
|
+
}}
|
|
227
|
+
/>
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
## 📱 示例
|
|
231
|
+
|
|
232
|
+
查看 [example](example/) 目录获取完整的示例项目。
|
|
233
|
+
|
|
234
|
+
```bash
|
|
235
|
+
# 克隆仓库
|
|
236
|
+
git clone https://github.com/alansuhe/react-native-directional-toggle.git
|
|
55
237
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
options={options}
|
|
59
|
-
value={'Option 2'}
|
|
60
|
-
height={36}
|
|
61
|
-
onChange={value => console.log(value)}
|
|
62
|
-
/>
|
|
63
|
-
</GestureHandlerRootView>
|
|
238
|
+
# 进入示例目录
|
|
239
|
+
cd react-native-directional-toggle/example
|
|
64
240
|
|
|
241
|
+
# 安装依赖
|
|
242
|
+
pnpm install
|
|
243
|
+
|
|
244
|
+
# 运行 iOS
|
|
245
|
+
pnpm ios
|
|
246
|
+
|
|
247
|
+
# 运行 Android
|
|
248
|
+
pnpm android
|
|
65
249
|
```
|
|
66
|
-
|
|
250
|
+
|
|
251
|
+
## 🤝 贡献
|
|
252
|
+
|
|
253
|
+
欢迎提交 Issue 和 Pull Request!
|
|
254
|
+
|
|
255
|
+
请阅读我们的[贡献指南](./CONTRIBUTING.md)了解详细信息。
|
|
256
|
+
|
|
257
|
+
## 📄 许可证
|
|
258
|
+
|
|
259
|
+
[MIT](./LICENSE) © [Alan Suhe](https://github.com/alansuhe)
|
|
260
|
+
|
|
261
|
+
---
|
|
262
|
+
|
|
263
|
+
<p align="center">
|
|
264
|
+
如果这个项目对您有帮助,请给它一个 ⭐️
|
|
265
|
+
</p>
|