shadcn-zod-formkit 1.12.0 → 1.13.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.d.mts CHANGED
@@ -124,12 +124,14 @@ interface inputGroudConfig {
124
124
  textRight?: string;
125
125
  }
126
126
  interface ListConfig {
127
+ children?: ReactNode | ((item: any, index: number) => ReactNode);
127
128
  list: InputOption[] | GroupedOption[];
128
129
  optionLabel?: string;
129
130
  optionValue?: InputOption | string | number | object;
130
131
  onOptionChange: (item?: InputOption | InputOption[] | GroupedOption) => void;
131
132
  optionDescription?: string;
132
133
  selectedList?: InputOption[];
134
+ sortable?: boolean;
133
135
  }
134
136
  interface InputOption {
135
137
  id: number | string;
@@ -171,11 +173,12 @@ declare enum TextInputType {
171
173
  PASSWORD = "password"
172
174
  }
173
175
 
174
- declare abstract class BaseInput {
176
+ declare abstract class BaseInput<T = unknown> {
175
177
  protected readonly input: FieldProps;
176
178
  protected readonly form: UseFormReturn;
177
179
  protected readonly isSubmitting?: boolean | undefined;
178
- constructor(input: FieldProps, form: UseFormReturn, isSubmitting?: boolean | undefined);
180
+ protected readonly children?: ReactNode | ((item: T, index: number) => ReactNode);
181
+ constructor(input: FieldProps, form: UseFormReturn, isSubmitting?: boolean | undefined, children?: ReactNode | ((item: T, index: number) => ReactNode));
179
182
  abstract render(): JSX.Element;
180
183
  }
181
184
  declare const entityToInputOption: (entitiy: any, name?: string, description?: string, groupedLabel?: string) => InputOption;
package/dist/index.d.ts CHANGED
@@ -124,12 +124,14 @@ interface inputGroudConfig {
124
124
  textRight?: string;
125
125
  }
126
126
  interface ListConfig {
127
+ children?: ReactNode | ((item: any, index: number) => ReactNode);
127
128
  list: InputOption[] | GroupedOption[];
128
129
  optionLabel?: string;
129
130
  optionValue?: InputOption | string | number | object;
130
131
  onOptionChange: (item?: InputOption | InputOption[] | GroupedOption) => void;
131
132
  optionDescription?: string;
132
133
  selectedList?: InputOption[];
134
+ sortable?: boolean;
133
135
  }
134
136
  interface InputOption {
135
137
  id: number | string;
@@ -171,11 +173,12 @@ declare enum TextInputType {
171
173
  PASSWORD = "password"
172
174
  }
173
175
 
174
- declare abstract class BaseInput {
176
+ declare abstract class BaseInput<T = unknown> {
175
177
  protected readonly input: FieldProps;
176
178
  protected readonly form: UseFormReturn;
177
179
  protected readonly isSubmitting?: boolean | undefined;
178
- constructor(input: FieldProps, form: UseFormReturn, isSubmitting?: boolean | undefined);
180
+ protected readonly children?: ReactNode | ((item: T, index: number) => ReactNode);
181
+ constructor(input: FieldProps, form: UseFormReturn, isSubmitting?: boolean | undefined, children?: ReactNode | ((item: T, index: number) => ReactNode));
179
182
  abstract render(): JSX.Element;
180
183
  }
181
184
  declare const entityToInputOption: (entitiy: any, name?: string, description?: string, groupedLabel?: string) => InputOption;
package/dist/index.mjs CHANGED
@@ -26,10 +26,10 @@ import { Toaster as Toaster$1 } from 'sonner';
26
26
  import * as SwitchPrimitive from '@radix-ui/react-switch';
27
27
  import * as TooltipPrimitive from '@radix-ui/react-tooltip';
28
28
  import { Command as Command$1 } from 'cmdk';
29
+ import * as SliderPrimitive from '@radix-ui/react-slider';
30
+ import { useSensors, useSensor, PointerSensor, KeyboardSensor, DndContext, closestCenter } from '@dnd-kit/core';
29
31
  import { sortableKeyboardCoordinates, SortableContext, verticalListSortingStrategy, arrayMove, useSortable } from '@dnd-kit/sortable';
30
32
  import { CSS } from '@dnd-kit/utilities';
31
- import { useSensors, useSensor, PointerSensor, KeyboardSensor, DndContext, closestCenter } from '@dnd-kit/core';
32
- import * as SliderPrimitive from '@radix-ui/react-slider';
33
33
  import z2 from 'zod';
34
34
  import { zodResolver } from '@hookform/resolvers/zod';
35
35
 
@@ -159,10 +159,11 @@ var validationMessages = {
159
159
 
160
160
  // src/components/custom/form/inputs/base/base-input.ts
161
161
  var BaseInput = class {
162
- constructor(input, form, isSubmitting) {
162
+ constructor(input, form, isSubmitting, children) {
163
163
  this.input = input;
164
164
  this.form = form;
165
165
  this.isSubmitting = isSubmitting;
166
+ this.children = children;
166
167
  }
167
168
  };
168
169
  var entityToInputOption = (entitiy, name = "name", description = "description", groupedLabel) => ({
@@ -3960,105 +3961,6 @@ var FieldDateTimeInput = ({ form, input, isSubmitting }) => {
3960
3961
  input.name
3961
3962
  );
3962
3963
  };
3963
- var SortableListInput = class extends BaseInput {
3964
- render() {
3965
- const { input, form, isSubmitting } = this;
3966
- return /* @__PURE__ */ jsx(FieldSortableList, { form, input, isSubmitting });
3967
- }
3968
- };
3969
- var FieldSortableList = ({ form, input, isSubmitting }) => {
3970
- const [items, setItems] = useState((input.listConfig?.list ?? []).filter((item) => "name" in item));
3971
- const sensors = useSensors(
3972
- useSensor(PointerSensor),
3973
- useSensor(KeyboardSensor, {
3974
- coordinateGetter: sortableKeyboardCoordinates
3975
- })
3976
- );
3977
- const handleDragEnd = (event) => {
3978
- const { active, over } = event;
3979
- if (!over || active.id === over.id) return;
3980
- const oldIndex = items.findIndex((i) => i.id === active.id);
3981
- const newIndex = items.findIndex((i) => i.id === over.id);
3982
- const newList = arrayMove(items, oldIndex, newIndex);
3983
- setItems(newList);
3984
- form.setValue(input.name, newList);
3985
- if (input.listConfig?.onOptionChange) {
3986
- input.listConfig.onOptionChange(newList);
3987
- }
3988
- };
3989
- return /* @__PURE__ */ jsx(
3990
- FormField,
3991
- {
3992
- control: form.control,
3993
- name: input.name,
3994
- render: () => /* @__PURE__ */ jsxs(FormItem, { className: cn("space-y-2", input.className), children: [
3995
- /* @__PURE__ */ jsx(FormLabel, { children: /* @__PURE__ */ jsx("b", { children: input.label }) }),
3996
- /* @__PURE__ */ jsx(FormControl, { children: /* @__PURE__ */ jsx("div", { className: "border rounded-md p-3 bg-white", children: /* @__PURE__ */ jsx(
3997
- DndContext,
3998
- {
3999
- sensors,
4000
- collisionDetection: closestCenter,
4001
- onDragEnd: handleDragEnd,
4002
- children: /* @__PURE__ */ jsx(
4003
- SortableContext,
4004
- {
4005
- items: items.map((i) => i.id),
4006
- strategy: verticalListSortingStrategy,
4007
- children: items.map((item) => /* @__PURE__ */ jsx(
4008
- SortableItem,
4009
- {
4010
- id: item.id,
4011
- name: item.name,
4012
- disabled: isSubmitting
4013
- },
4014
- item.id
4015
- ))
4016
- }
4017
- )
4018
- }
4019
- ) }) }),
4020
- input.description && /* @__PURE__ */ jsx(FormDescription, { children: input.description }),
4021
- /* @__PURE__ */ jsx(FormMessage, {})
4022
- ] })
4023
- },
4024
- input.name
4025
- );
4026
- };
4027
- function SortableItem({
4028
- id,
4029
- name,
4030
- disabled
4031
- }) {
4032
- const {
4033
- attributes,
4034
- listeners,
4035
- setNodeRef,
4036
- transform,
4037
- transition,
4038
- isDragging
4039
- } = useSortable({ id });
4040
- const style = {
4041
- transform: CSS.Transform.toString(transform),
4042
- transition
4043
- };
4044
- return /* @__PURE__ */ jsx(
4045
- "div",
4046
- {
4047
- ref: setNodeRef,
4048
- style,
4049
- ...attributes,
4050
- ...listeners,
4051
- className: cn(
4052
- "flex items-center justify-between p-2 border rounded-md mb-1 bg-muted/30 cursor-grab select-none",
4053
- isDragging && "opacity-60 bg-muted/50"
4054
- ),
4055
- children: /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2", children: [
4056
- /* @__PURE__ */ jsx(GripVertical, { className: "w-4 h-4 opacity-70" }),
4057
- /* @__PURE__ */ jsx("span", { children: name })
4058
- ] })
4059
- }
4060
- );
4061
- }
4062
3964
  var FileInput = class extends BaseInput {
4063
3965
  render() {
4064
3966
  const { input, form, isSubmitting } = this;
@@ -4969,6 +4871,103 @@ var FieldSlider = ({ input, form, isSubmitting }) => {
4969
4871
  input.name
4970
4872
  );
4971
4873
  };
4874
+ var SortableListInput = class extends BaseInput {
4875
+ render() {
4876
+ const { input, form, isSubmitting } = this;
4877
+ const children = input.listConfig?.children ?? void 0;
4878
+ return /* @__PURE__ */ jsx(
4879
+ FieldSortableList,
4880
+ {
4881
+ form,
4882
+ input,
4883
+ isSubmitting,
4884
+ children
4885
+ }
4886
+ );
4887
+ }
4888
+ };
4889
+ function FieldSortableList({
4890
+ form,
4891
+ input,
4892
+ isSubmitting,
4893
+ children
4894
+ }) {
4895
+ const [items, setItems] = useState(() => input.listConfig?.list ?? []);
4896
+ const sortableEnabled = input.listConfig?.sortable ?? true;
4897
+ useSensors(
4898
+ useSensor(PointerSensor),
4899
+ useSensor(KeyboardSensor, { coordinateGetter: sortableKeyboardCoordinates })
4900
+ );
4901
+ const handleDragEnd = (event) => {
4902
+ if (!sortableEnabled) return;
4903
+ const { active, over } = event;
4904
+ if (!over || active.id === over.id) return;
4905
+ const oldIndex = items.findIndex((i) => i.id === active.id);
4906
+ const newIndex = items.findIndex((i) => i.id === over.id);
4907
+ const newList = arrayMove(items, oldIndex, newIndex);
4908
+ setItems(newList);
4909
+ form.setValue(input.name, newList);
4910
+ input.listConfig?.onOptionChange?.(newList);
4911
+ };
4912
+ return /* @__PURE__ */ jsx(
4913
+ FormField,
4914
+ {
4915
+ control: form.control,
4916
+ name: input.name,
4917
+ render: () => /* @__PURE__ */ jsxs(FormItem, { className: cn("space-y-2", input.className), children: [
4918
+ input.label && /* @__PURE__ */ jsx(FormLabel, { children: /* @__PURE__ */ jsx("b", { children: input.label }) }),
4919
+ /* @__PURE__ */ jsx(FormControl, { children: sortableEnabled ? /* @__PURE__ */ jsx(DndContext, { collisionDetection: closestCenter, onDragEnd: handleDragEnd, children: /* @__PURE__ */ jsx(
4920
+ SortableContext,
4921
+ {
4922
+ items: items.map((i) => i.id),
4923
+ strategy: verticalListSortingStrategy,
4924
+ children: /* @__PURE__ */ jsx("div", { className: "flex flex-col gap-2", children: items.map((item, index) => /* @__PURE__ */ jsx(SortableWrapper, { id: item.id, children: typeof children === "function" ? children(item, index) : /* @__PURE__ */ jsx("div", { className: "p-3 border rounded-md bg-white", children: item.name }) }, item.id)) })
4925
+ }
4926
+ ) }) : /* @__PURE__ */ jsx("div", { className: "flex flex-col gap-2", children: items.map(
4927
+ (item, index) => typeof children === "function" ? children(item, index) : /* @__PURE__ */ jsx("div", { className: "p-3 border rounded-md bg-gray-50", children: item.name }, item.id)
4928
+ ) }) }),
4929
+ input.description && /* @__PURE__ */ jsx(FormDescription, { children: input.description }),
4930
+ /* @__PURE__ */ jsx(FormMessage, {})
4931
+ ] })
4932
+ },
4933
+ input.name
4934
+ );
4935
+ }
4936
+ function SortableWrapper({
4937
+ id,
4938
+ children,
4939
+ disabled
4940
+ }) {
4941
+ const {
4942
+ attributes,
4943
+ listeners,
4944
+ setNodeRef,
4945
+ transform,
4946
+ transition,
4947
+ isDragging
4948
+ } = useSortable({ id, disabled });
4949
+ const style = {
4950
+ transform: CSS.Transform.toString(transform),
4951
+ transition
4952
+ };
4953
+ return /* @__PURE__ */ jsxs(
4954
+ "div",
4955
+ {
4956
+ ref: setNodeRef,
4957
+ style,
4958
+ ...attributes,
4959
+ ...listeners,
4960
+ className: cn(
4961
+ "flex items-center gap-2 p-2 border rounded-md mb-1 bg-muted/30 cursor-grab select-none transition-all",
4962
+ isDragging && "opacity-60 bg-muted/50 scale-[0.98]"
4963
+ ),
4964
+ children: [
4965
+ /* @__PURE__ */ jsx(GripVertical, { className: "w-4 h-4 opacity-70" }),
4966
+ /* @__PURE__ */ jsx("div", { className: "flex-1", children })
4967
+ ]
4968
+ }
4969
+ );
4970
+ }
4972
4971
  var SwitchInput = class extends BaseInput {
4973
4972
  render() {
4974
4973
  const { input, form, isSubmitting } = this;