@yamada-ui/autocomplete 0.4.13 → 0.5.2
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/autocomplete-create.d.mts +1 -1
- package/dist/autocomplete-create.d.ts +1 -1
- package/dist/autocomplete-create.mjs +1 -1
- package/dist/autocomplete-empty.d.mts +1 -1
- package/dist/autocomplete-empty.d.ts +1 -1
- package/dist/autocomplete-empty.mjs +1 -1
- package/dist/autocomplete-icon.d.mts +2 -2
- package/dist/autocomplete-icon.d.ts +2 -2
- package/dist/autocomplete-icon.mjs +1 -1
- package/dist/autocomplete-list.d.mts +1 -1
- package/dist/autocomplete-list.d.ts +1 -1
- package/dist/autocomplete-list.mjs +1 -1
- package/dist/autocomplete-option-group.d.mts +2 -3
- package/dist/autocomplete-option-group.d.ts +2 -3
- package/dist/autocomplete-option-group.mjs +1 -1
- package/dist/autocomplete-option.d.mts +1 -2
- package/dist/autocomplete-option.d.ts +1 -2
- package/dist/autocomplete-option.mjs +1 -1
- package/dist/autocomplete.d.mts +5 -6
- package/dist/autocomplete.d.ts +5 -6
- package/dist/autocomplete.js +57 -60
- package/dist/autocomplete.mjs +1 -1
- package/dist/{chunk-EFOFI4S7.mjs → chunk-JKY5MMNC.mjs} +61 -64
- package/dist/index.d.mts +1 -2
- package/dist/index.d.ts +1 -2
- package/dist/index.js +61 -64
- package/dist/index.mjs +1 -1
- package/dist/multi-autocomplete.d.mts +5 -6
- package/dist/multi-autocomplete.d.ts +5 -6
- package/dist/multi-autocomplete.js +57 -60
- package/dist/multi-autocomplete.mjs +1 -1
- package/dist/{use-autocomplete-2422c1c8.d.ts → use-autocomplete-3e77a4da.d.ts} +31 -42
- package/dist/use-autocomplete.d.mts +1 -2
- package/dist/use-autocomplete.d.ts +1 -2
- package/dist/use-autocomplete.js +53 -56
- package/dist/use-autocomplete.mjs +1 -1
- package/package.json +6 -6
|
@@ -171,7 +171,7 @@ var Autocomplete = forwardRef2(
|
|
|
171
171
|
getPopoverProps,
|
|
172
172
|
getContainerProps,
|
|
173
173
|
getFieldProps,
|
|
174
|
-
|
|
174
|
+
allowCreate,
|
|
175
175
|
isEmpty,
|
|
176
176
|
inputValue,
|
|
177
177
|
computedChildren,
|
|
@@ -193,7 +193,7 @@ var Autocomplete = forwardRef2(
|
|
|
193
193
|
...rest,
|
|
194
194
|
formControlProps,
|
|
195
195
|
inputValue,
|
|
196
|
-
|
|
196
|
+
allowCreate,
|
|
197
197
|
isEmpty,
|
|
198
198
|
styles
|
|
199
199
|
},
|
|
@@ -215,9 +215,9 @@ var Autocomplete = forwardRef2(
|
|
|
215
215
|
),
|
|
216
216
|
/* @__PURE__ */ jsx2(AutocompleteIcon, { ...iconProps, ...formControlProps }),
|
|
217
217
|
!isEmpty ? /* @__PURE__ */ jsxs(AutocompleteList, { ...listProps, children: [
|
|
218
|
-
|
|
218
|
+
allowCreate ? /* @__PURE__ */ jsx2(AutocompleteCreate, {}) : /* @__PURE__ */ jsx2(AutocompleteEmpty, {}),
|
|
219
219
|
children != null ? children : computedChildren
|
|
220
|
-
] }) : /* @__PURE__ */ jsx2(AutocompleteList, { ...listProps, children:
|
|
220
|
+
] }) : /* @__PURE__ */ jsx2(AutocompleteList, { ...listProps, children: allowCreate && inputValue ? /* @__PURE__ */ jsx2(AutocompleteCreate, {}) : /* @__PURE__ */ jsx2(AutocompleteEmpty, {}) })
|
|
221
221
|
]
|
|
222
222
|
}
|
|
223
223
|
) })
|
|
@@ -488,7 +488,7 @@ var MultiAutocomplete = forwardRef7(
|
|
|
488
488
|
getPopoverProps,
|
|
489
489
|
getContainerProps,
|
|
490
490
|
getFieldProps,
|
|
491
|
-
|
|
491
|
+
allowCreate,
|
|
492
492
|
isEmpty,
|
|
493
493
|
inputValue,
|
|
494
494
|
computedChildren,
|
|
@@ -517,7 +517,7 @@ var MultiAutocomplete = forwardRef7(
|
|
|
517
517
|
value,
|
|
518
518
|
formControlProps,
|
|
519
519
|
inputValue,
|
|
520
|
-
|
|
520
|
+
allowCreate,
|
|
521
521
|
isEmpty,
|
|
522
522
|
styles
|
|
523
523
|
},
|
|
@@ -549,9 +549,9 @@ var MultiAutocomplete = forwardRef7(
|
|
|
549
549
|
}
|
|
550
550
|
) : /* @__PURE__ */ jsx7(AutocompleteIcon, { ...iconProps, ...formControlProps }),
|
|
551
551
|
!isEmpty ? /* @__PURE__ */ jsxs6(AutocompleteList, { ...listProps, children: [
|
|
552
|
-
|
|
552
|
+
allowCreate ? /* @__PURE__ */ jsx7(AutocompleteCreate, {}) : /* @__PURE__ */ jsx7(AutocompleteEmpty, {}),
|
|
553
553
|
children != null ? children : computedChildren
|
|
554
|
-
] }) : /* @__PURE__ */ jsx7(AutocompleteList, { ...listProps, children:
|
|
554
|
+
] }) : /* @__PURE__ */ jsx7(AutocompleteList, { ...listProps, children: allowCreate && inputValue ? /* @__PURE__ */ jsx7(AutocompleteCreate, {}) : /* @__PURE__ */ jsx7(AutocompleteEmpty, {}) })
|
|
555
555
|
]
|
|
556
556
|
}
|
|
557
557
|
) })
|
|
@@ -757,20 +757,20 @@ var defaultFormat = (value) => {
|
|
|
757
757
|
value = value.toUpperCase();
|
|
758
758
|
return value;
|
|
759
759
|
};
|
|
760
|
-
var
|
|
761
|
-
const
|
|
762
|
-
|
|
760
|
+
var flattenItems = (items) => {
|
|
761
|
+
const filterItems = (items2) => items2.map((item) => {
|
|
762
|
+
var _a;
|
|
763
|
+
const { isDisabled, isFocusable } = item;
|
|
763
764
|
const trulyDisabled = !!isDisabled && !isFocusable;
|
|
764
|
-
const isMulti = isArray(value);
|
|
765
765
|
if (trulyDisabled)
|
|
766
766
|
return;
|
|
767
|
-
if (
|
|
768
|
-
return
|
|
767
|
+
if ("items" in item) {
|
|
768
|
+
return filterItems((_a = item.items) != null ? _a : []);
|
|
769
769
|
} else {
|
|
770
|
-
return
|
|
770
|
+
return item;
|
|
771
771
|
}
|
|
772
772
|
}).filter(Boolean);
|
|
773
|
-
return
|
|
773
|
+
return filterItems(items).flat(Infinity);
|
|
774
774
|
};
|
|
775
775
|
var isTargetOption = (target) => {
|
|
776
776
|
var _a;
|
|
@@ -793,14 +793,15 @@ var useAutocomplete = ({
|
|
|
793
793
|
maxSelectedValues,
|
|
794
794
|
closeOnBlur = true,
|
|
795
795
|
closeOnEsc = true,
|
|
796
|
-
|
|
797
|
-
|
|
796
|
+
allowCreate = false,
|
|
797
|
+
insertPositionItem = "first",
|
|
798
798
|
emptyMessage = "No results found",
|
|
799
799
|
format = defaultFormat,
|
|
800
800
|
placement = "bottom-start",
|
|
801
801
|
duration = 0.2,
|
|
802
802
|
optionProps,
|
|
803
803
|
placeholder,
|
|
804
|
+
items,
|
|
804
805
|
children,
|
|
805
806
|
...rest
|
|
806
807
|
}) => {
|
|
@@ -824,7 +825,7 @@ var useAutocomplete = ({
|
|
|
824
825
|
const inputRef = useRef2(null);
|
|
825
826
|
const timeoutIds = useRef2(/* @__PURE__ */ new Set([]));
|
|
826
827
|
const isComposition = useRef2(false);
|
|
827
|
-
const [
|
|
828
|
+
const [resolvedItems, setResolvedItems] = useState(items);
|
|
828
829
|
const [value, setValue] = useControllableState({
|
|
829
830
|
value: rest.value,
|
|
830
831
|
defaultValue: rest.defaultValue,
|
|
@@ -837,19 +838,19 @@ var useAutocomplete = ({
|
|
|
837
838
|
const [isHit, setIsHit] = useState(true);
|
|
838
839
|
const [isOpen, setIsOpen] = useState(defaultIsOpen != null ? defaultIsOpen : false);
|
|
839
840
|
const isFocused = focusedIndex > -1;
|
|
840
|
-
const isCreate = focusedIndex === -2 &&
|
|
841
|
+
const isCreate = focusedIndex === -2 && allowCreate;
|
|
841
842
|
const isMulti = isArray(value);
|
|
842
843
|
const isEmptyValue = !isMulti ? !value : !value.length;
|
|
843
|
-
const [
|
|
844
|
-
if (isArray(
|
|
845
|
-
return
|
|
844
|
+
const [firstInsertPositionItem, secondInsertPositionItem] = useMemo2(() => {
|
|
845
|
+
if (isArray(insertPositionItem)) {
|
|
846
|
+
return insertPositionItem;
|
|
846
847
|
} else {
|
|
847
|
-
return [
|
|
848
|
+
return [insertPositionItem, "first"];
|
|
848
849
|
}
|
|
849
|
-
}, [
|
|
850
|
-
if (
|
|
850
|
+
}, [insertPositionItem]);
|
|
851
|
+
if (allowCreate && !isUndefined(children)) {
|
|
851
852
|
console.warn(
|
|
852
|
-
`${!isMulti ? "Autocomplete" : "MultiAutocomplete"}: ${!isMulti ? "Autocomplete" : "MultiAutocomplete"} internally prefers 'children'. If '
|
|
853
|
+
`${!isMulti ? "Autocomplete" : "MultiAutocomplete"}: ${!isMulti ? "Autocomplete" : "MultiAutocomplete"} internally prefers 'children'. If 'allowCreate' is true, it will not be reflected correctly. If want to reflect, please set 'items' in props.`
|
|
853
854
|
);
|
|
854
855
|
}
|
|
855
856
|
const selectedValues = descendants.enabledValues(
|
|
@@ -863,18 +864,18 @@ var useAutocomplete = ({
|
|
|
863
864
|
({ node, index }) => "target" in node.dataset && !selectedIndexes.includes(index)
|
|
864
865
|
);
|
|
865
866
|
const validChildren = getValidChildren2(children);
|
|
866
|
-
const computedChildren =
|
|
867
|
-
if (
|
|
867
|
+
const computedChildren = resolvedItems == null ? void 0 : resolvedItems.map((item, i) => {
|
|
868
|
+
if ("value" in item) {
|
|
869
|
+
const { label, value: value2, ...props } = item;
|
|
868
870
|
return /* @__PURE__ */ jsx8(AutocompleteOption, { value: value2, ...props, children: label }, i);
|
|
869
|
-
} else {
|
|
871
|
+
} else if ("items" in item) {
|
|
872
|
+
const { label, items: items2 = [], ...props } = item;
|
|
870
873
|
return /* @__PURE__ */ jsx8(
|
|
871
874
|
AutocompleteOptionGroup,
|
|
872
875
|
{
|
|
873
876
|
label,
|
|
874
877
|
...props,
|
|
875
|
-
children:
|
|
876
|
-
({ label: label2, value: value3, ...props2 }, i2) => !isArray(value3) ? /* @__PURE__ */ jsx8(AutocompleteOption, { value: value3, ...props2, children: label2 }, i2) : null
|
|
877
|
-
)
|
|
878
|
+
children: items2.map(({ label: label2, value: value2, ...props2 }, i2) => /* @__PURE__ */ jsx8(AutocompleteOption, { value: value2, ...props2, children: label2 }, i2))
|
|
878
879
|
},
|
|
879
880
|
i
|
|
880
881
|
);
|
|
@@ -885,13 +886,13 @@ var useAutocomplete = ({
|
|
|
885
886
|
var _a;
|
|
886
887
|
if (formControlProps.disabled || formControlProps.readOnly)
|
|
887
888
|
return;
|
|
888
|
-
if (!
|
|
889
|
+
if (!allowCreate && (isEmpty || isAllSelected))
|
|
889
890
|
return;
|
|
890
891
|
setIsOpen(true);
|
|
891
892
|
if (inputRef.current)
|
|
892
893
|
inputRef.current.focus();
|
|
893
894
|
(_a = rest.onOpen) == null ? void 0 : _a.call(rest);
|
|
894
|
-
}, [
|
|
895
|
+
}, [allowCreate, formControlProps, isAllSelected, isEmpty, rest]);
|
|
895
896
|
const onClose = useCallback(() => {
|
|
896
897
|
var _a;
|
|
897
898
|
setIsOpen(false);
|
|
@@ -1163,55 +1164,51 @@ var useAutocomplete = ({
|
|
|
1163
1164
|
isComposition.current = false;
|
|
1164
1165
|
}, []);
|
|
1165
1166
|
const onCreate = useCallback(() => {
|
|
1166
|
-
var _a;
|
|
1167
|
+
var _a, _b, _c;
|
|
1167
1168
|
if (!listRef.current)
|
|
1168
1169
|
return;
|
|
1169
|
-
const
|
|
1170
|
-
let
|
|
1171
|
-
if (
|
|
1172
|
-
|
|
1173
|
-
if (
|
|
1174
|
-
|
|
1175
|
-
} else if (
|
|
1176
|
-
|
|
1170
|
+
const newItem = { label: inputValue, value: inputValue };
|
|
1171
|
+
let newItems = [];
|
|
1172
|
+
if (resolvedItems)
|
|
1173
|
+
newItems = resolvedItems;
|
|
1174
|
+
if (firstInsertPositionItem === "first") {
|
|
1175
|
+
newItems = [newItem, ...newItems];
|
|
1176
|
+
} else if (firstInsertPositionItem === "last") {
|
|
1177
|
+
newItems = [...newItems, newItem];
|
|
1177
1178
|
} else {
|
|
1178
|
-
const i =
|
|
1179
|
-
({ label }) => label ===
|
|
1179
|
+
const i = newItems.findIndex(
|
|
1180
|
+
({ label }) => label === firstInsertPositionItem
|
|
1180
1181
|
);
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
...newOptions[i].value
|
|
1186
|
-
];
|
|
1182
|
+
const targetItem = newItems[i];
|
|
1183
|
+
if (i !== -1 && "items" in targetItem) {
|
|
1184
|
+
if (secondInsertPositionItem === "first") {
|
|
1185
|
+
targetItem.items = [newItem, ...(_a = targetItem.items) != null ? _a : []];
|
|
1187
1186
|
} else {
|
|
1188
|
-
|
|
1189
|
-
...newOptions[i].value,
|
|
1190
|
-
newOption
|
|
1191
|
-
];
|
|
1187
|
+
targetItem.items = [...(_b = targetItem.items) != null ? _b : [], newItem];
|
|
1192
1188
|
}
|
|
1189
|
+
newItems[i] = targetItem;
|
|
1193
1190
|
} else {
|
|
1194
1191
|
console.warn(
|
|
1195
|
-
`${!isMulti ? "Autocomplete" : "MultiAutocomplete"}: '${
|
|
1192
|
+
`${!isMulti ? "Autocomplete" : "MultiAutocomplete"}: '${firstInsertPositionItem}' specified in insertPositionItem does not exist in the option group.`
|
|
1196
1193
|
);
|
|
1197
1194
|
}
|
|
1198
1195
|
}
|
|
1199
|
-
|
|
1196
|
+
setResolvedItems(newItems);
|
|
1200
1197
|
onChange(inputValue);
|
|
1201
1198
|
rebirthOptions(false);
|
|
1202
|
-
const index =
|
|
1199
|
+
const index = flattenItems(newItems).findIndex(
|
|
1203
1200
|
({ value: value2 }) => value2 === inputValue
|
|
1204
1201
|
);
|
|
1205
1202
|
setFocusedIndex(index);
|
|
1206
|
-
(
|
|
1203
|
+
(_c = rest.onCreate) == null ? void 0 : _c.call(rest, newItem, newItems);
|
|
1207
1204
|
}, [
|
|
1208
1205
|
inputValue,
|
|
1209
|
-
|
|
1210
|
-
|
|
1206
|
+
resolvedItems,
|
|
1207
|
+
firstInsertPositionItem,
|
|
1211
1208
|
onChange,
|
|
1212
1209
|
rebirthOptions,
|
|
1213
1210
|
rest,
|
|
1214
|
-
|
|
1211
|
+
secondInsertPositionItem,
|
|
1215
1212
|
isMulti
|
|
1216
1213
|
]);
|
|
1217
1214
|
const onDelete = useCallback(() => {
|
|
@@ -1398,7 +1395,7 @@ var useAutocomplete = ({
|
|
|
1398
1395
|
focusedIndex,
|
|
1399
1396
|
omitSelectedValues,
|
|
1400
1397
|
closeOnSelect,
|
|
1401
|
-
|
|
1398
|
+
allowCreate,
|
|
1402
1399
|
emptyMessage,
|
|
1403
1400
|
isOpen,
|
|
1404
1401
|
isAllSelected,
|
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export { Autocomplete, AutocompleteProps } from './autocomplete.mjs';
|
|
2
2
|
export { AutocompleteOptionGroup, AutocompleteOptionGroupProps } from './autocomplete-option-group.mjs';
|
|
3
|
-
export { A as AutocompleteOption, a as AutocompleteOptionProps } from './use-autocomplete-
|
|
3
|
+
export { b as AutocompleteItem, A as AutocompleteOption, a as AutocompleteOptionProps } from './use-autocomplete-3e77a4da.js';
|
|
4
4
|
export { AutocompleteCreate, AutocompleteCreateProps } from './autocomplete-create.mjs';
|
|
5
5
|
export { AutocompleteEmpty, AutocompleteEmptyProps } from './autocomplete-empty.mjs';
|
|
6
6
|
export { MultiAutocomplete, MultiAutocompleteProps } from './multi-autocomplete.mjs';
|
|
@@ -11,5 +11,4 @@ import './autocomplete-list.mjs';
|
|
|
11
11
|
import 'react/jsx-runtime';
|
|
12
12
|
import '@yamada-ui/form-control';
|
|
13
13
|
import '@yamada-ui/popover';
|
|
14
|
-
import '@yamada-ui/select';
|
|
15
14
|
import '@yamada-ui/utils';
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export { Autocomplete, AutocompleteProps } from './autocomplete.js';
|
|
2
2
|
export { AutocompleteOptionGroup, AutocompleteOptionGroupProps } from './autocomplete-option-group.js';
|
|
3
|
-
export { A as AutocompleteOption, a as AutocompleteOptionProps } from './use-autocomplete-
|
|
3
|
+
export { b as AutocompleteItem, A as AutocompleteOption, a as AutocompleteOptionProps } from './use-autocomplete-3e77a4da.js';
|
|
4
4
|
export { AutocompleteCreate, AutocompleteCreateProps } from './autocomplete-create.js';
|
|
5
5
|
export { AutocompleteEmpty, AutocompleteEmptyProps } from './autocomplete-empty.js';
|
|
6
6
|
export { MultiAutocomplete, MultiAutocompleteProps } from './multi-autocomplete.js';
|
|
@@ -11,5 +11,4 @@ import './autocomplete-list.js';
|
|
|
11
11
|
import 'react/jsx-runtime';
|
|
12
12
|
import '@yamada-ui/form-control';
|
|
13
13
|
import '@yamada-ui/popover';
|
|
14
|
-
import '@yamada-ui/select';
|
|
15
14
|
import '@yamada-ui/utils';
|
package/dist/index.js
CHANGED
|
@@ -152,20 +152,20 @@ var defaultFormat = (value) => {
|
|
|
152
152
|
value = value.toUpperCase();
|
|
153
153
|
return value;
|
|
154
154
|
};
|
|
155
|
-
var
|
|
156
|
-
const
|
|
157
|
-
|
|
155
|
+
var flattenItems = (items) => {
|
|
156
|
+
const filterItems = (items2) => items2.map((item) => {
|
|
157
|
+
var _a;
|
|
158
|
+
const { isDisabled, isFocusable } = item;
|
|
158
159
|
const trulyDisabled = !!isDisabled && !isFocusable;
|
|
159
|
-
const isMulti = (0, import_utils.isArray)(value);
|
|
160
160
|
if (trulyDisabled)
|
|
161
161
|
return;
|
|
162
|
-
if (
|
|
163
|
-
return
|
|
162
|
+
if ("items" in item) {
|
|
163
|
+
return filterItems((_a = item.items) != null ? _a : []);
|
|
164
164
|
} else {
|
|
165
|
-
return
|
|
165
|
+
return item;
|
|
166
166
|
}
|
|
167
167
|
}).filter(Boolean);
|
|
168
|
-
return
|
|
168
|
+
return filterItems(items).flat(Infinity);
|
|
169
169
|
};
|
|
170
170
|
var isTargetOption = (target) => {
|
|
171
171
|
var _a;
|
|
@@ -188,14 +188,15 @@ var useAutocomplete = ({
|
|
|
188
188
|
maxSelectedValues,
|
|
189
189
|
closeOnBlur = true,
|
|
190
190
|
closeOnEsc = true,
|
|
191
|
-
|
|
192
|
-
|
|
191
|
+
allowCreate = false,
|
|
192
|
+
insertPositionItem = "first",
|
|
193
193
|
emptyMessage = "No results found",
|
|
194
194
|
format = defaultFormat,
|
|
195
195
|
placement = "bottom-start",
|
|
196
196
|
duration = 0.2,
|
|
197
197
|
optionProps,
|
|
198
198
|
placeholder,
|
|
199
|
+
items,
|
|
199
200
|
children,
|
|
200
201
|
...rest
|
|
201
202
|
}) => {
|
|
@@ -219,7 +220,7 @@ var useAutocomplete = ({
|
|
|
219
220
|
const inputRef = (0, import_react.useRef)(null);
|
|
220
221
|
const timeoutIds = (0, import_react.useRef)(/* @__PURE__ */ new Set([]));
|
|
221
222
|
const isComposition = (0, import_react.useRef)(false);
|
|
222
|
-
const [
|
|
223
|
+
const [resolvedItems, setResolvedItems] = (0, import_react.useState)(items);
|
|
223
224
|
const [value, setValue] = (0, import_use_controllable_state.useControllableState)({
|
|
224
225
|
value: rest.value,
|
|
225
226
|
defaultValue: rest.defaultValue,
|
|
@@ -232,19 +233,19 @@ var useAutocomplete = ({
|
|
|
232
233
|
const [isHit, setIsHit] = (0, import_react.useState)(true);
|
|
233
234
|
const [isOpen, setIsOpen] = (0, import_react.useState)(defaultIsOpen != null ? defaultIsOpen : false);
|
|
234
235
|
const isFocused = focusedIndex > -1;
|
|
235
|
-
const isCreate = focusedIndex === -2 &&
|
|
236
|
+
const isCreate = focusedIndex === -2 && allowCreate;
|
|
236
237
|
const isMulti = (0, import_utils.isArray)(value);
|
|
237
238
|
const isEmptyValue = !isMulti ? !value : !value.length;
|
|
238
|
-
const [
|
|
239
|
-
if ((0, import_utils.isArray)(
|
|
240
|
-
return
|
|
239
|
+
const [firstInsertPositionItem, secondInsertPositionItem] = (0, import_react.useMemo)(() => {
|
|
240
|
+
if ((0, import_utils.isArray)(insertPositionItem)) {
|
|
241
|
+
return insertPositionItem;
|
|
241
242
|
} else {
|
|
242
|
-
return [
|
|
243
|
+
return [insertPositionItem, "first"];
|
|
243
244
|
}
|
|
244
|
-
}, [
|
|
245
|
-
if (
|
|
245
|
+
}, [insertPositionItem]);
|
|
246
|
+
if (allowCreate && !(0, import_utils.isUndefined)(children)) {
|
|
246
247
|
console.warn(
|
|
247
|
-
`${!isMulti ? "Autocomplete" : "MultiAutocomplete"}: ${!isMulti ? "Autocomplete" : "MultiAutocomplete"} internally prefers 'children'. If '
|
|
248
|
+
`${!isMulti ? "Autocomplete" : "MultiAutocomplete"}: ${!isMulti ? "Autocomplete" : "MultiAutocomplete"} internally prefers 'children'. If 'allowCreate' is true, it will not be reflected correctly. If want to reflect, please set 'items' in props.`
|
|
248
249
|
);
|
|
249
250
|
}
|
|
250
251
|
const selectedValues = descendants.enabledValues(
|
|
@@ -258,18 +259,18 @@ var useAutocomplete = ({
|
|
|
258
259
|
({ node, index }) => "target" in node.dataset && !selectedIndexes.includes(index)
|
|
259
260
|
);
|
|
260
261
|
const validChildren = (0, import_utils.getValidChildren)(children);
|
|
261
|
-
const computedChildren =
|
|
262
|
-
if (
|
|
262
|
+
const computedChildren = resolvedItems == null ? void 0 : resolvedItems.map((item, i) => {
|
|
263
|
+
if ("value" in item) {
|
|
264
|
+
const { label, value: value2, ...props } = item;
|
|
263
265
|
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(AutocompleteOption, { value: value2, ...props, children: label }, i);
|
|
264
|
-
} else {
|
|
266
|
+
} else if ("items" in item) {
|
|
267
|
+
const { label, items: items2 = [], ...props } = item;
|
|
265
268
|
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
266
269
|
AutocompleteOptionGroup,
|
|
267
270
|
{
|
|
268
271
|
label,
|
|
269
272
|
...props,
|
|
270
|
-
children:
|
|
271
|
-
({ label: label2, value: value3, ...props2 }, i2) => !(0, import_utils.isArray)(value3) ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(AutocompleteOption, { value: value3, ...props2, children: label2 }, i2) : null
|
|
272
|
-
)
|
|
273
|
+
children: items2.map(({ label: label2, value: value2, ...props2 }, i2) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(AutocompleteOption, { value: value2, ...props2, children: label2 }, i2))
|
|
273
274
|
},
|
|
274
275
|
i
|
|
275
276
|
);
|
|
@@ -280,13 +281,13 @@ var useAutocomplete = ({
|
|
|
280
281
|
var _a;
|
|
281
282
|
if (formControlProps.disabled || formControlProps.readOnly)
|
|
282
283
|
return;
|
|
283
|
-
if (!
|
|
284
|
+
if (!allowCreate && (isEmpty || isAllSelected))
|
|
284
285
|
return;
|
|
285
286
|
setIsOpen(true);
|
|
286
287
|
if (inputRef.current)
|
|
287
288
|
inputRef.current.focus();
|
|
288
289
|
(_a = rest.onOpen) == null ? void 0 : _a.call(rest);
|
|
289
|
-
}, [
|
|
290
|
+
}, [allowCreate, formControlProps, isAllSelected, isEmpty, rest]);
|
|
290
291
|
const onClose = (0, import_react.useCallback)(() => {
|
|
291
292
|
var _a;
|
|
292
293
|
setIsOpen(false);
|
|
@@ -558,55 +559,51 @@ var useAutocomplete = ({
|
|
|
558
559
|
isComposition.current = false;
|
|
559
560
|
}, []);
|
|
560
561
|
const onCreate = (0, import_react.useCallback)(() => {
|
|
561
|
-
var _a;
|
|
562
|
+
var _a, _b, _c;
|
|
562
563
|
if (!listRef.current)
|
|
563
564
|
return;
|
|
564
|
-
const
|
|
565
|
-
let
|
|
566
|
-
if (
|
|
567
|
-
|
|
568
|
-
if (
|
|
569
|
-
|
|
570
|
-
} else if (
|
|
571
|
-
|
|
565
|
+
const newItem = { label: inputValue, value: inputValue };
|
|
566
|
+
let newItems = [];
|
|
567
|
+
if (resolvedItems)
|
|
568
|
+
newItems = resolvedItems;
|
|
569
|
+
if (firstInsertPositionItem === "first") {
|
|
570
|
+
newItems = [newItem, ...newItems];
|
|
571
|
+
} else if (firstInsertPositionItem === "last") {
|
|
572
|
+
newItems = [...newItems, newItem];
|
|
572
573
|
} else {
|
|
573
|
-
const i =
|
|
574
|
-
({ label }) => label ===
|
|
574
|
+
const i = newItems.findIndex(
|
|
575
|
+
({ label }) => label === firstInsertPositionItem
|
|
575
576
|
);
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
...newOptions[i].value
|
|
581
|
-
];
|
|
577
|
+
const targetItem = newItems[i];
|
|
578
|
+
if (i !== -1 && "items" in targetItem) {
|
|
579
|
+
if (secondInsertPositionItem === "first") {
|
|
580
|
+
targetItem.items = [newItem, ...(_a = targetItem.items) != null ? _a : []];
|
|
582
581
|
} else {
|
|
583
|
-
|
|
584
|
-
...newOptions[i].value,
|
|
585
|
-
newOption
|
|
586
|
-
];
|
|
582
|
+
targetItem.items = [...(_b = targetItem.items) != null ? _b : [], newItem];
|
|
587
583
|
}
|
|
584
|
+
newItems[i] = targetItem;
|
|
588
585
|
} else {
|
|
589
586
|
console.warn(
|
|
590
|
-
`${!isMulti ? "Autocomplete" : "MultiAutocomplete"}: '${
|
|
587
|
+
`${!isMulti ? "Autocomplete" : "MultiAutocomplete"}: '${firstInsertPositionItem}' specified in insertPositionItem does not exist in the option group.`
|
|
591
588
|
);
|
|
592
589
|
}
|
|
593
590
|
}
|
|
594
|
-
|
|
591
|
+
setResolvedItems(newItems);
|
|
595
592
|
onChange(inputValue);
|
|
596
593
|
rebirthOptions(false);
|
|
597
|
-
const index =
|
|
594
|
+
const index = flattenItems(newItems).findIndex(
|
|
598
595
|
({ value: value2 }) => value2 === inputValue
|
|
599
596
|
);
|
|
600
597
|
setFocusedIndex(index);
|
|
601
|
-
(
|
|
598
|
+
(_c = rest.onCreate) == null ? void 0 : _c.call(rest, newItem, newItems);
|
|
602
599
|
}, [
|
|
603
600
|
inputValue,
|
|
604
|
-
|
|
605
|
-
|
|
601
|
+
resolvedItems,
|
|
602
|
+
firstInsertPositionItem,
|
|
606
603
|
onChange,
|
|
607
604
|
rebirthOptions,
|
|
608
605
|
rest,
|
|
609
|
-
|
|
606
|
+
secondInsertPositionItem,
|
|
610
607
|
isMulti
|
|
611
608
|
]);
|
|
612
609
|
const onDelete = (0, import_react.useCallback)(() => {
|
|
@@ -793,7 +790,7 @@ var useAutocomplete = ({
|
|
|
793
790
|
focusedIndex,
|
|
794
791
|
omitSelectedValues,
|
|
795
792
|
closeOnSelect,
|
|
796
|
-
|
|
793
|
+
allowCreate,
|
|
797
794
|
emptyMessage,
|
|
798
795
|
isOpen,
|
|
799
796
|
isAllSelected,
|
|
@@ -1321,7 +1318,7 @@ var Autocomplete = (0, import_core4.forwardRef)(
|
|
|
1321
1318
|
getPopoverProps,
|
|
1322
1319
|
getContainerProps,
|
|
1323
1320
|
getFieldProps,
|
|
1324
|
-
|
|
1321
|
+
allowCreate,
|
|
1325
1322
|
isEmpty,
|
|
1326
1323
|
inputValue,
|
|
1327
1324
|
computedChildren,
|
|
@@ -1343,7 +1340,7 @@ var Autocomplete = (0, import_core4.forwardRef)(
|
|
|
1343
1340
|
...rest,
|
|
1344
1341
|
formControlProps,
|
|
1345
1342
|
inputValue,
|
|
1346
|
-
|
|
1343
|
+
allowCreate,
|
|
1347
1344
|
isEmpty,
|
|
1348
1345
|
styles
|
|
1349
1346
|
},
|
|
@@ -1365,9 +1362,9 @@ var Autocomplete = (0, import_core4.forwardRef)(
|
|
|
1365
1362
|
),
|
|
1366
1363
|
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)(AutocompleteIcon, { ...iconProps, ...formControlProps }),
|
|
1367
1364
|
!isEmpty ? /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(AutocompleteList, { ...listProps, children: [
|
|
1368
|
-
|
|
1365
|
+
allowCreate ? /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(AutocompleteCreate, {}) : /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(AutocompleteEmpty, {}),
|
|
1369
1366
|
children != null ? children : computedChildren
|
|
1370
|
-
] }) : /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(AutocompleteList, { ...listProps, children:
|
|
1367
|
+
] }) : /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(AutocompleteList, { ...listProps, children: allowCreate && inputValue ? /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(AutocompleteCreate, {}) : /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(AutocompleteEmpty, {}) })
|
|
1371
1368
|
]
|
|
1372
1369
|
}
|
|
1373
1370
|
) })
|
|
@@ -1630,7 +1627,7 @@ var MultiAutocomplete = (0, import_core9.forwardRef)(
|
|
|
1630
1627
|
getPopoverProps,
|
|
1631
1628
|
getContainerProps,
|
|
1632
1629
|
getFieldProps,
|
|
1633
|
-
|
|
1630
|
+
allowCreate,
|
|
1634
1631
|
isEmpty,
|
|
1635
1632
|
inputValue,
|
|
1636
1633
|
computedChildren,
|
|
@@ -1659,7 +1656,7 @@ var MultiAutocomplete = (0, import_core9.forwardRef)(
|
|
|
1659
1656
|
value,
|
|
1660
1657
|
formControlProps,
|
|
1661
1658
|
inputValue,
|
|
1662
|
-
|
|
1659
|
+
allowCreate,
|
|
1663
1660
|
isEmpty,
|
|
1664
1661
|
styles
|
|
1665
1662
|
},
|
|
@@ -1691,9 +1688,9 @@ var MultiAutocomplete = (0, import_core9.forwardRef)(
|
|
|
1691
1688
|
}
|
|
1692
1689
|
) : /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(AutocompleteIcon, { ...iconProps, ...formControlProps }),
|
|
1693
1690
|
!isEmpty ? /* @__PURE__ */ (0, import_jsx_runtime9.jsxs)(AutocompleteList, { ...listProps, children: [
|
|
1694
|
-
|
|
1691
|
+
allowCreate ? /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(AutocompleteCreate, {}) : /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(AutocompleteEmpty, {}),
|
|
1695
1692
|
children != null ? children : computedChildren
|
|
1696
|
-
] }) : /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(AutocompleteList, { ...listProps, children:
|
|
1693
|
+
] }) : /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(AutocompleteList, { ...listProps, children: allowCreate && inputValue ? /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(AutocompleteCreate, {}) : /* @__PURE__ */ (0, import_jsx_runtime9.jsx)(AutocompleteEmpty, {}) })
|
|
1697
1694
|
]
|
|
1698
1695
|
}
|
|
1699
1696
|
) })
|
package/dist/index.mjs
CHANGED
|
@@ -3,11 +3,10 @@ import { ThemeProps, HTMLUIProps } from '@yamada-ui/core';
|
|
|
3
3
|
import { FC, MouseEventHandler } from 'react';
|
|
4
4
|
import { AutocompleteIconProps } from './autocomplete-icon.mjs';
|
|
5
5
|
import { AutocompleteListProps } from './autocomplete-list.mjs';
|
|
6
|
-
import {
|
|
6
|
+
import { c as UseAutocompleteProps } from './use-autocomplete-3e77a4da.js';
|
|
7
7
|
import 'react/jsx-runtime';
|
|
8
8
|
import '@yamada-ui/form-control';
|
|
9
9
|
import '@yamada-ui/popover';
|
|
10
|
-
import '@yamada-ui/select';
|
|
11
10
|
import '@yamada-ui/utils';
|
|
12
11
|
|
|
13
12
|
type MultiAutocompleteOptions = {
|
|
@@ -49,15 +48,15 @@ type MultiAutocompleteOptions = {
|
|
|
49
48
|
/**
|
|
50
49
|
* Props for multi autocomplete container element.
|
|
51
50
|
*/
|
|
52
|
-
containerProps?: Omit<HTMLUIProps<
|
|
51
|
+
containerProps?: Omit<HTMLUIProps<"div">, "children">;
|
|
53
52
|
/**
|
|
54
53
|
* Props for multi autocomplete list element.
|
|
55
54
|
*/
|
|
56
|
-
listProps?: Omit<AutocompleteListProps,
|
|
55
|
+
listProps?: Omit<AutocompleteListProps, "children">;
|
|
57
56
|
/**
|
|
58
57
|
* Props for multi autocomplete input element.
|
|
59
58
|
*/
|
|
60
|
-
inputProps?: HTMLUIProps<
|
|
59
|
+
inputProps?: HTMLUIProps<"input">;
|
|
61
60
|
/**
|
|
62
61
|
* Props for multi autocomplete icon element.
|
|
63
62
|
*/
|
|
@@ -67,7 +66,7 @@ type MultiAutocompleteOptions = {
|
|
|
67
66
|
*/
|
|
68
67
|
clearIconProps?: AutocompleteIconProps;
|
|
69
68
|
};
|
|
70
|
-
type MultiAutocompleteProps = ThemeProps<
|
|
69
|
+
type MultiAutocompleteProps = ThemeProps<"Select"> & UseAutocompleteProps<string[]> & MultiAutocompleteOptions;
|
|
71
70
|
declare const MultiAutocomplete: _yamada_ui_core.Component<"div", MultiAutocompleteProps>;
|
|
72
71
|
|
|
73
72
|
export { MultiAutocomplete, MultiAutocompleteProps };
|