@keystatic/core

Search for an npm package
import { b as basicFormFieldWithSimpleReaderParse, u as IntegerFieldInput, F as FieldDataError, c as assertRequired } from './index-1a9da0fb.node.js';
import { jsx } from 'react/jsx-runtime';
function validateInteger(validation, value, label) {
if (value !== null && (typeof value !== 'number' || !Number.isInteger(value))) {
return `${label} must be a whole number`;
}
if (validation !== null && validation !== void 0 && validation.isRequired && value === null) {
return `${label} is required`;
}
if (value !== null) {
if ((validation === null || validation === void 0 ? void 0 : validation.min) !== undefined && value < validation.min) {
return `${label} must be at least ${validation.min}`;
}
if ((validation === null || validation === void 0 ? void 0 : validation.max) !== undefined && value > validation.max) {
return `${label} must be at most ${validation.max}`;
}
}
}
function integer({
label,
defaultValue,
validation,
description
}) {
return basicFormFieldWithSimpleReaderParse({
label,
Input(props) {
return /*#__PURE__*/jsx(IntegerFieldInput, {
label: label,
description: description,
validation: validation,
...props
});
},
defaultValue() {
return defaultValue !== null && defaultValue !== void 0 ? defaultValue : null;
},
parse(value) {
if (value === undefined) {
return null;
}
if (typeof value === 'number') {
return value;
}
throw new FieldDataError('Must be a number');
},
validate(value) {
const message = validateInteger(validation, value, label);
if (message !== undefined) {
throw new FieldDataError(message);
}
assertRequired(value, validation, label);
return value;
},
serialize(value) {
return {
value: value === null ? undefined : value
};
}
});
}
export { integer as i };