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

66 lines
1.6 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()
}
2024-06-30 18:15:30 +00:00
function handleOpen() {
setIsActive(true)
}
2024-06-30 15:36:44 +00:00
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-30 18:28:02 +00:00
tabIndex={0}
onKeyDown={e => {
if (e.code === "Enter" && !isActive) {
setIsActive(true)
}
}}
2024-06-29 15:21:56 +00:00
>
{isActive ?
2024-06-30 15:42:33 +00:00
<Input
value={value}
onChange={e => setValue(e.target.value)} // onBlur={handleClose}
autoFocus={true}
onBlur={handleClose}
onKeyDown={handleKeyDown}
className="w-full"
/>
2024-06-29 15:21:56 +00:00
: <p>{value}</p>
}
</div>)
}