diff --git a/adomin.code-workspace b/adomin.code-workspace index 9f1acac..a850eca 100644 --- a/adomin.code-workspace +++ b/adomin.code-workspace @@ -19,6 +19,9 @@ }, "files.associations": { "*.mdx": "markdown" + }, + "[typescript]": { + "editor.defaultFormatter": "esbenp.prettier-vscode" } }, } diff --git a/app/adomin/fields.types.ts b/app/adomin/fields.types.ts index 26121ef..d62c39a 100644 --- a/app/adomin/fields.types.ts +++ b/app/adomin/fields.types.ts @@ -267,8 +267,12 @@ export interface AdominArrayFieldConfig extends AdominBaseFieldConfig { type: 'array' /** * options for the select component + * + * Can be a function that returns the options */ - options?: AdominSelectOption[] + options?: + | AdominSelectOption[] + | (() => Promise[]>) } export type AdominFileFieldConfig = AdominBaseFieldConfig & { diff --git a/app/adomin/routes/get_validation_schema_from_lucid_model.ts b/app/adomin/routes/get_validation_schema_from_lucid_model.ts index 47783fd..c1b43cb 100644 --- a/app/adomin/routes/get_validation_schema_from_lucid_model.ts +++ b/app/adomin/routes/get_validation_schema_from_lucid_model.ts @@ -4,12 +4,12 @@ import { AdominFieldConfig } from '../fields.types.js' import { AdominValidationMode } from '../validation/adomin_validation_helpers.js' import { computeColumnConfigFields } from './models/get_model_config.js' -export const getValidationSchemaFromConfig = ( +export const getValidationSchemaFromConfig = async ( modelConfig: ModelConfig, validationMode: AdominValidationMode ) => { const foundConfig = modelConfig - const fields = computeColumnConfigFields(foundConfig.fields) + const fields = await computeColumnConfigFields(foundConfig.fields) const results = fields.map(({ adomin, name: columnName }) => { const notCreatable = adomin.creatable === false const notEditable = adomin.editable === false diff --git a/app/adomin/routes/models/get_model_config.ts b/app/adomin/routes/models/get_model_config.ts index d0c0138..e837dad 100644 --- a/app/adomin/routes/models/get_model_config.ts +++ b/app/adomin/routes/models/get_model_config.ts @@ -77,15 +77,17 @@ export const getModelConfigRoute = async (ctx: HttpContext) => { name, label, labelPluralized, - fields: computeColumnConfigFields(fields), + fields: await computeColumnConfigFields(fields), primaryKey, isHidden: isHidden ?? false, staticRights, } } -export function computeColumnConfigFields(input: ColumnConfig[]): ColumnConfig[] { - const res: ColumnConfig[] = input.map((field) => { +export async function computeColumnConfigFields(input: ColumnConfig[]): Promise { + const res: ColumnConfig[] = [] + + for (const field of input) { let { editable, creatable, sortable, filterable } = field.adomin const noCustomFilter = field.adomin.sqlFilter === undefined @@ -127,6 +129,11 @@ export function computeColumnConfigFields(input: ColumnConfig[]): ColumnConfig[] if (sortable === undefined && noCustomSort) sortable = false } + // load options for array field + if (field.adomin.type === 'array' && typeof field.adomin.options === 'function') { + field.adomin.options = await field.adomin.options() + } + const computedConfig: ColumnConfig = { name: field.name, isVirtual: field.isVirtual, @@ -139,8 +146,8 @@ export function computeColumnConfigFields(input: ColumnConfig[]): ColumnConfig[] }, } - return computedConfig - }) + res.push(computedConfig) + } return res } diff --git a/app/adomin/routes/models/read/get_data_list.ts b/app/adomin/routes/models/read/get_data_list.ts index 4d76624..7600108 100644 --- a/app/adomin/routes/models/read/get_data_list.ts +++ b/app/adomin/routes/models/read/get_data_list.ts @@ -26,7 +26,7 @@ export const getModelList = async ({ }: GetModelListOptions): Promise => { const Model = modelConfig.model() const { fields: rawFields, primaryKey, queryBuilderCallback } = modelConfig - const fields = computeColumnConfigFields(rawFields) + const fields = await computeColumnConfigFields(rawFields) const fieldsStrs = getModelFieldStrs(fields) const { pageIndex, pageSize } = paginationSettings diff --git a/app/adomin/routes/models/write/create_model.ts b/app/adomin/routes/models/write/create_model.ts index d845109..1db773d 100644 --- a/app/adomin/routes/models/write/create_model.ts +++ b/app/adomin/routes/models/write/create_model.ts @@ -37,7 +37,7 @@ export const createModel = async (ctx: HttpContext) => { const fields = modelConfig.fields - const schema = getValidationSchemaFromConfig(modelConfig, 'create') + const schema = await getValidationSchemaFromConfig(modelConfig, 'create') const parsedData = await request.validate({ schema, messages: getGenericMessages(Model) }) const createdInstance = new Model() diff --git a/app/adomin/routes/models/write/update_model.ts b/app/adomin/routes/models/write/update_model.ts index c40db20..b88e015 100644 --- a/app/adomin/routes/models/write/update_model.ts +++ b/app/adomin/routes/models/write/update_model.ts @@ -37,7 +37,7 @@ export const updateModel = async (ctx: HttpContext) => { if (res !== true) return } - const schema = getValidationSchemaFromConfig(modelConfig, 'update') + const schema = await getValidationSchemaFromConfig(modelConfig, 'update') const parsedData = await request.validate({ schema, messages: getGenericMessages(Model) }) const fields = modelConfig.fields diff --git a/app/test_adomin_config.ts b/app/test_adomin_config.ts index b21f289..00213aa 100644 --- a/app/test_adomin_config.ts +++ b/app/test_adomin_config.ts @@ -106,7 +106,7 @@ export const TEST_CONFIG = createModelViewConfig(() => Test, { stringArrayTest: { type: 'array', label: 'Test array', - options: [ + options: async () => [ { value: 'sun', label: 'Soleil' }, { value: 'moon', label: 'Lune' }, { value: 'mars', label: 'Mars' }, diff --git a/docs/src/content/docs/reference/views/models/array.md b/docs/src/content/docs/reference/views/models/array.md index 2efc23c..b4c2649 100644 --- a/docs/src/content/docs/reference/views/models/array.md +++ b/docs/src/content/docs/reference/views/models/array.md @@ -20,10 +20,14 @@ Use the Array field when you have an array of values contained in one column in ### options An optionnal array of options for the form select component. + +Can be an async function that returns the options + e.g. + ```ts const options = [ - { value: 'sun', label: 'Soleil' }, - { value: 'moon', label: 'Lune' }, + { value: 'sun', label: 'Soleil' }, + { value: 'moon', label: 'Lune' }, ] -``` \ No newline at end of file +```