react-conditional-ui 1.2.0
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/LICENSE +21 -0
- package/README.md +183 -0
- package/dist/index.cjs +1839 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.css +218 -0
- package/dist/index.css.map +1 -0
- package/dist/index.d.cts +225 -0
- package/dist/index.d.ts +225 -0
- package/dist/index.js +1802 -0
- package/dist/index.js.map +1 -0
- package/dist/styles.css +281 -0
- package/logo.png +0 -0
- package/package.json +82 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Asaf Dulberg
|
|
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.
|
package/README.md
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
<div align="left">
|
|
2
|
+
<img src="logo.png" alt="react-conditional-ui logo" width="380" />
|
|
3
|
+
</div>
|
|
4
|
+
|
|
5
|
+
[](https://github.com/asafdl/react-conditional-ui/actions/workflows/ci.yml)
|
|
6
|
+

|
|
7
|
+

|
|
8
|
+

|
|
9
|
+
|
|
10
|
+
React component library for building conditions via natural language input. Users type plain-English phrases (e.g. "status is active") instead of filling out combo boxes. A fuzzy parser resolves fields, operators, and values, with support for compound `AND`/`OR` conditions and drag-and-drop grouping.
|
|
11
|
+
|
|
12
|
+
**[Live Demo](https://asafdl.github.io/react-conditional-ui/)**
|
|
13
|
+
|
|
14
|
+
## Components
|
|
15
|
+
|
|
16
|
+
### `<ConditionalUI />`
|
|
17
|
+
|
|
18
|
+
All-in-one component: input field + parsed condition output with editing and drag-and-drop.
|
|
19
|
+
|
|
20
|
+
```tsx
|
|
21
|
+
import { ConditionalUI } from "react-conditional-ui";
|
|
22
|
+
|
|
23
|
+
<ConditionalUI
|
|
24
|
+
fields={[
|
|
25
|
+
{ label: "Status", value: "status" },
|
|
26
|
+
{ label: "Age", value: "age", type: "number" },
|
|
27
|
+
]}
|
|
28
|
+
onConditionsChange={(groups) => console.log(groups)}
|
|
29
|
+
/>;
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### `<Input />`
|
|
33
|
+
|
|
34
|
+
Standalone text input with fuzzy parsing, ghost autocomplete, and inline diagnostics.
|
|
35
|
+
|
|
36
|
+
```tsx
|
|
37
|
+
import { Input } from "react-conditional-ui";
|
|
38
|
+
|
|
39
|
+
<Input fields={fields} operators={operators} onSubmit={(group) => console.log(group)} />;
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Also supports a fully controlled mode via `useConditionalInput`:
|
|
43
|
+
|
|
44
|
+
```tsx
|
|
45
|
+
import { Input, useConditionalInput } from "react-conditional-ui";
|
|
46
|
+
|
|
47
|
+
const { text, diagnostics, handleChange, handleSubmit, getSuggestion } = useConditionalInput({
|
|
48
|
+
fields,
|
|
49
|
+
onSubmit: handleGroup,
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
<Input
|
|
53
|
+
value={text}
|
|
54
|
+
onChange={handleChange}
|
|
55
|
+
onSubmit={handleSubmit}
|
|
56
|
+
getSuggestion={getSuggestion}
|
|
57
|
+
diagnostics={diagnostics}
|
|
58
|
+
/>;
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### `<Output />`
|
|
62
|
+
|
|
63
|
+
Renders parsed condition groups as interactive chip rows with drag-and-drop reordering, chip editing via popovers, connector toggling, and entry removal.
|
|
64
|
+
|
|
65
|
+
Uncontrolled (manages its own state):
|
|
66
|
+
|
|
67
|
+
```tsx
|
|
68
|
+
import { Output } from "react-conditional-ui";
|
|
69
|
+
|
|
70
|
+
<Output fields={fields} operators={operators} />;
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Controlled:
|
|
74
|
+
|
|
75
|
+
```tsx
|
|
76
|
+
<Output groups={groups} fields={fields} operators={operators} onGroupsChange={setGroups} />
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
Read-only (pass `groups` without `onGroupsChange`):
|
|
80
|
+
|
|
81
|
+
```tsx
|
|
82
|
+
<Output groups={groups} fields={fields} operators={operators} />
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### Group configuration
|
|
86
|
+
|
|
87
|
+
Each `ConditionGroup` accepts an optional `config` to control per-group behavior:
|
|
88
|
+
|
|
89
|
+
```tsx
|
|
90
|
+
const group: ConditionGroup = {
|
|
91
|
+
id: "1",
|
|
92
|
+
entries: [...],
|
|
93
|
+
config: {
|
|
94
|
+
editable: false, // disable chip editing
|
|
95
|
+
removable: false, // hide remove buttons
|
|
96
|
+
variant: "filled", // "outlined" (default) or "filled"
|
|
97
|
+
label: "Filters", // label above the group
|
|
98
|
+
},
|
|
99
|
+
};
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
You can also set defaults for all groups via `defaultGroupConfig` on `<Output />`.
|
|
103
|
+
|
|
104
|
+
## Hooks Instead of Components
|
|
105
|
+
|
|
106
|
+
The library’s behavior is split between **presentation** (`<Input />`, `<Output />`, `<ConditionalUI />`) and **data**: a memoized `ConditionDataProvider` facade (parse, suggest, complete, diagnose) plus optional React hooks for local state. You can use the hooks and export types only, and build your own inputs (native `<input>`, design-system fields, mobile, etc.) or your own condition display (lists, tables, read-only summaries).
|
|
107
|
+
|
|
108
|
+
### `useConditionDataProvider`
|
|
109
|
+
|
|
110
|
+
Lowest-level hook: creates a stable `ConditionDataProvider` for the given `fields` and optional `operators`, and returns `parseComplexCondition`, `getSuggestion`, `getCompletions`, `diagnose`, plus the raw `provider` instance. No text state, no submit handler—only the core API. Use this when you already manage `value` / `onChange` and want full control over when to parse or show diagnostics.
|
|
111
|
+
|
|
112
|
+
```tsx
|
|
113
|
+
import { useConditionDataProvider, DEFAULT_OPERATORS } from "react-conditional-ui";
|
|
114
|
+
|
|
115
|
+
const { parseComplexCondition, getSuggestion, diagnose } = useConditionDataProvider({
|
|
116
|
+
fields,
|
|
117
|
+
operators: DEFAULT_OPERATORS,
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
const group = parseComplexCondition(raw);
|
|
121
|
+
const issues = diagnose(raw);
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
### `useConditionalInput`
|
|
125
|
+
|
|
126
|
+
Opinionated input helper: internal or controlled string state, clears diagnostics on change, validates on submit, and wires `parseCompound` / `diagnose` for you. Pair it with `<Input />` when you need controlled mode (see above), or with your own field by calling `handleChange`, `handleSubmit`, and passing through `getSuggestion` / `getCompletions` / `diagnostics`.
|
|
127
|
+
|
|
128
|
+
### `useConditionalOutput`
|
|
129
|
+
|
|
130
|
+
Group list and mutations without rendering `<Output />`. Same `groups` / `onGroupsChange` controlled or uncontrolled patterns as the component; use `mutations` (`addGroup`, `removeEntry`, `toggleConnector`, `updateCondition`, reorder helpers, etc.) from your own UI.
|
|
131
|
+
|
|
132
|
+
```tsx
|
|
133
|
+
import { useConditionalOutput } from "react-conditional-ui";
|
|
134
|
+
|
|
135
|
+
const { groups, mutations } = useConditionalOutput({
|
|
136
|
+
onGroupsChange: (groups) => console.log(groups),
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
mutations.addGroup(parsedGroup);
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
## Styling
|
|
143
|
+
|
|
144
|
+
All components accept `className` and `style` props. Internal elements use `rcui-*` CSS classes that can be overridden.
|
|
145
|
+
|
|
146
|
+
## Debug logging
|
|
147
|
+
|
|
148
|
+
The library uses the [`debug`](https://www.npmjs.com/package/debug) package. Logs are silent by default. Enable them to see how the fuzzy parser resolves fields, operators, and values:
|
|
149
|
+
|
|
150
|
+
```js
|
|
151
|
+
// Browser — enable all library logs
|
|
152
|
+
localStorage.debug = "react-conditional-ui:*";
|
|
153
|
+
|
|
154
|
+
// Browser — specific namespace only
|
|
155
|
+
localStorage.debug = "react-conditional-ui:parser";
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
```bash
|
|
159
|
+
# Node / SSR
|
|
160
|
+
DEBUG=react-conditional-ui:* node app.js
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
Available namespaces: `parser`, `match-engine`.
|
|
164
|
+
|
|
165
|
+
## Tech debt
|
|
166
|
+
|
|
167
|
+
- `matchOperator` hard word-count gate should be a scoring penalty instead
|
|
168
|
+
- `match-engine.ts` mixes fuzzy matching with parsing logic (`parse`, `identifyField`, `resolveOperator`, `getOperatorCandidates`) — extract parsing into its own module
|
|
169
|
+
- `SegmentResolver.resolve` is ~80 lines with deep nesting — break down into smaller functions
|
|
170
|
+
- `SuggestionsProvider.completionsForSegment` is ~80 lines with repetitive branching — simplify
|
|
171
|
+
- `stripLeadingNoise` + `NOISE_WORDS` in `word-utils.ts` is domain-specific, not a word utility
|
|
172
|
+
- `segments.ts` line 1: `import { log } from "debug"` is wrong — `debug` default-exports a factory
|
|
173
|
+
|
|
174
|
+
## Local development
|
|
175
|
+
|
|
176
|
+
```bash
|
|
177
|
+
npm install
|
|
178
|
+
npm run dev
|
|
179
|
+
npm run build
|
|
180
|
+
npm test
|
|
181
|
+
npm run lint
|
|
182
|
+
npm run format
|
|
183
|
+
```
|