import React from "react";
import { Slider, SliderProps, TextInput, TextInputProps } from "@mantine/core";
import {
  useController,
  FieldValues,
  UseControllerProps,
} from "react-hook-form";

type Props<T extends FieldValues = FieldValues> = SliderProps & {
  name: UseControllerProps<T>["name"];
  rules?: UseControllerProps<T>["rules"];
  defaultValue?: UseControllerProps<T>["defaultValue"];
};

function RHFSlider<T extends FieldValues = FieldValues>(props: Props<T>) {
  const { name, rules, defaultValue, ...others } = props;
  const {
    field,
    fieldState: { error },
  } = useController({ name, rules });

  return <Slider {...field} value={field.value ?? NaN} {...others} />;
}
export default RHFSlider;

export const createSliderField = <T extends FieldValues>() => {
  const Field = (props: Props<T>) => <RHFSlider {...props} />;
  return Field;
};
