import React, { PropsWithChildren, ReactElement, ReactNode, useCallback } from "react";
import {
ColorInput,
FormLayout,
InputNumbers,
Label,
type LayoutType,
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
Slider,
Switch,
} from "../../index";
import { useDialSchema } from "../DialProvider";
import { IconRenderer } from "../IconRenderer";
import type { LabelPositionT } from "../types";
// Helper component for wrapping inputs with labels
const DialInputWrapper: React.FC<{
label?: string;
labelPosition?: LabelPositionT;
icon?: string;
children: ReactNode;
}> = ({ label, labelPosition = "top", icon, children }) => {
if (!label) {
return <>{children}>;
}
return (
{children as ReactElement}
);
};
export interface DialNumInputProps extends PropsWithChildren {
name: string;
type?: string;
min?: number;
max?: number;
step?: number;
options?: unknown[];
label?: string;
labelPosition?: LabelPositionT;
size?: "sm" | "md" | "lg";
icon?: string;
}
// Base input component that connects to the dial store using InputNumbers for drag support
export const DialNumberInput: React.FC = ({
name,
min,
max,
step = 1,
label,
labelPosition = "top",
size = "sm",
icon,
...props
}) => {
const { getValue, setValue } = useDialSchema();
const value = (getValue(name) ?? 0) as number;
const handleValuesChange = useCallback(
(values: number[]) => {
const newValue = values[0];
if (!isNaN(newValue)) {
setValue(name, newValue);
}
},
[name, setValue],
);
const inputNumbersProps = {
value: [value],
onValuesChange: handleValuesChange,
min,
max,
step,
size,
prefix: labelPosition === "inline" && label ? [label] : undefined,
};
if (labelPosition === "inline") {
return ;
} else {
return (
);
}
};
export const DialBooleanInput: React.FC = ({
name,
label,
labelPosition,
icon,
...props
}) => {
const { getValue, setValue } = useDialSchema();
const value = (getValue(name) ?? false) as boolean;
const handleChange = useCallback((checked: boolean) => setValue(name, checked), [name, setValue]);
const switchProps = {
checked: value,
onCheckedChange: handleChange,
};
if (!label || labelPosition === "inline") {
return ;
}
return (
);
};
// Dial-wrapped ColorInput
export const DialColorInput: React.FC = ({ name, label, icon, ...props }) => {
const { getValue, setValue } = useDialSchema();
// Value can be string or number (0xffffff format)
const value = getValue(name) as string | number | undefined;
const handleValueChange = useCallback(
(hexColor: string) => setValue(name, hexColor),
[name, setValue],
);
const inputComp = (
);
return (
{inputComp}
);
};
export const DialSelectInput: React.FC = ({
name,
options = [],
label,
size = "sm",
icon,
...props
}) => {
const { getValue, setValue } = useDialSchema();
const value = getValue(name);
const handleValueChange = useCallback(
(val: string) => {
// Try to parse as number if possible
const parsed = !isNaN(Number(val)) ? Number(val) : val;
setValue(name, parsed);
},
[name, setValue],
);
const selectComponent = (
);
return (
{selectComponent}
);
};
export const DialSliderInput: React.FC = ({
name,
min = 0,
max = 100,
step = 1,
label,
icon,
...props
}) => {
const { getValue, setValue } = useDialSchema();
const value = (getValue(name) ?? min) as number;
const handleValueChange = useCallback(([val]: number[]) => setValue(name, val), [name, setValue]);
const sliderProps = {
value: [value],
onValueChange: handleValueChange,
min,
max,
step,
className: "flex-1 pl-1.5",
};
return (
{value}
);
};