All files / src/components/color/hex HexColorForm.tsx

0% Statements 0/58
100% Branches 1/1
100% Functions 1/1
0% Lines 0/58

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135                                                                                                                                                                                                                                                                             
import { Dispatch, FC } from 'react';
import { Status } from '../../../models/status';
import { ErrorResponse } from '../../../models/errorResponse';
import { OkResponse } from '../../../models/okResponse';
import { Pending } from '../../../models/pending';
import { Controller, SubmitHandler, useForm } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
import { hexColorSchema, HexColorSchemaType } from '../../../validations/hexColor';
import { useFetchUpdateColor } from '../../../hooks/useFetchUpdateColor';
import { MuiColorInput } from 'mui-color-input';
import { ErrorMessage } from '@hookform/error-message';
import styled from 'styled-components';
 
type Props = {
  id: number;
  hex_color_code: string;
  status: Status;
  setResult: Dispatch<React.SetStateAction<ErrorResponse | OkResponse | Pending | null>>;
};
 
const StyledLabel = styled.label`
  display: block;
  font-size: 1.6rem;
  margin: 20px 0 5px 0;
  padding: 0;
`;
 
const StyledMuiColorInput = styled(MuiColorInput)`
  display: block;
  border: none;
  margin: 0;
  padding: 0;
  width: 400px;
  & .MuiInputBase-input {
    height: 51px;
    margin: 0;
    padding: 0;
  }
  // 参考: https://muhimasri.com/blogs/mui-textfield-colors-styles/
  // Root class for the input field
  & .MuiOutlinedInput-root {
    color: #000000;
    font-size: 1.6rem;
    font-family: 'Noto Serif JP', serif;
    border: 1.5px solid #6f6f6f;
    border-radius: 0;
    // Class for the border around the input field
    & .MuiOutlinedInput-notchedOutline {
      border-radius: 0;
      border: none;
    }
    // Class for the input field when it is focused
    &.Mui-focused {
      outline: 2.5px solid #c7d01c;
    }
  }
`;
 
const StyledSubmitWrapper = styled.div`
  display: flex;
  justify-content: center;
  width: 100%;
  margin: 50px 0 10px 0;
`;
 
const StyledSubmitInput = styled.input`
  padding: 5px 20px;
  background-color: #caad63;
  border: none;
  font-size: 1.6rem;
  cursor: pointer;
`;
 
const StyledTitle = styled.h1`
  font-size: 2.6rem;
  font-weight: 400;
  text-align: center;
`;
 
const StyledBox = styled.div`
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
`;
 
const StatusColorForm: FC<Props> = (props) => {
  const {
    handleSubmit,
    formState: { errors },
    control,
  } = useForm<HexColorSchemaType>({
    resolver: zodResolver(hexColorSchema),
    defaultValues: {
      hex_color_code: props.hex_color_code,
    },
  });
  const onSubmit: SubmitHandler<HexColorSchemaType> = async (formData) => {
    props.setResult('pending');
    console.log('props.id:', props.id);
    console.log('formData.hex_color_code:', formData.hex_color_code);
    console.log('props.status:', props.status);
    const result: OkResponse | ErrorResponse = await useFetchUpdateColor(
      props.id,
      formData.hex_color_code,
      props.status,
      'hexColorCode'
    );
    props.setResult(result);
  };
 
  return (
    <StyledBox>
      <StyledTitle>色の変更</StyledTitle>
      <form onSubmit={handleSubmit(onSubmit)}>
        <StyledLabel htmlFor="hex_color_code">Hex Color Code</StyledLabel>
        <Controller
          name="hex_color_code"
          control={control}
          render={({ field }) => <StyledMuiColorInput {...field} style={{}} format="hex" isAlphaHidden={true} />}
        />
        <br />
        <ErrorMessage errors={errors} name="hex_color_code" message={errors.hex_color_code?.message} />
        <br />
        <StyledSubmitWrapper>
          <StyledSubmitInput type="submit" value="色の変更" />
        </StyledSubmitWrapper>
      </form>
    </StyledBox>
  );
};
 
export default StatusColorForm;