@blocklet/list 0.9.28 → 0.9.31

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.
@@ -63,27 +63,38 @@ function Autocomplete(_ref) {
63
63
  return data.dataList || [];
64
64
  }, [storeApi]);
65
65
  const onSubmit = (0, _react.useCallback)(e => {
66
- handleKeyword(e.state.query);
67
- }, [handleKeyword]);
68
- const onReset = (0, _react.useCallback)(() => {
69
- handleKeyword();
70
- }, [handleKeyword]);
71
- (0, _reactHotkeysHook.useHotkeys)('/, ctrl + k, command + k', e => {
72
- e.stopPropagation();
73
- e.preventDefault();
66
+ handleKeyword(e.state.query); // eslint-disable-next-line react-hooks/exhaustive-deps
67
+ }, []);
74
68
 
69
+ const setFocus = () => {
75
70
  if (containerRef.current) {
76
- const input = containerRef.current.querySelector('input.bl-autocomplete-input');
71
+ // see: https://discourse.algolia.com/t/autocomplete-focus-input-field/4019/5
72
+ setTimeout(() => {
73
+ const input = containerRef.current.querySelector('input.bl-autocomplete-input');
77
74
 
78
- if (input) {
79
- input.focus();
80
- }
75
+ if (input) {
76
+ input.focus();
77
+ }
78
+ });
81
79
  }
80
+ };
82
81
 
82
+ const onReset = (0, _react.useCallback)(autocompleteState => {
83
+ handleKeyword();
84
+ setFocus();
85
+ autocompleteState.setIsOpen(false); // eslint-disable-next-line react-hooks/exhaustive-deps
86
+ }, []);
87
+ (0, _reactHotkeysHook.useHotkeys)('/, ctrl + k, command + k', e => {
88
+ e.stopPropagation();
89
+ e.preventDefault();
90
+ setFocus();
83
91
  return false;
84
92
  }, {
85
93
  enableOnTags: ['INPUT']
86
94
  });
95
+ (0, _react.useEffect)(() => {
96
+ setFocus();
97
+ }, [endpoint]);
87
98
  (0, _react.useEffect)(() => {
88
99
  if (!containerRef.current) {
89
100
  return undefined;
@@ -126,7 +137,10 @@ function Autocomplete(_ref) {
126
137
  query: filters.keyword
127
138
  },
128
139
  onSubmit,
129
- onReset,
140
+
141
+ onReset(autocompleteState) {
142
+ onReset(autocompleteState);
143
+ },
130
144
 
131
145
  getSources(_ref3) {
132
146
  let {
@@ -199,7 +213,7 @@ function Autocomplete(_ref) {
199
213
  return () => {
200
214
  search.destroy();
201
215
  };
202
- }, [endpoint, filters, getResults, muiTheme, onReset, onSubmit, onSelect, t]);
216
+ }, [endpoint, filters, getResults, muiTheme, onSubmit, onSelect, t, onReset]);
203
217
  return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
204
218
  className: "bl-search-container",
205
219
  ref: containerRef
@@ -53,11 +53,11 @@ function FilterProvider(_ref) {
53
53
  onSearchSelect,
54
54
  extraFilter
55
55
  } = _ref;
56
-
57
- const storeApi = _axios.default.create({
58
- baseURL: endpoint
59
- });
60
-
56
+ const storeApi = (0, _react.useMemo)(() => {
57
+ return _axios.default.create({
58
+ baseURL: endpoint
59
+ });
60
+ }, [endpoint]);
61
61
  const finalFilters = (0, _react.useMemo)(() => {
62
62
  return _objectSpread({
63
63
  sortBy: 'popularity',
@@ -122,16 +122,14 @@ function FilterProvider(_ref) {
122
122
 
123
123
  return (0, _orderBy.default)(list, [i => i.name], ['asc']);
124
124
  }, [allCategories]);
125
-
126
- const translate = (key, data) => {
125
+ const translate = (0, _react.useCallback)((key, data) => {
127
126
  if (!_locale.default[locale] || !_locale.default[locale][key]) {
128
127
  console.warn("Warning: no ".concat(key, " translation of ").concat(locale));
129
128
  return key;
130
129
  }
131
130
 
132
131
  return (0, _utils.replaceTranslate)(_locale.default[locale][key], data);
133
- };
134
-
132
+ }, [locale]);
135
133
  const blockletList = (0, _react.useMemo)(() => {
136
134
  let blocklets = blockletsState.list || []; // 用户传入的过滤函数
137
135
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blocklet/list",
3
- "version": "0.9.28",
3
+ "version": "0.9.31",
4
4
  "description": "Common ux components of blocklet",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -42,7 +42,7 @@
42
42
  "dependencies": {
43
43
  "@algolia/autocomplete-js": "^1.7.1",
44
44
  "@algolia/autocomplete-theme-classic": "^1.7.1",
45
- "@arcblock/ux": "^2.4.13",
45
+ "@arcblock/ux": "^2.4.16",
46
46
  "@emotion/react": "^11.10.0",
47
47
  "@emotion/styled": "^11.10.0",
48
48
  "@mui/icons-material": "^5.8.4",
@@ -69,5 +69,5 @@
69
69
  "eslint": "^8.22.0",
70
70
  "prettier": "^2.7.1"
71
71
  },
72
- "gitHead": "bddbed0128ffa0254182d41a14e640c764884fb0"
72
+ "gitHead": "6de6f60cc8d0e2a152add1b5d4578215b5de56b0"
73
73
  }
@@ -23,38 +23,50 @@ export default function Autocomplete({ onSelect }) {
23
23
  const getResults = useCallback(
24
24
  async (params) => {
25
25
  const { data } = await storeApi.get(constant.blockletsPath, { params });
26
-
27
26
  return data.dataList || [];
28
27
  },
29
28
  [storeApi]
30
29
  );
31
30
 
32
- const onSubmit = useCallback(
33
- (e) => {
34
- handleKeyword(e.state.query);
35
- },
36
- [handleKeyword]
37
- );
38
- const onReset = useCallback(() => {
31
+ const onSubmit = useCallback((e) => {
32
+ handleKeyword(e.state.query);
33
+ // eslint-disable-next-line react-hooks/exhaustive-deps
34
+ }, []);
35
+
36
+ const setFocus = () => {
37
+ if (containerRef.current) {
38
+ // see: https://discourse.algolia.com/t/autocomplete-focus-input-field/4019/5
39
+ setTimeout(() => {
40
+ const input = containerRef.current.querySelector('input.bl-autocomplete-input');
41
+ if (input) {
42
+ input.focus();
43
+ }
44
+ });
45
+ }
46
+ };
47
+
48
+ const onReset = useCallback((autocompleteState) => {
39
49
  handleKeyword();
40
- }, [handleKeyword]);
50
+ setFocus();
51
+ autocompleteState.setIsOpen(false);
52
+ // eslint-disable-next-line react-hooks/exhaustive-deps
53
+ }, []);
41
54
 
42
55
  useHotkeys(
43
56
  '/, ctrl + k, command + k',
44
57
  (e) => {
45
58
  e.stopPropagation();
46
59
  e.preventDefault();
47
- if (containerRef.current) {
48
- const input = containerRef.current.querySelector('input.bl-autocomplete-input');
49
- if (input) {
50
- input.focus();
51
- }
52
- }
60
+ setFocus();
53
61
  return false;
54
62
  },
55
63
  { enableOnTags: ['INPUT'] }
56
64
  );
57
65
 
66
+ useEffect(() => {
67
+ setFocus();
68
+ }, [endpoint]);
69
+
58
70
  useEffect(() => {
59
71
  if (!containerRef.current) {
60
72
  return undefined;
@@ -86,7 +98,9 @@ export default function Autocomplete({ onSelect }) {
86
98
  query: filters.keyword,
87
99
  },
88
100
  onSubmit,
89
- onReset,
101
+ onReset(autocompleteState) {
102
+ onReset(autocompleteState);
103
+ },
90
104
  getSources({ query }) {
91
105
  const params = {
92
106
  ...filters,
@@ -132,7 +146,7 @@ export default function Autocomplete({ onSelect }) {
132
146
  return () => {
133
147
  search.destroy();
134
148
  };
135
- }, [endpoint, filters, getResults, muiTheme, onReset, onSubmit, onSelect, t]);
149
+ }, [endpoint, filters, getResults, muiTheme, onSubmit, onSelect, t, onReset]);
136
150
 
137
151
  return <div className="bl-search-container" ref={containerRef} />;
138
152
  }
@@ -1,4 +1,4 @@
1
- import { useContext, createContext, useMemo, useState, useEffect } from 'react';
1
+ import { useContext, createContext, useMemo, useState, useEffect, useCallback } from 'react';
2
2
  import PropTypes from 'prop-types';
3
3
  import { useRequest, useReactive, useInfiniteScroll } from 'ahooks';
4
4
  import orderBy from 'lodash/orderBy';
@@ -21,9 +21,11 @@ function FilterProvider({
21
21
  onSearchSelect,
22
22
  extraFilter,
23
23
  }) {
24
- const storeApi = axios.create({
25
- baseURL: endpoint,
26
- });
24
+ const storeApi = useMemo(() => {
25
+ return axios.create({
26
+ baseURL: endpoint,
27
+ });
28
+ }, [endpoint]);
27
29
 
28
30
  const finalFilters = useMemo(() => {
29
31
  return { sortBy: 'popularity', sortDirection: 'desc', ...filters };
@@ -83,14 +85,17 @@ function FilterProvider({
83
85
  return orderBy(list, [(i) => i.name], ['asc']);
84
86
  }, [allCategories]);
85
87
 
86
- const translate = (key, data) => {
87
- if (!translations[locale] || !translations[locale][key]) {
88
- console.warn(`Warning: no ${key} translation of ${locale}`);
89
- return key;
90
- }
88
+ const translate = useCallback(
89
+ (key, data) => {
90
+ if (!translations[locale] || !translations[locale][key]) {
91
+ console.warn(`Warning: no ${key} translation of ${locale}`);
92
+ return key;
93
+ }
91
94
 
92
- return replaceTranslate(translations[locale][key], data);
93
- };
95
+ return replaceTranslate(translations[locale][key], data);
96
+ },
97
+ [locale]
98
+ );
94
99
  const blockletList = useMemo(() => {
95
100
  let blocklets = blockletsState.list || [];
96
101
  // 用户传入的过滤函数