@selfcommunity/react-ui
Version:
React UI Components to integrate a Community created with SelfCommunity Platform.
91 lines (90 loc) • 4.58 kB
JavaScript
import { __rest } from "tslib";
import { jsx as _jsx } 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 parse from 'autosuggest-highlight/parse';
import match from 'autosuggest-highlight/match';
import { useSCFetchAddressingTagList } from '@selfcommunity/react-core';
import { styled } from '@mui/material/styles';
import { useThemeProps } from '@mui/system';
import TagChip from '../../shared/TagChip';
const PREFIX = 'SCTagAutocomplete';
const classes = {
root: `${PREFIX}-root`
};
const Root = styled(Autocomplete, {
name: PREFIX,
slot: 'Root',
overridesResolver: (props, styles) => styles.root
})(() => ({}));
/**
* > API documentation for the Community-JS Tag 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 tags available in the application.
*
* #### Import
* ```jsx
* import {TagAutocomplete} from '@selfcommunity/react-ui';
* ```
* #### Component Name
* The name `SCTagAutocomplete` can be used when providing style overrides in the theme.
*
* #### CSS
*
* |Rule Name|Global class|Description|
* |---|---|---|
* |root|.SCTagAutocomplete-root|Styles applied to the root element.|
*
* @param inProps
*/
const TagAutocomplete = (inProps) => {
const props = useThemeProps({
props: inProps,
name: PREFIX
});
// Props
const { onChange, defaultValue = null, TextFieldProps = {
variant: 'outlined',
label: _jsx(FormattedMessage, { id: "ui.composer.layer.audience.tags.label", defaultMessage: "ui.composer.layer.audience.tags.label" })
}, TagChipProps = {} } = props, rest = __rest(props, ["onChange", "defaultValue", "TextFieldProps", "TagChipProps"]);
// State
const [open, setOpen] = useState(false);
const [value, setValue] = useState(typeof defaultValue === 'string' ? null : defaultValue);
// HOOKS
const { scAddressingTags } = useSCFetchAddressingTagList({ fetch: open || defaultValue });
useEffect(() => {
if (value === null) {
return;
}
onChange && onChange(value);
}, [value]);
useEffect(() => {
if ((scAddressingTags === null || scAddressingTags === void 0 ? void 0 : scAddressingTags.length) !== 0 && typeof defaultValue === 'string') {
setValue(scAddressingTags.find((t) => t.id === Number(defaultValue)));
}
}, [scAddressingTags, defaultValue]);
// Handlers
const handleOpen = () => {
setOpen(true);
};
const handleClose = () => {
setOpen(false);
};
const handleChange = (event, newValue) => {
setValue(newValue);
};
// Render
return (_jsx(Root, Object.assign({ className: classes.root, open: open, onOpen: handleOpen, onClose: handleClose, options: scAddressingTags || [], getOptionLabel: (option) => option.name || '', value: value, selectOnFocus: true, clearOnBlur: true, handleHomeEndKeys: true, clearIcon: null, noOptionsText: _jsx(FormattedMessage, { id: "ui.composer.layer.audience.tags.empty", defaultMessage: "ui.composer.layer.audience.tags.empty" }), onChange: handleChange, isOptionEqualToValue: (option, value) => (value === null || value === void 0 ? void 0 : value.id) === (option === null || option === void 0 ? void 0 : option.id), renderTags: (value, getTagProps) => {
return value.map((option, index) => _jsx(TagChip, Object.assign({ tag: option }, getTagProps({ index })), option.id));
}, renderOption: (props, option, { selected, inputValue }) => {
const matches = match(option.name, inputValue);
const parts = parse(option.name, matches);
return (_jsx("li", Object.assign({}, props, { children: _jsx(TagChip, Object.assign({ tag: option, label: _jsx(React.Fragment, { children: parts.map((part, index) => (_jsx("span", Object.assign({ style: { fontWeight: part.highlight ? 700 : 400 } }, { children: part.text }), index))) }) }, TagChipProps), option.id) })));
}, renderInput: (params) => {
return (_jsx(TextField, Object.assign({}, params, TextFieldProps, { InputProps: Object.assign(Object.assign({}, params.InputProps), { autoComplete: 'addressing', endAdornment: _jsx(React.Fragment, { children: params.InputProps.endAdornment }) }) })));
} }, rest)));
};
export default TagAutocomplete;