| import { aC as basicFormFieldWithSimpleReaderParse, aD as FieldDataError, aE as assertRequired } from './index-8ceee4cf.node.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 [blurred, onBlur] = useReducer(() => true, false); |
| return /*#__PURE__*/jsx(NumberField, { |
| label: props.label, |
| description: props.description, |
| isRequired: (_props$validation = props.validation) === null || _props$validation === void 0 ? void 0 : _props$validation.isRequired, |
| errorMessage: props.forceValidation || blurred ? validateInteger(props.validation, props.value, props.label) : undefined, |
| onBlur: onBlur, |
| autoFocus: props.autoFocus, |
| value: props.value === null ? undefined : props.value, |
| onChange: val => { |
| props.onChange(val === undefined ? null : val); |
| } |
| }); |
| } |
|
|
| function integer({ |
| label, |
| defaultValue, |
| validation, |
| description |
| }) { |
| return basicFormFieldWithSimpleReaderParse({ |
| 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 }; |