1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【React】MantineのSwitchコンポーネントでformの値を反映させる

Posted at

概要

UIフレームワークのMantineではuse-formのドキュメントにある通り、フォームの値を管理する機能もあります。ただ、Switchコンポーネントにuse-formのドキュメント通りに適用しても、デフォルト値をtrueに設定しても表示に反映されない動作でした(2025年4月時点だと)。
ので、formの設定内容をSwitchコンポーネントにどうやって適用させるのかという対応のメモ書きです。

前提

  • 使用したManitneのバージョンは7.17.2です。

対応

以下の実装の通り、getInputPropsに{ type: 'checkbox' }を設定することで反映するようになりました。

export type TaskInputFormValues = {
  displayFlag: boolean;
};

export const TaskInputComponent: FC<Props> = ({ submit }) => {
  const form = useForm<TaskInputFormValues>({
    mode: "uncontrolled",
    initialValues: {
      displayFlag: true,
    },
  });

  return (
    <form onSubmit={form.onSubmit((values) => submit(values))}>
      <Switch
        key={form.key("displayFlag")}
        {...form.getInputProps("displayFlag", { type: "checkbox" })} // { type: "checkbox" }を設定することでform値を表示に反映
        label="表示フラグ"
      />
    </form>
  );
};
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?