Enable default values on options columns with validation and fix default values being available on primary display columns

This commit is contained in:
Andrew Kingston 2024-09-24 12:43:41 +01:00
parent 2d1662c0c7
commit fd21503f3e
No known key found for this signature in database
2 changed files with 37 additions and 22 deletions

View File

@ -136,9 +136,8 @@
}
$: initialiseField(field, savingColumn)
$: checkConstraints(editableColumn)
$: required = hasDefault
? false
: !!editableColumn?.constraints?.presence || primaryDisplay
$: required =
primaryDisplay || (!hasDefault && !!editableColumn?.constraints?.presence)
$: uneditable =
$tables.selected?._id === TableNames.USERS &&
UNEDITABLE_USER_FIELDS.includes(editableColumn.name)
@ -168,7 +167,9 @@
$: canBeDisplay =
canBeDisplayColumn(editableColumn.type) && !editableColumn.autocolumn
$: canHaveDefault =
isEnabled("DEFAULT_VALUES") && canHaveDefaultColumn(editableColumn.type)
!required &&
isEnabled("DEFAULT_VALUES") &&
canHaveDefaultColumn(editableColumn.type)
$: canBeRequired =
editableColumn?.type !== LINK_TYPE &&
!uneditable &&
@ -187,11 +188,11 @@
(originalName &&
SWITCHABLE_TYPES[field.type] &&
!editableColumn?.autocolumn)
$: allowedTypes = getAllowedTypes(datasource).map(t => ({
fieldId: makeFieldId(t.type, t.subtype),
...t,
}))
$: bindings = getBindings({ table })
const fieldDefinitions = Object.values(FIELDS).reduce(
// Storing the fields by complex field id
@ -281,6 +282,20 @@
delete saveColumn.fieldName
}
// Ensure the field is not required if we have a default value
if (saveColumn.default) {
saveColumn.constraints.presence = false
}
// Delete default value for options fields if the option is no longer available
if (
saveColumn.type === FieldType.OPTIONS &&
saveColumn.default &&
!saveColumn.constraints.inclusion?.includes(saveColumn.default)
) {
delete saveColumn.default
}
try {
await tables.saveField({
originalName,
@ -727,7 +742,7 @@
formula: e.detail,
}
}}
bindings={getBindings({ table })}
{bindings}
allowJS
context={rowGoldenSample}
/>
@ -766,27 +781,27 @@
{/if}
{#if canHaveDefault}
<div>
{#if editableColumn.type === FieldType.OPTIONS}
<Select
options={editableColumn.constraints?.inclusion || []}
label="Default value"
value={editableColumn.default}
on:change={e => (editableColumn.default = e.detail)}
placeholder="None"
/>
{:else}
<ModalBindableInput
panel={ServerBindingPanel}
title="Default"
label="Default"
title="Default value"
label="Default value"
placeholder="None"
value={editableColumn.default}
on:change={e => {
editableColumn = {
...editableColumn,
default: e.detail,
}
if (e.detail) {
setRequired(false)
}
}}
bindings={getBindings({ table })}
on:change={e => (editableColumn.default = e.detail)}
{bindings}
allowJS
context={rowGoldenSample}
/>
</div>
{/if}
{/if}
</Layout>

View File

@ -53,8 +53,8 @@ const allowDefaultColumnByType: Record<FieldType, boolean> = {
[FieldType.DATETIME]: true,
[FieldType.LONGFORM]: true,
[FieldType.STRING]: true,
[FieldType.OPTIONS]: true,
[FieldType.OPTIONS]: false,
[FieldType.AUTO]: false,
[FieldType.INTERNAL]: false,
[FieldType.BARCODEQR]: false,