UNPKG

@keycloakify/keycloak-admin-ui

Version:
62 lines 3.14 kB
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime"; import { ActionGroup, ClipboardCopy, Form, FormGroup, Grid, GridItem, Stack, StackItem, TextArea, } from "@patternfly/react-core"; import { Children, cloneElement, isValidElement, } from "react"; import { Controller } from "react-hook-form"; import { useAccess } from "../../context/access/Access"; import { FixedButtonsGroup } from "../../components/form/FixedButtonGroup"; /** * Use this in place of a patternfly Form component and add the `role` and `fineGrainedAccess` properties. * @param {FormAccessProps} param0 - all properties of Form + role and fineGrainedAccess */ export const FormAccess = ({ children, role, fineGrainedAccess = false, isReadOnly = false, unWrap = false, ...rest }) => { const { hasAccess } = useAccess(); const recursiveCloneChildren = (children, newProps) => { return Children.map(children, (child) => { if (!isValidElement(child)) { return child; } if (child.props) { const element = child; if (child.type === Controller) { return cloneElement(child, { ...element.props, render: (props) => { const renderElement = element.props.render(props); return cloneElement(renderElement, { ...renderElement.props, ...newProps, }); }, }); } const children = recursiveCloneChildren(element.props.children, newProps); switch (child.type) { case FixedButtonsGroup: return cloneElement(child, { isActive: !newProps.isDisabled, children, }); case TextArea: return cloneElement(child, { readOnly: newProps.isDisabled, children, }); } return cloneElement(child, child.type === FormGroup || child.type === GridItem || child.type === Grid || child.type === ActionGroup || child.type === ClipboardCopy || child.type === Stack || child.type === StackItem ? { children } : { ...newProps, children }); } return child; }); }; const isDisabled = isReadOnly || (!hasAccess(role) && !fineGrainedAccess); return (_jsxs(_Fragment, { children: [!unWrap && (_jsx(Form, { ...rest, className: "keycloak__form " + (rest.className || ""), children: recursiveCloneChildren(children, isDisabled ? { isDisabled } : {}) })), unWrap && recursiveCloneChildren(children, isDisabled ? { isDisabled } : {})] })); }; //# sourceMappingURL=FormAccess.js.map