-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAddBoard.tsx
123 lines (114 loc) · 3.41 KB
/
AddBoard.tsx
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
import React, { useEffect, useState } from "react";
import styled from "styled-components";
import { useForm } from "react-hook-form";
import { useRecoilValue, useSetRecoilState } from "recoil";
import { boardColorList, taskState } from "../models/atoms";
import { Draggable } from "react-beautiful-dnd";
import { loadColorCount, saveColorCount } from "../models/localStorage";
interface INewBoardForm {
boardId: string;
}
const Wrapper = styled.div`
width: 400px;
margin-left: 100px;
`;
const NewBoardForm = styled.form`
width: 100%;
`;
const NewBoardInput = styled.input<{ boardColor: string; disabled: boolean }>`
width: 122px;
padding: 2px 4px;
margin-left: 10px;
//style
border-style: ${(props) => (props.disabled ? "none" : "inherit")};
border-radius: 5px;
background-color: ${(props) =>
props.disabled
? (props) => props.theme.bgColor
: (props) => props.boardColor};
//font
font-size: 13px;
text-align: center;
line-height: 2;
color: ${(props) =>
props.disabled
? (props) => props.theme.textBlurColor
: (props) => props.theme.textColor};
&:focus {
outline: none;
&::placeholder {
color: transparent;
}
}
&::placeholder {
color: inherit;
}
`;
const ErrorMessage = styled.div`
padding: 30px 10px 10px 10px;
text-align: left;
color: ${(props) => props.theme.textBlurColor};
`;
const LimitMessage = styled.div<{ disabled: boolean }>`
display: ${(props) => (props.disabled ? "block" : "none")};
min-height: 400px;
color: transparent;
&:hover {
color: ${(props) => props.theme.textBlurColor};
}
`;
function AddBoard({ index }: { index: number }) {
const setTasks = useSetRecoilState(taskState);
const colors = useRecoilValue(boardColorList);
const [disabled, setDisabled] = useState(false);
const [colorCount, setColorCount] = useState(loadColorCount);
const nextColor = colors[colorCount % colors.length];
const {
register,
handleSubmit,
setValue,
formState: { errors },
} = useForm<INewBoardForm>();
const onValid = ({ boardId }: INewBoardForm) => {
setTasks((allTasks) => ({
// Board ID structure.
...allTasks,
[boardId + "-" + nextColor + "-" + Date.now()]: [],
}));
setColorCount(colorCount + 1);
//Save Count in localStorage
saveColorCount({ count: colorCount });
setValue("boardId", "");
};
useEffect(() => {
index > 4 ? setDisabled(true) : setDisabled(false);
}, [index]);
return (
<Draggable draggableId={"addBoard"} index={index}>
{({ innerRef, draggableProps, dragHandleProps }) => (
<Wrapper ref={innerRef} {...draggableProps} {...dragHandleProps}>
<LimitMessage disabled={disabled}>
👻 LIMITED TO 5 BOARDS 👻
</LimitMessage>
<NewBoardForm onSubmit={handleSubmit(onValid)}>
<NewBoardInput
disabled={disabled}
boardColor={nextColor}
autoComplete={"off"}
{...register("boardId", {
required: true,
maxLength: {
value: 12,
message: "Board name must be less than 12 characters",
},
})}
placeholder={disabled ? "" : "+ New Board"}
/>
<ErrorMessage>{errors?.boardId?.message}</ErrorMessage>
</NewBoardForm>
</Wrapper>
)}
</Draggable>
);
}
export default React.memo(AddBoard);