@keystatic/core

Search for an npm package
import { a$ as basicFormFieldWithSimpleReaderParse, ar as FieldDataError, b7 as assertRequired } from './index-48f075ab.js';
import { c } from './react-compiler-runtime-0011f46e.js';
import { NumberField } from '@keystar/ui/number-field';
import { useReducer } from 'react';
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 IntegerFieldInput(props) {
var _props$validation;
const $ = c(15);
let t0;
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
t0 = () => true;
$[0] = t0;
} else {
t0 = $[0];
}
const [blurred, onBlur] = useReducer(t0, false);
const t1 = (_props$validation = props.validation) === null || _props$validation === void 0 ? void 0 : _props$validation.isRequired;
let t2;
if ($[1] !== props || $[2] !== blurred) {
t2 = props.forceValidation || blurred ? validateInteger(props.validation, props.value, props.label) : undefined;
$[1] = props;
$[2] = blurred;
$[3] = t2;
} else {
t2 = $[3];
}
const t3 = props.value === null ? undefined : props.value;
let t4;
if ($[4] !== props) {
t4 = val => {
props.onChange(val === undefined ? null : val);
};
$[4] = props;
$[5] = t4;
} else {
t4 = $[5];
}
let t5;
if ($[6] !== props.label || $[7] !== props.description || $[8] !== props.autoFocus || $[9] !== t1 || $[10] !== t2 || $[11] !== onBlur || $[12] !== t3 || $[13] !== t4) {
t5 = /*#__PURE__*/jsx(NumberField, {
label: props.label,
description: props.description,
isRequired: t1,
errorMessage: t2,
onBlur: onBlur,
autoFocus: props.autoFocus,
value: t3,
onChange: t4
});
$[6] = props.label;
$[7] = props.description;
$[8] = props.autoFocus;
$[9] = t1;
$[10] = t2;
$[11] = onBlur;
$[12] = t3;
$[13] = t4;
$[14] = t5;
} else {
t5 = $[14];
}
return t5;
}
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 };