UNPKG

@selfcommunity/react-ui

Version:

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

98 lines (97 loc) 5.25 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 react_intl_1 = require("react-intl"); const Autocomplete_1 = tslib_1.__importDefault(require("@mui/material/Autocomplete")); const TextField_1 = tslib_1.__importDefault(require("@mui/material/TextField")); const CircularProgress_1 = tslib_1.__importDefault(require("@mui/material/CircularProgress")); const parse_1 = tslib_1.__importDefault(require("autosuggest-highlight/parse")); const match_1 = tslib_1.__importDefault(require("autosuggest-highlight/match")); const material_1 = require("@mui/material"); const react_core_1 = require("@selfcommunity/react-core"); const styles_1 = require("@mui/material/styles"); const system_1 = require("@mui/system"); const PREFIX = 'SCEventAutocomplete'; const classes = { root: `${PREFIX}-root` }; const Root = (0, styles_1.styled)(Autocomplete_1.default, { name: PREFIX, slot: 'Root', overridesResolver: (props, styles) => styles.root })(() => ({})); /** * > API documentation for the Community-JS Event Autocomplete component. Learn about the available props and the CSS API. * * * This component renders a bar that allows users to search (with autocomplete) for all the events available in the application. * * #### Import * ```jsx * import {EventAutocomplete} from '@selfcommunity/react-ui'; * ``` * #### Component Name * The name `SCEventAutocomplete` can be used when providing style overrides in the theme. * * #### CSS * * |Rule Name|Global class|Description| * |---|---|---| * |root|.SCEventAutocomplete-root|Styles applied to the root element.| * * @param inProps */ const EventAutocomplete = (inProps) => { const props = (0, system_1.useThemeProps)({ props: inProps, name: PREFIX }); // Props const { onChange, defaultValue = null, disabled = false, TextFieldProps = { variant: 'outlined', label: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.eventAutocomplete.label", defaultMessage: "ui.eventAutocomplete.label" }) } } = props, rest = tslib_1.__rest(props, ["onChange", "defaultValue", "disabled", "TextFieldProps"]); // State const [open, setOpen] = (0, react_1.useState)(false); const [value, setValue] = (0, react_1.useState)(typeof defaultValue === 'string' ? null : defaultValue); // HOOKS const { events, isLoading } = (0, react_core_1.useSCFetchEvents)(); (0, react_1.useEffect)(() => { if (value === null) { return; } onChange && onChange(value); }, [value]); (0, react_1.useEffect)(() => { if (!isLoading && typeof defaultValue === 'string') { setValue(events.find((e) => e.id === Number(defaultValue))); } }, [isLoading]); // Handlers const handleOpen = () => { setOpen(true); }; const handleClose = () => { setOpen(false); }; const handleChange = (e, value) => { setValue(value); }; // Render return ((0, jsx_runtime_1.jsx)(Root, Object.assign({ className: classes.root, open: open, onOpen: handleOpen, onClose: handleClose, options: events || [], getOptionLabel: (option) => option.name || '', value: value, selectOnFocus: true, clearOnBlur: true, blurOnSelect: true, handleHomeEndKeys: true, clearIcon: null, disabled: disabled || isLoading, noOptionsText: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.eventAutocomplete.empty", defaultMessage: "ui.eventAutocomplete.empty" }), onChange: handleChange, isOptionEqualToValue: (option, value) => value.id === option.id, // renderTags={(value, getTagProps) => { // return value.map((option: any, index) => ( // <Chip key={option.id} id={option.id} label={option.name} color={option.color} {...getTagProps({index})} /> // )); // }} renderOption: (props, option, { inputValue }) => { const matches = (0, match_1.default)(option.name, inputValue); const parts = (0, parse_1.default)(option.name, matches); return ((0, jsx_runtime_1.jsxs)(material_1.Box, Object.assign({ component: "li" }, props, { children: [(0, jsx_runtime_1.jsx)(material_1.Avatar, { alt: option.name, src: option.image_small, sx: { marginRight: 1 } }), (0, jsx_runtime_1.jsx)(react_1.default.Fragment, { children: parts.map((part, index) => ((0, jsx_runtime_1.jsx)(material_1.Typography, Object.assign({ sx: { fontWeight: part.highlight ? 700 : 400, marginRight: 0.2 } }, { children: part.text }), index))) })] }))); }, renderInput: (params) => { return ((0, jsx_runtime_1.jsx)(TextField_1.default, Object.assign({}, params, TextFieldProps, { margin: "dense", InputProps: Object.assign(Object.assign({}, params.InputProps), { autoComplete: 'events', endAdornment: ((0, jsx_runtime_1.jsxs)(react_1.default.Fragment, { children: [isLoading ? (0, jsx_runtime_1.jsx)(CircularProgress_1.default, { color: "inherit", size: 20 }) : null, params.InputProps.endAdornment] })) }) }))); } }, rest))); }; exports.default = EventAutocomplete;