UNPKG

@selfcommunity/react-ui

Version:

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

104 lines (103 loc) 5.5 kB
import { __rest } from "tslib"; import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; import React, { useEffect, useState } from 'react'; import { FormattedMessage } from 'react-intl'; import Autocomplete from '@mui/material/Autocomplete'; import TextField from '@mui/material/TextField'; import CircularProgress from '@mui/material/CircularProgress'; import Checkbox from '@mui/material/Checkbox'; import parse from 'autosuggest-highlight/parse'; import match from 'autosuggest-highlight/match'; import { Chip } from '@mui/material'; import { useSCFetchCategories } from '@selfcommunity/react-core'; import { styled } from '@mui/material/styles'; import { useThemeProps } from '@mui/system'; const PREFIX = 'SCCategoryAutocomplete'; const classes = { root: `${PREFIX}-root` }; const Root = styled(Autocomplete, { name: PREFIX, slot: 'Root', overridesResolver: (props, styles) => styles.root })(() => ({})); /** * > API documentation for the Community-JS Category 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 categories available in the application. * Take a look at our <strong>demo</strong> component [here](/docs/sdk/community-js/react-ui/Components/CategoryAutocomplete) * * #### Import * ```jsx * import {CategoryAutocomplete} from '@selfcommunity/react-ui'; * ``` * #### Component Name * The name `SCCategoryAutocomplete` can be used when providing style overrides in the theme. * * #### CSS * * |Rule Name|Global class|Description| * |---|---|---| * |root|.SCCategoryAutocomplete-root|Styles applied to the root element.| * * @param inProps */ const CategoryAutocomplete = (inProps) => { const props = useThemeProps({ props: inProps, name: PREFIX }); // Props const { onChange, multiple = false, defaultValue = multiple ? [] : null, limitCountCategories = 0, checkboxSelect = false, disabled = false, endpointQueryParams = {}, TextFieldProps = { variant: 'outlined', label: _jsx(FormattedMessage, { id: "ui.categoryAutocomplete.label", defaultMessage: "ui.categoryAutocomplete.label" }) } } = props, rest = __rest(props, ["onChange", "multiple", "defaultValue", "limitCountCategories", "checkboxSelect", "disabled", "endpointQueryParams", "TextFieldProps"]); // State const [open, setOpen] = useState(false); // eslint-disable-next-line @typescript-eslint/ban-ts-ignore // @ts-ignore const [value, setValue] = useState(typeof defaultValue === 'string' ? null : defaultValue); // HOOKS const { categories, isLoading } = useSCFetchCategories({ endpointQueryParams }); useEffect(() => { if (value === null) { return; } onChange && onChange(value); }, [value]); useEffect(() => { if (!isLoading && typeof defaultValue === 'string') { setValue(multiple ? categories.filter((cat) => cat.id === Number(defaultValue)) : categories.find((cat) => cat.id === Number(defaultValue))); } }, [isLoading]); // Handlers const handleOpen = () => { setOpen(true); }; const handleClose = () => { setOpen(false); }; const handleChange = (event, value) => { let newValue = null; if (multiple && limitCountCategories > 0) { const [...rest] = value; newValue = rest.slice(-1 * limitCountCategories); } else { newValue = value; } setValue(newValue); }; // Render return (_jsx(Root, Object.assign({ className: classes.root, multiple: multiple, open: open, onOpen: handleOpen, onClose: handleClose, filterSelectedOptions: !checkboxSelect, disableCloseOnSelect: checkboxSelect, options: categories || [], getOptionLabel: (option) => option.name || '', value: value, selectOnFocus: true, clearOnBlur: true, blurOnSelect: true, handleHomeEndKeys: true, clearIcon: null, disabled: disabled || isLoading, noOptionsText: _jsx(FormattedMessage, { id: "ui.categoryAutocomplete.empty", defaultMessage: "ui.categoryAutocomplete.empty" }), onChange: handleChange, isOptionEqualToValue: (option, value) => value.id === option.id, renderTags: (value, getTagProps) => { return value.map((option, index) => (_jsx(Chip, Object.assign({ id: option.id, label: option.name, color: option.color }, getTagProps({ index })), option.id))); }, renderOption: (props, option, { selected, inputValue }) => { const matches = match(option.name, inputValue); const parts = parse(option.name, matches); return (_jsxs("li", Object.assign({}, props, { children: [checkboxSelect && _jsx(Checkbox, { style: { marginRight: 8 }, checked: selected }), _jsx(Chip, { label: _jsx(React.Fragment, { children: parts.map((part, index) => (_jsx("span", Object.assign({ style: { fontWeight: part.highlight ? 700 : 400 } }, { children: part.text }), index))) }) })] }))); }, renderInput: (params) => { return (_jsx(TextField, Object.assign({}, params, TextFieldProps, { margin: "dense", InputProps: Object.assign(Object.assign({}, params.InputProps), { autoComplete: 'categories', endAdornment: (_jsxs(React.Fragment, { children: [isLoading ? _jsx(CircularProgress, { color: "inherit", size: 20 }) : null, params.InputProps.endAdornment] })) }) }))); } }, rest))); }; export default CategoryAutocomplete;