Merge pull request #14635 from Budibase/default-values-options

Default values for options and array types (frontend)
This commit is contained in:
Andrew Kingston 2024-10-03 10:26:42 +01:00 committed by GitHub
commit ade2ed5a05
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 87 additions and 30 deletions

View File

@ -4,6 +4,7 @@
Button, Button,
Label, Label,
Select, Select,
Multiselect,
Toggle, Toggle,
Icon, Icon,
DatePicker, DatePicker,
@ -137,9 +138,10 @@
} }
$: initialiseField(field, savingColumn) $: initialiseField(field, savingColumn)
$: checkConstraints(editableColumn) $: checkConstraints(editableColumn)
$: required = hasDefault $: required =
? false primaryDisplay ||
: !!editableColumn?.constraints?.presence || primaryDisplay editableColumn?.constraints?.presence === true ||
editableColumn?.constraints?.presence?.allowEmpty === false
$: uneditable = $: uneditable =
$tables.selected?._id === TableNames.USERS && $tables.selected?._id === TableNames.USERS &&
UNEDITABLE_USER_FIELDS.includes(editableColumn.name) UNEDITABLE_USER_FIELDS.includes(editableColumn.name)
@ -168,8 +170,8 @@
// used to select what different options can be displayed for column type // used to select what different options can be displayed for column type
$: canBeDisplay = $: canBeDisplay =
canBeDisplayColumn(editableColumn) && !editableColumn.autocolumn canBeDisplayColumn(editableColumn) && !editableColumn.autocolumn
$: canHaveDefault = $: defaultValuesEnabled = isEnabled("DEFAULT_VALUES")
isEnabled("DEFAULT_VALUES") && canHaveDefaultColumn(editableColumn.type) $: canHaveDefault = !required && canHaveDefaultColumn(editableColumn.type)
$: canBeRequired = $: canBeRequired =
editableColumn?.type !== LINK_TYPE && editableColumn?.type !== LINK_TYPE &&
!uneditable && !uneditable &&
@ -188,7 +190,6 @@
(originalName && (originalName &&
SWITCHABLE_TYPES[field.type] && SWITCHABLE_TYPES[field.type] &&
!editableColumn?.autocolumn) !editableColumn?.autocolumn)
$: allowedTypes = getAllowedTypes(datasource).map(t => ({ $: allowedTypes = getAllowedTypes(datasource).map(t => ({
fieldId: makeFieldId(t.type, t.subtype), fieldId: makeFieldId(t.type, t.subtype),
...t, ...t,
@ -206,6 +207,11 @@
}, },
...getUserBindings(), ...getUserBindings(),
] ]
$: sanitiseDefaultValue(
editableColumn.type,
editableColumn.constraints?.inclusion || [],
editableColumn.default
)
const fieldDefinitions = Object.values(FIELDS).reduce( const fieldDefinitions = Object.values(FIELDS).reduce(
// Storing the fields by complex field id // Storing the fields by complex field id
@ -295,6 +301,22 @@
delete saveColumn.fieldName delete saveColumn.fieldName
} }
// Ensure we don't have a default value if we can't have one
if (!canHaveDefault || !defaultValuesEnabled) {
delete saveColumn.default
}
// Ensure primary display columns are always required and don't have default values
if (primaryDisplay) {
saveColumn.constraints.presence = { allowEmpty: false }
delete saveColumn.default
}
// Ensure the field is not required if we have a default value
if (saveColumn.default) {
saveColumn.constraints.presence = false
}
try { try {
await tables.saveField({ await tables.saveField({
originalName, originalName,
@ -541,6 +563,20 @@
return newError return newError
} }
const sanitiseDefaultValue = (type, options, defaultValue) => {
if (!defaultValue?.length) {
return
}
// Delete default value for options fields if the option is no longer available
if (type === FieldType.OPTIONS && !options.includes(defaultValue)) {
delete editableColumn.default
}
// Filter array default values to only valid options
if (type === FieldType.ARRAY) {
editableColumn.default = defaultValue.filter(x => options.includes(x))
}
}
onMount(() => { onMount(() => {
mounted = true mounted = true
}) })
@ -748,9 +784,9 @@
</div> </div>
</div> </div>
{:else if editableColumn.type === JSON_TYPE} {:else if editableColumn.type === JSON_TYPE}
<Button primary text on:click={openJsonSchemaEditor} <Button primary text on:click={openJsonSchemaEditor}>
>Open schema editor</Button Open schema editor
> </Button>
{/if} {/if}
{#if editableColumn.type === AUTO_TYPE || editableColumn.autocolumn} {#if editableColumn.type === AUTO_TYPE || editableColumn.autocolumn}
<Select <Select
@ -779,27 +815,39 @@
</div> </div>
{/if} {/if}
{#if canHaveDefault} {#if defaultValuesEnabled}
<div> {#if editableColumn.type === FieldType.OPTIONS}
<ModalBindableInput <Select
panel={ServerBindingPanel} disabled={!canHaveDefault}
title="Default" options={editableColumn.constraints?.inclusion || []}
label="Default" label="Default value"
value={editableColumn.default} value={editableColumn.default}
on:change={e => { on:change={e => (editableColumn.default = e.detail)}
editableColumn = { placeholder="None"
...editableColumn, />
default: e.detail, {:else if editableColumn.type === FieldType.ARRAY}
} <Multiselect
disabled={!canHaveDefault}
if (e.detail) { options={editableColumn.constraints?.inclusion || []}
setRequired(false) label="Default value"
} value={editableColumn.default}
}} on:change={e =>
(editableColumn.default = e.detail?.length ? e.detail : undefined)}
placeholder="None"
/>
{:else}
<ModalBindableInput
disabled={!canHaveDefault}
panel={ServerBindingPanel}
title="Default value"
label="Default value"
placeholder="None"
value={editableColumn.default}
on:change={e => (editableColumn.default = e.detail)}
bindings={defaultValueBindings} bindings={defaultValueBindings}
allowJS allowJS
/> />
</div> {/if}
{/if} {/if}
</Layout> </Layout>

View File

@ -1,6 +1,7 @@
import { derived, get } from "svelte/store" import { derived, get } from "svelte/store"
import { getDatasourceDefinition, getDatasourceSchema } from "../../../fetch" import { getDatasourceDefinition, getDatasourceSchema } from "../../../fetch"
import { enrichSchemaWithRelColumns, memo } from "../../../utils" import { enrichSchemaWithRelColumns, memo } from "../../../utils"
import { cloneDeep } from "lodash"
export const createStores = () => { export const createStores = () => {
const definition = memo(null) const definition = memo(null)
@ -164,10 +165,18 @@ export const createActions = context => {
// Updates the datasources primary display column // Updates the datasources primary display column
const changePrimaryDisplay = async column => { const changePrimaryDisplay = async column => {
return await saveDefinition({ let newDefinition = cloneDeep(get(definition))
...get(definition),
primaryDisplay: column, // Update primary display
}) newDefinition.primaryDisplay = column
// Sanitise schema to ensure field is required and has no default value
if (!newDefinition.schema[column].constraints) {
newDefinition.schema[column].constraints = {}
}
newDefinition.schema[column].constraints.presence = { allowEmpty: false }
delete newDefinition.schema[column].default
return await saveDefinition(newDefinition)
} }
// Adds a schema mutation for a single field // Adds a schema mutation for a single field