feat(widget-list): add min max configuration (#4394)

This commit is contained in:
KoljaTM
2020-10-15 19:27:23 +02:00
committed by GitHub
parent 1bdd858b31
commit 5fdfe40dd2
8 changed files with 237 additions and 22 deletions

View File

@ -1,6 +1,8 @@
import * as stringTemplate from './stringTemplate';
import * as validations from './validations';
export const NetlifyCmsLibWidgets = {
stringTemplate,
validations,
};
export { stringTemplate };
export { stringTemplate, validations };

View File

@ -0,0 +1,28 @@
import { isNumber } from 'lodash';
import { List } from 'immutable';
export const validateMinMax = (
t: (key: string, options: unknown) => string,
fieldLabel: string,
value?: List<unknown>,
min?: number,
max?: number,
) => {
const minMaxError = (messageKey: string) => ({
type: 'RANGE',
message: t(`editor.editorControlPane.widget.${messageKey}`, {
fieldLabel,
minCount: min,
maxCount: max,
count: min,
}),
});
if ([min, max, value?.size].every(isNumber) && (value!.size < min! || value!.size > max!)) {
return minMaxError(min === max ? 'rangeCountExact' : 'rangeCount');
} else if (isNumber(min) && min > 0 && value?.size && value.size < min) {
return minMaxError('rangeMin');
} else if (isNumber(max) && value?.size && value.size > max) {
return minMaxError('rangeMax');
}
};