Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

NewColumn.tsx

70 lines1.9 KB

1import { useRef, useState } from 'react'
2import invariant from 'tiny-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 boardId,
32 name: inputRef.current.value,
33 })
34​
35 inputRef.current.value = ''
36​
37 onNewColumnAdded()
38 }}
39 onBlur={(event) => {
40 if (!event.currentTarget.contains(event.relatedTarget)) {
41 setEditing(false)
42 }
43 }}
44 >
45 <input
46 autoFocus
47 required
48 ref={inputRef}
49 type="text"
50 name="columnName"
51 autoComplete="off"
52 className="border border-slate-400 w-full rounded-lg py-1 px-2 font-medium text-black"
53 />
54 <div className="flex justify-between">
55 <SaveButton>Save Column</SaveButton>
56 <CancelButton onClick={() => setEditing(false)}>Cancel</CancelButton>
57 </div>
58 </form>
59 ) : (
60 <button
61 onClick={() => {
62 setEditing(true)
63 }}
64 aria-label="Add new column"
65 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"
66 >
67 <Icon name="plus" size="xl" />
68 </button>
69 )