@aokiapp/rjsf-mantine-theme
Version:
Mantine theme, fields and widgets for react-jsonschema-form
83 lines (80 loc) • 2.67 kB
JavaScript
import { jsxs, Fragment, jsx } from 'react/jsx-runtime';
import { useCallback } from 'react';
import { PasswordInput, TextInput } from '@mantine/core';
import { getInputProps, labelValue, examplesId, ariaDescribedByIds } from '@rjsf/utils';
import { createErrors } from '../utils/createErrors.mjs';
import { useFieldContext } from './FieldTemplate.mjs';
function BaseInputTemplate(props) {
const {
id,
placeholder,
label,
hideLabel,
value,
required,
readonly,
disabled,
onChange,
onChangeOverride,
onBlur,
onFocus,
autofocus,
options,
schema,
type,
rawErrors,
className,
hideError
} = props;
if (!id) {
console.log("No id for", props);
throw new Error(`no id for props ${JSON.stringify(props)}`);
}
const { description } = useFieldContext();
const inputProps = getInputProps(schema, type, options);
let inputValue;
if (inputProps.type === "number" || inputProps.type === "integer") {
inputValue = value || value === 0 ? value : "";
} else {
inputValue = value == null ? "" : value;
}
const _onChange = useCallback(
({ target: { value: value2 } }) => onChange(value2 === "" ? options.emptyValue : value2),
[onChange, options]
);
const _onBlur = useCallback(({ target: { value: value2 } }) => onBlur(id, value2), [onBlur, id]);
const _onFocus = useCallback(
({ target: { value: value2 } }) => onFocus(id, value2),
[onFocus, id]
);
const InputComponent = type === "password" ? PasswordInput : TextInput;
return /* @__PURE__ */ jsxs(Fragment, { children: [
/* @__PURE__ */ jsx(
InputComponent,
{
id,
placeholder,
description,
...inputProps,
label: labelValue(label, hideLabel, void 0),
required,
autoFocus: autofocus,
disabled: disabled || readonly,
list: schema.examples ? examplesId(id) : void 0,
value: inputValue,
error: createErrors(rawErrors, hideError),
onChange: onChangeOverride || _onChange,
onBlur: _onBlur,
onFocus: _onFocus,
"aria-describedby": ariaDescribedByIds(id, !!schema.examples),
className: `armt-widget-input ${className || ""}`
},
id
),
Array.isArray(schema.examples) && /* @__PURE__ */ jsx("datalist", { id: examplesId(id), children: schema.examples.concat(schema.default && !schema.examples.includes(schema.default) ? [schema.default] : []).map((example) => {
return /* @__PURE__ */ jsx("option", { value: example }, example);
}) })
] });
}
export { BaseInputTemplate as default };
//# sourceMappingURL=BaseInputTemplate.mjs.map