subman-nextjs/src/app/ui/inputs/textInput.tsx

61 lines
1.8 KiB
TypeScript
Raw Normal View History

2024-06-30 15:36:44 +00:00
import { Cell, CellContext, Table } from "@tanstack/react-table"
2024-06-29 15:21:56 +00:00
import { useState, useEffect } from "react"
import { Input } from "@/components/ui/input"
import { Button } from "@/components/ui/button"
import { Check, CircleX } from "lucide-react"
2024-06-30 15:36:44 +00:00
import { updateTextField } from "app/lib/update"
2024-06-29 15:21:56 +00:00
2024-06-30 15:36:44 +00:00
export const TextInputCell = (props: CellContext<any, any>) => {
let initialValue = props.getValue()
const [value, setValue] = useState(initialValue)
2024-06-29 15:21:56 +00:00
const [isActive, setIsActive] = useState(false)
2024-06-30 15:36:44 +00:00
const table = props.table.options.meta.tableName
const id = props.row.original.id
const column = props.column.id
const pathname = props.table.options.meta.pathname
function handleConfirm() {
updateTextField({
id,
table,
text: value,
column,
pathname
})
initialValue = value
handleClose()
}
function handleClose() {
setValue(initialValue)
setIsActive(false)
}
function handleKeyDown(event: React.KeyboardEvent<HTMLInputElement>) {
if (event.code === "Enter") {
handleConfirm()
}
}
2024-06-29 15:21:56 +00:00
return (<div
onDoubleClick={() => setIsActive(prev => !prev)}
2024-06-30 15:36:44 +00:00
className="w-full h-fit flex items-center justify-center"
2024-06-29 15:21:56 +00:00
>
{isActive ?
<div className="flex flex-col">
<Input
value={value}
2024-06-30 15:36:44 +00:00
onChange={e => setValue(e.target.value)} // onBlur={handleClose}
2024-06-29 15:21:56 +00:00
autoFocus={true}
2024-06-30 15:36:44 +00:00
onKeyDown={handleKeyDown}
2024-06-29 15:21:56 +00:00
/>
<div className="flex flex-row justify-end gap-1 w-full pt-2">
2024-06-30 15:36:44 +00:00
<Button variant="outline" className="p-2 h-fit" onClick={handleConfirm}><Check size="1rem" /></Button>
<Button variant="outline" className="p-2 h-fit" onClick={handleClose}><CircleX size="1rem" /></Button>
2024-06-29 15:21:56 +00:00
</div>
</div>
: <p>{value}</p>
}
</div>)
}