@light-sheet/react
Version:
FortuneSheet is a drop-in javascript spreadsheet library that provides rich features like Excel and Google Sheets
139 lines • 7.29 kB
JavaScript
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 React, { useContext, useState, useCallback, useMemo } from "react";
import { cancelNormalSelected, getSheetIndex, locale, update } from "@light-sheet/core";
import _ from "lodash";
import WorkbookContext from "../../context";
import "./index.css";
import { useDialog } from "../../hooks/useDialog";
export var FormatSearch = function FormatSearch(_ref) {
var type = _ref.type,
_onCancel = _ref.onCancel;
var _useContext = useContext(WorkbookContext),
context = _useContext.context,
setContext = _useContext.setContext,
cellInput = _useContext.refs.cellInput;
var _useState = useState(2),
_useState2 = _slicedToArray(_useState, 2),
decimalPlace = _useState2[0],
setDecimalPlace = _useState2[1];
var _useState3 = useState(0),
_useState4 = _slicedToArray(_useState3, 2),
selectedFormatIndex = _useState4[0],
setSelectedFormatIndex = _useState4[1];
var _locale = locale(context),
button = _locale.button,
format = _locale.format,
currencyDetail = _locale.currencyDetail,
dateFmtList = _locale.dateFmtList,
numberFmtList = _locale.numberFmtList;
var _useDialog = useDialog(),
showDialog = _useDialog.showDialog;
var toolbarFormatAll = useMemo(function () {
return {
currency: currencyDetail,
date: dateFmtList,
number: numberFmtList
};
}, [currencyDetail, dateFmtList, numberFmtList]);
var toolbarFormat = useMemo(function () {
return toolbarFormatAll[type];
}, [toolbarFormatAll, type]);
var tips = _.get(format, type);
var onConfirm = useCallback(function () {
if (decimalPlace < 0 || decimalPlace > 9) {
_onCancel();
showDialog(format.tipDecimalPlaces, "ok");
return;
}
setContext(function (ctx) {
var index = getSheetIndex(ctx, ctx.currentSheetId);
if (_.isNil(index)) return;
var selectedFormatVal = toolbarFormat[selectedFormatIndex].value;
var selectedFormatPos;
if ("pos" in toolbarFormat[selectedFormatIndex]) selectedFormatPos = toolbarFormat[selectedFormatIndex].pos || "before";
_.forEach(ctx.luckysheet_select_save, function (selection) {
for (var r = selection.row[0]; r <= selection.row[1]; r += 1) {
for (var c = selection.column[0]; c <= selection.column[1]; c += 1) {
var _ctx$luckysheetfile$i, _ctx$luckysheetfile$i2;
if ((_ctx$luckysheetfile$i = ctx.luckysheetfile[index].data) !== null && _ctx$luckysheetfile$i !== void 0 && _ctx$luckysheetfile$i[r][c] && ((_ctx$luckysheetfile$i2 = ctx.luckysheetfile[index].data) === null || _ctx$luckysheetfile$i2 === void 0 || (_ctx$luckysheetfile$i2 = _ctx$luckysheetfile$i2[r][c]) === null || _ctx$luckysheetfile$i2 === void 0 || (_ctx$luckysheetfile$i2 = _ctx$luckysheetfile$i2.ct) === null || _ctx$luckysheetfile$i2 === void 0 ? void 0 : _ctx$luckysheetfile$i2.t) === "n") {
var zero = 0;
if (selectedFormatPos === "after") {
ctx.luckysheetfile[index].data[r][c].ct.fa = zero.toFixed(decimalPlace).concat("".concat(selectedFormatVal));
ctx.luckysheetfile[index].data[r][c].m = update(zero.toFixed(decimalPlace).concat("".concat(selectedFormatVal)), ctx.luckysheetfile[index].data[r][c].v);
} else {
ctx.luckysheetfile[index].data[r][c].ct.fa = "".concat(selectedFormatVal).concat(zero.toFixed(decimalPlace));
ctx.luckysheetfile[index].data[r][c].m = update("".concat(selectedFormatVal).concat(zero.toFixed(decimalPlace)), ctx.luckysheetfile[index].data[r][c].v);
}
}
}
}
});
_onCancel();
});
}, [_onCancel, decimalPlace, format.tipDecimalPlaces, selectedFormatIndex, setContext, showDialog, toolbarFormat]);
var onCancel = useCallback(function () {
setContext(function (ctx) {
cancelNormalSelected(ctx);
if (cellInput.current) {
cellInput.current.innerHTML = "";
}
});
_onCancel();
}, [_onCancel, cellInput, setContext]);
return /*#__PURE__*/React.createElement("div", {
id: "luckysheet-search-format"
}, /*#__PURE__*/React.createElement("div", {
className: "listbox",
style: {
height: 200
}
}, /*#__PURE__*/React.createElement("div", {
style: {
marginBottom: 16
}
}, tips, format.format, "\uFF1A"), /*#__PURE__*/React.createElement("div", {
className: "inpbox",
style: {
display: "block"
}
}, format.decimalPlaces, "\uFF1A", /*#__PURE__*/React.createElement("input", {
className: "decimal-places-input",
id: "decimal-places-input",
min: 0,
max: 9,
defaultValue: 2,
type: "number",
onChange: function onChange(e) {
setDecimalPlace(parseInt(e.target.value, 10));
}
})), /*#__PURE__*/React.createElement("div", {
className: "format-list"
}, toolbarFormat.map(function (v, index) {
return /*#__PURE__*/React.createElement("div", {
className: "listBox".concat(index === selectedFormatIndex ? " on" : ""),
key: v.name,
onClick: function onClick() {
setSelectedFormatIndex(index);
},
tabIndex: 0
}, /*#__PURE__*/React.createElement("div", null, v.name), /*#__PURE__*/React.createElement("div", null, v.value));
}))), /*#__PURE__*/React.createElement("div", {
className: "fortune-dialog-box-button-container",
style: {
marginTop: 40
}
}, /*#__PURE__*/React.createElement("div", {
className: "fortune-message-box-button button-primary",
onClick: onConfirm,
tabIndex: 0
}, button.confirm), /*#__PURE__*/React.createElement("div", {
className: "fortune-message-box-button button-default",
onClick: onCancel,
tabIndex: 0
}, button.cancel)));
};