@capyx/components-library 0.0.1 → 0.0.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.md +250 -210
- package/dist/addons/AutocompleteInput.d.ts +40 -0
- package/dist/addons/AutocompleteInput.d.ts.map +1 -0
- package/dist/addons/AutocompleteInput.js +101 -0
- package/dist/addons/CharacterCountInput.d.ts +73 -0
- package/dist/addons/CharacterCountInput.d.ts.map +1 -0
- package/dist/addons/CharacterCountInput.js +130 -0
- package/dist/addons/index.d.ts +5 -0
- package/dist/addons/index.d.ts.map +1 -0
- package/dist/addons/index.js +2 -0
- package/dist/components/CheckInput.d.ts +49 -0
- package/dist/components/CheckInput.d.ts.map +1 -0
- package/dist/components/CheckInput.js +58 -0
- package/dist/components/DateInput.d.ts +63 -0
- package/dist/components/DateInput.d.ts.map +1 -0
- package/dist/components/DateInput.js +86 -0
- package/dist/components/FileInput.d.ts +102 -0
- package/dist/components/FileInput.d.ts.map +1 -0
- package/dist/components/FileInput.js +164 -0
- package/dist/components/RichTextInput.d.ts +34 -0
- package/dist/components/RichTextInput.d.ts.map +1 -0
- package/dist/components/RichTextInput.js +57 -0
- package/dist/components/SelectInput.d.ts +54 -0
- package/dist/components/SelectInput.d.ts.map +1 -0
- package/dist/components/SelectInput.js +64 -0
- package/dist/components/SwitchInput.d.ts +46 -0
- package/dist/components/SwitchInput.d.ts.map +1 -0
- package/dist/components/SwitchInput.js +53 -0
- package/dist/components/TagsInput.d.ts +35 -0
- package/dist/components/TagsInput.d.ts.map +1 -0
- package/dist/components/TagsInput.js +67 -0
- package/dist/components/TextAreaInput.d.ts +71 -0
- package/dist/components/TextAreaInput.d.ts.map +1 -0
- package/dist/components/TextAreaInput.js +113 -0
- package/dist/components/TextInput.d.ts +68 -0
- package/dist/components/TextInput.d.ts.map +1 -0
- package/dist/components/TextInput.js +77 -0
- package/dist/components/index.d.ts +10 -0
- package/dist/components/index.d.ts.map +1 -0
- package/dist/index.cjs +18 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/package.json +87 -72
- package/.storybook/main.ts +0 -33
- package/.storybook/preview.ts +0 -36
- package/.storybook/vitest.setup.ts +0 -7
- package/biome.json +0 -37
- package/lib/addons/CharacterCountInput.tsx +0 -204
- package/lib/addons/index.ts +0 -2
- package/lib/components/CheckInput.tsx +0 -126
- package/lib/components/DateInput.tsx +0 -179
- package/lib/components/FileInput.tsx +0 -353
- package/lib/components/RichTextInput.tsx +0 -112
- package/lib/components/SelectInput.tsx +0 -144
- package/lib/components/SwitchInput.tsx +0 -116
- package/lib/components/TagsInput.tsx +0 -118
- package/lib/components/TextAreaInput.tsx +0 -211
- package/lib/components/TextInput.tsx +0 -381
- package/stories/CharacterCountInput.stories.tsx +0 -104
- package/stories/CheckInput.stories.tsx +0 -80
- package/stories/DateInput.stories.tsx +0 -137
- package/stories/FileInput.stories.tsx +0 -125
- package/stories/RichTextInput.stories.tsx +0 -77
- package/stories/SelectInput.stories.tsx +0 -131
- package/stories/SwitchInput.stories.tsx +0 -80
- package/stories/TagsInput.stories.tsx +0 -69
- package/stories/TextAreaInput.stories.tsx +0 -117
- package/stories/TextInput.stories.tsx +0 -167
- package/vitest.config.ts +0 -37
- package/vitest.shims.d.ts +0 -1
- /package/{lib/components/index.ts → dist/components/index.js} +0 -0
- /package/{lib/index.ts → dist/index.js} +0 -0
package/README.md
CHANGED
|
@@ -1,210 +1,250 @@
|
|
|
1
|
-
# Components Library
|
|
2
|
-
|
|
3
|
-
A comprehensive React component library built with TypeScript, React 19, react-hook-form, react-bootstrap, and Material-UI (MUI).
|
|
4
|
-
|
|
5
|
-
## Architecture
|
|
6
|
-
|
|
7
|
-
This library follows a clear separation between **Components** and **Addons**:
|
|
8
|
-
|
|
9
|
-
### Components (Base Input Types)
|
|
10
|
-
|
|
11
|
-
Components are specific input elements based on HTML input types and specialized use cases:
|
|
12
|
-
|
|
13
|
-
- **CheckInput** - Checkbox input for boolean values
|
|
14
|
-
- **DateInput** - Date picker with formatted string output (using MUI DatePicker)
|
|
15
|
-
- **FileInput** - File upload with validation and preview
|
|
16
|
-
- **SelectInput** - Dropdown selection for single/multiple options
|
|
17
|
-
- **SwitchInput** - Toggle switch for on/off values
|
|
18
|
-
- **TagsInput** - Tag management with MUI Autocomplete
|
|
19
|
-
- **TextAreaInput** - Multiline text input with auto-growing height
|
|
20
|
-
- **TextInput** - Single-line text input
|
|
21
|
-
|
|
22
|
-
### Addons (Enhancement Wrappers)
|
|
23
|
-
|
|
24
|
-
Addons wrap existing components to add functionality:
|
|
25
|
-
|
|
26
|
-
- **
|
|
27
|
-
- **
|
|
28
|
-
- **
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
- ✅ **
|
|
34
|
-
- ✅ **
|
|
35
|
-
- ✅ **
|
|
36
|
-
- ✅ **
|
|
37
|
-
- ✅
|
|
38
|
-
- ✅
|
|
39
|
-
- ✅
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
###
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
</
|
|
101
|
-
);
|
|
102
|
-
}
|
|
103
|
-
```
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
1
|
+
# Components Library
|
|
2
|
+
|
|
3
|
+
A comprehensive React component library built with TypeScript, React 19, react-hook-form, react-bootstrap, and Material-UI (MUI).
|
|
4
|
+
|
|
5
|
+
## Architecture
|
|
6
|
+
|
|
7
|
+
This library follows a clear separation between **Components** and **Addons**:
|
|
8
|
+
|
|
9
|
+
### Components (Base Input Types)
|
|
10
|
+
|
|
11
|
+
Components are specific input elements based on HTML input types and specialized use cases:
|
|
12
|
+
|
|
13
|
+
- **CheckInput** - Checkbox input for boolean values
|
|
14
|
+
- **DateInput** - Date picker with formatted string output (using MUI DatePicker)
|
|
15
|
+
- **FileInput** - File upload with validation and preview
|
|
16
|
+
- **SelectInput** - Dropdown selection for single/multiple options
|
|
17
|
+
- **SwitchInput** - Toggle switch for on/off values
|
|
18
|
+
- **TagsInput** - Tag management with MUI Autocomplete
|
|
19
|
+
- **TextAreaInput** - Multiline text input with auto-growing height
|
|
20
|
+
- **TextInput** - Single-line text input for various types (text, email, password, number, etc.)
|
|
21
|
+
|
|
22
|
+
### Addons (Enhancement Wrappers)
|
|
23
|
+
|
|
24
|
+
Addons wrap existing components to add functionality:
|
|
25
|
+
|
|
26
|
+
- **AutocompleteInput** - Adds autocomplete/suggestions dropdown to text inputs
|
|
27
|
+
- **CharacterCountInput** - Adds character counting to text, textarea, or editor inputs
|
|
28
|
+
- **EditorAddon** - Wraps TextAreaInput to add rich text editing (ReactQuill)
|
|
29
|
+
- **Editor** (Legacy) - Standalone rich text editor (deprecated, use EditorAddon instead)
|
|
30
|
+
|
|
31
|
+
## Features
|
|
32
|
+
|
|
33
|
+
- ✅ **React 19** compatible
|
|
34
|
+
- ✅ **TypeScript** with full type safety and auto-generated type declarations
|
|
35
|
+
- ✅ **ESM and CommonJS** dual module support
|
|
36
|
+
- ✅ **react-hook-form** integration for form state management and validation
|
|
37
|
+
- ✅ **react-bootstrap** for consistent styling
|
|
38
|
+
- ✅ **Material-UI (MUI)** for advanced components (DatePicker, Autocomplete)
|
|
39
|
+
- ✅ Standalone or form-integrated modes
|
|
40
|
+
- ✅ Consistent error handling
|
|
41
|
+
- ✅ Customizable styling
|
|
42
|
+
|
|
43
|
+
## Installation
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
npm install @capyx/components-library
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Peer Dependencies
|
|
50
|
+
|
|
51
|
+
This library requires the following peer dependencies to be installed in your project:
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
npm install react react-dom react-hook-form react-bootstrap @mui/material @mui/x-date-pickers dayjs react-quill-new react-autosuggest
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## TypeScript Support
|
|
58
|
+
|
|
59
|
+
This library is written in TypeScript and includes full type definitions. Type declarations are automatically provided when you install the package, giving you:
|
|
60
|
+
|
|
61
|
+
- Full IntelliSense and autocomplete in your IDE
|
|
62
|
+
- Type checking for component props
|
|
63
|
+
- Improved development experience with inline documentation
|
|
64
|
+
|
|
65
|
+
No additional `@types` packages are needed!
|
|
66
|
+
|
|
67
|
+
## Usage
|
|
68
|
+
|
|
69
|
+
### Basic Component Usage
|
|
70
|
+
|
|
71
|
+
```tsx
|
|
72
|
+
import { TextInput, CheckInput, DateInput } from '@your-package/components-library';
|
|
73
|
+
import { FormProvider, useForm } from 'react-hook-form';
|
|
74
|
+
|
|
75
|
+
function MyForm() {
|
|
76
|
+
const methods = useForm();
|
|
77
|
+
|
|
78
|
+
return (
|
|
79
|
+
<FormProvider {...methods}>
|
|
80
|
+
<form>
|
|
81
|
+
<TextInput
|
|
82
|
+
name='username'
|
|
83
|
+
label='Username'
|
|
84
|
+
required
|
|
85
|
+
maxLength={50}
|
|
86
|
+
/>
|
|
87
|
+
|
|
88
|
+
<CheckInput
|
|
89
|
+
name='terms'
|
|
90
|
+
label='I agree to terms'
|
|
91
|
+
required
|
|
92
|
+
/>
|
|
93
|
+
|
|
94
|
+
<DateInput
|
|
95
|
+
name='birthdate'
|
|
96
|
+
label='Date of Birth'
|
|
97
|
+
required
|
|
98
|
+
/>
|
|
99
|
+
</form>
|
|
100
|
+
</FormProvider>
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### Using Addons
|
|
106
|
+
|
|
107
|
+
#### Autocomplete Addon
|
|
108
|
+
|
|
109
|
+
```tsx
|
|
110
|
+
import { TextInput, AutocompleteInput } from '@your-package/components-library';
|
|
111
|
+
|
|
112
|
+
function MyForm() {
|
|
113
|
+
const countries = ['USA', 'Canada', 'UK', 'Australia'];
|
|
114
|
+
|
|
115
|
+
return (
|
|
116
|
+
<AutocompleteInput suggestions={countries}>
|
|
117
|
+
<TextInput
|
|
118
|
+
name='country'
|
|
119
|
+
label='Country'
|
|
120
|
+
placeholder='Type to search...'
|
|
121
|
+
/>
|
|
122
|
+
</AutocompleteInput>
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
#### Character Count Addon
|
|
128
|
+
|
|
129
|
+
```tsx
|
|
130
|
+
import { TextInput, CharacterCountInput } from '@your-package/components-library';
|
|
131
|
+
|
|
132
|
+
function MyForm() {
|
|
133
|
+
return (
|
|
134
|
+
<CharacterCountInput>
|
|
135
|
+
<TextInput
|
|
136
|
+
name='bio'
|
|
137
|
+
label='Biography'
|
|
138
|
+
maxLength={200}
|
|
139
|
+
/>
|
|
140
|
+
</CharacterCountInput>
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
#### Editor Addon (Rich Text)
|
|
146
|
+
|
|
147
|
+
```tsx
|
|
148
|
+
import { EditorAddon } from '@your-package/components-library';
|
|
149
|
+
import { Controller, useFormContext } from 'react-hook-form';
|
|
150
|
+
|
|
151
|
+
function MyForm() {
|
|
152
|
+
const { control } = useFormContext();
|
|
153
|
+
|
|
154
|
+
return (
|
|
155
|
+
<Controller
|
|
156
|
+
name='content'
|
|
157
|
+
control={control}
|
|
158
|
+
render={({ field }) => (
|
|
159
|
+
<EditorAddon
|
|
160
|
+
value={field.value}
|
|
161
|
+
onChange={field.onChange}
|
|
162
|
+
maxLength={5000}
|
|
163
|
+
/>
|
|
164
|
+
)}
|
|
165
|
+
/>
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
#### Combining Addons
|
|
171
|
+
|
|
172
|
+
Addons can be nested to combine their functionality:
|
|
173
|
+
|
|
174
|
+
```tsx
|
|
175
|
+
import { TextInput, AutocompleteInput, CharacterCountInput } from '@your-package/components-library';
|
|
176
|
+
|
|
177
|
+
function MyForm() {
|
|
178
|
+
const cities = ['New York', 'Los Angeles', 'Chicago', 'Houston'];
|
|
179
|
+
|
|
180
|
+
return (
|
|
181
|
+
<CharacterCountInput>
|
|
182
|
+
<AutocompleteInput suggestions={cities}>
|
|
183
|
+
<TextInput
|
|
184
|
+
name='city'
|
|
185
|
+
label='City'
|
|
186
|
+
maxLength={50}
|
|
187
|
+
/>
|
|
188
|
+
</AutocompleteInput>
|
|
189
|
+
</CharacterCountInput>
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
## Component API
|
|
195
|
+
|
|
196
|
+
### Common Props
|
|
197
|
+
|
|
198
|
+
All components support these common props when used with react-hook-form:
|
|
199
|
+
|
|
200
|
+
- `name: string` (required) - Field name for form registration
|
|
201
|
+
- `label?: string` - Label text
|
|
202
|
+
- `required?: boolean` - Whether the field is required
|
|
203
|
+
- `disabled?: boolean` - Whether the field is disabled
|
|
204
|
+
|
|
205
|
+
### Standalone Mode
|
|
206
|
+
|
|
207
|
+
All components can be used without react-hook-form by providing `value` and `onChange` props:
|
|
208
|
+
|
|
209
|
+
```tsx
|
|
210
|
+
<TextInput
|
|
211
|
+
name='standalone'
|
|
212
|
+
value={value}
|
|
213
|
+
onChange={(newValue) => setValue(newValue)}
|
|
214
|
+
/>
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
## Styling
|
|
218
|
+
|
|
219
|
+
Components use react-bootstrap and MUI components, making them easy to customize:
|
|
220
|
+
|
|
221
|
+
- Override Bootstrap variables for global theming
|
|
222
|
+
- Use MUI theme provider for MUI components
|
|
223
|
+
- Add custom className props (where supported)
|
|
224
|
+
|
|
225
|
+
## TypeScript Support
|
|
226
|
+
|
|
227
|
+
All components are fully typed with TypeScript. Import types as needed:
|
|
228
|
+
|
|
229
|
+
```tsx
|
|
230
|
+
import type { TextAreaInputProps, CheckInputProps } from '@your-package/components-library';
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
## React 19 Compatibility
|
|
234
|
+
|
|
235
|
+
This library is built and tested with React 19. All components use modern React patterns:
|
|
236
|
+
|
|
237
|
+
- Functional components with hooks
|
|
238
|
+
- No deprecated lifecycle methods
|
|
239
|
+
- No unsafe React APIs
|
|
240
|
+
- Full concurrent mode support
|
|
241
|
+
|
|
242
|
+
## Contributing
|
|
243
|
+
|
|
244
|
+
When adding new components:
|
|
245
|
+
|
|
246
|
+
1. **Components** go in `lib/components/` - base input types
|
|
247
|
+
2. **Addons** go in `lib/addons/` - wrappers that enhance components
|
|
248
|
+
3. All components must support both react-hook-form and standalone modes
|
|
249
|
+
4. Export types alongside components
|
|
250
|
+
5. Update index files for proper exports
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { FC, PropsWithChildren } from 'react';
|
|
2
|
+
/**
|
|
3
|
+
* Props for the AutocompleteInput component
|
|
4
|
+
*/
|
|
5
|
+
export type AutocompleteInputProps = PropsWithChildren<{
|
|
6
|
+
/** Array of suggestion values for autocomplete */
|
|
7
|
+
suggestions: string[];
|
|
8
|
+
/** Whether to highlight the first suggestion (default: true) */
|
|
9
|
+
highlightFirstSuggestion?: boolean;
|
|
10
|
+
}>;
|
|
11
|
+
/**
|
|
12
|
+
* AutocompleteInput - A wrapper addon that adds autocomplete/suggestions to text inputs
|
|
13
|
+
*
|
|
14
|
+
* Wraps any text input component (like TextInput) and enhances it with dropdown
|
|
15
|
+
* suggestions that filter based on user input. Works seamlessly with both standalone
|
|
16
|
+
* and react-hook-form integrated inputs.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```tsx
|
|
20
|
+
* // With react-hook-form
|
|
21
|
+
* <AutocompleteInput suggestions={["USA", "Canada", "UK"]}>
|
|
22
|
+
* <TextInput
|
|
23
|
+
* name="country"
|
|
24
|
+
* label="Country"
|
|
25
|
+
* placeholder="Type to search..."
|
|
26
|
+
* />
|
|
27
|
+
* </AutocompleteInput>
|
|
28
|
+
*
|
|
29
|
+
* // Standalone mode
|
|
30
|
+
* <AutocompleteInput suggestions={skills}>
|
|
31
|
+
* <TextInput
|
|
32
|
+
* name="skill"
|
|
33
|
+
* value={value}
|
|
34
|
+
* onChange={setValue}
|
|
35
|
+
* />
|
|
36
|
+
* </AutocompleteInput>
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export declare const AutocompleteInput: FC<AutocompleteInputProps>;
|
|
40
|
+
//# sourceMappingURL=AutocompleteInput.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AutocompleteInput.d.ts","sourceRoot":"","sources":["../../lib/addons/AutocompleteInput.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EAAE,EAAE,iBAAiB,EAAgC,MAAM,OAAO,CAAC;AAKjF;;GAEG;AACH,MAAM,MAAM,sBAAsB,GAAG,iBAAiB,CAAC;IACtD,kDAAkD;IAClD,WAAW,EAAE,MAAM,EAAE,CAAC;IACtB,gEAAgE;IAChE,wBAAwB,CAAC,EAAE,OAAO,CAAC;CACnC,CAAC,CAAC;AAEH;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,eAAO,MAAM,iBAAiB,EAAE,EAAE,CAAC,sBAAsB,CAgHxD,CAAC"}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { cloneElement, useEffect, useRef, useState } from 'react';
|
|
3
|
+
import Autosuggest from 'react-autosuggest';
|
|
4
|
+
import { useFormContext } from 'react-hook-form';
|
|
5
|
+
/**
|
|
6
|
+
* AutocompleteInput - A wrapper addon that adds autocomplete/suggestions to text inputs
|
|
7
|
+
*
|
|
8
|
+
* Wraps any text input component (like TextInput) and enhances it with dropdown
|
|
9
|
+
* suggestions that filter based on user input. Works seamlessly with both standalone
|
|
10
|
+
* and react-hook-form integrated inputs.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```tsx
|
|
14
|
+
* // With react-hook-form
|
|
15
|
+
* <AutocompleteInput suggestions={["USA", "Canada", "UK"]}>
|
|
16
|
+
* <TextInput
|
|
17
|
+
* name="country"
|
|
18
|
+
* label="Country"
|
|
19
|
+
* placeholder="Type to search..."
|
|
20
|
+
* />
|
|
21
|
+
* </AutocompleteInput>
|
|
22
|
+
*
|
|
23
|
+
* // Standalone mode
|
|
24
|
+
* <AutocompleteInput suggestions={skills}>
|
|
25
|
+
* <TextInput
|
|
26
|
+
* name="skill"
|
|
27
|
+
* value={value}
|
|
28
|
+
* onChange={setValue}
|
|
29
|
+
* />
|
|
30
|
+
* </AutocompleteInput>
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export const AutocompleteInput = ({ suggestions: allSuggestions, children, highlightFirstSuggestion = true, }) => {
|
|
34
|
+
const formContext = useFormContext();
|
|
35
|
+
const [filteredSuggestions, setFilteredSuggestions] = useState([]);
|
|
36
|
+
const [inputValue, setInputValue] = useState('');
|
|
37
|
+
const nameRef = useRef('');
|
|
38
|
+
// Extract child props
|
|
39
|
+
const childElement = children;
|
|
40
|
+
const childProps = childElement?.props;
|
|
41
|
+
const name = childProps?.name || '';
|
|
42
|
+
nameRef.current = name;
|
|
43
|
+
// Sync input value with form context or child value
|
|
44
|
+
useEffect(() => {
|
|
45
|
+
if (formContext && name) {
|
|
46
|
+
const formValue = formContext.getValues(name);
|
|
47
|
+
setInputValue(formValue || '');
|
|
48
|
+
}
|
|
49
|
+
else if (childProps?.value !== undefined) {
|
|
50
|
+
setInputValue(String(childProps.value));
|
|
51
|
+
}
|
|
52
|
+
}, [formContext, name, childProps?.value]);
|
|
53
|
+
// Filter suggestions based on input value
|
|
54
|
+
const getSuggestions = (value) => {
|
|
55
|
+
const trimmedValue = value.trim().toLowerCase();
|
|
56
|
+
if (trimmedValue.length === 0)
|
|
57
|
+
return [];
|
|
58
|
+
return allSuggestions.filter((suggestion) => suggestion.toLowerCase().includes(trimmedValue));
|
|
59
|
+
};
|
|
60
|
+
const onSuggestionsFetchRequested = ({ value }) => {
|
|
61
|
+
setFilteredSuggestions(getSuggestions(value));
|
|
62
|
+
};
|
|
63
|
+
const onSuggestionsClearRequested = () => {
|
|
64
|
+
setFilteredSuggestions([]);
|
|
65
|
+
};
|
|
66
|
+
const getSuggestionValue = (suggestion) => suggestion;
|
|
67
|
+
const renderSuggestion = (suggestion) => _jsx("span", { children: suggestion });
|
|
68
|
+
const handleChange = (_event, { newValue }) => {
|
|
69
|
+
setInputValue(newValue);
|
|
70
|
+
// Update form context if available
|
|
71
|
+
if (formContext && name) {
|
|
72
|
+
formContext.setValue(name, newValue);
|
|
73
|
+
}
|
|
74
|
+
// Call child's onChange if provided
|
|
75
|
+
childProps?.onChange?.(newValue);
|
|
76
|
+
};
|
|
77
|
+
return (_jsx(Autosuggest, { suggestions: filteredSuggestions, onSuggestionsFetchRequested: onSuggestionsFetchRequested, onSuggestionsClearRequested: onSuggestionsClearRequested, getSuggestionValue: getSuggestionValue, renderSuggestion: renderSuggestion, highlightFirstSuggestion: highlightFirstSuggestion, inputProps: {
|
|
78
|
+
value: inputValue,
|
|
79
|
+
onChange: handleChange,
|
|
80
|
+
}, renderInputComponent: (inputProps) => {
|
|
81
|
+
// Clone the child element and merge with autosuggest props
|
|
82
|
+
const { value, onChange: autosuggestOnChange, ...autosuggestProps } = inputProps;
|
|
83
|
+
return cloneElement(childElement, {
|
|
84
|
+
...autosuggestProps,
|
|
85
|
+
value,
|
|
86
|
+
onChange: (newValue) => {
|
|
87
|
+
// Handle both event objects and direct values
|
|
88
|
+
const actualValue = typeof newValue === 'string'
|
|
89
|
+
? newValue
|
|
90
|
+
: newValue?.target
|
|
91
|
+
?.value || '';
|
|
92
|
+
// Create synthetic event for Autosuggest
|
|
93
|
+
const syntheticEvent = {};
|
|
94
|
+
autosuggestOnChange?.(syntheticEvent, {
|
|
95
|
+
newValue: actualValue,
|
|
96
|
+
method: 'type',
|
|
97
|
+
});
|
|
98
|
+
},
|
|
99
|
+
});
|
|
100
|
+
} }));
|
|
101
|
+
};
|