@zhenliang/sheet 0.2.3 → 0.2.4-beta.1

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.
@@ -2,3 +2,4 @@ export { default as getCascaderEditor } from './cascaderEditor';
2
2
  export { getDateEditor } from './dateEditor';
3
3
  export { getNumberEditor, NumberEditor } from './numberEditor';
4
4
  export { default as getSelectEditor } from './selectEditor';
5
+ export { default as getInputSelectEditor } from './inputSelectEditor/inputSelect';
@@ -1,4 +1,5 @@
1
1
  export { default as getCascaderEditor } from "./cascaderEditor";
2
2
  export { getDateEditor } from "./dateEditor";
3
3
  export { getNumberEditor, NumberEditor } from "./numberEditor";
4
- export { default as getSelectEditor } from "./selectEditor";
4
+ export { default as getSelectEditor } from "./selectEditor";
5
+ export { default as getInputSelectEditor } from "./inputSelectEditor/inputSelect";
@@ -0,0 +1,2 @@
1
+ export { getInputSelectEditor } from './inputSelect';
2
+ export { getInputSelectEditor as default } from './inputSelect';
@@ -0,0 +1,2 @@
1
+ export { getInputSelectEditor } from "./inputSelect";
2
+ export { getInputSelectEditor as default } from "./inputSelect";
@@ -0,0 +1,39 @@
1
+ .harvest-sheet-container .number-editor {
2
+ border: none !important;
3
+ background: transparent !important;
4
+ width: 100% !important;
5
+ height: var(--cell-inner-height) !important;
6
+ display: block !important;
7
+ font-size: 12px !important;
8
+ box-shadow: none !important;
9
+ text-align: inherit !important;
10
+ padding-left: 0;
11
+
12
+ .ant-input-wrapper {
13
+ text-align: inherit !important;
14
+ }
15
+
16
+ .ant-input {
17
+ text-align: inherit !important;
18
+ }
19
+
20
+ .ant-input-wrapper > input {
21
+ padding: 0;
22
+ height: 38px;
23
+ border: none;
24
+ outline: none;
25
+ box-shadow: none;
26
+ background: transparent;
27
+ font-size: 12px;
28
+ }
29
+
30
+ .ant-input-group-addon {
31
+ background: transparent;
32
+ border: none;
33
+ padding: 0;
34
+ }
35
+ }
36
+
37
+ .select-editor-dropdown {
38
+ min-width: 80px !important;
39
+ }
@@ -0,0 +1,9 @@
1
+ import type { SheetType } from "../../../type";
2
+ import { InputProps } from 'antd';
3
+ import 'antd/es/input/style/index.css';
4
+ import './index.less';
5
+ export declare const getInputSelectEditor: (options: SheetType.Options[], valueKey?: string, extra?: React.ReactNode, inputProps?: Partial<InputProps>, format?: ((value: any) => any) | undefined, warnMethod?: ((record: any) => void) | undefined, rangeMethod?: ((record: any) => {
6
+ max?: number;
7
+ min?: number;
8
+ }) | undefined, getCustomOptions?: ((record: any) => SheetType.Options[]) | undefined) => SheetType.CellEditor;
9
+ export default getInputSelectEditor;
@@ -0,0 +1,176 @@
1
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
2
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
3
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
4
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
5
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
6
+ function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
7
+ function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
8
+ function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
9
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
10
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
11
+ function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
12
+ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
13
+ /* eslint-disable eqeqeq */
14
+ // 使用 antd 的 Input + Dropdown,实现可搜索且可自由输入的编辑器
15
+
16
+ import { Dropdown, Input, Menu } from 'antd';
17
+ import 'antd/es/input/style/index.css';
18
+ import { isNil } from 'lodash';
19
+ import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
20
+ import "./index.less";
21
+ import { Fragment as _Fragment } from "react/jsx-runtime";
22
+ import { jsx as _jsx } from "react/jsx-runtime";
23
+ import { jsxs as _jsxs } from "react/jsx-runtime";
24
+ export var getInputSelectEditor = function getInputSelectEditor(options) {
25
+ var valueKey = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'value';
26
+ var extra = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : /*#__PURE__*/_jsx(_Fragment, {});
27
+ var inputProps = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
28
+ var format = arguments.length > 4 ? arguments[4] : undefined;
29
+ var warnMethod = arguments.length > 5 ? arguments[5] : undefined;
30
+ var rangeMethod = arguments.length > 6 ? arguments[6] : undefined;
31
+ var getCustomOptions = arguments.length > 7 ? arguments[7] : undefined;
32
+ var InputSelectEditor = function InputSelectEditor(props) {
33
+ var value = props.value,
34
+ onConfirm = props.onConfirm,
35
+ onChange = props.onChange,
36
+ record = props.record,
37
+ isEditing = props.isEditing;
38
+ var _useState = useState(true),
39
+ _useState2 = _slicedToArray(_useState, 2),
40
+ open = _useState2[0],
41
+ setOpen = _useState2[1];
42
+ var _useState3 = useState(function () {
43
+ return String(value !== null && value !== void 0 ? value : '');
44
+ }),
45
+ _useState4 = _slicedToArray(_useState3, 2),
46
+ inputValue = _useState4[0],
47
+ setInputValue = _useState4[1];
48
+ var baseOptions = useMemo(function () {
49
+ return getCustomOptions ? getCustomOptions(record !== null && record !== void 0 ? record : {}) : options;
50
+ }, [getCustomOptions, options, record]);
51
+ var filteredOptions = useMemo(function () {
52
+ return baseOptions.filter(function (item) {
53
+ var _item$label;
54
+ return String((_item$label = item.label) !== null && _item$label !== void 0 ? _item$label : '').toLowerCase().includes(String(inputValue).toLowerCase());
55
+ });
56
+ }, [baseOptions, inputValue]);
57
+ var handleOptionClick = function handleOptionClick(opt) {
58
+ setInputValue(opt.label);
59
+ onConfirm(opt.value);
60
+ setOpen(false);
61
+ };
62
+ var dropdownOverlay = /*#__PURE__*/_jsxs(Menu, {
63
+ className: "select-editor-dropdown",
64
+ onMouseDown: function onMouseDown(e) {
65
+ // 防止点击下拉时触发行内编辑器失焦
66
+ e.preventDefault();
67
+ e.stopPropagation();
68
+ },
69
+ children: [filteredOptions.map(function (opt) {
70
+ var _opt$label;
71
+ return /*#__PURE__*/_jsx(Menu.Item, {
72
+ className: "select-editor-dropdown-item",
73
+ onClick: function onClick() {
74
+ return handleOptionClick(opt);
75
+ },
76
+ children: (_opt$label = opt.label) !== null && _opt$label !== void 0 ? _opt$label : opt.value
77
+ }, String(opt.value));
78
+ }), extra]
79
+ });
80
+ var inputNumberRef = useRef(null);
81
+ var handleChange = useCallback(function (e) {
82
+ setInputValue(e.target.value);
83
+ onChange(!isNil(e.target.value) ? e.target.value : null);
84
+ }, [onChange]);
85
+ useEffect(function () {
86
+ if (isEditing) {
87
+ var _inputNumberRef$curre;
88
+ inputNumberRef === null || inputNumberRef === void 0 || (_inputNumberRef$curre = inputNumberRef.current) === null || _inputNumberRef$curre === void 0 || _inputNumberRef$curre.focus();
89
+ }
90
+ }, [isEditing]);
91
+ useEffect(function () {
92
+ setOpen(filteredOptions.length > 0);
93
+ }, [filteredOptions]);
94
+ var reFocus = useCallback(function () {
95
+ if (isEditing) {
96
+ var _inputNumberRef$curre2;
97
+ inputNumberRef === null || inputNumberRef === void 0 || (_inputNumberRef$curre2 = inputNumberRef.current) === null || _inputNumberRef$curre2 === void 0 || _inputNumberRef$curre2.focus();
98
+ }
99
+ }, [isEditing]);
100
+ return /*#__PURE__*/_jsx(Dropdown, {
101
+ overlay: dropdownOverlay,
102
+ open: open,
103
+ children: /*#__PURE__*/_jsx(Input, _objectSpread({
104
+ autoFocus: true,
105
+ className: "number-editor",
106
+ onMouseDown: function onMouseDown(e) {
107
+ return e.stopPropagation();
108
+ },
109
+ value: value,
110
+ onChange: handleChange,
111
+ ref: inputNumberRef,
112
+ onBlur: reFocus
113
+ }, inputProps))
114
+ });
115
+ };
116
+
117
+ // 校验:允许在选项中存在的值,或任意字符串(自定义值)
118
+ InputSelectEditor.checker = function (value, record) {
119
+ if (isNil(value)) return true;
120
+ var customOptions = getCustomOptions ? getCustomOptions(record !== null && record !== void 0 ? record : {}) : options;
121
+ var inOptions = customOptions.some(function (item) {
122
+ return item.value == value;
123
+ }) || customOptions.some(function (item) {
124
+ return item.label === value;
125
+ });
126
+ if (inOptions) return true;
127
+ var result = format ? format(value) : value;
128
+ if (result === undefined) {
129
+ return false;
130
+ }
131
+ // parse number with thousands separator
132
+ var range = rangeMethod === null || rangeMethod === void 0 ? void 0 : rangeMethod(record);
133
+ if (!isNil(range === null || range === void 0 ? void 0 : range.max) && !isNil(range === null || range === void 0 ? void 0 : range.min)) {
134
+ var _ref = range || {},
135
+ _ref$max = _ref.max,
136
+ max = _ref$max === void 0 ? 0 : _ref$max,
137
+ _ref$min = _ref.min,
138
+ min = _ref$min === void 0 ? 0 : _ref$min;
139
+ if (result < min || result > max) {
140
+ warnMethod === null || warnMethod === void 0 || warnMethod(record);
141
+ return false;
142
+ }
143
+ }
144
+ return true;
145
+ };
146
+
147
+ // 粘贴/内部解析:如果在选项中,则取 valueKey 对应字段;否则直接返回原值
148
+ InputSelectEditor.parser = function (value, record) {
149
+ var customOptions = getCustomOptions ? getCustomOptions(record !== null && record !== void 0 ? record : {}) : options;
150
+ var hitByValue = customOptions.find(function (item) {
151
+ return item.value == value;
152
+ });
153
+ if (hitByValue) return hitByValue[valueKey];
154
+ var hitByLabel = customOptions.find(function (item) {
155
+ return item.label === value;
156
+ });
157
+ if (hitByLabel) return hitByLabel[valueKey];
158
+ return value;
159
+ };
160
+
161
+ // 展示:如果在选项中,用 label;否则展示原始值(自定义)
162
+ InputSelectEditor.formatter = function (value, record) {
163
+ var customOptions = getCustomOptions ? getCustomOptions(record !== null && record !== void 0 ? record : {}) : options;
164
+ var hitByValue = customOptions.find(function (item) {
165
+ return item.value == value;
166
+ });
167
+ if (hitByValue) return hitByValue.label;
168
+ var hitByLabel = customOptions.find(function (item) {
169
+ return item.label === value;
170
+ });
171
+ if (hitByLabel) return hitByLabel.label;
172
+ return value;
173
+ };
174
+ return InputSelectEditor;
175
+ };
176
+ export default getInputSelectEditor;
@@ -93,13 +93,13 @@ export var getNumberEditor = function getNumberEditor(extraProps, getExtraProps)
93
93
  }
94
94
  }, [isEditing]);
95
95
  return /*#__PURE__*/_jsx(Input, _objectSpread(_objectSpread({
96
- ref: inputNumberRef
96
+ ref: inputNumberRef,
97
+ onBlur: reFocus
97
98
  }, inputArgs), {}, {
98
99
  className: "number-editor",
99
100
  onMouseDown: function onMouseDown(e) {
100
101
  return e.stopPropagation();
101
102
  },
102
- onBlur: reFocus,
103
103
  value: (_ref3 = value) !== null && _ref3 !== void 0 ? _ref3 : '',
104
104
  onChange: function onChange(e) {
105
105
  handleChange(e.target.value);
@@ -14,14 +14,35 @@ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o =
14
14
  function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
15
15
  function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
16
16
  function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
17
- import { Table, getNumberEditor } from "./..";
18
- import { Space, Tag } from 'antd';
17
+ import { Table, getNumberEditor, getInputSelectEditor } from "./..";
18
+ import { Space, Tag, Select } from 'antd';
19
19
  import 'antd/dist/antd.css';
20
20
  import React, { useCallback, useState } from 'react';
21
21
  import { jsx as _jsx } from "react/jsx-runtime";
22
22
  import { Fragment as _Fragment } from "react/jsx-runtime";
23
23
  import { jsxs as _jsxs } from "react/jsx-runtime";
24
24
  var columns = [{
25
+ title: 'Status',
26
+ dataIndex: 'status',
27
+ editor: getInputSelectEditor([{
28
+ label: 'Open',
29
+ value: 'open'
30
+ }, {
31
+ label: 'Closed',
32
+ value: 'closed'
33
+ }, {
34
+ label: '1.5%',
35
+ value: '1500'
36
+ }, {
37
+ label: '2%',
38
+ value: '2000'
39
+ }, {
40
+ label: '3%',
41
+ value: '3000'
42
+ }], 'value', null, {}, function (value) {
43
+ return isNaN(value) ? null : value;
44
+ })
45
+ }, {
25
46
  title: 'Name',
26
47
  dataIndex: 'name',
27
48
  key: 'name',
@@ -110,6 +131,7 @@ var App = function App() {
110
131
  dataSource = _useState2[0],
111
132
  setData = _useState2[1];
112
133
  var onChange = useCallback(function (changes) {
134
+ console.log('changes', changes);
113
135
  var newData = _toConsumableArray(dataSource);
114
136
  changes.forEach(function (_ref4) {
115
137
  var _String;
@@ -120,12 +142,38 @@ var App = function App() {
120
142
  });
121
143
  setData(newData);
122
144
  }, [dataSource]);
123
- return /*#__PURE__*/_jsx(Table, {
124
- draggable: true,
125
- columns: columns,
126
- dataSource: dataSource,
127
- onChange: onChange,
128
- showQuickLocationBtn: true
145
+ return /*#__PURE__*/_jsxs("div", {
146
+ children: [/*#__PURE__*/_jsx(Table, {
147
+ draggable: true,
148
+ columns: columns,
149
+ dataSource: dataSource,
150
+ onChange: onChange,
151
+ showQuickLocationBtn: true
152
+ }), /*#__PURE__*/_jsx(Select, {
153
+ showSearch: true,
154
+ placeholder: "Select a person",
155
+ optionFilterProp: "children",
156
+ onChange: function onChange(value) {
157
+ console.log('onChange', value);
158
+ },
159
+ onSearch: function onSearch(value) {
160
+ console.log('onSearch', value);
161
+ },
162
+ filterOption: function filterOption(input, option) {
163
+ var _option$label;
164
+ return ((_option$label = option === null || option === void 0 ? void 0 : option.label) !== null && _option$label !== void 0 ? _option$label : '').toLowerCase().includes(input.toLowerCase());
165
+ },
166
+ options: [{
167
+ value: 'jack',
168
+ label: 'Jack'
169
+ }, {
170
+ value: 'lucy',
171
+ label: 'Lucy'
172
+ }, {
173
+ value: 'tom',
174
+ label: 'Tom'
175
+ }]
176
+ })]
129
177
  });
130
178
  };
131
179
  export default App;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zhenliang/sheet",
3
- "version": "0.2.3",
3
+ "version": "0.2.4-beta.1",
4
4
  "description": "A react library developed with dumi",
5
5
  "license": "MIT",
6
6
  "module": "dist/index.js",
@@ -65,10 +65,10 @@
65
65
  "@types/react": "^17.0.40",
66
66
  "@types/react-dom": "^17.0.13",
67
67
  "@umijs/lint": "^4.0.0",
68
- "dumi": "^2.2.0",
68
+ "dumi": "^2.4.21",
69
69
  "eslint": "^8.23.0",
70
70
  "father": "^4.1.0",
71
- "husky": "^8.0.1",
71
+ "husky": "^8.0.3",
72
72
  "lint-staged": "^13.0.3",
73
73
  "prettier": "^2.7.1",
74
74
  "prettier-plugin-organize-imports": "^3.0.0",