drf-react-by-schema
Version:
Components and Tools for building a React App having Django Rest Framework (DRF) as server
269 lines • 8.63 kB
TypeScript
import { JSX, ReactElement } from 'react';
import { AutocompleteRenderOptionState } from '@mui/material/Autocomplete/Autocomplete';
import { GridActionsColDef, GridColDef } from '@mui/x-data-grid/models/colDef/gridColDef';
import { GridFilterModel } from '@mui/x-data-grid/models/gridFilterModel';
import { GridSortModel } from '@mui/x-data-grid/models/gridSortModel';
import { DateView } from '@mui/x-date-pickers/models/views';
import { Control, FieldValues, UseFormGetValues, UseFormSetValue } from 'react-hook-form';
import { SxProps } from '@mui/material/styles';
import { OnEditModelType } from '../context/APIWrapperContext';
import { ServerEndPoint } from '../context/DRFReactBySchemaContext';
import { AlertColor } from '@mui/material/Alert';
export type Id = string | number | null;
export type GenericValue = any;
export type Item = Record<string, GenericValue>;
export type OptionsAC = Record<string, Item[]>;
export type OptionsACWithLoaders = Record<string, Item[] | (() => Promise<Item[]>)>;
export type PaginatedResult = {
count: number;
next: number;
previous: number;
sum_rows: null | Record<string, number>;
results: Item[];
};
export interface PaginationModel {
page: number;
pageSize: number;
filter?: GridFilterModel;
sort?: GridSortModel;
}
export type SchemaType = Record<string, Field>;
export type SchemaOptionsType = {
name: string;
description: string;
actions: {
POST: SchemaType;
};
renders: string[];
parses: string[];
verbose_name: string;
verbose_name_plural: string;
};
export type SchemaOptionsSingleActionType = Omit<SchemaOptionsType, 'actions'> & {
action: {
POST: SchemaType;
};
};
export type ModelOptionsType = Omit<SchemaOptionsType, 'actions' | 'renders' | 'parses'>;
export type ChoiceValue = string | number;
export interface Choice {
value: ChoiceValue;
display_name: string;
}
type FieldChild = Record<string, SchemaType>;
export interface Field {
type: string;
child?: FieldChild;
children?: SchemaType;
model_default?: GenericValue;
model_required?: boolean;
choices?: Choice[];
max_length?: number | string;
max_digits?: number;
decimal_places?: number;
label: string;
read_only?: boolean;
is_currency?: boolean;
prefix?: string;
suffix?: string;
creatable?: boolean;
related_editable?: boolean;
validators_regex?: Item[];
many?: boolean;
date_views?: DateView[];
required?: boolean;
disabled?: boolean;
help_text?: string;
model_multiline?: boolean;
}
export type GridEnrichedBySchemaColDef = (GridColDef<Item> | GridActionsColDef<Item>) & {
isIndexField?: boolean;
creatable?: boolean;
disabled?: boolean;
orderable?: boolean;
patternFormat?: string;
};
export interface DataSchemaColumnsType {
data: Item[];
schema: SchemaType;
modelOptions: ModelOptionsType;
rowCount?: number;
sumRowsTotals?: null | Record<string, number>;
columns?: GridEnrichedBySchemaColDef[];
}
export interface ItemSchemaColumnsType {
data: Item;
schema: SchemaType;
modelOptions: ModelOptionsType;
columns?: GridEnrichedBySchemaColDef[];
}
export interface CommonFieldProps {
value?: GenericValue;
multiline?: boolean;
setValue?: UseFormSetValue<FieldValues>;
getValues?: UseFormGetValues<FieldValues>;
fieldKey?: string;
labelKey?: string;
index?: number;
optionsAC?: OptionsAC;
optionsModel?: string;
getOptionLabel?: (option: Item | string) => string;
renderOption?: (props: React.HTMLAttributes<HTMLLIElement>, option: Item, state: AutocompleteRenderOptionState) => React.ReactNode;
onEditModel?: (x: OnEditModelType) => void;
fieldsLayout?: FormFieldLayout[];
multiple?: boolean;
sx?: SxProps;
options?: Item[];
isSemaphoric?: boolean;
label?: string;
onValueChange?: (x: GenericValue) => void;
decimalScale?: number;
isPassword?: boolean;
type?: React.HTMLInputTypeAttribute;
autocomplete?: string;
minRows?: number;
optionIdKey?: string;
optionLabelKey?: string;
disabled?: boolean;
}
export interface FieldBySchemaProps extends Omit<CommonFieldProps, 'value'> {
name: string;
schema: SchemaType;
control: Control;
errors: Item;
autoFocus?: boolean;
}
export interface DetailFieldBySchemaProps {
name: string;
schema: SchemaType;
value: GenericValue;
labelKey?: string;
decimalScale?: number;
optionIdKey?: string;
optionLabelKey?: string;
sxField?: SxProps;
sxLabel?: SxProps;
sxValue?: SxProps;
sxValueList?: SxProps;
sxValueListItem?: SxProps;
sxValueListItemText?: SxProps;
}
type LinkProps = {
to: string | object;
href?: string;
state?: object;
className?: string;
style?: React.CSSProperties;
children?: React.ReactNode;
} & React.AnchorHTMLAttributes<HTMLAnchorElement>;
export type LinkComponentType = React.ComponentType<LinkProps>;
export type AddParametersToEnd<TFunction extends (...args: any) => any, TParameters extends [...args: any]> = (...args: [...Parameters<TFunction>, ...TParameters]) => ReturnType<TFunction>;
export type ActionType = 'editInline' | 'remove' | 'edit' | 'view';
export interface CustomAction {
key: string;
icon: ReactElement;
label: string;
handleClick: (item: Item | undefined) => undefined;
}
export type BulkUpdateData = (newData: Item[]) => Promise<{
id: Id;
success: boolean;
}[]>;
export type BulkDeleteData = (ids: Id[]) => Promise<{
id: Id;
success: boolean;
}[]>;
export type OnSelectActionCustom = (selectedData: Item[], bulkUpdateData: BulkUpdateData) => void;
export type OnSelectActionTypes = OnSelectActionCustom | 'bulkDelete' | 'bulkCreate';
export type OnSelectActions = {
title: string;
action: OnSelectActionTypes;
};
interface CustomFormField {
key: string;
CustomElement: (x: CommonFieldProps) => JSX.Element;
}
export interface FormFieldLayout {
title?: string;
rows?: (string | (string | CustomFormField)[] | CustomFormField)[];
CustomElement?: React.ReactNode;
}
export interface ExtraSxCommonFieldProps {
sxRow?: SxProps;
sxRowMultiple?: SxProps;
sxField?: SxProps;
sxLabel?: SxProps;
sxValue?: SxProps;
sxValueList?: SxProps;
sxValueListItem?: SxProps;
sxValueListItemText?: SxProps;
}
export type DetailCommonFieldProps = CommonFieldProps & ExtraSxCommonFieldProps;
interface CustomDetailField {
key: string;
CustomElement: (x: DetailCommonFieldProps) => JSX.Element;
}
export interface DetailFieldLayout {
title?: string;
rows?: (string | (string | CustomDetailField)[] | CustomDetailField)[];
CustomElement?: React.ReactNode;
}
export interface DetailBySchemaProps extends ExtraSxCommonFieldProps {
values: Item;
schema: SchemaType;
editLink?: string;
editLabel?: string;
labelKey?: string;
decimalScale?: number;
fieldsLayout?: DetailFieldLayout[];
hiddenFields?: string[];
fieldsProps?: Record<string, DetailCommonFieldProps>;
}
export type OnSuccessEvent = React.MouseEventHandler<HTMLButtonElement>;
export interface TargetApiParams {
path: string;
serverEndPoint: ServerEndPoint | null;
data: Item;
id: Id;
}
export type TargetApiParamsLocal = Omit<TargetApiParams, 'serverEndPoint'>;
export type TargetApiPostParams = Omit<TargetApiParams, 'id'>;
export type TargetApiPostParamsLocal = Omit<TargetApiPostParams, 'serverEndPoint'>;
export interface SumRowsType {
rows: SumRowsItem[];
severity?: AlertColor;
}
interface SumRowsItem {
field: string;
prefix?: string;
suffix?: string;
isCount?: boolean;
}
export interface GetGenericModelListProps {
model: string;
serverEndPoint: ServerEndPoint | null;
id?: Id;
relatedModel?: string;
relatedModelId?: Id;
columnFields: string[];
hiddenFields?: string[];
creatableFields?: string[];
disabledFields?: string[];
isInBatches?: boolean;
loadedSchema?: SchemaType | boolean;
loadedModelOptions?: ModelOptionsType | boolean;
page?: number;
filter?: GridFilterModel;
queryParams?: string[];
sort?: GridSortModel;
sumRows?: SumRowsType;
}
export type GetGenericModelListPropsLocal = Omit<GetGenericModelListProps, 'serverEndPoint'>;
export interface MobileListRenderItemInfo {
item: Item;
index: number;
id: Id;
}
export type MobileListRenderItemType = (info: MobileListRenderItemInfo) => ReactElement | null;
export {};
//# sourceMappingURL=index.d.ts.map