Open History to see the last commit
1import { useRef, useState } from 'react'
2import { invariant } from '../invariant'
3β
4import { Icon } from '../icons/icons'
5import { useCreateColumnMutation } from '../queries'
6import { CancelButton } from '~/components/CancelButton'
7import { SaveButton } from '~/components/SaveButton'
8β
9export function NewColumn({
10 boardId,
11 editInitially,
12 onNewColumnAdded,
13}: {
14 boardId: string
15 editInitially: boolean
16 onNewColumnAdded: () => void
17}) {
18 const [editing, setEditing] = useState(editInitially)
19 const inputRef = useRef<HTMLInputElement>(null)
20β
21 const newColumnMutation = useCreateColumnMutation()
22β
23 return editing ? (
24 <form
25 className="ml-2 p-2 shrink-0 flex flex-col gap-5 overflow-hidden max-h-full w-80 border rounded-xl shadow-sm bg-slate-100"
26 onSubmit={(event) => {
27 event.preventDefault()
28 invariant(inputRef.current, 'missing input ref')
29β
30 newColumnMutation.mutate({
31 data: {
32 boardId,
33 name: inputRef.current.value,
34 },
35 })
36β
37 inputRef.current.value = ''
38β
39 onNewColumnAdded()
40 }}
41 onBlur={(event) => {
42 if (!event.currentTarget.contains(event.relatedTarget)) {
43 setEditing(false)
44 }
45 }}
46 >
47 <input
48 autoFocus
49 required
50 ref={inputRef}
51 type="text"
52 name="columnName"
53 autoComplete="off"
54 className="border border-slate-400 w-full rounded-lg py-1 px-2 font-medium text-black"
55 />
56 <div className="flex justify-between">
57 <SaveButton>Save Column</SaveButton>
58 <CancelButton onClick={() => setEditing(false)}>Cancel</CancelButton>
59 </div>
60 </form>
61 ) : (
62 <button
63 onClick={() => {
64 setEditing(true)
65 }}
66 aria-label="Add new column"
67 className="ml-2 shrink-0 flex justify-center h-16 w-16 bg-black hover:bg-white bg-opacity-10 hover:bg-opacity-5 rounded-xl"
68 >
69 <Icon name="plus" size="xl" />
70 </button>
71 )
72}