// packages/core/src/components/form/form-submit.tsx
import type { JSX } from "solid-js";
import { splitProps } from "solid-js";
import { useFormContext } from "./form-context";
import type { FormSubmitProps } from "./form.props";

export type { FormSubmitProps };

/**
 * Renders a <button type="submit"> that is automatically disabled when the
 * form's disabled state is true.
 */
export function FormSubmit(props: FormSubmitProps): JSX.Element {
  const [local, rest] = splitProps(props, ["children", "disabled"]);
  const formCtx = useFormContext();

  return (
    <button
      type="submit"
      disabled={local.disabled ?? formCtx.disabled()}
      {...rest}
    >
      {local.children}
    </button>
  );
}
