fix enter key buy (partially)
This commit is contained in:
parent
23058ed48b
commit
380fc56d17
BIN
prisma/dev.db
BIN
prisma/dev.db
Binary file not shown.
|
@ -8,8 +8,9 @@ import { z } from "zod";
|
||||||
import { useForm } from "react-hook-form";
|
import { useForm } from "react-hook-form";
|
||||||
import { zodResolver } from "@hookform/resolvers/zod";
|
import { zodResolver } from "@hookform/resolvers/zod";
|
||||||
import { toast } from "@/components/ui/use-toast";
|
import { toast } from "@/components/ui/use-toast";
|
||||||
import { Form, FormControl, FormField, FormItem } from "@/components/ui/form";
|
import { Form, FormControl, FormField, FormItem, FormMessage } from "@/components/ui/form";
|
||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
|
import { useFormState } from "react-dom";
|
||||||
|
|
||||||
export default function NumberInputCell(props: CellContext<any, any>) {
|
export default function NumberInputCell(props: CellContext<any, any>) {
|
||||||
const [isActive, setIsActive] = useState(false)
|
const [isActive, setIsActive] = useState(false)
|
||||||
|
@ -18,18 +19,17 @@ export default function NumberInputCell(props: CellContext<any, any>) {
|
||||||
const id = props.row.original.id
|
const id = props.row.original.id
|
||||||
const column = props.column.id
|
const column = props.column.id
|
||||||
const pathname = props.table.options.meta.pathname
|
const pathname = props.table.options.meta.pathname
|
||||||
let formSchema = props.column.columnDef.meta.formSchema
|
const value = props.cell.getValue()
|
||||||
|
const formSchema = props.column.columnDef.meta.formSchema.pick({ [column]: true })
|
||||||
|
|
||||||
formSchema = formSchema.pick({ [column]: true })
|
|
||||||
|
|
||||||
const form = useForm<z.infer<typeof formSchema>>({
|
const form = useForm<z.infer<typeof formSchema>>({
|
||||||
resolver: zodResolver(formSchema),
|
resolver: zodResolver(formSchema),
|
||||||
defaultValues: {
|
defaultValues: {
|
||||||
[column]: props.cell.getValue()
|
[column]: props.cell.getValue()
|
||||||
}
|
},
|
||||||
|
// mode: "onChange"
|
||||||
})
|
})
|
||||||
|
const isDirty = form.formState.isDirty
|
||||||
|
|
||||||
function onSubmit(value: z.infer<typeof formSchema>) {
|
function onSubmit(value: z.infer<typeof formSchema>) {
|
||||||
toast({
|
toast({
|
||||||
|
@ -40,14 +40,14 @@ export default function NumberInputCell(props: CellContext<any, any>) {
|
||||||
</pre>
|
</pre>
|
||||||
),
|
),
|
||||||
})
|
})
|
||||||
// updateField({
|
updateField({
|
||||||
// id,
|
id,
|
||||||
// table,
|
table,
|
||||||
// number: value[column],
|
number: value[column],
|
||||||
// column,
|
column,
|
||||||
// pathname
|
pathname
|
||||||
// })
|
})
|
||||||
// setIsActive(false)
|
setIsActive(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
function onErrors(errors) {
|
function onErrors(errors) {
|
||||||
|
@ -62,24 +62,42 @@ export default function NumberInputCell(props: CellContext<any, any>) {
|
||||||
console.log(JSON.stringify(errors))
|
console.log(JSON.stringify(errors))
|
||||||
}
|
}
|
||||||
return (
|
return (
|
||||||
<TableInputContainer isActive={isActive} setIsActive={setIsActive}>
|
<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)
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
>
|
||||||
{isActive ?
|
{isActive ?
|
||||||
<Form {...form}>
|
<Form {...form}>
|
||||||
<form onSubmit={form.handleSubmit(onSubmit, onErrors)}>
|
<form onSubmit={e => {
|
||||||
|
e.preventDefault()
|
||||||
|
if (isDirty) {
|
||||||
|
console.log("SUBMIT")
|
||||||
|
form.handleSubmit(onSubmit, onErrors)
|
||||||
|
}
|
||||||
|
}} >
|
||||||
<FormField
|
<FormField
|
||||||
control={form.control}
|
control={form.control}
|
||||||
name={column}
|
name={column}
|
||||||
render={({ field }) => (
|
render={({ field }) => (
|
||||||
<FormItem>
|
<FormItem
|
||||||
<FormControl>
|
onBlur={() => setIsActive(false)}
|
||||||
|
>
|
||||||
|
<FormControl
|
||||||
|
>
|
||||||
<Input
|
<Input
|
||||||
type="number"
|
type="number"
|
||||||
autoFocus={true}
|
autoFocus={true}
|
||||||
step={props.column.columnDef.meta.step}
|
step={props.column.columnDef.meta.step}
|
||||||
onKeyDown={e => { if (e.code === "Enter") { form.handleSubmit(onSubmit, onErrors) } }}
|
|
||||||
{...field}
|
{...field}
|
||||||
/>
|
/>
|
||||||
</FormControl>
|
</FormControl>
|
||||||
|
<FormMessage />
|
||||||
</FormItem>
|
</FormItem>
|
||||||
)}
|
)}
|
||||||
/>
|
/>
|
||||||
|
@ -87,6 +105,6 @@ export default function NumberInputCell(props: CellContext<any, any>) {
|
||||||
</Form>
|
</Form>
|
||||||
: <p>{props.cell.getValue()}</p>
|
: <p>{props.cell.getValue()}</p>
|
||||||
}
|
}
|
||||||
</TableInputContainer>
|
</div >
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,7 +41,7 @@ export const TextInputCell = (props: CellContext<any, any>) => {
|
||||||
{isActive ?
|
{isActive ?
|
||||||
<Input
|
<Input
|
||||||
value={value}
|
value={value}
|
||||||
onChange={e => setValue(e.target.value)} // onBlur={handleClose}
|
onChange={e => setValue(e.target.value)}
|
||||||
autoFocus={true}
|
autoFocus={true}
|
||||||
onBlur={handleExit}
|
onBlur={handleExit}
|
||||||
onKeyDown={e => { if (e.code === "Enter") { handleConfirm() } }}
|
onKeyDown={e => { if (e.code === "Enter") { handleConfirm() } }}
|
||||||
|
|
Loading…
Reference in New Issue