@blocklet/list 0.9.16 → 0.9.19

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/base.js CHANGED
@@ -50,6 +50,7 @@ function ListBase() {
50
50
  handleSort,
51
51
  handleCategory,
52
52
  handlePrice,
53
+ handleSearchSelect,
53
54
  t,
54
55
  getCategoryLocale,
55
56
  priceOptions
@@ -71,7 +72,9 @@ function ListBase() {
71
72
  className: "filter-bar",
72
73
  display: "flex",
73
74
  alignItems: "center",
74
- children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_autocomplete.default, {}), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_material.Box, {
75
+ children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_autocomplete.default, {
76
+ onSelect: handleSearchSelect
77
+ }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_material.Box, {
75
78
  mt: 0,
76
79
  ml: "16px",
77
80
  className: "filter-container",
@@ -7,6 +7,8 @@ exports.default = Autocomplete;
7
7
 
8
8
  var _react = require("react");
9
9
 
10
+ var _propTypes = _interopRequireDefault(require("prop-types"));
11
+
10
12
  var _client = require("react-dom/client");
11
13
 
12
14
  var _autocompleteJs = require("@algolia/autocomplete-js");
@@ -35,7 +37,10 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
35
37
 
36
38
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
37
39
 
38
- function Autocomplete() {
40
+ function Autocomplete(_ref) {
41
+ let {
42
+ onSelect
43
+ } = _ref;
39
44
  const containerRef = (0, _react.useRef)(null);
40
45
  const panelRootRef = (0, _react.useRef)(null);
41
46
  const rootRef = (0, _react.useRef)(null);
@@ -65,11 +70,6 @@ function Autocomplete() {
65
70
 
66
71
  return blockletDetail;
67
72
  }, [endpoint, serverUrl]);
68
-
69
- const onClickItem = detailUrl => {
70
- window.location.href = detailUrl;
71
- };
72
-
73
73
  const onSubmit = (0, _react.useCallback)(e => {
74
74
  handleKeyword(e.state.query);
75
75
  }, [handleKeyword]);
@@ -89,10 +89,10 @@ function Autocomplete() {
89
89
  render: () => {}
90
90
  },
91
91
 
92
- render(_ref, root) {
92
+ render(_ref2, root) {
93
93
  let {
94
94
  children
95
- } = _ref;
95
+ } = _ref2;
96
96
 
97
97
  if (!panelRootRef.current || rootRef.current !== root) {
98
98
  var _panelRootRef$current;
@@ -119,10 +119,10 @@ function Autocomplete() {
119
119
  onSubmit,
120
120
  onReset,
121
121
 
122
- getSources(_ref2) {
122
+ getSources(_ref3) {
123
123
  let {
124
124
  query
125
- } = _ref2;
125
+ } = _ref3;
126
126
 
127
127
  const params = _objectSpread(_objectSpread({}, filters), {}, {
128
128
  sortBy: _constant.default[filters.sortBy],
@@ -139,29 +139,31 @@ function Autocomplete() {
139
139
  },
140
140
 
141
141
  // 选中后填充 搜索框中值
142
- getItemInputValue(_ref3) {
142
+ getItemInputValue(_ref4) {
143
143
  let {
144
144
  item
145
- } = _ref3;
145
+ } = _ref4;
146
146
  return item.title;
147
147
  },
148
148
 
149
- getItemUrl(_ref4) {
149
+ onSelect(_ref5) {
150
150
  let {
151
151
  item
152
- } = _ref4;
153
- const blockletDetail = getBlockletDetail(item);
154
- return blockletDetail;
152
+ } = _ref5;
153
+ onSelect({
154
+ blocklet: item,
155
+ detailUrl: getBlockletDetail(item),
156
+ storeUrl: endpoint
157
+ });
155
158
  },
156
159
 
157
160
  templates: {
158
161
  // eslint-disable-next-line react/no-unstable-nested-components
159
- item(_ref5) {
162
+ item(_ref6) {
160
163
  let {
161
164
  item
162
- } = _ref5;
165
+ } = _ref6;
163
166
  const logoUrl = item.logo ? (0, _urlJoin.default)(endpoint, 'assets', item.did, item.logo) : null;
164
- const blockletDetail = getBlockletDetail(item);
165
167
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_styles.ThemeProvider, {
166
168
  theme: muiTheme,
167
169
  children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_react2.ThemeProvider, {
@@ -171,8 +173,7 @@ function Autocomplete() {
171
173
  title: (item === null || item === void 0 ? void 0 : item.title) || (item === null || item === void 0 ? void 0 : item.name),
172
174
  did: item.did,
173
175
  description: item.description,
174
- cover: logoUrl,
175
- onMainClick: () => onClickItem(blockletDetail)
176
+ cover: logoUrl
176
177
  })
177
178
  })
178
179
  });
@@ -190,9 +191,13 @@ function Autocomplete() {
190
191
  return () => {
191
192
  search.destroy();
192
193
  };
193
- }, [endpoint, filters, getBlockletDetail, getResults, muiTheme, onReset, onSubmit, t]);
194
+ }, [endpoint, filters, getBlockletDetail, getResults, muiTheme, onReset, onSubmit, onSelect, t]);
194
195
  return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
195
196
  className: "bl-search-container",
196
197
  ref: containerRef
197
198
  });
198
- }
199
+ }
200
+
201
+ Autocomplete.propTypes = {
202
+ onSelect: _propTypes.default.func.isRequired
203
+ };
@@ -17,7 +17,7 @@ var _Avatar = _interopRequireDefault(require("@arcblock/did-connect/lib/Avatar")
17
17
 
18
18
  var _jsxRuntime = require("react/jsx-runtime");
19
19
 
20
- const _excluded = ["cover", "onMainClick", "item", "className"];
20
+ const _excluded = ["cover", "item", "className"];
21
21
 
22
22
  var _templateObject;
23
23
 
@@ -40,46 +40,22 @@ const Div = _styled.default.div(_templateObject || (_templateObject = _taggedTem
40
40
  function Blocklet(_ref) {
41
41
  let {
42
42
  cover,
43
- onMainClick,
44
43
  item,
45
44
  className
46
45
  } = _ref,
47
46
  rest = _objectWithoutProperties(_ref, _excluded);
48
47
 
49
- const wrapHandler = function wrapHandler(handler) {
50
- let stopFn = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : () => false;
51
- return function (e) {
52
- if (stopFn()) {
53
- e.preventDefault();
54
- e.stopPropagation();
55
- } else if (handler instanceof Function) {
56
- e.preventDefault();
57
- e.stopPropagation();
58
-
59
- for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
60
- args[_key - 1] = arguments[_key];
61
- }
62
-
63
- handler(...args);
64
- }
65
- };
66
- };
67
-
68
- const _onMainClick = wrapHandler(onMainClick);
69
-
70
48
  const {
71
49
  did,
72
50
  description,
73
51
  title,
74
52
  name
75
53
  } = item;
76
- const blockletTitle = title || name;
77
54
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(Div, _objectSpread(_objectSpread({
78
55
  className: "".concat(className, " arcblock-blocklet")
79
56
  }, rest), {}, {
80
57
  children: /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
81
58
  className: "arcblock-blocklet__content",
82
- onClick: _onMainClick,
83
59
  children: [cover ? /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
84
60
  className: "arcblock-blocklet__cover",
85
61
  children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Img.default, {
@@ -103,7 +79,7 @@ function Blocklet(_ref) {
103
79
  children: /*#__PURE__*/(0, _jsxRuntime.jsx)(ShowAttributes, {
104
80
  item: item,
105
81
  attribute: "title",
106
- value: blockletTitle
82
+ value: title || name
107
83
  })
108
84
  }), description && /*#__PURE__*/(0, _jsxRuntime.jsx)(_Typography.default, {
109
85
  component: "div",
@@ -124,13 +100,11 @@ function Blocklet(_ref) {
124
100
 
125
101
  Blocklet.propTypes = {
126
102
  cover: _propTypes.default.string,
127
- onMainClick: _propTypes.default.func,
128
103
  className: _propTypes.default.string,
129
104
  item: _propTypes.default.object.isRequired
130
105
  };
131
106
  Blocklet.defaultProps = {
132
107
  cover: null,
133
- onMainClick: null,
134
108
  className: null
135
109
  };
136
110
 
@@ -140,13 +114,17 @@ function ShowAttributes(_ref2) {
140
114
  attribute,
141
115
  value
142
116
  } = _ref2;
143
- // eslint-disable-next-line react/no-danger
144
- if (item._formatted) return /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
145
- "data-cy": "bl-autocomplete-item",
146
- dangerouslySetInnerHTML: {
147
- __html: item._formatted[attribute]
148
- }
149
- });
117
+
118
+ if (item._formatted) {
119
+ // eslint-disable-next-line react/no-danger
120
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
121
+ "data-cy": "bl-autocomplete-item",
122
+ dangerouslySetInnerHTML: {
123
+ __html: item._formatted[attribute]
124
+ }
125
+ });
126
+ }
127
+
150
128
  return value;
151
129
  }
152
130
 
@@ -50,6 +50,7 @@ function FilterProvider(_ref) {
50
50
  locale,
51
51
  blockletRender,
52
52
  onFilterChange,
53
+ onSearchSelect,
53
54
  extraFilter,
54
55
  serverUrl
55
56
  } = _ref;
@@ -114,7 +115,7 @@ function FilterProvider(_ref) {
114
115
  const {
115
116
  data
116
117
  } = await storeApi.get(_constant.default.categoriesPath);
117
- return data;
118
+ return Array.isArray(data) ? data : [];
118
119
  }, {
119
120
  initialData: []
120
121
  });
@@ -239,8 +240,9 @@ function FilterProvider(_ref) {
239
240
 
240
241
  return (blocklet === null || blocklet === void 0 ? void 0 : (_blocklet$owner = blocklet.owner) === null || _blocklet$owner === void 0 ? void 0 : _blocklet$owner.did) === finalFilters.owner;
241
242
  })) === null || _blocklets$find === void 0 ? void 0 : (_blocklets$find$owner = _blocklets$find.owner) === null || _blocklets$find$owner === void 0 ? void 0 : _blocklets$find$owner.name) || '';
242
- }
243
+ },
243
244
 
245
+ handleSearchSelect: onSearchSelect
244
246
  };
245
247
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(Provider, {
246
248
  value: filterStore,
@@ -22,6 +22,7 @@ const propTypes = {
22
22
  endpoint: _propTypes.default.string.isRequired,
23
23
  blockletRender: _propTypes.default.func.isRequired,
24
24
  onFilterChange: _propTypes.default.func,
25
+ onSearchSelect: _propTypes.default.func,
25
26
  locale: _propTypes.default.oneOf(['zh', 'en']),
26
27
  serverUrl: _propTypes.default.string
27
28
  };
@@ -30,6 +31,12 @@ const defaultProps = {
30
31
  locale: 'zh',
31
32
  filters: {},
32
33
  onFilterChange: () => {},
34
+ onSearchSelect: _ref => {
35
+ let {
36
+ detailUrl
37
+ } = _ref;
38
+ window.location.href = detailUrl;
39
+ },
33
40
  extraFilter: list => list,
34
41
  serverUrl: null
35
42
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blocklet/list",
3
- "version": "0.9.16",
3
+ "version": "0.9.19",
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.9",
45
+ "@arcblock/ux": "^2.4.11",
46
46
  "@emotion/react": "^11.10.0",
47
47
  "@emotion/styled": "^11.10.0",
48
48
  "@mui/icons-material": "^5.8.4",
@@ -68,5 +68,5 @@
68
68
  "eslint": "^8.22.0",
69
69
  "prettier": "^2.7.1"
70
70
  },
71
- "gitHead": "5a44b42bbcf2e691b3a0665c6639585a5c875ede"
71
+ "gitHead": "c166d6de848c0c1eecf38a65360ca75a56a8a020"
72
72
  }
package/src/base.js CHANGED
@@ -21,6 +21,7 @@ function ListBase() {
21
21
  handleSort,
22
22
  handleCategory,
23
23
  handlePrice,
24
+ handleSearchSelect,
24
25
  t,
25
26
  getCategoryLocale,
26
27
  priceOptions,
@@ -38,7 +39,7 @@ function ListBase() {
38
39
  <StyledMin>
39
40
  <FilterContainer>
40
41
  <Box className="filter-bar" display="flex" alignItems="center">
41
- <Autocomplete />
42
+ <Autocomplete onSelect={handleSearchSelect} />
42
43
  <Box mt={0} ml="16px" className="filter-container">
43
44
  <Hidden mdUp>
44
45
  {/* 小屏幕下类别 */}
@@ -1,4 +1,5 @@
1
1
  import { createElement, Fragment, useEffect, useRef, useCallback } from 'react';
2
+ import PropTypes from 'prop-types';
2
3
  import { createRoot } from 'react-dom/client';
3
4
  import { autocomplete } from '@algolia/autocomplete-js';
4
5
 
@@ -11,7 +12,7 @@ import { debounced } from '../../libs/utils';
11
12
  import { useFilterContext } from '../../contexts/filter';
12
13
  import Blocklet from './item';
13
14
 
14
- export default function Autocomplete() {
15
+ export default function Autocomplete({ onSelect }) {
15
16
  const containerRef = useRef(null);
16
17
  const panelRootRef = useRef(null);
17
18
  const rootRef = useRef(null);
@@ -38,9 +39,6 @@ export default function Autocomplete() {
38
39
  [endpoint, serverUrl]
39
40
  );
40
41
 
41
- const onClickItem = (detailUrl) => {
42
- window.location.href = detailUrl;
43
- };
44
42
  const onSubmit = useCallback(
45
43
  (e) => {
46
44
  handleKeyword(e.state.query);
@@ -100,15 +98,13 @@ export default function Autocomplete() {
100
98
  getItemInputValue({ item }) {
101
99
  return item.title;
102
100
  },
103
- getItemUrl({ item }) {
104
- const blockletDetail = getBlockletDetail(item);
105
- return blockletDetail;
101
+ onSelect({ item }) {
102
+ onSelect({ blocklet: item, detailUrl: getBlockletDetail(item), storeUrl: endpoint });
106
103
  },
107
104
  templates: {
108
105
  // eslint-disable-next-line react/no-unstable-nested-components
109
106
  item({ item }) {
110
107
  const logoUrl = item.logo ? joinUrl(endpoint, 'assets', item.did, item.logo) : null;
111
- const blockletDetail = getBlockletDetail(item);
112
108
  return (
113
109
  <MuiThemeProvider theme={muiTheme}>
114
110
  <EmotionThemeProvider theme={muiTheme}>
@@ -118,7 +114,6 @@ export default function Autocomplete() {
118
114
  did={item.did}
119
115
  description={item.description}
120
116
  cover={logoUrl}
121
- onMainClick={() => onClickItem(blockletDetail)}
122
117
  />
123
118
  </EmotionThemeProvider>
124
119
  </MuiThemeProvider>
@@ -136,7 +131,11 @@ export default function Autocomplete() {
136
131
  return () => {
137
132
  search.destroy();
138
133
  };
139
- }, [endpoint, filters, getBlockletDetail, getResults, muiTheme, onReset, onSubmit, t]);
134
+ }, [endpoint, filters, getBlockletDetail, getResults, muiTheme, onReset, onSubmit, onSelect, t]);
140
135
 
141
136
  return <div className="bl-search-container" ref={containerRef} />;
142
137
  }
138
+
139
+ Autocomplete.propTypes = {
140
+ onSelect: PropTypes.func.isRequired,
141
+ };
@@ -60,26 +60,12 @@ const Div = styled.div`
60
60
  }
61
61
  `;
62
62
 
63
- export default function Blocklet({ cover, onMainClick, item, className, ...rest }) {
64
- const wrapHandler =
65
- (handler, stopFn = () => false) =>
66
- (e, ...args) => {
67
- if (stopFn()) {
68
- e.preventDefault();
69
- e.stopPropagation();
70
- } else if (handler instanceof Function) {
71
- e.preventDefault();
72
- e.stopPropagation();
73
- handler(...args);
74
- }
75
- };
76
- const _onMainClick = wrapHandler(onMainClick);
63
+ export default function Blocklet({ cover, item, className, ...rest }) {
77
64
  const { did, description, title, name } = item;
78
- const blockletTitle = title || name;
79
65
 
80
66
  return (
81
67
  <Div className={`${className} arcblock-blocklet`} {...rest}>
82
- <div className="arcblock-blocklet__content" onClick={_onMainClick}>
68
+ <div className="arcblock-blocklet__content">
83
69
  {cover ? (
84
70
  <div className="arcblock-blocklet__cover">
85
71
  <Img src={cover} />
@@ -94,7 +80,7 @@ export default function Blocklet({ cover, onMainClick, item, className, ...rest
94
80
  <div className="arcblock-blocklet__info">
95
81
  <div className="arcblock-blocklet__text">
96
82
  <Typography component="h3" variant="h3" className="arcblock-blocklet__title" title={title}>
97
- <ShowAttributes item={item} attribute="title" value={blockletTitle} />
83
+ <ShowAttributes item={item} attribute="title" value={title || name} />
98
84
  </Typography>
99
85
  {description && (
100
86
  <Typography component="div" variant="body2" className="arcblock-blocklet__describe" title={description}>
@@ -110,21 +96,21 @@ export default function Blocklet({ cover, onMainClick, item, className, ...rest
110
96
 
111
97
  Blocklet.propTypes = {
112
98
  cover: PropTypes.string,
113
- onMainClick: PropTypes.func,
114
99
  className: PropTypes.string,
115
100
  item: PropTypes.object.isRequired,
116
101
  };
117
102
 
118
103
  Blocklet.defaultProps = {
119
104
  cover: null,
120
- onMainClick: null,
121
105
  className: null,
122
106
  };
123
107
 
124
108
  function ShowAttributes({ item, attribute, value }) {
125
- // eslint-disable-next-line react/no-danger
126
- if (item._formatted)
109
+ if (item._formatted) {
110
+ // eslint-disable-next-line react/no-danger
127
111
  return <span data-cy="bl-autocomplete-item" dangerouslySetInnerHTML={{ __html: item._formatted[attribute] }} />;
112
+ }
113
+
128
114
  return value;
129
115
  }
130
116
  ShowAttributes.propTypes = {
@@ -18,6 +18,7 @@ function FilterProvider({
18
18
  locale,
19
19
  blockletRender,
20
20
  onFilterChange,
21
+ onSearchSelect,
21
22
  extraFilter,
22
23
  serverUrl,
23
24
  }) {
@@ -72,7 +73,7 @@ function FilterProvider({
72
73
  } = useRequest(
73
74
  async () => {
74
75
  const { data } = await storeApi.get(constant.categoriesPath);
75
- return data;
76
+ return Array.isArray(data) ? data : [];
76
77
  },
77
78
  { initialData: [] }
78
79
  );
@@ -169,6 +170,7 @@ function FilterProvider({
169
170
  const blocklets = blockletList;
170
171
  return blocklets.find((blocklet) => blocklet?.owner?.did === finalFilters.owner)?.owner?.name || '';
171
172
  },
173
+ handleSearchSelect: onSearchSelect,
172
174
  };
173
175
 
174
176
  return <Provider value={filterStore}>{children}</Provider>;
@@ -13,6 +13,7 @@ const propTypes = {
13
13
  endpoint: PropTypes.string.isRequired,
14
14
  blockletRender: PropTypes.func.isRequired,
15
15
  onFilterChange: PropTypes.func,
16
+ onSearchSelect: PropTypes.func,
16
17
  locale: PropTypes.oneOf(['zh', 'en']),
17
18
  serverUrl: PropTypes.string,
18
19
  };
@@ -21,6 +22,9 @@ const defaultProps = {
21
22
  locale: 'zh',
22
23
  filters: {},
23
24
  onFilterChange: () => {},
25
+ onSearchSelect: ({ detailUrl }) => {
26
+ window.location.href = detailUrl;
27
+ },
24
28
  extraFilter: (list) => list,
25
29
  serverUrl: null,
26
30
  };