mirror of
https://github.com/strapi/strapi.git
synced 2025-09-26 17:00:55 +00:00
(content-manager): types for field sizes service (#18888)
This commit is contained in:
parent
eb7b88426d
commit
52321fa7ad
@ -26,7 +26,7 @@ const strapi = {
|
||||
})),
|
||||
})),
|
||||
},
|
||||
};
|
||||
} as any;
|
||||
|
||||
describe('field sizes service', () => {
|
||||
it('should return the correct field sizes', () => {
|
||||
@ -73,7 +73,7 @@ describe('field sizes service', () => {
|
||||
const fieldSizes = getAllFieldSizes();
|
||||
|
||||
expect(fieldSizes).not.toHaveProperty('plugin::mycustomfields.color');
|
||||
expect(fieldSizes['plugin::mycustomfields.smallColor'].default).toBe(4);
|
||||
expect(fieldSizes['plugin::mycustomfields.smallColor'].isResizable).toBe(false);
|
||||
expect(fieldSizes['plugin::mycustomfields.smallColor']?.default).toBe(4);
|
||||
expect(fieldSizes['plugin::mycustomfields.smallColor']?.isResizable).toBe(false);
|
||||
});
|
||||
});
|
||||
|
@ -1,23 +1,26 @@
|
||||
import { errors } from '@strapi/utils';
|
||||
import { LoadedStrapi as Strapi, CustomFields } from '@strapi/types';
|
||||
|
||||
const { ApplicationError } = errors;
|
||||
|
||||
const needsFullSize = {
|
||||
type FieldSize = CustomFields.CustomFieldServerOptions['inputSize'];
|
||||
|
||||
const needsFullSize: FieldSize = {
|
||||
default: 12,
|
||||
isResizable: false,
|
||||
};
|
||||
|
||||
const smallSize = {
|
||||
const smallSize: FieldSize = {
|
||||
default: 4,
|
||||
isResizable: true,
|
||||
};
|
||||
|
||||
const defaultSize = {
|
||||
const defaultSize: FieldSize = {
|
||||
default: 6,
|
||||
isResizable: true,
|
||||
};
|
||||
|
||||
const fieldSizes: any = {
|
||||
const fieldSizes: Record<string, FieldSize> = {
|
||||
// Full row and not resizable
|
||||
dynamiczone: needsFullSize,
|
||||
component: needsFullSize,
|
||||
@ -47,17 +50,17 @@ const fieldSizes: any = {
|
||||
uid: defaultSize,
|
||||
};
|
||||
|
||||
const createFieldSizesService = ({ strapi }: any) => {
|
||||
const createFieldSizesService = ({ strapi }: { strapi: Strapi }) => {
|
||||
const fieldSizesService = {
|
||||
getAllFieldSizes() {
|
||||
return fieldSizes;
|
||||
},
|
||||
|
||||
hasFieldSize(type: any) {
|
||||
hasFieldSize(type: string) {
|
||||
return !!fieldSizes[type];
|
||||
},
|
||||
|
||||
getFieldSize(type?: any) {
|
||||
getFieldSize(type?: string) {
|
||||
if (!type) {
|
||||
throw new ApplicationError('The type is required');
|
||||
}
|
||||
@ -70,7 +73,7 @@ const createFieldSizesService = ({ strapi }: any) => {
|
||||
return fieldSize;
|
||||
},
|
||||
|
||||
setFieldSize(type: any, size: any) {
|
||||
setFieldSize(type: string, size: FieldSize) {
|
||||
if (!type) {
|
||||
throw new ApplicationError('The type is required');
|
||||
}
|
||||
@ -87,7 +90,8 @@ const createFieldSizesService = ({ strapi }: any) => {
|
||||
const customFields = strapi.container.get('custom-fields').getAll();
|
||||
|
||||
// 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) {
|
||||
fieldSizesService.setFieldSize(uid, customField.inputSize);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user