@delightui/components 0.1.122 → 0.1.123
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/dist/cjs/components/molecules/ChipInput/ChipInput.d.ts +3 -4
- package/dist/cjs/components/molecules/ChipInput/ChipInput.presenter.d.ts +10 -24
- package/dist/cjs/components/molecules/ChipInput/ChipInput.types.d.ts +122 -10
- package/dist/cjs/components/molecules/ChipInput/ChipInput.utils.d.ts +63 -2
- package/dist/cjs/library.css +3 -0
- package/dist/cjs/library.js +3 -3
- package/dist/cjs/library.js.map +1 -1
- package/dist/esm/components/molecules/ChipInput/ChipInput.d.ts +3 -4
- package/dist/esm/components/molecules/ChipInput/ChipInput.presenter.d.ts +10 -24
- package/dist/esm/components/molecules/ChipInput/ChipInput.types.d.ts +122 -10
- package/dist/esm/components/molecules/ChipInput/ChipInput.utils.d.ts +63 -2
- package/dist/esm/library.css +3 -0
- package/dist/esm/library.js +1 -1
- package/dist/esm/library.js.map +1 -1
- package/dist/index.d.ts +125 -11
- package/docs/components/molecules/ChipInput.md +200 -9
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
2
|
import * as React$1 from 'react';
|
|
3
|
-
import React__default, { ImgHTMLAttributes, SyntheticEvent, HTMLAttributes, ReactNode, MouseEvent, FormHTMLAttributes, Ref, InputHTMLAttributes, TextareaHTMLAttributes, ComponentType, LiHTMLAttributes, CSSProperties,
|
|
3
|
+
import React__default, { ImgHTMLAttributes, SyntheticEvent, HTMLAttributes, ReactNode, MouseEvent, FormHTMLAttributes, Ref, InputHTMLAttributes, TextareaHTMLAttributes, ComponentType, LiHTMLAttributes, CSSProperties, TableHTMLAttributes, TdHTMLAttributes, ReactElement, AriaRole, KeyboardEventHandler } from 'react';
|
|
4
4
|
import { LinkProps } from 'react-router-dom';
|
|
5
5
|
import { Plugin } from 'flatpickr/dist/types/options';
|
|
6
6
|
import FlatPickr from 'react-flatpickr';
|
|
@@ -2007,22 +2007,136 @@ declare const useSelectContext: () => SelectContextType;
|
|
|
2007
2007
|
*/
|
|
2008
2008
|
declare const SelectProvider: ({ children, value, multiple, disabled, invalid, onValueChange, closeSelectOptions, }: SelectProviderProps) => react_jsx_runtime.JSX.Element;
|
|
2009
2009
|
|
|
2010
|
-
|
|
2011
|
-
|
|
2012
|
-
|
|
2013
|
-
|
|
2014
|
-
|
|
2010
|
+
/**
|
|
2011
|
+
* Represents an option that can be selected in the ChipInput component.
|
|
2012
|
+
* This is the normalized format used internally to handle both string arrays and object arrays.
|
|
2013
|
+
*/
|
|
2014
|
+
type OptionLike = {
|
|
2015
|
+
/** Unique identifier for the option. Can be string or number. */
|
|
2016
|
+
key: string | number;
|
|
2017
|
+
/** Display text shown to the user for this option. */
|
|
2018
|
+
label: string;
|
|
2015
2019
|
};
|
|
2016
|
-
|
|
2020
|
+
/**
|
|
2021
|
+
* Type alias for the key property of OptionLike.
|
|
2022
|
+
* Used for type safety when working with selected values.
|
|
2023
|
+
*/
|
|
2024
|
+
type OptionLikeKey = OptionLike['key'];
|
|
2025
|
+
/**
|
|
2026
|
+
* Props for the ChipInput component.
|
|
2027
|
+
*
|
|
2028
|
+
* @template T - The type of the value array. Can be string[] or OptionLike[].
|
|
2029
|
+
* This generic ensures type consistency between input and output.
|
|
2030
|
+
*
|
|
2031
|
+
* @example
|
|
2032
|
+
* ```tsx
|
|
2033
|
+
* // String array usage
|
|
2034
|
+
* const [fruits, setFruits] = useState<string[]>(['apple']);
|
|
2035
|
+
* <ChipInput
|
|
2036
|
+
* value={fruits}
|
|
2037
|
+
* onValueChange={setFruits}
|
|
2038
|
+
* options={['apple', 'banana', 'cherry']}
|
|
2039
|
+
* />
|
|
2040
|
+
*
|
|
2041
|
+
* // OptionLike array usage
|
|
2042
|
+
* const [items, setItems] = useState<OptionLike[]>([{key: 1, label: 'Apple'}]);
|
|
2043
|
+
* <ChipInput
|
|
2044
|
+
* value={items}
|
|
2045
|
+
* onValueChange={setItems}
|
|
2046
|
+
* options={[{key: 1, label: 'Apple'}, {key: 2, label: 'Banana'}]}
|
|
2047
|
+
* />
|
|
2048
|
+
* ```
|
|
2049
|
+
*/
|
|
2050
|
+
type ChipInputProps<T = string[] | OptionLike[]> = Omit<ControlledFormComponentProps<OptionLikeKey[]>, 'value' | 'onValueChange'> & {
|
|
2051
|
+
/**
|
|
2052
|
+
* Current selected values. Type matches the generic T parameter.
|
|
2053
|
+
* For string arrays: ['apple', 'banana']
|
|
2054
|
+
* For OptionLike arrays: [{key: 1, label: 'Apple'}, {key: 2, label: 'Banana'}]
|
|
2055
|
+
*/
|
|
2056
|
+
value?: T;
|
|
2057
|
+
/**
|
|
2058
|
+
* Callback fired when the selected values change.
|
|
2059
|
+
* Returns the same type as the input value for type consistency.
|
|
2060
|
+
*
|
|
2061
|
+
* @param value - The new array of selected values in the same format as input
|
|
2062
|
+
*/
|
|
2063
|
+
onValueChange?: (value: T) => void;
|
|
2064
|
+
/**
|
|
2065
|
+
* Current value of the text input field.
|
|
2066
|
+
* Used for controlled input behavior and filtering.
|
|
2067
|
+
*/
|
|
2017
2068
|
inputValue?: string;
|
|
2069
|
+
/**
|
|
2070
|
+
* Callback fired when the input field value changes.
|
|
2071
|
+
*
|
|
2072
|
+
* @param value - The new input field value
|
|
2073
|
+
*/
|
|
2018
2074
|
onInputChange?: (value: string) => void;
|
|
2019
|
-
|
|
2075
|
+
/**
|
|
2076
|
+
* Available options for selection. Can be either:
|
|
2077
|
+
* - string[]: Simple array of strings like ['apple', 'banana']
|
|
2078
|
+
* - OptionLike[]: Array of objects like [{key: 1, label: 'Apple'}]
|
|
2079
|
+
*
|
|
2080
|
+
* Options are automatically filtered as the user types in the input field.
|
|
2081
|
+
*/
|
|
2082
|
+
options?: string[] | OptionLike[];
|
|
2083
|
+
/**
|
|
2084
|
+
* Placeholder text shown in the input field when it's empty.
|
|
2085
|
+
*
|
|
2086
|
+
* @default undefined
|
|
2087
|
+
*/
|
|
2020
2088
|
placeholder?: string;
|
|
2089
|
+
/**
|
|
2090
|
+
* Additional CSS class name applied to the root element.
|
|
2091
|
+
*/
|
|
2021
2092
|
className?: string;
|
|
2022
|
-
|
|
2093
|
+
/**
|
|
2094
|
+
* Optional callback for creating new items when they don't exist in options.
|
|
2095
|
+
* Can be synchronous or asynchronous for API integration.
|
|
2096
|
+
*
|
|
2097
|
+
* When this prop is provided:
|
|
2098
|
+
* - Optimistic UI updates are applied immediately
|
|
2099
|
+
* - If async, the UI shows loading states and handles errors
|
|
2100
|
+
* - Failed operations are automatically reverted
|
|
2101
|
+
*
|
|
2102
|
+
* @param newItemValue - The text value entered by the user
|
|
2103
|
+
* @returns Promise<OptionLike> for async operations, OptionLike for sync operations
|
|
2104
|
+
*
|
|
2105
|
+
* @example
|
|
2106
|
+
* ```tsx
|
|
2107
|
+
* // Sync operation
|
|
2108
|
+
* onAddItem={(value) => ({ key: value, label: value })}
|
|
2109
|
+
*
|
|
2110
|
+
* // Async operation
|
|
2111
|
+
* onAddItem={async (value) => {
|
|
2112
|
+
* const newItem = await api.createItem(value);
|
|
2113
|
+
* return { key: newItem.id, label: newItem.name };
|
|
2114
|
+
* }}
|
|
2115
|
+
* ```
|
|
2116
|
+
*/
|
|
2117
|
+
onAddItem?: (newItemValue: string) => Promise<OptionLike> | OptionLike;
|
|
2118
|
+
/**
|
|
2119
|
+
* Optional callback for deleting items.
|
|
2120
|
+
* Typically used for API integration to sync deletions with backend.
|
|
2121
|
+
*
|
|
2122
|
+
* When provided, this callback is called when a chip is removed.
|
|
2123
|
+
* The component handles UI updates automatically.
|
|
2124
|
+
*
|
|
2125
|
+
* @param keyToDelete - The key of the item being deleted
|
|
2126
|
+
* @returns Promise<void> for the deletion operation
|
|
2127
|
+
*
|
|
2128
|
+
* @example
|
|
2129
|
+
* ```tsx
|
|
2130
|
+
* onDeleteItem={async (key) => {
|
|
2131
|
+
* await api.deleteItem(key);
|
|
2132
|
+
* // Component automatically updates UI
|
|
2133
|
+
* }}
|
|
2134
|
+
* ```
|
|
2135
|
+
*/
|
|
2136
|
+
onDeleteItem?: (keyToDelete: OptionLikeKey) => Promise<void>;
|
|
2023
2137
|
};
|
|
2024
2138
|
|
|
2025
|
-
declare const
|
|
2139
|
+
declare const ChipInput: <T extends string[] | OptionLike[]>(props: ChipInputProps<T>) => react_jsx_runtime.JSX.Element;
|
|
2026
2140
|
|
|
2027
2141
|
/**
|
|
2028
2142
|
* Enum for different search modes.
|
|
@@ -2548,4 +2662,4 @@ declare const NotificationContext: React__default.Context<NotificationContextVal
|
|
|
2548
2662
|
declare const NotificationProvider: React__default.FC<NotificationProviderProps>;
|
|
2549
2663
|
declare const useNotification: () => NotificationContextValue;
|
|
2550
2664
|
|
|
2551
|
-
export { type AccessibilityActions, type AccessibilityProps, Accordion, AccordionDetails, AccordionGroup, AccordionSummary, ActionCard, type ActionCardProps, ActionImage, type ActionImageProps, Breadcrumb, type BreadcrumbProps, Breadcrumbs, type BreadcrumbsProps, Breakpoint, type BreakpointProps, Button, ButtonGroup, type ButtonGroupProps, type ButtonProps, Card, type CardProps, Checkbox, CheckboxItem, type CheckboxItemProps, type CheckboxLabelAlignmentEnum, type CheckboxProps, type CheckboxSizeEnum, type CheckboxTypeEnum, Chip,
|
|
2665
|
+
export { type AccessibilityActions, type AccessibilityProps, Accordion, AccordionDetails, AccordionGroup, AccordionSummary, ActionCard, type ActionCardProps, ActionImage, type ActionImageProps, Breadcrumb, type BreadcrumbProps, Breadcrumbs, type BreadcrumbsProps, Breakpoint, type BreakpointProps, Button, ButtonGroup, type ButtonGroupProps, type ButtonProps, Card, type CardProps, Checkbox, CheckboxItem, type CheckboxItemProps, type CheckboxLabelAlignmentEnum, type CheckboxProps, type CheckboxSizeEnum, type CheckboxTypeEnum, Chip, ChipInput, type ChipInputProps, type ChipProps, ConditionalView, type ConditionalViewProps, ContextMenu, type ContextMenuProps, type CustomTimePickerConfig, CustomToggle, type CustomToggleProps, DatePicker, type DatePickerProps, SortableItem as DraggableItem, SortableTrigger as DraggableItemTrigger, Dropzone, DropzoneClear, DropzoneContent, type DropzoneContentProps, type DropzoneContentTypeEnum, DropzoneFilename, DropzoneFilename as DropzonePreview, type DropzoneProps, DropzoneSupportedFormats as DropzoneReject, DropzoneTrigger as DropzoneRoot, DropzoneSupportedFormats, DropzoneTrigger, type FieldValidationFunction, type FieldValidators, type FieldValue, Form, type FormContextValues, type FormErrors, FormField, type FormFieldProps, type FormProps, type FormProviderProps, type FormState, type FormStateChangeHandler, type FormSubmitHandler, type FormValidator, Grid, GridItem, type GridItemProps, GridList, type GridListProps, type GridProps, Icon, IconButton, type IconButtonProps, type IconButtonStyleEnum, type IconProps, type IconSizeEnum, type IconStyleEnum, Image, type ImageFitEnum, type ImageProps, Input, type InputProps, type InputTypeEnum, List, ListItem, type ListItemProps$1 as ListItemProps, type ListProps, Modal, type ModalComponentProps, ModalFooter, type ModalFooterProps, ModalHeader, type ModalHeaderProps, type ModalProps, ModalProvider, type ModalProviderProps, Nav, NavItem, type NavItemProps, NavLink, type NavLinkProps, type NavProps, type NotificationContainerProps, NotificationContext, NotificationProvider, Option, type OptionLike, type OptionLikeKey, type OptionProps, type OverlayDirectionEnum, Pagination, PaginationNumberField, type PaginationNumberFieldProps, type PaginationProps, Password, Popover, type PopoverHandle, type PopoverProps, ProgressBar, type ProgressBarProps, RadioButton, RadioButtonItem, type RadioButtonItemProps, type RadioButtonLabelAlignmentEnum, type RadioButtonProps, type RadioButtonSizeEnum, RadioGroup, type RadioGroupProps, RenderStateView, type RenderStateViewProps, RepeaterList, type RepeaterListProps, type RequiredFields, ResponsiveComponent, type ResponsiveComponentProps, Search, type SearchCallback, type SearchStyleEnum as SearchModeEnum, type SearchProps, Select, SelectListItem, type SelectListItemProps, type SelectProps, SelectProvider, SlideOutPanel, type SlideOutPanelDirectionEnum, type SlideOutPanelProps, type SlideOutPanelSizeEnum, Slider, type SliderProps, Spinner, type SpinnerProps, TabContent, type TabContentProps, TabItem, type TabItemProps, Table, TableBody, type TableBodyProps, TableCell, type TableCellProps, TableHeader, TableHeaderCell, type TableHeaderCellProps, type TableHeaderProps, type TableProps, TableRow, type TableRowProps, Tabs, type TabsProps, Text, TextArea, type TextAreaProps, type TextDecorationEnum, type TextProps, type TextTypeEnum, type TextWeightEnum, ThemeContext, type ThemeContextType, ThemeProvider, type ThemeProviderProps, Toggle, ToggleButton, type ToggleButtonProps, type ToggleLabelAlignmentEnum, type ToggleProps, Tooltip, type TooltipProps, type UseModalReturn, WrapTextNodes, type WrapTextNodesProps, applyPropsToChildren, getClickAccessibilityProps, mergeRefs, useDebounce, useDropzoneContext, useInflateView, useModal, useNotification, useSelectContext, useTab };
|
|
@@ -2,7 +2,17 @@
|
|
|
2
2
|
|
|
3
3
|
## Description
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
A powerful input field component that allows users to enter multiple values as chips or tags. Features automatic input filtering, backspace deletion, async operations with optimistic UI updates, and support for both simple strings and complex objects. Perfect for managing tags, categories, email addresses, team assignments, or any collection of discrete values.
|
|
6
|
+
|
|
7
|
+
## Key Features
|
|
8
|
+
|
|
9
|
+
- **📝 Input Filtering**: Real-time filtering of dropdown options as you type
|
|
10
|
+
- **⌫ Backspace Deletion**: Press backspace when input is empty to delete last chip
|
|
11
|
+
- **⚡ Async Operations**: Support for async add/delete operations with optimistic UI updates
|
|
12
|
+
- **🔄 Error Handling**: Automatic reversion of failed operations
|
|
13
|
+
- **📊 Type Flexibility**: Works with both `string[]` and `OptionLike[]` arrays
|
|
14
|
+
- **🎯 Form Integration**: Full compatibility with form validation and controlled components
|
|
15
|
+
- **♿ Accessibility**: Keyboard navigation and screen reader support
|
|
6
16
|
|
|
7
17
|
## Aliases
|
|
8
18
|
|
|
@@ -14,33 +24,214 @@ An input field component that allows users to enter multiple values as chips or
|
|
|
14
24
|
|
|
15
25
|
## Props Breakdown
|
|
16
26
|
|
|
17
|
-
**
|
|
27
|
+
**Generic Type:** `ChipInputProps<T = string[] | OptionLike[]>`
|
|
18
28
|
|
|
19
29
|
| Prop | Type | Default | Required | Description |
|
|
20
30
|
|------|------|---------|----------|-------------|
|
|
21
|
-
| `value` | `string[]
|
|
22
|
-
| `onValueChange` | `(value:
|
|
23
|
-
| `inputValue` | `string` | - | No | Current input field value |
|
|
31
|
+
| `value` | `T` (string[] or OptionLike[]) | - | No | Current array of selected values |
|
|
32
|
+
| `onValueChange` | `(value: T) => void` | - | No | Callback fired when selected values change |
|
|
33
|
+
| `inputValue` | `string` | - | No | Current input field value for controlled input |
|
|
24
34
|
| `onInputChange` | `(value: string) => void` | - | No | Callback fired when input value changes |
|
|
25
|
-
| `options` | `string[]` | - | No |
|
|
35
|
+
| `options` | `string[]` or `OptionLike[]` | - | No | Available options (automatically filtered by input) |
|
|
26
36
|
| `placeholder` | `string` | - | No | Placeholder text for the input field |
|
|
27
37
|
| `className` | `string` | - | No | Additional CSS class names |
|
|
28
|
-
| `
|
|
38
|
+
| `onAddItem` | `(value: string) => Promise<OptionLike>` or `OptionLike` | - | No | Async/sync callback for creating new items |
|
|
39
|
+
| `onDeleteItem` | `(key: OptionLikeKey) => Promise<void>` | - | No | Async callback for deleting items |
|
|
40
|
+
| `onAddNewOption` | `(value: string) => OptionLike` or `void` | - | No | **Deprecated:** Use `onAddItem` instead |
|
|
29
41
|
|
|
30
42
|
## Examples
|
|
31
43
|
|
|
32
|
-
### Basic Usage
|
|
44
|
+
### Basic Usage with Filtering
|
|
33
45
|
```tsx
|
|
34
46
|
import { ChipInput } from '@delightui/components';
|
|
35
47
|
|
|
36
48
|
function BasicExample() {
|
|
37
49
|
const [tags, setTags] = useState(['react', 'javascript']);
|
|
50
|
+
const availableTags = ['react', 'javascript', 'typescript', 'vue', 'angular', 'svelte'];
|
|
51
|
+
|
|
52
|
+
return (
|
|
53
|
+
<ChipInput
|
|
54
|
+
value={tags}
|
|
55
|
+
onValueChange={setTags}
|
|
56
|
+
options={availableTags}
|
|
57
|
+
placeholder="Type to filter tags..."
|
|
58
|
+
/>
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### OptionLike Objects
|
|
64
|
+
```tsx
|
|
65
|
+
function OptionLikeExample() {
|
|
66
|
+
const [selectedFruits, setSelectedFruits] = useState([]);
|
|
67
|
+
|
|
68
|
+
const fruitOptions = [
|
|
69
|
+
{ key: 1, label: 'Apple 🍎' },
|
|
70
|
+
{ key: 2, label: 'Banana 🍌' },
|
|
71
|
+
{ key: 3, label: 'Cherry 🍒' },
|
|
72
|
+
{ key: 4, label: 'Date 🌴' },
|
|
73
|
+
];
|
|
74
|
+
|
|
75
|
+
return (
|
|
76
|
+
<ChipInput
|
|
77
|
+
value={selectedFruits}
|
|
78
|
+
onValueChange={setSelectedFruits}
|
|
79
|
+
options={fruitOptions}
|
|
80
|
+
placeholder="Select fruits..."
|
|
81
|
+
/>
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### Async Operations with Optimistic UI
|
|
87
|
+
```tsx
|
|
88
|
+
function AsyncExample() {
|
|
89
|
+
const [items, setItems] = useState([]);
|
|
90
|
+
const [options, setOptions] = useState([
|
|
91
|
+
{ key: 1, label: 'Existing Item 1' },
|
|
92
|
+
{ key: 2, label: 'Existing Item 2' },
|
|
93
|
+
]);
|
|
94
|
+
|
|
95
|
+
const handleAddItem = async (value) => {
|
|
96
|
+
// Simulate API call
|
|
97
|
+
await new Promise(resolve => setTimeout(resolve, 1000));
|
|
98
|
+
|
|
99
|
+
// Simulate random error for demo
|
|
100
|
+
if (value === 'error') {
|
|
101
|
+
throw new Error('Simulated error');
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const newItem = {
|
|
105
|
+
key: Date.now(),
|
|
106
|
+
label: `${value} ✨`
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
setOptions(prev => [...prev, newItem]);
|
|
110
|
+
return newItem;
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
const handleDeleteItem = async (key) => {
|
|
114
|
+
// Simulate API call
|
|
115
|
+
await new Promise(resolve => setTimeout(resolve, 500));
|
|
116
|
+
setOptions(prev => prev.filter(opt => opt.key !== key));
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
return (
|
|
120
|
+
<ChipInput
|
|
121
|
+
value={items}
|
|
122
|
+
onValueChange={setItems}
|
|
123
|
+
options={options}
|
|
124
|
+
onAddItem={handleAddItem}
|
|
125
|
+
onDeleteItem={handleDeleteItem}
|
|
126
|
+
placeholder='Try typing "error" to see error handling'
|
|
127
|
+
/>
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
### Keyboard Navigation Demo
|
|
133
|
+
```tsx
|
|
134
|
+
function KeyboardNavigationExample() {
|
|
135
|
+
const [tags, setTags] = useState(['frontend', 'javascript']);
|
|
136
|
+
const options = [
|
|
137
|
+
'frontend', 'backend', 'javascript', 'typescript', 'react',
|
|
138
|
+
'vue', 'angular', 'node', 'python', 'java'
|
|
139
|
+
];
|
|
140
|
+
|
|
141
|
+
return (
|
|
142
|
+
<div>
|
|
143
|
+
<ChipInput
|
|
144
|
+
value={tags}
|
|
145
|
+
onValueChange={setTags}
|
|
146
|
+
options={options}
|
|
147
|
+
placeholder="Try: type to filter, backspace to delete, enter to add"
|
|
148
|
+
/>
|
|
149
|
+
<div style={{ marginTop: '8px', fontSize: '12px', color: '#666' }}>
|
|
150
|
+
<strong>Keyboard shortcuts:</strong>
|
|
151
|
+
<ul style={{ margin: 0, paddingLeft: '16px' }}>
|
|
152
|
+
<li>Type to filter available options</li>
|
|
153
|
+
<li>Press <kbd>Backspace</kbd> when input is empty to delete last chip</li>
|
|
154
|
+
<li>Press <kbd>Enter</kbd> to add typed text as new chip</li>
|
|
155
|
+
</ul>
|
|
156
|
+
</div>
|
|
157
|
+
</div>
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
### API Integration Example
|
|
163
|
+
```tsx
|
|
164
|
+
function APIIntegrationExample() {
|
|
165
|
+
const [selectedUsers, setSelectedUsers] = useState([]);
|
|
166
|
+
const [availableUsers, setAvailableUsers] = useState([
|
|
167
|
+
{ key: 1, label: 'John Doe' },
|
|
168
|
+
{ key: 2, label: 'Jane Smith' },
|
|
169
|
+
]);
|
|
170
|
+
|
|
171
|
+
const createUser = async (name) => {
|
|
172
|
+
// Call your API
|
|
173
|
+
const response = await fetch('/api/users', {
|
|
174
|
+
method: 'POST',
|
|
175
|
+
headers: { 'Content-Type': 'application/json' },
|
|
176
|
+
body: JSON.stringify({ name })
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
if (!response.ok) {
|
|
180
|
+
throw new Error('Failed to create user');
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const newUser = await response.json();
|
|
184
|
+
setAvailableUsers(prev => [...prev, newUser]);
|
|
185
|
+
return newUser;
|
|
186
|
+
};
|
|
38
187
|
|
|
188
|
+
const deleteUser = async (userId) => {
|
|
189
|
+
await fetch(`/api/users/${userId}`, { method: 'DELETE' });
|
|
190
|
+
setAvailableUsers(prev => prev.filter(u => u.key !== userId));
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
return (
|
|
194
|
+
<ChipInput
|
|
195
|
+
value={selectedUsers}
|
|
196
|
+
onValueChange={setSelectedUsers}
|
|
197
|
+
options={availableUsers}
|
|
198
|
+
onAddItem={createUser}
|
|
199
|
+
onDeleteItem={deleteUser}
|
|
200
|
+
placeholder="Type to create or select users..."
|
|
201
|
+
/>
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
### Migration from Legacy onAddNewOption
|
|
207
|
+
```tsx
|
|
208
|
+
// ❌ Old way (still works but deprecated)
|
|
209
|
+
function LegacyExample() {
|
|
210
|
+
const [tags, setTags] = useState([]);
|
|
211
|
+
|
|
212
|
+
return (
|
|
213
|
+
<ChipInput
|
|
214
|
+
value={tags}
|
|
215
|
+
onValueChange={setTags}
|
|
216
|
+
onAddNewOption={(value) => {
|
|
217
|
+
console.log('Added:', value);
|
|
218
|
+
}}
|
|
219
|
+
/>
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// ✅ New way (recommended)
|
|
224
|
+
function ModernExample() {
|
|
225
|
+
const [tags, setTags] = useState([]);
|
|
226
|
+
|
|
39
227
|
return (
|
|
40
228
|
<ChipInput
|
|
41
229
|
value={tags}
|
|
42
230
|
onValueChange={setTags}
|
|
43
|
-
|
|
231
|
+
onAddItem={(value) => ({
|
|
232
|
+
key: value,
|
|
233
|
+
label: value
|
|
234
|
+
})}
|
|
44
235
|
/>
|
|
45
236
|
);
|
|
46
237
|
}
|