UNPKG

@novin-dev/formalite

Version:

Generate MUI form with few line of code

50 lines (47 loc) 3.72 kB
import { __rest, __assign } from 'tslib'; import { FormControl, RadioGroup, FormControlLabel, Radio, FormHelperText, FormLabel } from '@mui/material'; import { getData, checkIsRequired } from '../../config/utils.js'; import React from 'react'; import { RadioGroupStateEnum } from './RadioGroupView.type.js'; import ViewError from '../../components/ViewError.js'; import ViewPending from '../../components/ViewPending.js'; var CustomLabel = function (_a) { var name = _a.name, allInputProps = _a.allInputProps, labelProps = _a.labelProps, validationSchema = _a.validationSchema, formik = _a.formik; return (React.createElement(FormLabel, __assign({ "data-testid": "label", component: "legend", required: checkIsRequired({ schema: validationSchema, formikValues: formik.values, key: name, }) }, labelProps), allInputProps.label)); }; var RadioGroupViewAllViews = function (props) { var formik = props.formik, name = props.name, validationSchema = props.validationSchema, translator = props.translator, dataStatus = props.dataStatus, loadFunction = props.loadFunction, allInputProps = props.allInputProps, labelProps = props.labelProps; var helperText = allInputProps.helperText, onChange = allInputProps.onChange, inputProps = __rest(allInputProps, ["helperText", "onChange"]); if (dataStatus.status === RadioGroupStateEnum.READY) { return (React.createElement(FormControl, { component: "fieldset", error: getData({ source: formik.touched, key: name }) && Boolean(getData({ source: formik.errors, key: name })), fullWidth: true }, React.createElement(CustomLabel, { validationSchema: validationSchema, name: name, allInputProps: allInputProps, labelProps: labelProps, formik: formik }), React.createElement(RadioGroup, __assign({ name: name, row: true }, inputProps), Object.entries(dataStatus.data).map(function (_a) { var key = _a[0], value = _a[1]; return (React.createElement(FormControlLabel, { key: key, value: key, label: value.label, control: React.createElement(Radio, __assign({ checked: getData({ source: formik.values, key: name }) === key, onChange: function (event) { if (event.target.checked) { formik.setFieldValue(name, key); if (typeof onChange === "function") { onChange(key, value.additionalData); } } } }, value.props)) })); })), React.createElement(FormHelperText, { "data-i18n": "[html]content.body", dangerouslySetInnerHTML: { __html: getData({ source: formik.touched, key: name }) && getData({ source: formik.errors, key: name }) ? translator(getData({ source: formik.errors, key: name })) : helperText, } }))); } if (dataStatus.status === RadioGroupStateEnum.PENDING) { return (React.createElement(ViewPending, { label: React.createElement(CustomLabel, { validationSchema: validationSchema, name: name, allInputProps: allInputProps, labelProps: labelProps, formik: formik }) })); } return (React.createElement(ViewError, { error: dataStatus.error, reloadFunction: loadFunction, label: React.createElement(CustomLabel, { validationSchema: validationSchema, name: name, allInputProps: allInputProps, labelProps: labelProps, formik: formik }) })); }; export { RadioGroupViewAllViews as default }; //# sourceMappingURL=RadioGroupViewAllViews.js.map