2
0
mirror of https://github.com/frappe/books.git synced 2025-02-02 12:08:27 +00:00

incr: update AutoComplete

This commit is contained in:
18alantom 2022-04-22 17:31:04 +05:30
parent 6accde0e17
commit b3533698c0
4 changed files with 50 additions and 34 deletions

View File

@ -133,15 +133,6 @@ export class DocHandler {
return doc; return doc;
} }
getModel(schemaName: string): typeof Doc {
const Model = this.models[schemaName];
if (Model === undefined) {
return Doc;
}
return Model;
}
async getSingle(schemaName: string) { async getSingle(schemaName: string) {
return await this.getDoc(schemaName, schemaName); return await this.getDoc(schemaName, schemaName);
} }
@ -169,13 +160,17 @@ export class DocHandler {
schema?: Schema, schema?: Schema,
Model?: typeof Doc Model?: typeof Doc
): Doc { ): Doc {
Model ??= this.getModel(schemaName); if (!this.models[schemaName] && Model) {
this.models[schemaName] = Model;
}
Model ??= this.models[schemaName];
schema ??= this.fyo.schemaMap[schemaName]; schema ??= this.fyo.schemaMap[schemaName];
if (schema === undefined) { if (schema === undefined) {
throw new Error(`Schema not found for ${schemaName}`); throw new Error(`Schema not found for ${schemaName}`);
} }
const doc = new Model(schema, data, this.fyo); const doc = new Model!(schema, data, this.fyo);
doc.setDefaults(); doc.setDefaults();
return doc; return doc;
} }

View File

@ -31,9 +31,9 @@ export type FormulaMap = Record<string, Formula | undefined>;
export type DefaultMap = Record<string, Default | undefined>; export type DefaultMap = Record<string, Default | undefined>;
export type ValidationMap = Record<string, Validation | undefined>; export type ValidationMap = Record<string, Validation | undefined>;
export type RequiredMap = Record<string, Required | undefined>; export type RequiredMap = Record<string, Required | undefined>;
export type CurrenciesMap = Record<string, GetCurrency>; export type CurrenciesMap = Record<string, GetCurrency | undefined>;
export type HiddenMap = Record<string, Hidden>; export type HiddenMap = Record<string, Hidden | undefined>;
export type ReadOnlyMap = Record<string, ReadOnly>; export type ReadOnlyMap = Record<string, ReadOnly | undefined>;
export type DependsOnMap = Record<string, string[]>; export type DependsOnMap = Record<string, string[]>;
/** /**
@ -57,7 +57,7 @@ export type EmptyMessageFunction = (doc: Doc, fyo: Fyo) => string;
export type EmptyMessageMap = Record<string, EmptyMessageFunction>; export type EmptyMessageMap = Record<string, EmptyMessageFunction>;
export type ListFunction = (doc?: Doc) => string[]; export type ListFunction = (doc?: Doc) => string[];
export type ListsMap = Record<string, ListFunction>; export type ListsMap = Record<string, ListFunction | undefined>;
export interface Action { export interface Action {
label: string; label: string;

View File

@ -32,9 +32,10 @@
</template> </template>
<script> <script>
import Dropdown from 'src/components/Dropdown'; import Dropdown from 'src/components/Dropdown.vue';
import { fuzzyMatch } from 'src/utils'; import { fuzzyMatch } from 'src/utils';
import Base from './Base'; import { getOptionList } from 'src/utils/doc';
import Base from './Base.vue';
export default { export default {
name: 'AutoComplete', name: 'AutoComplete',
@ -83,26 +84,13 @@ export default {
}, },
async getSuggestions(keyword = '') { async getSuggestions(keyword = '') {
keyword = keyword.toLowerCase(); keyword = keyword.toLowerCase();
const options = getOptionList(this.df, this.doc);
let list = this.df.getList
? await this.df.getList(this.doc)
: this.df.options || [];
let items = list.map((d) => {
if (typeof d === 'string') {
return {
label: d,
value: d,
};
}
return d;
});
if (!keyword) { if (!keyword) {
return items; return options;
} }
return items return options
.map((item) => ({ ...fuzzyMatch(keyword, item.value), item })) .map((item) => ({ ...fuzzyMatch(keyword, item.value), item }))
.filter(({ isMatch }) => isMatch) .filter(({ isMatch }) => isMatch)
.sort((a, b) => a.distance - b.distance) .sort((a, b) => a.distance - b.distance)

View File

@ -1,5 +1,6 @@
import Doc from 'fyo/model/doc'; import Doc from 'fyo/model/doc';
import { Field } from 'schemas/types'; import { Field, OptionField, SelectOption } from 'schemas/types';
import { fyo } from 'src/initFyo';
export function evaluateReadOnly(field: Field, doc: Doc) { export function evaluateReadOnly(field: Field, doc: Doc) {
if (field.readOnly !== undefined) { if (field.readOnly !== undefined) {
@ -26,3 +27,35 @@ export function evaluateHidden(field: Field, doc: Doc) {
return false; return false;
} }
export function getOptionList(field: Field, doc: Doc): SelectOption[] {
const list = _getOptionList(field, doc);
return list.map((option) => {
if (typeof option === 'string') {
return {
label: option,
value: option,
};
}
return option;
});
}
function _getOptionList(field: Field, doc: Doc) {
if ((field as OptionField).options) {
return (field as OptionField).options;
}
const Model = fyo.models[doc.schemaName];
if (Model === undefined) {
return [];
}
const getList = Model.lists[field.fieldname];
if (getList === undefined) {
return [];
}
return getList(doc);
}