subman-nextjs/src/app/ui/tables/inputs/numberInput.tsx

111 lines
3.2 KiB
TypeScript
Raw Normal View History

"use client"
2024-06-30 21:22:46 +00:00
import { Input } from "@/components/ui/input";
import { CellContext } from "@tanstack/react-table";
import { updateField } from "app/lib/update";
import { useState } from "react";
import { TableInputContainer } from "./inputContainer";
import { z } from "zod";
import { useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import { toast } from "@/components/ui/use-toast";
2024-07-02 14:59:55 +00:00
import { Form, FormControl, FormField, FormItem, FormMessage } from "@/components/ui/form";
import { Button } from "@/components/ui/button";
2024-07-02 14:59:55 +00:00
import { useFormState } from "react-dom";
2024-06-30 21:22:46 +00:00
export default function NumberInputCell(props: CellContext<any, any>) {
const [isActive, setIsActive] = useState(false)
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
2024-07-02 14:59:55 +00:00
const value = props.cell.getValue()
const formSchema = props.column.columnDef.meta.formSchema.pick({ [column]: true })
const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
defaultValues: {
[column]: props.cell.getValue()
2024-07-02 14:59:55 +00:00
},
// mode: "onChange"
})
2024-07-02 14:59:55 +00:00
const isDirty = form.formState.isDirty
function onSubmit(value: z.infer<typeof formSchema>) {
toast({
title: "You submitted the following values:",
description: (
<pre className="mt-2 w-[340px] rounded-md bg-slate-950 p-4">
<code className="text-white">{JSON.stringify(value, null, 2)}</code>
</pre>
),
2024-06-30 21:22:46 +00:00
})
2024-07-02 14:59:55 +00:00
updateField({
id,
table,
number: value[column],
column,
pathname
})
setIsActive(false)
2024-06-30 21:22:46 +00:00
}
function onErrors(errors) {
toast({
title: "You have errors",
description: (
<pre className="mt-2 w-[340px] rounded-md bg-slate-950 p-4">
<code className="text-white">{JSON.stringify(errors, null, 2)}</code>
</pre>
),
})
console.log(JSON.stringify(errors))
}
2024-06-30 21:22:46 +00:00
return (
2024-07-02 14:59:55 +00:00
<div
onDoubleClick={() => setIsActive(prev => !prev)}
className="w-full h-fit flex items-center justify-center"
tabIndex={0}
onKeyDown={e => {
if (e.code === "Enter" && !isActive) {
setIsActive(true)
}
}}
>
2024-06-30 21:22:46 +00:00
{isActive ?
<Form {...form}>
2024-07-02 14:59:55 +00:00
<form onSubmit={e => {
e.preventDefault()
if (isDirty) {
console.log("SUBMIT")
form.handleSubmit(onSubmit, onErrors)
}
}} >
<FormField
control={form.control}
name={column}
render={({ field }) => (
2024-07-02 14:59:55 +00:00
<FormItem
onBlur={() => setIsActive(false)}
>
<FormControl
>
<Input
type="number"
autoFocus={true}
step={props.column.columnDef.meta.step}
{...field}
/>
</FormControl>
2024-07-02 14:59:55 +00:00
<FormMessage />
</FormItem>
)}
/>
</form>
</Form>
: <p>{props.cell.getValue()}</p>
2024-06-30 21:22:46 +00:00
}
2024-07-02 14:59:55 +00:00
</div >
2024-06-30 21:22:46 +00:00
)
}