mirror of
https://github.com/frappe/books.git
synced 2024-11-09 15:20:56 +00:00
refactor: improve type definition of Field
This commit is contained in:
parent
5cb136d22e
commit
5fd192174d
@ -188,7 +188,7 @@ function getField(df: string | Field): Field {
|
||||
label: '',
|
||||
fieldname: '',
|
||||
fieldtype: df as FieldType,
|
||||
};
|
||||
} as Field;
|
||||
}
|
||||
|
||||
return df;
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { DateTime } from 'luxon';
|
||||
import { AccountRootType } from 'models/baseModels/Account/types';
|
||||
import { BaseField, RawValue } from 'schemas/types';
|
||||
import { BaseField, FieldType, RawValue } from 'schemas/types';
|
||||
|
||||
export type ExportExtention = 'csv' | 'json';
|
||||
|
||||
@ -24,7 +24,8 @@ export interface ReportRow {
|
||||
foldedBelow?: boolean;
|
||||
}
|
||||
export type ReportData = ReportRow[];
|
||||
export interface ColumnField extends BaseField {
|
||||
export interface ColumnField extends Omit<BaseField, 'fieldtype'> {
|
||||
fieldtype: FieldType;
|
||||
align?: 'left' | 'right' | 'center';
|
||||
width?: number;
|
||||
}
|
||||
|
@ -1,28 +1,56 @@
|
||||
export enum FieldTypeEnum {
|
||||
Data = 'Data',
|
||||
Select = 'Select',
|
||||
Link = 'Link',
|
||||
Date = 'Date',
|
||||
Datetime = 'Datetime',
|
||||
Table = 'Table',
|
||||
AutoComplete = 'AutoComplete',
|
||||
Check = 'Check',
|
||||
AttachImage = 'AttachImage',
|
||||
DynamicLink = 'DynamicLink',
|
||||
Int = 'Int',
|
||||
Float = 'Float',
|
||||
Currency = 'Currency',
|
||||
Text = 'Text',
|
||||
Color = 'Color',
|
||||
Attachment = 'Attachment',
|
||||
}
|
||||
import { PropertyEnum } from "utils/types";
|
||||
|
||||
export type FieldType =
|
||||
| 'Data'
|
||||
| 'Select'
|
||||
| 'Link'
|
||||
| 'Date'
|
||||
| 'Datetime'
|
||||
| 'Table'
|
||||
| 'AutoComplete'
|
||||
| 'Check'
|
||||
| 'AttachImage'
|
||||
| 'DynamicLink'
|
||||
| 'Int'
|
||||
| 'Float'
|
||||
| 'Currency'
|
||||
| 'Text'
|
||||
| 'Color'
|
||||
| 'Attachment';
|
||||
|
||||
export const FieldTypeEnum: PropertyEnum<Record<FieldType, FieldType>> = {
|
||||
Data: 'Data',
|
||||
Select: 'Select',
|
||||
Link: 'Link',
|
||||
Date: 'Date',
|
||||
Datetime: 'Datetime',
|
||||
Table: 'Table',
|
||||
AutoComplete: 'AutoComplete',
|
||||
Check: 'Check',
|
||||
AttachImage: 'AttachImage',
|
||||
DynamicLink: 'DynamicLink',
|
||||
Int: 'Int',
|
||||
Float: 'Float',
|
||||
Currency: 'Currency',
|
||||
Text: 'Text',
|
||||
Color: 'Color',
|
||||
Attachment: 'Attachment',
|
||||
};
|
||||
|
||||
type OptionFieldType = 'Select' | 'AutoComplete' | 'Color';
|
||||
type TargetFieldType = 'Table' | 'Link';
|
||||
type NumberFieldType = 'Int' | 'Float';
|
||||
type DynamicLinkFieldType = 'DynamicLink';
|
||||
type BaseFieldType = Exclude<
|
||||
FieldType,
|
||||
TargetFieldType | DynamicLinkFieldType | OptionFieldType | NumberFieldType
|
||||
>;
|
||||
|
||||
export type FieldType = keyof typeof FieldTypeEnum;
|
||||
export type RawValue = string | number | boolean | null;
|
||||
|
||||
export interface BaseField {
|
||||
fieldname: string; // Column name in the db
|
||||
fieldtype: FieldType; // UI Descriptive field types that map to column types
|
||||
fieldname: string; // Column name in the db
|
||||
fieldtype: BaseFieldType; // UI Descriptive field types that map to column types
|
||||
label: string; // Translateable UI facing name
|
||||
schemaName?: string; // Convenient access to schemaName incase just the field is passed
|
||||
required?: boolean; // Implies Not Null
|
||||
@ -39,31 +67,28 @@ export interface BaseField {
|
||||
}
|
||||
|
||||
export type SelectOption = { value: string; label: string };
|
||||
export interface OptionField extends BaseField {
|
||||
fieldtype:
|
||||
| FieldTypeEnum.Select
|
||||
| FieldTypeEnum.AutoComplete
|
||||
| FieldTypeEnum.Color;
|
||||
export interface OptionField extends Omit<BaseField, 'fieldtype'> {
|
||||
fieldtype: OptionFieldType;
|
||||
options: SelectOption[];
|
||||
emptyMessage?: string;
|
||||
allowCustom?: boolean;
|
||||
}
|
||||
|
||||
export interface TargetField extends BaseField {
|
||||
fieldtype: FieldTypeEnum.Table | FieldTypeEnum.Link;
|
||||
export interface TargetField extends Omit<BaseField, 'fieldtype'> {
|
||||
fieldtype: TargetFieldType;
|
||||
target: string; // Name of the table or group of tables to fetch values
|
||||
create?: boolean; // Whether to show Create in the dropdown
|
||||
edit?: boolean; // Whether the Table has quick editable columns
|
||||
}
|
||||
|
||||
export interface DynamicLinkField extends BaseField {
|
||||
fieldtype: FieldTypeEnum.DynamicLink;
|
||||
export interface DynamicLinkField extends Omit<BaseField, 'fieldtype'> {
|
||||
fieldtype: DynamicLinkFieldType;
|
||||
emptyMessage?: string;
|
||||
references: string; // Reference to an option field that links to schema
|
||||
}
|
||||
|
||||
export interface NumberField extends BaseField {
|
||||
fieldtype: FieldTypeEnum.Float | FieldTypeEnum.Int;
|
||||
export interface NumberField extends Omit<BaseField, 'fieldtype'> {
|
||||
fieldtype: NumberFieldType;
|
||||
minvalue?: number; // UI Facing used to restrict lower bound
|
||||
maxvalue?: number; // UI Facing used to restrict upper bound
|
||||
}
|
||||
@ -80,7 +105,7 @@ export type Naming = 'autoincrement' | 'random' | 'numberSeries' | 'manual';
|
||||
export interface Schema {
|
||||
name: string; // Table name
|
||||
label: string; // Translateable UI facing name
|
||||
fields: Field[]; // Maps to database columns
|
||||
fields: Field[]; // Maps to database columns
|
||||
isTree?: boolean; // Used for nested set, eg for Chart of Accounts
|
||||
extends?: string; // Value points to an Abstract schema. Indicates Subclass schema
|
||||
isChild?: boolean; // Indicates a child table, i.e table with "parent" FK column
|
||||
|
@ -306,7 +306,7 @@ export class Importer {
|
||||
|
||||
for (let f = 0; f < fields.length; f++) {
|
||||
const field = fields[f];
|
||||
const value = Converter.toDocValue(row[f], field, this.fyo);
|
||||
const value = Converter.toDocValue(row[f], field as Field, this.fyo);
|
||||
|
||||
if (field.parentField) {
|
||||
cts[field.parentField] ??= {};
|
||||
|
@ -1,12 +1,18 @@
|
||||
import { Fyo } from 'fyo';
|
||||
import { RawValueMap } from 'fyo/core/types';
|
||||
import { Field, FieldTypeEnum, RawValue, TargetField } from 'schemas/types';
|
||||
import {
|
||||
Field,
|
||||
FieldType,
|
||||
FieldTypeEnum,
|
||||
RawValue,
|
||||
TargetField,
|
||||
} from 'schemas/types';
|
||||
import { generateCSV } from 'utils/csvParser';
|
||||
import { GetAllOptions, QueryFilter } from 'utils/db/types';
|
||||
import { getMapFromList, safeParseFloat } from 'utils/index';
|
||||
import { ExportField, ExportTableField } from './types';
|
||||
|
||||
const excludedFieldTypes = [
|
||||
const excludedFieldTypes: FieldType[] = [
|
||||
FieldTypeEnum.AttachImage,
|
||||
FieldTypeEnum.Attachment,
|
||||
];
|
||||
@ -26,7 +32,7 @@ export function getExportFields(
|
||||
.filter((f) => !f.computed && f.label && !exclude.includes(f.fieldname))
|
||||
.map((field) => {
|
||||
const { fieldname, label } = field;
|
||||
const fieldtype = field.fieldtype as FieldTypeEnum;
|
||||
const fieldtype = field.fieldtype as FieldType;
|
||||
return {
|
||||
fieldname,
|
||||
fieldtype,
|
||||
@ -323,7 +329,7 @@ async function getChildTableData(
|
||||
return data;
|
||||
}
|
||||
|
||||
function convertRawPesaToFloat(data: RawValueMap[], fields: Field[]) {
|
||||
function convertRawPesaToFloat(data: RawValueMap[], fields: ExportField[]) {
|
||||
const currencyFields = fields.filter(
|
||||
(f) => f.fieldtype === FieldTypeEnum.Currency
|
||||
);
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { Doc } from "fyo/model/doc";
|
||||
import { FieldTypeEnum } from "schemas/types";
|
||||
import { FieldType } from "schemas/types";
|
||||
import { QueryFilter } from "utils/db/types";
|
||||
|
||||
export interface MessageDialogButton {
|
||||
@ -58,7 +58,7 @@ export interface SidebarItem {
|
||||
|
||||
export interface ExportField {
|
||||
fieldname: string;
|
||||
fieldtype: FieldTypeEnum;
|
||||
fieldtype: FieldType;
|
||||
label: string;
|
||||
export: boolean;
|
||||
}
|
||||
|
@ -43,4 +43,8 @@ export interface SelectFileReturn {
|
||||
success: boolean;
|
||||
data: Buffer;
|
||||
canceled: boolean;
|
||||
}
|
||||
}
|
||||
|
||||
export type PropertyEnum<T extends Record<string, any>> = {
|
||||
[key in keyof Required<T>]: key;
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user