UNPKG

@selfcommunity/react-ui

Version:

React UI Components to integrate a Community created with SelfCommunity Platform.

79 lines (74 loc) 3.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const jsx_runtime_1 = require("react/jsx-runtime"); const react_1 = tslib_1.__importStar(require("react")); const system_1 = require("@mui/system"); const styles_1 = require("@mui/material/styles"); const material_1 = require("@mui/material"); const react_intl_1 = require("react-intl"); const react_core_1 = require("@selfcommunity/react-core"); const classnames_1 = tslib_1.__importDefault(require("classnames")); const GroupForm_1 = tslib_1.__importDefault(require("../GroupForm")); const HiddenPlaceholder_1 = tslib_1.__importDefault(require("../../shared/HiddenPlaceholder")); const PREFIX = 'SCCreateGroupButton'; const classes = { root: `${PREFIX}-root` }; const Root = (0, styles_1.styled)(material_1.Button, { name: PREFIX, slot: 'Root', overridesResolver: (props, styles) => styles.root })(({ theme }) => ({})); /** *> API documentation for the Community-JS Create Group Button component. Learn about the available props and the CSS API. * #### Import ```jsx import {CreateGroupButton} from '@selfcommunity/react-ui'; ``` #### Component Name The name `SCCreateGroupButton` can be used when providing style overrides in the theme. #### CSS |Rule Name|Global class|Description| |---|---|---| |root|.SCCreateGroupButton-root|Styles applied to the root element.| * @param inProps */ function CreateGroupButton(inProps) { var _a; //PROPS const props = (0, system_1.useThemeProps)({ props: inProps, name: PREFIX }); const { className, GroupFormProps = {}, children } = props, rest = tslib_1.__rest(props, ["className", "GroupFormProps", "children"]); // CONTEXT const scUserContext = (0, react_1.useContext)(react_core_1.SCUserContext); // STATE const [open, setOpen] = react_1.default.useState(false); // CONST const authUserId = scUserContext.user ? scUserContext.user.id : null; const preferences = (0, react_core_1.useSCPreferences)(); const onlyStaffEnabled = (0, react_1.useMemo)(() => preferences.preferences[react_core_1.SCPreferences.CONFIGURATIONS_GROUPS_ONLY_STAFF_ENABLED].value, [preferences.preferences]); // eslint-disable-next-line @typescript-eslint/ban-ts-ignore // @ts-ignore const canCreateGroup = (0, react_1.useMemo)(() => { var _a, _b; return (_b = (_a = scUserContext === null || scUserContext === void 0 ? void 0 : scUserContext.user) === null || _a === void 0 ? void 0 : _a.permission) === null || _b === void 0 ? void 0 : _b.create_group; }, [(_a = scUserContext === null || scUserContext === void 0 ? void 0 : scUserContext.user) === null || _a === void 0 ? void 0 : _a.permission]); /** * Handle click on button */ const handleClick = () => { setOpen((o) => !o); }; /** * If there's no authUserId, component is hidden. */ if ((!canCreateGroup && onlyStaffEnabled) || !authUserId) { return (0, jsx_runtime_1.jsx)(HiddenPlaceholder_1.default, {}); } /** * Renders root object */ return ((0, jsx_runtime_1.jsxs)(react_1.default.Fragment, { children: [(0, jsx_runtime_1.jsx)(Root, Object.assign({ className: (0, classnames_1.default)(classes.root, className), onClick: handleClick, variant: "contained", startIcon: (0, jsx_runtime_1.jsx)(material_1.Icon, { children: "add" }) }, rest, { children: children !== null && children !== void 0 ? children : (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.createGroupButton", defaultMessage: "ui.createGroupButton" }) })), open && (0, jsx_runtime_1.jsx)(GroupForm_1.default, Object.assign({}, GroupFormProps, { open: true, onClose: handleClick }))] })); } exports.default = CreateGroupButton;