# カレンダーの予定作成機能2

次に予定作成するために、入力フォームを作成していきます。フォームの入力をstateにて管理するため、`newSchedule`というstateを定義しましょう。その前にNewScheduleという型を定義しましょう。

`types/calendar.ts`に定義しましょう。

<pre class="language-typescript" data-title="types/calendar.ts◎"><code class="lang-typescript">export type Schedule = {
  id: number;
  date: Date;
  title: string;
  description: string;
}

export type DateList = {
  date: Date;
  schedules: Schedule[]
}[][]

<strong>export type NewSchedule = {
</strong><strong>  title: string;
</strong><strong>  date: string;
</strong><strong>  description: string;
</strong><strong>};
</strong></code></pre>

では`newSchedule`というstateを定義します。そして`newSchedule`を変更する関数を定義します。

newScheduleに関しては、`2024-01-01`という形式の文字列で扱いたいため、

（`<input type="date`"で取り扱うのに、`2024-01-01`という形式でないと扱えないため）

`format`という関数を用いて、第一引数のDateオブジェクトを、第二引数の形式の文字列に変換することができます。

<pre class="language-tsx" data-title="organisms/CreateScheduleModal.tsx◎"><code class="lang-tsx">import Modal from "react-modal";
import { ChangeEvent, useState } from "react";
import { format } from "date-fns";
import { Input } from "../atoms/Input";
import { PrimaryBtn } from "../atoms/PrimaryBtn";
import { NewSchedule } from "../../types/calendar";

type PropsType = {
  isOpen: boolean;
  closeModal: () => void;
};

const customStyles = {
  content: {
    top: "50%",
    left: "50%",
    width: "30%",
    height: "50vh",
    transform: "translate(-50%, -50%)",
  },
};

export const CreateScheduleModal = ({
  isOpen,
  closeModal,
}: PropsType) => {
<strong>  const [newSchedule, setNewSchedule] = useState&#x3C;NewSchedule>({
</strong><strong>    title: "",
</strong><strong>    date: format(new Date(), "yyyy-MM-dd"),
</strong><strong>    description: "",
</strong><strong>  });
</strong><strong>  
</strong><strong>  const changeNewSchedule = (
</strong><strong>    event: ChangeEvent&#x3C;HTMLInputElement | HTMLTextAreaElement>
</strong><strong>  ) => {
</strong><strong>    const { name, value } = event.target;
</strong><strong>    setNewSchedule({ ...newSchedule, [name]: value });
</strong><strong>  }
</strong>...
</code></pre>

次にform部分を作成します。

<pre class="language-tsx" data-title="organisms/CreateScheduleModal.tsx◎"><code class="lang-tsx">...
  return (
    &#x3C;Modal isOpen={isOpen} style={customStyles} onRequestClose={closeModal}>
      &#x3C;div>
        &#x3C;h3 className="text-center text-3xl text-lime-800 font-bold pb-5">
          予定作成
        &#x3C;/h3>
<strong>        &#x3C;form className="flex flex-col gap-8">
</strong><strong>          &#x3C;div className="w-[100%] flex items-center">
</strong><strong>            &#x3C;label htmlFor="title-form" className="w-[30%] text-lime-800">タイトル&#x3C;/label>
</strong><strong>            &#x3C;Input
</strong><strong>              id="title-form"
</strong><strong>              name="title"
</strong><strong>              type="text"
</strong><strong>              value={newSchedule.title}
</strong><strong>              onChange={changeNewSchedule}
</strong><strong>            />
</strong><strong>          &#x3C;/div>
</strong><strong>          &#x3C;div className="w-[100%] flex items-center">
</strong><strong>            &#x3C;label htmlFor="date-form" className="w-[30%] text-lime-800">日付&#x3C;/label>
</strong><strong>            &#x3C;Input
</strong><strong>              id="date-form"
</strong><strong>              name="date"
</strong><strong>              type="date"
</strong><strong>              value={newSchedule.date}
</strong><strong>              onChange={changeNewSchedule}
</strong><strong>            />
</strong><strong>          &#x3C;/div>
</strong><strong>          &#x3C;div className="w-[100%] flex items-center">
</strong><strong>            &#x3C;label htmlFor="description-form" className="w-[30%] text-lime-800">内容&#x3C;/label>
</strong><strong>            &#x3C;textarea
</strong><strong>              id="description-form"
</strong><strong>              name="description"
</strong><strong>              value={newSchedule.description}
</strong><strong>              onChange={changeNewSchedule}
</strong><strong>              className="w-full border-4 border-solid border-lime-800 rounded-md p-2"
</strong><strong>            />
</strong><strong>          &#x3C;/div>
</strong><strong>          &#x3C;div className="flex justify-center">
</strong><strong>            &#x3C;PrimaryBtn size="lg" onClick={() => null}>
</strong><strong>              作成
</strong><strong>            &#x3C;/PrimaryBtn>
</strong><strong>          &#x3C;/div>
</strong><strong>        &#x3C;/form>
</strong>      &#x3C;/div>
    &#x3C;/Modal>
  )
...
</code></pre>

`nesSchedule`の`description`に関しては、`textarea`にて長文を入力できるようにします。`textarea`をコンポーネントに切り出しましょう。`atoms`配下に`Textarea.tsx`を作成します。

こちらはpropsとして、textareaタグの属性全てを受け取れるようにするため、`ComponentProps<"textarea">`とします。

<pre class="language-tsx" data-title="atoms/Textarea.tsx◎"><code class="lang-tsx"><strong>import { ComponentProps } from "react";
</strong><strong>
</strong><strong>export const Textarea = (props: ComponentProps&#x3C;"textarea">) => {
</strong><strong>  return (
</strong><strong>    &#x3C;textarea
</strong><strong>      {...props}
</strong><strong>      className="w-full border-4 border-solid border-lime-800 rounded-md p-2"
</strong><strong>    />
</strong><strong>  );
</strong><strong>};
</strong></code></pre>

こちらを`CreateScheduleModal.tsx`にて呼び出しましょう。

<pre class="language-tsx" data-title="organisms/CreateScheduleModal.tsx◎"><code class="lang-tsx">...
          &#x3C;div className="w-[100%] flex items-center">
            &#x3C;label className="w-[30%] text-lime-800">内容&#x3C;/label>
<strong>            &#x3C;Textarea
</strong><strong>              name="description"
</strong><strong>              value={newSchedule.description}
</strong><strong>              onChange={changeNewSchedule}
</strong><strong>            />
</strong>          &#x3C;/div>
...
</code></pre>

上記にて、以下のような画面が表示され、`newSchedule`を変更することができるかと思います。

<figure><img src="https://1869761657-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcUBbYqol4PMzZJggiMqV%2Fuploads%2F9DEbJ2tYvBlwAB01qx5z%2F7a.png?alt=media&#x26;token=e956d0f5-7eee-4afd-bcf0-b625aa9a16a0" alt=""><figcaption></figcaption></figure>

ここまでの作業をcommitしましょう。

```bash
git add .
```

```bash
git commit
```

commitメッセージは

```
feat: カレンダー作成モーダルのフォーム部分を作成
```

としましょう。
