@premieroctet/next-admin
Version:
Next-Admin provides a customizable and turnkey admin dashboard for applications built with Next.js and powered by the Prisma ORM. It aims to simplify the development process by providing a turnkey admin system that can be easily integrated into your proje
94 lines (93 loc) • 3.81 kB
JavaScript
import { jsx, jsxs } from "react/jsx-runtime";
import { DndContext } from "@dnd-kit/core";
import { SortableContext } from "@dnd-kit/sortable";
import { useEffect, useState } from "react";
import { useFormState } from "../../../context/FormStateContext.mjs";
import { useI18n } from "../../../context/I18nContext.mjs";
import Button_mjs_default from "../../radix/Button.mjs";
import external_ScalarArrayFieldItem_mjs_default from "./ScalarArrayFieldItem.mjs";
const ScalarArrayField = ({ formData, onChange, name, disabled, schema, customInput })=>{
const { t } = useI18n();
const { setFieldDirty } = useFormState();
const [formDataList, setFormDataList] = useState(()=>formData.map((value)=>({
id: crypto.randomUUID(),
value
})));
const onDragEnd = (event)=>{
const { active, over } = event;
if (active.id !== over?.id) {
const activeIndex = formDataList.findIndex((value)=>value.id === active.id);
const overIndex = formDataList.findIndex((value)=>value.id === over?.id);
const newFormData = [
...formDataList
];
newFormData.splice(overIndex, 0, newFormData.splice(activeIndex, 1)[0]);
setFieldDirty(name);
onChange(newFormData.map((val)=>val.value));
setFormDataList(newFormData);
}
};
const renderList = ()=>formDataList.map((value)=>/*#__PURE__*/ jsx(external_ScalarArrayFieldItem_mjs_default, {
value: value.value.toString(),
onRemoveClick: ()=>{
setFormDataList((prev)=>prev.filter((val)=>val.id !== value.id));
setFieldDirty(name);
},
inputValue: value.value.toString(),
disabled: disabled,
onChange: (newValue)=>{
setFormDataList((prev)=>prev.map((val)=>val.id === value.id ? {
...val,
value: newValue
} : val));
setFieldDirty(name);
},
id: value.id,
scalarType: schema.items.type,
customInput: customInput
}, value.id));
const onAddNewItem = ()=>{
setFormDataList((prev)=>{
if (schema.maxItems && prev.length >= schema.maxItems) return prev;
return [
...prev,
{
id: crypto.randomUUID(),
value: ""
}
];
});
setFieldDirty(name);
};
useEffect(()=>{
onChange(formDataList.map((val)=>val.value));
}, [
formDataList
]);
return /*#__PURE__*/ jsxs("div", {
className: "relative flex flex-col gap-3",
children: [
/*#__PURE__*/ jsx("input", {
type: "hidden",
name: name,
value: JSON.stringify(formDataList.map((val)=>val.value))
}),
/*#__PURE__*/ jsx(DndContext, {
onDragEnd: onDragEnd,
children: /*#__PURE__*/ jsx(SortableContext, {
items: formDataList.map((val)=>val.id) ?? [],
children: renderList()
})
}),
/*#__PURE__*/ jsx(Button_mjs_default, {
type: "button",
className: "w-fit",
disabled: disabled || (schema.maxItems ? formDataList.length >= schema.maxItems : false),
onClick: onAddNewItem,
children: t("form.widgets.scalar_array.add")
})
]
});
};
const ScalarArray_ScalarArrayField = ScalarArrayField;
export { ScalarArray_ScalarArrayField as default };