UNPKG

@light-sheet/react

Version:

FortuneSheet is a drop-in javascript spreadsheet library that provides rich features like Excel and Google Sheets

145 lines (142 loc) 6.5 kB
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } 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."); } 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); } 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; } 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; } } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } import { getCellValue, getDropdownList, getFlowdata, getSheetIndex, mergeBorder, setDropcownValue } from "@light-sheet/core"; import React, { useCallback, useContext, useEffect, useRef, useState } from "react"; import WorkbookContext from "../../context"; import { useOutsideClick } from "../../hooks/useOutsideClick"; import SVGIcon from "../SVGIcon"; import "./index.css"; var DropDownList = function DropDownList() { var _useContext = useContext(WorkbookContext), context = _useContext.context, setContext = _useContext.setContext; var containerRef = useRef(null); var _useState = useState([]), _useState2 = _slicedToArray(_useState, 2), list = _useState2[0], setList = _useState2[1]; var _useState3 = useState(false), _useState4 = _slicedToArray(_useState3, 2), isMul = _useState4[0], setIsMul = _useState4[1]; var _useState5 = useState(), _useState6 = _slicedToArray(_useState5, 2), position = _useState6[0], setPosition = _useState6[1]; var _useState7 = useState([]), _useState8 = _slicedToArray(_useState7, 2), selected = _useState8[0], setSelected = _useState8[1]; var close = useCallback(function () { setContext(function (ctx) { ctx.dataVerificationDropDownList = false; }); }, [setContext]); useOutsideClick(containerRef, close, [close]); // 初始化 useEffect(function () { if (!context.luckysheet_select_save) return; var last = context.luckysheet_select_save[context.luckysheet_select_save.length - 1]; var rowIndex = last.row_focus; var colIndex = last.column_focus; if (rowIndex == null || colIndex == null) return; var row = context.visibledatarow[rowIndex]; var col_pre = colIndex === 0 ? 0 : context.visibledatacolumn[colIndex - 1]; var d = getFlowdata(context); if (!d) return; var margeSet = mergeBorder(context, d, rowIndex, colIndex); if (margeSet) { var _margeSet$row = _slicedToArray(margeSet.row, 2); row = _margeSet$row[1]; var _margeSet$column = _slicedToArray(margeSet.column, 2); col_pre = _margeSet$column[0]; } var index = getSheetIndex(context, context.currentSheetId); var dataVerification = context.luckysheetfile[index].dataVerification; var item = dataVerification["".concat(rowIndex, "_").concat(colIndex)]; var dropdownList = getDropdownList(context, item.value1); // 初始化多选的下拉列表 var cellValue = getCellValue(rowIndex, colIndex, d); if (cellValue) { setSelected(cellValue.toString().split(",")); } setList(dropdownList); setPosition({ left: col_pre, top: row }); setIsMul(item.type2 === "true"); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); // 设置下拉列表的值 useEffect(function () { if (!context.luckysheet_select_save) return; var last = context.luckysheet_select_save[context.luckysheet_select_save.length - 1]; var rowIndex = last.row_focus; var colIndex = last.column_focus; if (rowIndex == null || colIndex == null) return; var index = getSheetIndex(context, context.currentSheetId); var dataVerification = context.luckysheetfile[index].dataVerification; var item = dataVerification["".concat(rowIndex, "_").concat(colIndex)]; if (item.type2 !== "true") return; var d = getFlowdata(context); if (!d) return; var cellValue = getCellValue(rowIndex, colIndex, d); if (cellValue) { setSelected(cellValue.toString().split(",")); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [context.luckysheetfile]); return /*#__PURE__*/React.createElement("div", { id: "luckysheet-dataVerification-dropdown-List", style: position, ref: containerRef, onClick: function onClick(e) { return e.stopPropagation(); }, onChange: function onChange(e) { return e.stopPropagation(); }, onKeyDown: function onKeyDown(e) { return e.stopPropagation(); }, onMouseDown: function onMouseDown(e) { return e.stopPropagation(); }, onMouseUp: function onMouseUp(e) { return e.stopPropagation(); }, tabIndex: 0 }, list.map(function (v, i) { return /*#__PURE__*/React.createElement("div", { className: "dropdown-List-item", key: i, onClick: function onClick() { setContext(function (ctx) { var arr = selected; var index = arr.indexOf(v); if (index < 0) { arr.push(v); } else { arr.splice(index, 1); } setSelected(arr); setDropcownValue(ctx, v, arr); }); }, tabIndex: 0 }, /*#__PURE__*/React.createElement(SVGIcon, { name: "check", width: 12, style: { verticalAlign: "middle", display: isMul && selected.indexOf(v) >= 0 ? "inline" : "none" } }), v); })); }; export default DropDownList;