0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

react-hook-form

Posted at
import React from 'react';
import { useForm, Controller } from 'react-hook-form';
import { DatePicker, Stack, PrimaryButton, TextField } from '@fluentui/react';

type FormData = {
  datePicker1: Date | null;
  textField1: string;
  textField2: string;
};

function App() {
  const { control, handleSubmit } = useForm<FormData>();

  const onSubmit = (data: FormData) => {
    console.log('Form data:', data);
  };

  return (
    <div className="App">
      <h1>Fluent UI Date Picker and TextFields Form</h1>
      <form onSubmit={handleSubmit(onSubmit)}>
        <Stack tokens={{ childrenGap: 15 }}>
          <Controller
            name="datePicker1"
            control={control}
            defaultValue={null}
            render={({ field }) => (
              <DatePicker
                label="Select a date"
                value={field.value}
                onSelectDate={(date) => field.onChange(date)}
              />
            )}
          />
          <Controller
            name="textField1"
            control={control}
            defaultValue=""
            render={({ field }) => (
              <TextField
                label="Text Field 1"
                value={field.value}
                onChange={(_, value) => field.onChange(value)}
              />
            )}
          />
          <Controller
            name="textField2"
            control={control}
            defaultValue=""
            render={({ field }) => (
              <TextField
                label="Text Field 2"
                value={field.value}
                onChange={(_, value) => field.onChange(value)}
              />
            )}
          />
          <PrimaryButton text="Log to Console" type="submit" />
        </Stack>
      </form>
    </div>
  );
}

export default App;

このコードは、TypeScriptで書かれており、FormData型を使用してフォームデータの型を定義しています。useForm()を使用してフォームを設定し、Controllerコンポーネントを使ってフォームの各フィールドを制御しています。データを送信する際には、onSubmitハンドラでフォームデータをログに出力しています。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?