fix: plugins: defaultValue not working for config.array.fields

Massimo Melina committed May 30, 2024 at 15:48 UTC 4c7529c36d1d71ed9142a27294732c27508eda89
3 files changed +7 -4
admin/src/ArrayField.ts
+2 -2
@@ -12,7 +12,7 @@ import _ from 'lodash'
12 import { Center, IconBtn } from './mui'
13
14 type ArrayFieldProps<T> = FieldProps<T[]> & { fields: FieldDescriptor[], height?: number, reorder?: boolean, prepend?: boolean }
15 -export function ArrayField<T extends object>({ label, helperText, fields, value, onChange, onError, setApi, reorder, prepend, noRows, ...rest }: ArrayFieldProps<T>) {
15 +export function ArrayField<T extends object>({ label, helperText, fields, value, onChange, onError, setApi, reorder, prepend, noRows, valuesForAdd, ...rest }: ArrayFieldProps<T>) {
16 const rows = useMemo(() => (value||[]).map((x,$idx) =>
17 setHidden({ ...x } as any, x.hasOwnProperty('id') ? { $idx } : { id: $idx })),
18 [JSON.stringify(value)]) //eslint-disable-line
@@ -67,7 +67,7 @@ export function ArrayField<T extends object>({ label, helperText, fields, value,
67 title,
68 size: 'small',
69 onClick: ev =>
70 - formDialog<T>({ form, title }).then(x => {
70 + formDialog<T>({ form, title, values: valuesForAdd }).then(x => {
71 if (!x) return
72 const newValue = value?.slice() || []
73 if (prepend) newValue.unshift(x)
admin/src/InstalledPlugins.ts
+4 -2
@@ -5,7 +5,7 @@ import { createElement as h, Fragment, useEffect } from 'react'
5 import { Box, Link } from '@mui/material'
6 import { DataTable, DataTableColumn } from './DataTable'
7 import { Delete, Error as ErrorIcon, FormatPaint as ThemeIcon, PlayCircle, Settings, StopCircle, Upgrade } from '@mui/icons-material'
8 -import { HTTP_FAILED_DEPENDENCY, prefix, with_, xlate } from './misc'
8 +import { HTTP_FAILED_DEPENDENCY, newObj, prefix, with_, xlate } from './misc'
9 import { alertDialog, confirmDialog, formDialog, toast } from './dialog'
10 import _ from 'lodash'
11 import { Account } from './AccountsPage'
@@ -159,8 +159,10 @@ function makeFields(config: any) {
159 return o
160 let { type, defaultValue, fields, frontend, ...rest } = o
161 const comp = (type2comp as any)[type] as Field<any> | undefined
162 - if (comp === ArrayField)
162 + if (comp === ArrayField) {
163 + rest.valuesForAdd = newObj(fields, x => x.defaultValue)
164 fields = makeFields(fields)
165 + }
166 if (defaultValue !== undefined && type === 'boolean')
167 rest.placeholder = `Default value is ${JSON.stringify(defaultValue)}`
168 return { k, comp, fields, ...rest }
dev-plugins.md
+1
@@ -201,6 +201,7 @@ The `api` object you get as parameter of the `init` contains the following:
201 exported inside `customApi: object`
202
203 - `openDb(filename, options): Promise<{ get, put, del, close, unlink, sublevel }>` LevelDB-like class for storage.
204 + The specified file name will be stored in the "storage" folder of the plugin, by default.
205 Refer to [dedicated documentation](https://www.npmjs.com/package/@rejetto/kvstorage) for details.
206
207 ## Front-end specific