All files / packages/app/utils forms.tsx

100% Statements 12/12
100% Branches 0/0
100% Functions 4/4
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62                          245x   1095x       1376x 1376x 38x 38x   1376x     245x                       245x                                     223x 106x    
import React from "react";
 
import type { FormApi } from "@tanstack/react-form";
import {
	createFormHook,
	createFormHookContexts,
	useStore,
} from "@tanstack/react-form";
 
import { Form as RawForm } from "~components/form";
import { Input } from "~components/input";
import { NumberInput } from "~components/number-input";
 
const { useFormContext, fieldContext, formContext } = createFormHookContexts();
 
const Form: React.FC<React.ComponentProps<typeof RawForm>> = ({
	onSubmit: onSubmitRaw,
	...props
}) => {
	const form = useFormContext();
	const onSubmit = React.useCallback(() => {
		void form.handleSubmit();
		onSubmitRaw?.();
	}, [form, onSubmitRaw]);
	return <RawForm {...props} onSubmit={onSubmit} />;
};
 
export const { useAppForm } = createFormHook({
	fieldComponents: {
		TextField: Input,
		NumberField: NumberInput,
	},
	formComponents: {
		Form,
	},
	fieldContext,
	formContext,
});
 
export const useTypedValues = <Form, DefaultValues extends Partial<Form>>(
	formStore: FormApi<
		Form,
		// oxlint-disable typescript/no-explicit-any
		any,
		any,
		any,
		any,
		any,
		any,
		any,
		any,
		any,
		any
		// oxlint-enable typescript/no-explicit-any
	>["store"],
	// This is only needed for types
	_defaultValues: DefaultValues,
) => {
	const formValues = useStore(formStore, (store) => store.values);
	return formValues as Partial<Form> & DefaultValues;
};