rewrite certain fields to tsx

This commit is contained in:
honzapatCZ 2024-12-01 20:05:57 +01:00
parent 70f4f86dc3
commit cbe6381233
9 changed files with 57 additions and 54 deletions

View File

@ -1,31 +0,0 @@
import tw from "twin.macro";
import { useDropzone } from "react-dropzone";
import { useMemo } from "react";
import { useApiClient, useCompany } from "@utils/NejManager/NejProvider";
import axios from "axios";
import { useField } from "formik";
import NejDropzone from "./NejDropzone";
/**
* Renders a file input field with dropzone functionality.
*
* @param {Object} props - The component props.
* @param {boolean} props.single - Whether to allow only one file to be uploaded
* @param {string | string[]} props.accept - Array of supported file extensions.
* @returns {JSX.Element} The rendered file input field.
*/
export function FileField({ single, accept, ...props }) {
const [field, meta, helpers] = useField(props);
/**
* Handles the upload of files.
*
* @param {Array<File>} files - The files to be uploaded.
*/
function onUpload(files) {
console.log(files)
field.onChange({ target: { value: single ? files[0] : files, name: field.name } });
}
return <NejDropzone value={field.value} accept={accept} onDrop={onUpload} single={single}/>;
}

27
Form/Core/FileField.tsx Normal file
View File

@ -0,0 +1,27 @@
import { FieldHookConfig, useField } from "formik";
import NejDropzone from "./NejDropzone";
import { Accept } from "react-dropzone";
/**
* Renders a file input field with dropzone functionality.
*
* @param props - The component props.
* @param props.single - Whether to allow only one file to be uploaded
* @param props.accept - Array of supported file extensions.
* @returns {JSX.Element} The rendered file input field.
*/
export function FileField({ single, accept, ...props }: FieldHookConfig<File | File[]> & { single?: boolean, accept?: Accept }) {
const [field, meta, helpers] = useField(props);
/**
* Handles the upload of files.
*
* @param {Array<File>} files - The files to be uploaded.
*/
function onUpload(files) {
console.log(files)
field.onChange({ target: { value: single ? files[0] : files, name: field.name } });
}
return <NejDropzone value={field.value} accept={accept} onDrop={onUpload} single={single} />;
}

View File

@ -1,8 +1,6 @@
import tw from "twin.macro"; import tw from "twin.macro";
import { useDropzone } from "react-dropzone"; import { Accept, FileError, useDropzone } from "react-dropzone";
import { useMemo } from "react"; import { HTMLAttributes, useMemo } from "react";
import { useApiClient, useCompany } from "@utils/NejManager/NejProvider";
import axios from "axios";
/** /**
* NejDropzone component for handling file uploads. * NejDropzone component for handling file uploads.
@ -15,7 +13,7 @@ import axios from "axios";
* @param {string} props.text - The text to be displayed in the dropzone. * @param {string} props.text - The text to be displayed in the dropzone.
* @returns {JSX.Element} - NejDropzone component. * @returns {JSX.Element} - NejDropzone component.
*/ */
export default function NejDropzone({ value, accept, onDrop, validator, single, text, css, children, ...props }) { export default function NejDropzone({ value, accept, onDrop, validator, single, text, css, children, ...props }: HTMLAttributes<HTMLDivElement> & { value: File | File[], accept?: Accept, onDrop: (files: File[]) => void, validator?: (file: File) => FileError | FileError[] | null, single?: boolean, text?: string, css?: any, children?: any }) {
const { getRootProps, getInputProps, isFocused, isDragAccept, isDragReject } = useDropzone({ const { getRootProps, getInputProps, isFocused, isDragAccept, isDragReject } = useDropzone({
//validator: validator, //validator: validator,
@ -31,12 +29,14 @@ export default function NejDropzone({ value, accept, onDrop, validator, single,
...(isFocused && tw`bg-secondary`), ...(isFocused && tw`bg-secondary`),
...(isDragAccept && tw`border-accent`), ...(isDragAccept && tw`border-accent`),
...(isDragReject && tw`border-red-600`), ...(isDragReject && tw`border-red-600`),
css ...css
}), }),
[isFocused, isDragAccept, isDragReject, css] [isFocused, isDragAccept, isDragReject, css]
); );
let fil = value == null ? null : single ? [value] : value; //if file is not array, make it 1 item array
const array = Array.isArray(value) ? value : [value];
const fil = value == null ? null : array;
return ( return (
<div {...getRootProps({ style })} {...props}> <div {...getRootProps({ style })} {...props}>

View File

@ -10,6 +10,8 @@ type TableInstanceProp<TData extends MRT_RowData> = {
export type NejReactTableOptions<TData extends MRT_RowData> = Omit<MRT_TableOptions<TData>, "data"> & { data?: TData[] }; export type NejReactTableOptions<TData extends MRT_RowData> = Omit<MRT_TableOptions<TData>, "data"> & { data?: TData[] };
export type CommonlyForwardedNejReactTableOptions<TData extends MRT_RowData> = Omit<NejReactTableOptions<TData>, "data" | "columns">
type Props<TData extends MRT_RowData> = Xor< type Props<TData extends MRT_RowData> = Xor<
TableInstanceProp<TData>, TableInstanceProp<TData>,
NejReactTableOptions<TData> NejReactTableOptions<TData>

View File

@ -1,15 +1,20 @@
import { MRT_RowData, MRT_TableInstance, MRT_TableOptions, useMantineReactTable } from "mantine-react-table"; import { MRT_RowData, MRT_TableInstance, MRT_TableOptions, useMantineReactTable } from "mantine-react-table";
import { NejReactTableOptions, TableInstance } from "./NejReactTable";
export const useNejReactTable = <TData extends MRT_RowData>( export const useNejReactTable = <TData extends MRT_RowData>(
tableOptions: MRT_TableOptions<TData>, tableOptions: NejReactTableOptions<TData>,
): MRT_TableInstance<TData> => { ): TableInstance<TData> => {
return useMantineReactTable({ return {
...useMantineReactTable({
enablePagination: false, enablePagination: false,
enableFullScreenToggle: false, enableFullScreenToggle: false,
enableDensityToggle: false, enableDensityToggle: false,
enableBottomToolbar: false, enableBottomToolbar: false,
enableTopToolbar: tableOptions.renderTopToolbarCustomActions != null, enableTopToolbar: tableOptions.renderTopToolbarCustomActions != null,
...tableOptions, ...tableOptions,
}); data: tableOptions.data ?? [],
}),
loading: tableOptions.data == null
};
} }

View File

@ -78,7 +78,7 @@ function useMethod({ references, state, babel }) {
let useStateHookImport = addNamed(declPath, "useState", "react"); let useStateHookImport = addNamed(declPath, "useState", "react");
let useCallbackHookImport = addNamed(declPath, "useCallback", "react"); let useCallbackHookImport = addNamed(declPath, "useCallback", "react");
let servicePath = "@services/accounting-api"; let servicePath = "@services/portal-api";
let apiErrorImport = addNamed(declPath, "ApiError", servicePath); let apiErrorImport = addNamed(declPath, "ApiError", servicePath);
let nejServicesPath = "@utils/NejManager/NejProvider"; let nejServicesPath = "@utils/NejManager/NejProvider";

View File

@ -1,5 +1,5 @@
import { CancelablePromise, Error } from "src/services/accounting-api"; import { CancelablePromise, Error } from "src/services/portal-api";
export default function useMethod<T, Arg>(method: (...args: Arg) => CancelablePromise<T>, export default function useMethod<T, Arg>(method: (...args: Arg) => CancelablePromise<T>,
onDone?: (data: T) => void, onDone?: (data: T) => void,
onError?: (error: Error) => void = null): [(...args: Arg) => void, { data: T, loading: boolean, error: Error }]; onError?: (error: Error) => void = null): [(...args: Arg) => CancelablePromise<T>, { data: T, loading: boolean, error: Error }];

View File

@ -87,7 +87,7 @@ function useQuery({ references, state, babel }) {
} }
let fullCallPathString = reduceCallee(call.callee) let fullCallPathString = reduceCallee(call.callee)
let servicePath = "@services/accounting-api"; let servicePath = "@services/portal-api";
let apiErrorImport = addNamed(declPath, "ApiError", servicePath); let apiErrorImport = addNamed(declPath, "ApiError", servicePath);
let useStateSrc = babel.template("[DATA, CALL_SET] = USE_STATE({data: null, error: null, loading: true})")({ let useStateSrc = babel.template("[DATA, CALL_SET] = USE_STATE({data: null, error: null, loading: true})")({

View File

@ -1,4 +1,4 @@
import { CancelablePromise, Error } from "src/services/accounting-api"; import { CancelablePromise, Error } from "src/services/portal-api";
//default export is a function //default export is a function
/* /*