@asymmetric-effort/specifyjs 0.3.7 → 0.3.8
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.
|
@@ -3089,11 +3089,16 @@ function Select(props) {
|
|
|
3089
3089
|
disabled = false,
|
|
3090
3090
|
error,
|
|
3091
3091
|
label,
|
|
3092
|
-
helpText
|
|
3092
|
+
helpText,
|
|
3093
|
+
creatable = false,
|
|
3094
|
+
onCreate,
|
|
3095
|
+
createLabel = "+ Create new..."
|
|
3093
3096
|
} = props;
|
|
3094
3097
|
const [isOpen, setIsOpen] = useState(false);
|
|
3095
3098
|
const [searchQuery, setSearchQuery] = useState("");
|
|
3096
3099
|
const [focusedIndex, setFocusedIndex] = useState(-1);
|
|
3100
|
+
const [isCreating, setIsCreating] = useState(false);
|
|
3101
|
+
const [createValue, setCreateValue] = useState("");
|
|
3097
3102
|
const containerRef = useRef(null);
|
|
3098
3103
|
const selectedValues = useMemo(
|
|
3099
3104
|
() => Array.isArray(value) ? value : value ? [value] : [],
|
|
@@ -3128,6 +3133,8 @@ function Select(props) {
|
|
|
3128
3133
|
if (containerRef.current && !containerRef.current.contains(target)) {
|
|
3129
3134
|
setIsOpen(false);
|
|
3130
3135
|
setSearchQuery("");
|
|
3136
|
+
setIsCreating(false);
|
|
3137
|
+
setCreateValue("");
|
|
3131
3138
|
}
|
|
3132
3139
|
};
|
|
3133
3140
|
document.addEventListener("mousedown", handleClick);
|
|
@@ -3263,7 +3270,7 @@ function Select(props) {
|
|
|
3263
3270
|
elements.push(renderOption(opt, flatIdx++));
|
|
3264
3271
|
}
|
|
3265
3272
|
}
|
|
3266
|
-
if (elements.length === 0) {
|
|
3273
|
+
if (elements.length === 0 && !creatable) {
|
|
3267
3274
|
elements.push(
|
|
3268
3275
|
createElement(
|
|
3269
3276
|
"div",
|
|
@@ -3272,6 +3279,82 @@ function Select(props) {
|
|
|
3272
3279
|
)
|
|
3273
3280
|
);
|
|
3274
3281
|
}
|
|
3282
|
+
if (creatable) {
|
|
3283
|
+
if (isCreating) {
|
|
3284
|
+
elements.push(
|
|
3285
|
+
createElement(
|
|
3286
|
+
"div",
|
|
3287
|
+
{
|
|
3288
|
+
key: "__create-input__",
|
|
3289
|
+
style: {
|
|
3290
|
+
padding: "4px 8px",
|
|
3291
|
+
borderTop: elements.length > 0 ? "1px solid #e5e7eb" : "none"
|
|
3292
|
+
}
|
|
3293
|
+
},
|
|
3294
|
+
createElement("input", {
|
|
3295
|
+
type: "text",
|
|
3296
|
+
value: createValue,
|
|
3297
|
+
placeholder: "Type new value...",
|
|
3298
|
+
autoFocus: true,
|
|
3299
|
+
onInput: (e) => {
|
|
3300
|
+
setCreateValue(e.target.value);
|
|
3301
|
+
},
|
|
3302
|
+
onKeyDown: (e) => {
|
|
3303
|
+
const key = e.key;
|
|
3304
|
+
if (key === "Enter") {
|
|
3305
|
+
e.preventDefault();
|
|
3306
|
+
const trimmed = createValue.trim();
|
|
3307
|
+
if (trimmed) {
|
|
3308
|
+
if (onCreate) onCreate(trimmed);
|
|
3309
|
+
if (!multiple) {
|
|
3310
|
+
onChange(trimmed);
|
|
3311
|
+
setIsOpen(false);
|
|
3312
|
+
}
|
|
3313
|
+
setCreateValue("");
|
|
3314
|
+
setIsCreating(false);
|
|
3315
|
+
}
|
|
3316
|
+
} else if (key === "Escape") {
|
|
3317
|
+
setCreateValue("");
|
|
3318
|
+
setIsCreating(false);
|
|
3319
|
+
}
|
|
3320
|
+
},
|
|
3321
|
+
style: {
|
|
3322
|
+
width: "100%",
|
|
3323
|
+
boxSizing: "border-box",
|
|
3324
|
+
padding: "6px 8px",
|
|
3325
|
+
border: "1px solid #d1d5db",
|
|
3326
|
+
borderRadius: "4px",
|
|
3327
|
+
outline: "none",
|
|
3328
|
+
fontSize: "14px"
|
|
3329
|
+
}
|
|
3330
|
+
})
|
|
3331
|
+
)
|
|
3332
|
+
);
|
|
3333
|
+
} else {
|
|
3334
|
+
elements.push(
|
|
3335
|
+
createElement(
|
|
3336
|
+
"div",
|
|
3337
|
+
{
|
|
3338
|
+
key: "__create-new__",
|
|
3339
|
+
style: {
|
|
3340
|
+
padding: "8px 12px",
|
|
3341
|
+
cursor: "pointer",
|
|
3342
|
+
color: "#3b82f6",
|
|
3343
|
+
fontSize: "14px",
|
|
3344
|
+
borderTop: elements.length > 0 ? "1px solid #e5e7eb" : "none"
|
|
3345
|
+
},
|
|
3346
|
+
onClick: () => {
|
|
3347
|
+
setIsCreating(true);
|
|
3348
|
+
setCreateValue("");
|
|
3349
|
+
},
|
|
3350
|
+
role: "option",
|
|
3351
|
+
"aria-label": "Create new option"
|
|
3352
|
+
},
|
|
3353
|
+
createLabel
|
|
3354
|
+
)
|
|
3355
|
+
);
|
|
3356
|
+
}
|
|
3357
|
+
}
|
|
3275
3358
|
return elements;
|
|
3276
3359
|
};
|
|
3277
3360
|
const chevron = createElement(
|