AFileForm.tsx 911 Bytes
Newer Older
1
import React, { useState } from "react";
2
import { FileType, AnswersType, AnswerProps } from "../types";
3

4
interface Props extends AnswerProps {
5
6
  element: FileType;
  addFiles: (oneFile: { questionId: string; file: File }) => void;
7
}
8
9
10

export const AFileForm = ({
  element,
11
  answers,
12
13
14
15
16
17
18
  handleAnswer,
  addFiles,
}: Props) => {
  const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
    if (event.currentTarget.files) {
      const uploadFile = event.currentTarget.files[0];
      addFiles({ questionId: element._id, file: uploadFile });
Yoon, Daeki's avatar
Yoon, Daeki committed
19
      answers && (answers.answer = uploadFile.name);
20
21
22
23
24
25
26
27
28
29
      handleAnswer();
    }
  };
  return (
    <div id="content" className="flex mt-4 w-full justify-center">
      <input
        type="file"
        name="file"
        className=" w-11/12 h-16"
        onChange={handleChange}
Jiwon Yoon's avatar
Jiwon Yoon committed
30
        required={element.isRequired}
31
32
33
34
      ></input>
    </div>
  );
};