(content-manager): types for field sizes service (#18888)

This commit is contained in:
Jamie Howard 2023-11-23 12:45:10 +00:00 committed by GitHub
parent eb7b88426d
commit 52321fa7ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 12 deletions

View File

@ -26,7 +26,7 @@ const strapi = {
})), })),
})), })),
}, },
}; } as any;
describe('field sizes service', () => { describe('field sizes service', () => {
it('should return the correct field sizes', () => { it('should return the correct field sizes', () => {
@ -73,7 +73,7 @@ describe('field sizes service', () => {
const fieldSizes = getAllFieldSizes(); const fieldSizes = getAllFieldSizes();
expect(fieldSizes).not.toHaveProperty('plugin::mycustomfields.color'); expect(fieldSizes).not.toHaveProperty('plugin::mycustomfields.color');
expect(fieldSizes['plugin::mycustomfields.smallColor'].default).toBe(4); expect(fieldSizes['plugin::mycustomfields.smallColor']?.default).toBe(4);
expect(fieldSizes['plugin::mycustomfields.smallColor'].isResizable).toBe(false); expect(fieldSizes['plugin::mycustomfields.smallColor']?.isResizable).toBe(false);
}); });
}); });

View File

@ -1,23 +1,26 @@
import { errors } from '@strapi/utils'; import { errors } from '@strapi/utils';
import { LoadedStrapi as Strapi, CustomFields } from '@strapi/types';
const { ApplicationError } = errors; const { ApplicationError } = errors;
const needsFullSize = { type FieldSize = CustomFields.CustomFieldServerOptions['inputSize'];
const needsFullSize: FieldSize = {
default: 12, default: 12,
isResizable: false, isResizable: false,
}; };
const smallSize = { const smallSize: FieldSize = {
default: 4, default: 4,
isResizable: true, isResizable: true,
}; };
const defaultSize = { const defaultSize: FieldSize = {
default: 6, default: 6,
isResizable: true, isResizable: true,
}; };
const fieldSizes: any = { const fieldSizes: Record<string, FieldSize> = {
// Full row and not resizable // Full row and not resizable
dynamiczone: needsFullSize, dynamiczone: needsFullSize,
component: needsFullSize, component: needsFullSize,
@ -47,17 +50,17 @@ const fieldSizes: any = {
uid: defaultSize, uid: defaultSize,
}; };
const createFieldSizesService = ({ strapi }: any) => { const createFieldSizesService = ({ strapi }: { strapi: Strapi }) => {
const fieldSizesService = { const fieldSizesService = {
getAllFieldSizes() { getAllFieldSizes() {
return fieldSizes; return fieldSizes;
}, },
hasFieldSize(type: any) { hasFieldSize(type: string) {
return !!fieldSizes[type]; return !!fieldSizes[type];
}, },
getFieldSize(type?: any) { getFieldSize(type?: string) {
if (!type) { if (!type) {
throw new ApplicationError('The type is required'); throw new ApplicationError('The type is required');
} }
@ -70,7 +73,7 @@ const createFieldSizesService = ({ strapi }: any) => {
return fieldSize; return fieldSize;
}, },
setFieldSize(type: any, size: any) { setFieldSize(type: string, size: FieldSize) {
if (!type) { if (!type) {
throw new ApplicationError('The type is required'); throw new ApplicationError('The type is required');
} }
@ -87,7 +90,8 @@ const createFieldSizesService = ({ strapi }: any) => {
const customFields = strapi.container.get('custom-fields').getAll(); const customFields = strapi.container.get('custom-fields').getAll();
// If they have a custom field size, register it // If they have a custom field size, register it
Object.entries(customFields).forEach(([uid, customField]: any) => { // TODO types can be inferred when customFields is typed
Object.entries(customFields).forEach(([uid, customField]: [string, any]) => {
if (customField.inputSize) { if (customField.inputSize) {
fieldSizesService.setFieldSize(uid, customField.inputSize); fieldSizesService.setFieldSize(uid, customField.inputSize);
} }