conditional-selection 1.1.5 → 1.1.6
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.md +199 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
# conditional-selection
|
|
2
2
|
|
|
3
3
|
<p>
|
|
4
|
-
<a href="
|
|
4
|
+
<a href="#english">English</a> | <a href="#中文">中文</a>
|
|
5
5
|
</p>
|
|
6
6
|
|
|
7
|
+
<h2 id="english">English</h2>
|
|
8
|
+
|
|
7
9
|
A lightweight React component library for building conditional rule trees. Supports recursive nesting, AND/OR grouping, dynamic add/remove, and fully customizable condition rows via render props.
|
|
8
10
|
|
|
9
11
|
## Installation
|
|
@@ -195,3 +197,199 @@ Components are exported from `packages/index.ts`. Styles are written in Less (`p
|
|
|
195
197
|
## License
|
|
196
198
|
|
|
197
199
|
MIT
|
|
200
|
+
|
|
201
|
+
---
|
|
202
|
+
|
|
203
|
+
<h2 id="中文">中文</h2>
|
|
204
|
+
|
|
205
|
+
一个轻量级的 React 条件规则树组件库,支持递归嵌套、AND/OR 分组、动态增删,以及通过 render props 完全自定义条件行内容。
|
|
206
|
+
|
|
207
|
+
## 安装
|
|
208
|
+
|
|
209
|
+
```bash
|
|
210
|
+
npm install conditional-selection
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
> **同级依赖(peerDependencies):** `react >= 16.8.0`,`react-dom >= 16.8.0`
|
|
214
|
+
|
|
215
|
+
## 快速开始
|
|
216
|
+
|
|
217
|
+
```tsx
|
|
218
|
+
import { ConditionalSelection } from 'conditional-selection';
|
|
219
|
+
import type { TConditionalSelection } from 'conditional-selection';
|
|
220
|
+
|
|
221
|
+
export default function App() {
|
|
222
|
+
const [rules, setRules] = useState<TConditionalSelection | undefined>();
|
|
223
|
+
|
|
224
|
+
return (
|
|
225
|
+
<ConditionalSelection
|
|
226
|
+
conditionalRules={rules}
|
|
227
|
+
maxDeep={3}
|
|
228
|
+
onChange={setRules}
|
|
229
|
+
renderConditionRules={(item, change) => (
|
|
230
|
+
<MyConditionRow item={item} onChange={change} />
|
|
231
|
+
)}
|
|
232
|
+
/>
|
|
233
|
+
);
|
|
234
|
+
}
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
## Props 参数说明
|
|
238
|
+
|
|
239
|
+
| 参数 | 类型 | 默认值 | 说明 |
|
|
240
|
+
|------|------|--------|------|
|
|
241
|
+
| `conditionalRules` | `TConditionalSelection \| null` | `undefined` | 条件树数据,传入 `null` 或 `undefined` 时自动初始化 |
|
|
242
|
+
| `maxDeep` | `number` | `1` | 最大递归层级,必须大于 `0` |
|
|
243
|
+
| `disabled` | `boolean \| TConditionalSelectionDisabledProps` | `false` | 禁用全部或部分操作 |
|
|
244
|
+
| `onChange` | `(value: TConditionalSelection \| undefined) => void` | — | 数据变更回调 |
|
|
245
|
+
| `renderConditionRules` | `(item, change) => ReactNode` | — | 条件行渲染插槽,参数为当前 `INDIVIDUAL` 节点和更新 handler |
|
|
246
|
+
| `renderCreateCondition` | `(params) => ReactNode` | — | 自定义"添加条件"按钮的渲染插槽 |
|
|
247
|
+
|
|
248
|
+
### `disabled` 对象格式
|
|
249
|
+
|
|
250
|
+
```ts
|
|
251
|
+
type TConditionalSelectionDisabledProps = {
|
|
252
|
+
addItem?: boolean; // 禁用添加条件
|
|
253
|
+
delItem?: boolean; // 禁用删除条件
|
|
254
|
+
linkChange?: boolean; // 禁用 AND/OR 切换
|
|
255
|
+
};
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
## 数据结构
|
|
259
|
+
|
|
260
|
+
```ts
|
|
261
|
+
type TConditionalSelection<T = any> = {
|
|
262
|
+
_id: string;
|
|
263
|
+
framework: 'group' | 'individual';
|
|
264
|
+
link?: 'and' | 'or';
|
|
265
|
+
group?: TConditionalSelection<T>[];
|
|
266
|
+
individual?: Record<string, any>;
|
|
267
|
+
level: number;
|
|
268
|
+
};
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
- **`group`** 节点 — 包含子节点,支持 AND/OR 关系切换
|
|
272
|
+
- **`individual`** 节点 — 单个条件行,自定义数据存放在 `individual` 字段中
|
|
273
|
+
|
|
274
|
+
### 数据示例
|
|
275
|
+
|
|
276
|
+
**扁平结构** — 两个 `individual` 条件以 AND 连接:
|
|
277
|
+
|
|
278
|
+
```json
|
|
279
|
+
{
|
|
280
|
+
"_id": "2dfd8d9d-4bbf-4bc3-aae6-6c747ccbdd10",
|
|
281
|
+
"framework": "group",
|
|
282
|
+
"level": 0,
|
|
283
|
+
"link": "and",
|
|
284
|
+
"group": [
|
|
285
|
+
{
|
|
286
|
+
"_id": "02399b6c-10a3-41d4-9722-cce2db9ae056",
|
|
287
|
+
"framework": "individual",
|
|
288
|
+
"individual": { "field": 1, "function": 1, "value": 1 },
|
|
289
|
+
"level": 1
|
|
290
|
+
},
|
|
291
|
+
{
|
|
292
|
+
"_id": "6c627903-b970-43a8-a3f5-f5aba89f2200",
|
|
293
|
+
"framework": "individual",
|
|
294
|
+
"individual": { "field": 1, "function": 1, "value": 1 },
|
|
295
|
+
"level": 1
|
|
296
|
+
}
|
|
297
|
+
]
|
|
298
|
+
}
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
**嵌套结构** — 一个 `individual` 与一个嵌套 `group`(OR)并列于第 1 层:
|
|
302
|
+
|
|
303
|
+
```json
|
|
304
|
+
{
|
|
305
|
+
"_id": "2dfd8d9d-4bbf-4bc3-aae6-6c747ccbdd10",
|
|
306
|
+
"framework": "group",
|
|
307
|
+
"level": 0,
|
|
308
|
+
"link": "and",
|
|
309
|
+
"group": [
|
|
310
|
+
{
|
|
311
|
+
"_id": "02399b6c-10a3-41d4-9722-cce2db9ae056",
|
|
312
|
+
"framework": "individual",
|
|
313
|
+
"individual": { "field": 1, "function": 1, "value": 1 },
|
|
314
|
+
"level": 1
|
|
315
|
+
},
|
|
316
|
+
{
|
|
317
|
+
"_id": "2f808fe6-4dd8-488d-9b79-d2888915fddc",
|
|
318
|
+
"framework": "group",
|
|
319
|
+
"level": 1,
|
|
320
|
+
"link": "or",
|
|
321
|
+
"group": [
|
|
322
|
+
{
|
|
323
|
+
"_id": "17336bf2-2885-4350-8055-233b0058ec39",
|
|
324
|
+
"framework": "individual",
|
|
325
|
+
"individual": { "field": 1, "function": 1, "value": 1 },
|
|
326
|
+
"level": 2
|
|
327
|
+
},
|
|
328
|
+
{
|
|
329
|
+
"_id": "6e21bd9a-f994-474a-bba5-d77ac9013a65",
|
|
330
|
+
"framework": "individual",
|
|
331
|
+
"individual": { "field": 1, "function": 1, "value": 1 },
|
|
332
|
+
"level": 2
|
|
333
|
+
}
|
|
334
|
+
]
|
|
335
|
+
}
|
|
336
|
+
]
|
|
337
|
+
}
|
|
338
|
+
```
|
|
339
|
+
|
|
340
|
+
## 通过 Ref 获取数据
|
|
341
|
+
|
|
342
|
+
```tsx
|
|
343
|
+
const ref = useRef<any>(null);
|
|
344
|
+
|
|
345
|
+
<ConditionalSelection ref={ref} ... />
|
|
346
|
+
|
|
347
|
+
// 返回当前条件树的深拷贝
|
|
348
|
+
const data = await ref.current.getConditionalSelectionData();
|
|
349
|
+
```
|
|
350
|
+
|
|
351
|
+
## 自定义条件行示例
|
|
352
|
+
|
|
353
|
+
```tsx
|
|
354
|
+
const MyConditionRow = ({ item, onChange }) => {
|
|
355
|
+
const data = item.individual ?? {};
|
|
356
|
+
return (
|
|
357
|
+
<div style={{ display: 'flex', gap: 8 }}>
|
|
358
|
+
<select value={data.field ?? ''} onChange={e => onChange({ field: e.target.value })}>
|
|
359
|
+
<option value="">选择字段</option>
|
|
360
|
+
<option value="name">名称</option>
|
|
361
|
+
</select>
|
|
362
|
+
<select value={data.operator ?? ''} onChange={e => onChange({ ...data, operator: e.target.value })}>
|
|
363
|
+
<option value="">选择操作符</option>
|
|
364
|
+
<option value="eq">等于</option>
|
|
365
|
+
<option value="ne">不等于</option>
|
|
366
|
+
</select>
|
|
367
|
+
<input
|
|
368
|
+
value={data.value ?? ''}
|
|
369
|
+
onChange={e => onChange({ ...data, value: e.target.value })}
|
|
370
|
+
placeholder="输入值"
|
|
371
|
+
/>
|
|
372
|
+
</div>
|
|
373
|
+
);
|
|
374
|
+
};
|
|
375
|
+
```
|
|
376
|
+
|
|
377
|
+
## 性能优化建议
|
|
378
|
+
|
|
379
|
+
- 将 handler 和 render props 用 `useCallback` 包裹,避免不必要的重渲染
|
|
380
|
+
- 如需深度性能分析,可借助 [React Profiler](https://react.dev/reference/react/Profiler)
|
|
381
|
+
|
|
382
|
+
## 贡献与开发
|
|
383
|
+
|
|
384
|
+
```bash
|
|
385
|
+
git clone https://github.com/1512-Yolo/conditional-selection.git
|
|
386
|
+
cd conditional-selection
|
|
387
|
+
npm install
|
|
388
|
+
npm run dev
|
|
389
|
+
```
|
|
390
|
+
|
|
391
|
+
组件统一从 `packages/index.ts` 导出,样式使用 Less(`packages/ConditionalSelection/index.less`)编写。
|
|
392
|
+
|
|
393
|
+
## License
|
|
394
|
+
|
|
395
|
+
MIT
|
package/package.json
CHANGED
|
@@ -12,8 +12,8 @@
|
|
|
12
12
|
"vite-plugin-lib-inject-css": "^2.2.2"
|
|
13
13
|
},
|
|
14
14
|
"name": "conditional-selection",
|
|
15
|
-
"version": "1.1.
|
|
16
|
-
"description": "",
|
|
15
|
+
"version": "1.1.6",
|
|
16
|
+
"description": "conditional-selection",
|
|
17
17
|
"main": "dist/ui.cjs.js",
|
|
18
18
|
"module": "dist/ui.es.js",
|
|
19
19
|
"types": "dist/types/ui.es.d.ts",
|