UNPKG

@selfcommunity/react-ui

Version:

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

116 lines (115 loc) 5.95 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 TextField_1 = tslib_1.__importDefault(require("@mui/material/TextField")); const CircularProgress_1 = tslib_1.__importDefault(require("@mui/material/CircularProgress")); const material_1 = require("@mui/material"); const api_services_1 = require("@selfcommunity/api-services"); const styles_1 = require("@mui/material/styles"); const parse_1 = tslib_1.__importDefault(require("autosuggest-highlight/parse")); const match_1 = tslib_1.__importDefault(require("autosuggest-highlight/match")); const system_1 = require("@mui/system"); const PREFIX = 'SCLocationAutocomplete'; const classes = { root: `${PREFIX}-root` }; const Root = (0, styles_1.styled)(material_1.Autocomplete, { name: PREFIX, slot: 'Root', overridesResolver: (props, styles) => styles.root })(() => ({ minWidth: 120 })); /** * > API documentation for the Community-JS Location Autocomplete component. Learn about the available props and the CSS API. * * * This component renders a bar that allows users to search (with autocomplete) for cities names. * Take a look at our <strong>demo</strong> component [here](/docs/sdk/community-js/react-ui/Components/LocationAutocomplete) * * #### Import * ```jsx * import {LocationAutocomplete} from '@selfcommunity/react-ui'; * ``` * #### Component Name * The name `SCLocationAutocomplete` can be used when providing style overrides in the theme. * * #### CSS * * |Rule Name|Global class|Description| * |---|---|---| * |root|.SCLocationAutocomplete-root|Styles applied to the root element.| * * @param inProps */ function LocationAutocomplete(inProps) { // Props const props = (0, system_1.useThemeProps)({ props: inProps, name: PREFIX }); const { defaultValue = null, disabled = false, onChange, TextFieldProps = { variant: 'outlined', label: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.locationAutocomplete.label", defaultMessage: "ui.composer.locations.label" }) } } = props, rest = tslib_1.__rest(props, ["defaultValue", "disabled", "onChange", "TextFieldProps"]); // State const [isLoading, setIsLoading] = (0, react_1.useState)(false); const [locations, setLocations] = (0, react_1.useState)([]); const [value, setValue] = (0, react_1.useState)(defaultValue); const [search, setSearch] = (0, react_1.useState)(''); const load = (offset = 0, limit = 20) => { api_services_1.http .request({ url: api_services_1.Endpoints.ComposerLocalitySearch.url(), method: api_services_1.Endpoints.ComposerLocalitySearch.method, params: { offset, limit, search: search.trim() } }) .then((res) => { setLocations(res.data.results); }) .then(() => setIsLoading(false)); }; // Component update (0, react_1.useEffect)(() => { if (!isLoading && search.trim().length > 0) { load(); } }, [search]); (0, react_1.useEffect)(() => { if (value) { onChange && onChange(value); } }, [value]); // Handlers const handleChange = (event, value) => { setValue(value ? { location: value.full_address, lat: value.lat, lng: value.lng } : null); onChange && onChange(value); }; const handleSearch = (event, _search) => { setSearch(_search); }; // Render const options = (0, react_1.useMemo)(() => { if (!value || typeof value === 'string' || locations.find((loc) => value.lat === loc.lat && value.lng === loc.lng)) { return locations; } return [...locations, { lat: value.lat, lng: value.lng, full_address: value.location }]; }, [value, locations]); return ((0, jsx_runtime_1.jsx)(Root, Object.assign({ className: classes.root, options: options || [], // @ts-ignore getOptionLabel: (option) => (option === null || option === void 0 ? void 0 : option.full_address) || (option === null || option === void 0 ? void 0 : option.location) || '', filterOptions: (x) => x, autoComplete: true, includeInputInList: true, value: value || null, selectOnFocus: true, handleHomeEndKeys: true, disabled: disabled, noOptionsText: (0, jsx_runtime_1.jsx)(react_intl_1.FormattedMessage, { id: "ui.locationAutocomplete.empty", defaultMessage: "ui.locationAutocomplete.empty" }), onChange: handleChange, onInputChange: handleSearch, isOptionEqualToValue: (option, value) => value.lat === option.lat && value.lng === option.lng, 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: 'location', 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] })) }) }))); }, renderOption: (props, option, { inputValue }) => { const matches = (0, match_1.default)(option.full_address, inputValue); const parts = (0, parse_1.default)(option.full_address, matches); return ((0, jsx_runtime_1.jsx)("li", Object.assign({}, props, { style: { whiteSpace: 'break-spaces' } }, { children: parts.map((part, index) => part.highlight ? ((0, jsx_runtime_1.jsx)("span", Object.assign({ style: { fontWeight: 700 } }, { children: part.text }), index)) : (part.text)) }), `${option.lat}_${option.lng}`)); } }, rest))); } exports.default = LocationAutocomplete;