@selfcommunity/react-ui 0.11.0-alpha.110 → 0.11.0-alpha.111
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/components/TagAutocomplete/TagAutocomplete.d.ts +1 -1
- package/lib/cjs/components/TagAutocomplete/TagAutocomplete.js +32 -20
- package/lib/esm/components/TagAutocomplete/TagAutocomplete.d.ts +1 -1
- package/lib/esm/components/TagAutocomplete/TagAutocomplete.js +35 -23
- package/lib/umd/react-ui.js +1 -1
- package/package.json +2 -2
|
@@ -2,7 +2,7 @@ import { AutocompleteProps, TextFieldProps } from '@mui/material';
|
|
|
2
2
|
import { SCTagType } from '@selfcommunity/types';
|
|
3
3
|
import { TagChipProps } from '../../shared/TagChip';
|
|
4
4
|
export interface TagAutocompleteProps extends Pick<AutocompleteProps<SCTagType | null, any, any, any>, Exclude<keyof AutocompleteProps<SCTagType | null, any, any, any>, 'open' | 'onOpen' | 'onClose' | 'onChange' | 'filterSelectedOptions' | 'disableCloseOnSelect' | 'options' | 'getOptionLabel' | 'value' | 'selectOnFocus' | 'clearOnBlur' | 'blurOnSelect' | 'handleHomeEndKeys' | 'noOptionsText' | 'isOptionEqualToValue' | 'renderTags' | 'renderOption' | 'renderInput'>> {
|
|
5
|
-
defaultValue: SCTagType[] |
|
|
5
|
+
defaultValue: SCTagType[] | string;
|
|
6
6
|
/**
|
|
7
7
|
* The props applied to text field
|
|
8
8
|
* @default {variant: 'outlined, label: tags_label}
|
|
@@ -10,6 +10,8 @@ const material_1 = require("@mui/material");
|
|
|
10
10
|
const system_1 = require("@mui/system");
|
|
11
11
|
const TagChip_1 = tslib_1.__importDefault(require("../../shared/TagChip"));
|
|
12
12
|
const api_services_1 = require("@selfcommunity/api-services");
|
|
13
|
+
const Errors_1 = require("../../constants/Errors");
|
|
14
|
+
const utils_1 = require("@selfcommunity/utils");
|
|
13
15
|
const PREFIX = 'SCTagAutocomplete';
|
|
14
16
|
const classes = {
|
|
15
17
|
root: `${PREFIX}-root`
|
|
@@ -55,24 +57,28 @@ const TagAutocomplete = (inProps) => {
|
|
|
55
57
|
const [value, setValue] = (0, react_1.useState)(typeof defaultValue === 'string' ? null : defaultValue);
|
|
56
58
|
const [tags, setTags] = (0, react_1.useState)([]);
|
|
57
59
|
const [inputValue, setInputValue] = (0, react_1.useState)('');
|
|
60
|
+
const [loading, setLoading] = (0, react_1.useState)(true);
|
|
61
|
+
const fetchTags = (query) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
62
|
+
setLoading(true);
|
|
63
|
+
try {
|
|
64
|
+
const res = yield api_services_1.TagService.searchUserTags({ search: query || '' });
|
|
65
|
+
const results = (res === null || res === void 0 ? void 0 : res.results) || [];
|
|
66
|
+
setTags(results);
|
|
67
|
+
return results;
|
|
68
|
+
}
|
|
69
|
+
catch (error) {
|
|
70
|
+
utils_1.Logger.error(Errors_1.SCOPE_SC_UI, error);
|
|
71
|
+
setTags([]);
|
|
72
|
+
return [];
|
|
73
|
+
}
|
|
74
|
+
finally {
|
|
75
|
+
setLoading(false);
|
|
76
|
+
}
|
|
77
|
+
});
|
|
58
78
|
(0, react_1.useEffect)(() => {
|
|
59
|
-
let active = true;
|
|
60
79
|
if (open && inputValue.length >= 3) {
|
|
61
|
-
|
|
62
|
-
.then((res) => {
|
|
63
|
-
if (active) {
|
|
64
|
-
setTags((res === null || res === void 0 ? void 0 : res.results) || []);
|
|
65
|
-
}
|
|
66
|
-
})
|
|
67
|
-
.catch(() => {
|
|
68
|
-
if (active) {
|
|
69
|
-
setTags([]);
|
|
70
|
-
}
|
|
71
|
-
});
|
|
80
|
+
fetchTags(inputValue);
|
|
72
81
|
}
|
|
73
|
-
return () => {
|
|
74
|
-
active = false;
|
|
75
|
-
};
|
|
76
82
|
}, [open, inputValue]);
|
|
77
83
|
(0, react_1.useEffect)(() => {
|
|
78
84
|
if (value !== null) {
|
|
@@ -80,10 +86,16 @@ const TagAutocomplete = (inProps) => {
|
|
|
80
86
|
}
|
|
81
87
|
}, [value]);
|
|
82
88
|
(0, react_1.useEffect)(() => {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
89
|
+
const loadDefault = () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
|
|
90
|
+
if (typeof defaultValue === 'string' && defaultValue.trim() !== '') {
|
|
91
|
+
const results = yield fetchTags(defaultValue);
|
|
92
|
+
const match = results.find((t) => t.name === defaultValue);
|
|
93
|
+
if (match)
|
|
94
|
+
setValue(match);
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
loadDefault();
|
|
98
|
+
}, [defaultValue]);
|
|
87
99
|
// Handlers
|
|
88
100
|
const handleOpen = () => {
|
|
89
101
|
if (inputValue.length >= 3) {
|
|
@@ -97,7 +109,7 @@ const TagAutocomplete = (inProps) => {
|
|
|
97
109
|
const parts = (0, parse_1.default)(option.name, matches);
|
|
98
110
|
return ((0, jsx_runtime_1.jsx)("li", Object.assign({}, props, { children: (0, jsx_runtime_1.jsx)(TagChip_1.default, Object.assign({ disposable: false, tag: option, label: (0, jsx_runtime_1.jsx)(react_1.default.Fragment, { children: parts.map((part, index) => ((0, jsx_runtime_1.jsx)("span", Object.assign({ style: { fontWeight: part.highlight ? 700 : 400 } }, { children: part.text }), index))) }) }, TagChipProps), option.id) })));
|
|
99
111
|
}, renderInput: (params) => {
|
|
100
|
-
return ((0, jsx_runtime_1.jsx)(material_1.TextField, Object.assign({}, params, TextFieldProps, { InputProps: Object.assign(Object.assign({}, params.InputProps), { autoComplete: 'addressing', endAdornment: tags.length > 0 ? (0, jsx_runtime_1.
|
|
112
|
+
return ((0, jsx_runtime_1.jsx)(material_1.TextField, Object.assign({}, params, TextFieldProps, { InputProps: Object.assign(Object.assign({}, params.InputProps), { autoComplete: 'addressing', endAdornment: tags.length > 0 ? ((0, jsx_runtime_1.jsxs)(react_1.Fragment, { children: [' ', loading && (0, jsx_runtime_1.jsx)(material_1.CircularProgress, { color: "inherit", size: 20 }), params.InputProps.endAdornment] })) : null }) })));
|
|
101
113
|
} }, rest)));
|
|
102
114
|
};
|
|
103
115
|
exports.default = TagAutocomplete;
|
|
@@ -2,7 +2,7 @@ import { AutocompleteProps, TextFieldProps } from '@mui/material';
|
|
|
2
2
|
import { SCTagType } from '@selfcommunity/types';
|
|
3
3
|
import { TagChipProps } from '../../shared/TagChip';
|
|
4
4
|
export interface TagAutocompleteProps extends Pick<AutocompleteProps<SCTagType | null, any, any, any>, Exclude<keyof AutocompleteProps<SCTagType | null, any, any, any>, 'open' | 'onOpen' | 'onClose' | 'onChange' | 'filterSelectedOptions' | 'disableCloseOnSelect' | 'options' | 'getOptionLabel' | 'value' | 'selectOnFocus' | 'clearOnBlur' | 'blurOnSelect' | 'handleHomeEndKeys' | 'noOptionsText' | 'isOptionEqualToValue' | 'renderTags' | 'renderOption' | 'renderInput'>> {
|
|
5
|
-
defaultValue: SCTagType[] |
|
|
5
|
+
defaultValue: SCTagType[] | string;
|
|
6
6
|
/**
|
|
7
7
|
* The props applied to text field
|
|
8
8
|
* @default {variant: 'outlined, label: tags_label}
|
|
@@ -1,13 +1,15 @@
|
|
|
1
|
-
import { __rest } from "tslib";
|
|
2
|
-
import { jsx as _jsx } from "react/jsx-runtime";
|
|
1
|
+
import { __awaiter, __rest } from "tslib";
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
3
|
import React, { Fragment, useEffect, useState } from 'react';
|
|
4
4
|
import { FormattedMessage } from 'react-intl';
|
|
5
5
|
import parse from 'autosuggest-highlight/parse';
|
|
6
6
|
import match from 'autosuggest-highlight/match';
|
|
7
|
-
import { Autocomplete, TextField, styled } from '@mui/material';
|
|
7
|
+
import { Autocomplete, TextField, styled, CircularProgress } from '@mui/material';
|
|
8
8
|
import { useThemeProps } from '@mui/system';
|
|
9
9
|
import TagChip from '../../shared/TagChip';
|
|
10
10
|
import { TagService } from '@selfcommunity/api-services';
|
|
11
|
+
import { SCOPE_SC_UI } from '../../constants/Errors';
|
|
12
|
+
import { Logger } from '@selfcommunity/utils';
|
|
11
13
|
const PREFIX = 'SCTagAutocomplete';
|
|
12
14
|
const classes = {
|
|
13
15
|
root: `${PREFIX}-root`
|
|
@@ -53,24 +55,28 @@ const TagAutocomplete = (inProps) => {
|
|
|
53
55
|
const [value, setValue] = useState(typeof defaultValue === 'string' ? null : defaultValue);
|
|
54
56
|
const [tags, setTags] = useState([]);
|
|
55
57
|
const [inputValue, setInputValue] = useState('');
|
|
58
|
+
const [loading, setLoading] = useState(true);
|
|
59
|
+
const fetchTags = (query) => __awaiter(void 0, void 0, void 0, function* () {
|
|
60
|
+
setLoading(true);
|
|
61
|
+
try {
|
|
62
|
+
const res = yield TagService.searchUserTags({ search: query || '' });
|
|
63
|
+
const results = (res === null || res === void 0 ? void 0 : res.results) || [];
|
|
64
|
+
setTags(results);
|
|
65
|
+
return results;
|
|
66
|
+
}
|
|
67
|
+
catch (error) {
|
|
68
|
+
Logger.error(SCOPE_SC_UI, error);
|
|
69
|
+
setTags([]);
|
|
70
|
+
return [];
|
|
71
|
+
}
|
|
72
|
+
finally {
|
|
73
|
+
setLoading(false);
|
|
74
|
+
}
|
|
75
|
+
});
|
|
56
76
|
useEffect(() => {
|
|
57
|
-
let active = true;
|
|
58
77
|
if (open && inputValue.length >= 3) {
|
|
59
|
-
|
|
60
|
-
.then((res) => {
|
|
61
|
-
if (active) {
|
|
62
|
-
setTags((res === null || res === void 0 ? void 0 : res.results) || []);
|
|
63
|
-
}
|
|
64
|
-
})
|
|
65
|
-
.catch(() => {
|
|
66
|
-
if (active) {
|
|
67
|
-
setTags([]);
|
|
68
|
-
}
|
|
69
|
-
});
|
|
78
|
+
fetchTags(inputValue);
|
|
70
79
|
}
|
|
71
|
-
return () => {
|
|
72
|
-
active = false;
|
|
73
|
-
};
|
|
74
80
|
}, [open, inputValue]);
|
|
75
81
|
useEffect(() => {
|
|
76
82
|
if (value !== null) {
|
|
@@ -78,10 +84,16 @@ const TagAutocomplete = (inProps) => {
|
|
|
78
84
|
}
|
|
79
85
|
}, [value]);
|
|
80
86
|
useEffect(() => {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
87
|
+
const loadDefault = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
88
|
+
if (typeof defaultValue === 'string' && defaultValue.trim() !== '') {
|
|
89
|
+
const results = yield fetchTags(defaultValue);
|
|
90
|
+
const match = results.find((t) => t.name === defaultValue);
|
|
91
|
+
if (match)
|
|
92
|
+
setValue(match);
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
loadDefault();
|
|
96
|
+
}, [defaultValue]);
|
|
85
97
|
// Handlers
|
|
86
98
|
const handleOpen = () => {
|
|
87
99
|
if (inputValue.length >= 3) {
|
|
@@ -95,7 +107,7 @@ const TagAutocomplete = (inProps) => {
|
|
|
95
107
|
const parts = parse(option.name, matches);
|
|
96
108
|
return (_jsx("li", Object.assign({}, props, { children: _jsx(TagChip, Object.assign({ disposable: false, 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) })));
|
|
97
109
|
}, renderInput: (params) => {
|
|
98
|
-
return (_jsx(TextField, Object.assign({}, params, TextFieldProps, { InputProps: Object.assign(Object.assign({}, params.InputProps), { autoComplete: 'addressing', endAdornment: tags.length > 0 ?
|
|
110
|
+
return (_jsx(TextField, Object.assign({}, params, TextFieldProps, { InputProps: Object.assign(Object.assign({}, params.InputProps), { autoComplete: 'addressing', endAdornment: tags.length > 0 ? (_jsxs(Fragment, { children: [' ', loading && _jsx(CircularProgress, { color: "inherit", size: 20 }), params.InputProps.endAdornment] })) : null }) })));
|
|
99
111
|
} }, rest)));
|
|
100
112
|
};
|
|
101
113
|
export default TagAutocomplete;
|