shadcn-zod-formkit 1.11.0 → 1.12.0

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/index.cjs CHANGED
@@ -27,6 +27,9 @@ var sonner = require('sonner');
27
27
  var SwitchPrimitive = require('@radix-ui/react-switch');
28
28
  var TooltipPrimitive = require('@radix-ui/react-tooltip');
29
29
  var cmdk = require('cmdk');
30
+ var sortable = require('@dnd-kit/sortable');
31
+ var utilities = require('@dnd-kit/utilities');
32
+ var core = require('@dnd-kit/core');
30
33
  var SliderPrimitive = require('@radix-ui/react-slider');
31
34
  var z2 = require('zod');
32
35
  var zod = require('@hookform/resolvers/zod');
@@ -269,9 +272,11 @@ var InputTypes = /* @__PURE__ */ ((InputTypes2) => {
269
272
  InputTypes2["REPEATER"] = "repeater";
270
273
  InputTypes2["MULTI_SELECT"] = "multi_select";
271
274
  InputTypes2["COMBOBOX"] = "COMBO_BOX";
275
+ InputTypes2["SORTABLE_LIST"] = "sortable_list";
272
276
  return InputTypes2;
273
277
  })(InputTypes || {});
274
278
  var inputFieldComp = [
279
+ "sortable_list" /* SORTABLE_LIST */,
275
280
  "COMBO_BOX" /* COMBOBOX */,
276
281
  "multi_select" /* MULTI_SELECT */,
277
282
  "repeater" /* REPEATER */,
@@ -3992,6 +3997,105 @@ var FieldDateTimeInput = ({ form, input, isSubmitting }) => {
3992
3997
  input.name
3993
3998
  );
3994
3999
  };
4000
+ var SortableListInput = class extends BaseInput {
4001
+ render() {
4002
+ const { input, form, isSubmitting } = this;
4003
+ return /* @__PURE__ */ jsxRuntime.jsx(FieldSortableList, { form, input, isSubmitting });
4004
+ }
4005
+ };
4006
+ var FieldSortableList = ({ form, input, isSubmitting }) => {
4007
+ const [items, setItems] = React3.useState((input.listConfig?.list ?? []).filter((item) => "name" in item));
4008
+ const sensors = core.useSensors(
4009
+ core.useSensor(core.PointerSensor),
4010
+ core.useSensor(core.KeyboardSensor, {
4011
+ coordinateGetter: sortable.sortableKeyboardCoordinates
4012
+ })
4013
+ );
4014
+ const handleDragEnd = (event) => {
4015
+ const { active, over } = event;
4016
+ if (!over || active.id === over.id) return;
4017
+ const oldIndex = items.findIndex((i) => i.id === active.id);
4018
+ const newIndex = items.findIndex((i) => i.id === over.id);
4019
+ const newList = sortable.arrayMove(items, oldIndex, newIndex);
4020
+ setItems(newList);
4021
+ form.setValue(input.name, newList);
4022
+ if (input.listConfig?.onOptionChange) {
4023
+ input.listConfig.onOptionChange(newList);
4024
+ }
4025
+ };
4026
+ return /* @__PURE__ */ jsxRuntime.jsx(
4027
+ FormField,
4028
+ {
4029
+ control: form.control,
4030
+ name: input.name,
4031
+ render: () => /* @__PURE__ */ jsxRuntime.jsxs(FormItem, { className: cn("space-y-2", input.className), children: [
4032
+ /* @__PURE__ */ jsxRuntime.jsx(FormLabel, { children: /* @__PURE__ */ jsxRuntime.jsx("b", { children: input.label }) }),
4033
+ /* @__PURE__ */ jsxRuntime.jsx(FormControl, { children: /* @__PURE__ */ jsxRuntime.jsx("div", { className: "border rounded-md p-3 bg-white", children: /* @__PURE__ */ jsxRuntime.jsx(
4034
+ core.DndContext,
4035
+ {
4036
+ sensors,
4037
+ collisionDetection: core.closestCenter,
4038
+ onDragEnd: handleDragEnd,
4039
+ children: /* @__PURE__ */ jsxRuntime.jsx(
4040
+ sortable.SortableContext,
4041
+ {
4042
+ items: items.map((i) => i.id),
4043
+ strategy: sortable.verticalListSortingStrategy,
4044
+ children: items.map((item) => /* @__PURE__ */ jsxRuntime.jsx(
4045
+ SortableItem,
4046
+ {
4047
+ id: item.id,
4048
+ name: item.name,
4049
+ disabled: isSubmitting
4050
+ },
4051
+ item.id
4052
+ ))
4053
+ }
4054
+ )
4055
+ }
4056
+ ) }) }),
4057
+ input.description && /* @__PURE__ */ jsxRuntime.jsx(FormDescription, { children: input.description }),
4058
+ /* @__PURE__ */ jsxRuntime.jsx(FormMessage, {})
4059
+ ] })
4060
+ },
4061
+ input.name
4062
+ );
4063
+ };
4064
+ function SortableItem({
4065
+ id,
4066
+ name,
4067
+ disabled
4068
+ }) {
4069
+ const {
4070
+ attributes,
4071
+ listeners,
4072
+ setNodeRef,
4073
+ transform,
4074
+ transition,
4075
+ isDragging
4076
+ } = sortable.useSortable({ id });
4077
+ const style = {
4078
+ transform: utilities.CSS.Transform.toString(transform),
4079
+ transition
4080
+ };
4081
+ return /* @__PURE__ */ jsxRuntime.jsx(
4082
+ "div",
4083
+ {
4084
+ ref: setNodeRef,
4085
+ style,
4086
+ ...attributes,
4087
+ ...listeners,
4088
+ className: cn(
4089
+ "flex items-center justify-between p-2 border rounded-md mb-1 bg-muted/30 cursor-grab select-none",
4090
+ isDragging && "opacity-60 bg-muted/50"
4091
+ ),
4092
+ children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-2", children: [
4093
+ /* @__PURE__ */ jsxRuntime.jsx(lucideReact.GripVertical, { className: "w-4 h-4 opacity-70" }),
4094
+ /* @__PURE__ */ jsxRuntime.jsx("span", { children: name })
4095
+ ] })
4096
+ }
4097
+ );
4098
+ }
3995
4099
  var FileInput = class extends BaseInput {
3996
4100
  render() {
3997
4101
  const { input, form, isSubmitting } = this;
@@ -5196,6 +5300,7 @@ var inputMap = {
5196
5300
  ["repeater" /* REPEATER */]: RepeaterInput,
5197
5301
  ["multi_select" /* MULTI_SELECT */]: MultiSelectInput,
5198
5302
  ["COMBO_BOX" /* COMBOBOX */]: ComboboxInput,
5303
+ ["sortable_list" /* SORTABLE_LIST */]: SortableListInput,
5199
5304
  //ToDos: ============================================================
5200
5305
  ["slider" /* SLIDER */]: SliderInput,
5201
5306
  //ToDo: // PENDIENTE ... VISUALMENTE NO SE VE BIEN.!!!
@@ -5346,7 +5451,6 @@ var DynamicForm = ({
5346
5451
  const schema = React3.useMemo(() => getDynamicSchema(fields, extraValidations), [fields, extraValidations]);
5347
5452
  const resolver = zod.zodResolver(schema);
5348
5453
  const initialValues = React3.useMemo(() => getDefaultValues(record), [record]);
5349
- console.log("initialValues", initialValues);
5350
5454
  const form = reactHookForm.useForm({
5351
5455
  resolver,
5352
5456
  defaultValues: initialValues