Skip to content

Commit

Permalink
feat: allow array field config async options
Browse files Browse the repository at this point in the history
  • Loading branch information
mle-moni committed Aug 16, 2024
1 parent 5364cea commit cfdc112
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 15 deletions.
3 changes: 3 additions & 0 deletions adomin.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
},
"files.associations": {
"*.mdx": "markdown"
},
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
},
}
6 changes: 5 additions & 1 deletion app/adomin/fields.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string | number>[]
options?:
| AdominSelectOption<string | number>[]
| (() => Promise<AdominSelectOption<string | number>[]>)
}

export type AdominFileFieldConfig = AdominBaseFieldConfig & {
Expand Down
4 changes: 2 additions & 2 deletions app/adomin/routes/get_validation_schema_from_lucid_model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 12 additions & 5 deletions app/adomin/routes/models/get_model_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ColumnConfig[]> {
const res: ColumnConfig[] = []

for (const field of input) {
let { editable, creatable, sortable, filterable } = field.adomin

const noCustomFilter = field.adomin.sqlFilter === undefined
Expand Down Expand Up @@ -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,
Expand All @@ -139,8 +146,8 @@ export function computeColumnConfigFields(input: ColumnConfig[]): ColumnConfig[]
},
}

return computedConfig
})
res.push(computedConfig)
}

return res
}
2 changes: 1 addition & 1 deletion app/adomin/routes/models/read/get_data_list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const getModelList = async ({
}: GetModelListOptions): Promise<PaginatedData> => {
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
Expand Down
2 changes: 1 addition & 1 deletion app/adomin/routes/models/write/create_model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion app/adomin/routes/models/write/update_model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion app/test_adomin_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand Down
10 changes: 7 additions & 3 deletions docs/src/content/docs/reference/views/models/array.md
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
]
```
```

0 comments on commit cfdc112

Please sign in to comment.