@aokiapp/rjsf-mantine-theme
Version:
Mantine theme, fields and widgets for react-jsonschema-form
31 lines • 2.19 kB
JavaScript
import { jsx as _jsx } from "react/jsx-runtime";
import { NumberInput } from '@mantine/core';
import { ariaDescribedByIds, labelValue, } from '@rjsf/utils';
import { useCallback } from 'react';
import { createErrors } from '../utils/createErrors';
import { useFieldContext } from '../templates/FieldTemplate';
/** The `UpDownWidget` component uses the `BaseInputTemplate` changing the type to `number`.
*
* @param props - The `WidgetProps` for this component
*/
export default function UpDownWidget(props) {
const { id, placeholder, label, hideLabel, value, required, readonly, disabled, onChange, onChangeOverride, onBlur, onFocus, autofocus, options, schema, rawErrors, className, hideError, } = props;
// Note: since React 15.2.0 we can't forward unknown element attributes, so we
// exclude the "options" and "schema" ones here.
if (!id) {
console.log('No id for', props);
throw new Error(`no id for props ${JSON.stringify(props)}`);
}
// Mantine NumberInput have many customizations such as suffix, decimal scale, thousands separator, etc.
const overrideProps = typeof options['props'] === 'object' ? options['props'] : {};
const _onChange = useCallback((e) => {
const callTarget = onChangeOverride || onChange;
callTarget(e);
}, [onChangeOverride, onChange]);
const _onBlur = useCallback(({ target: { value } }) => onBlur(id, value), [onBlur, id]);
const _onFocus = useCallback(({ target: { value } }) => onFocus(id, value), [onFocus, id]);
const { description } = useFieldContext();
const inputValue = value || value === 0 ? value : '';
return (_jsx(NumberInput, { id: id, placeholder: placeholder, description: description, max: schema.maximum, min: schema.minimum, label: labelValue(label, hideLabel, undefined), required: required, autoFocus: autofocus, disabled: disabled || readonly, value: inputValue, error: createErrors(rawErrors, hideError), onChange: _onChange, onBlur: _onBlur, onFocus: _onFocus, "aria-describedby": ariaDescribedByIds(id, !!schema.examples), className: `armt-widget-updown ${className || ''}`, ...overrideProps }, id));
}
//# sourceMappingURL=UpDownWidget.js.map