Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: admin UI nullable custom fields #2360

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,13 @@ describe('createUpdatedTranslatable()', () => {

it('coerces empty customFields to correct type', () => {
const customFieldConfig: CustomFieldConfig[] = [
{ name: 'a', type: 'boolean', list: false },
{ name: 'b', type: 'int', list: false },
{ name: 'c', type: 'float', list: false },
{ name: 'd', type: 'datetime', list: false },
{ name: 'e', type: 'string', list: false },
{ name: 'a', type: 'boolean', list: false, nullable: false },
{ name: 'b', type: 'int', list: false, nullable: false },
{ name: 'c', type: 'float', list: false, nullable: false },
{ name: 'd', type: 'datetime', list: false, nullable: false },
{ name: 'e', type: 'string', list: false, nullable: false },
{ name: 'f', type: 'int', list: false, nullable: true },
{ name: 'g', type: 'datetime', list: false, nullable: true },
];

const formValue = {
Expand All @@ -198,6 +200,8 @@ describe('createUpdatedTranslatable()', () => {
c: '',
d: '',
e: '',
f: '',
g: '',
},
};

Expand All @@ -213,5 +217,7 @@ describe('createUpdatedTranslatable()', () => {
expect(result.customFields.c).toBe(0);
expect(result.customFields.d instanceof Date).toBe(true);
expect(result.customFields.e).toBe('');
expect(result.customFields.f).toBe(null);
expect(result.customFields.g).toBe(null);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ export function createUpdatedTranslatable<T extends { translations: any[] } & Ma
newTranslatedCustomFields[field.name] = value;
} else {
newCustomFields[field.name] =
value === '' ? getDefaultValue(field.type as CustomFieldType) : value;
value === ''
? getDefaultValue(field.type as CustomFieldType, field.nullable ?? true)
: value;
}
}
newTranslation.customFields = newTranslatedCustomFields;
Expand All @@ -59,7 +61,7 @@ export function createUpdatedTranslatable<T extends { translations: any[] } & Ma
return newTranslatable;
}

function getDefaultValue(type: CustomFieldType): any {
function getDefaultValue(type: CustomFieldType, isNullable?: boolean): any {
switch (type) {
case 'localeString':
case 'string':
Expand All @@ -70,9 +72,9 @@ function getDefaultValue(type: CustomFieldType): any {
return false;
case 'float':
case 'int':
return 0;
return isNullable ? null : 0;
case 'datetime':
return new Date();
return isNullable ? null : new Date();
case 'relation':
return null;
default:
Expand Down