@undermuz/react-json-form
Version:
Generate JSON-based forms with react
90 lines (88 loc) • 2.8 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/utils/useTabs.ts
var useTabs_exports = {};
__export(useTabs_exports, {
default: () => useTabs_default
});
module.exports = __toCommonJS(useTabs_exports);
var import_react = require("react");
var import_arrayMove = require("./arrayMove.js");
var useTabs = (props) => {
const { value, defValue, onChange, onTabRemove } = props;
const [tab, setTab] = (0, import_react.useState)(() => {
if (value.length > 0)
return value[0].id;
return 1;
});
const addTab = () => {
let maxId = Math.max(...value.map((item) => item.id));
if (isNaN(maxId) || !isFinite(maxId) || maxId < 0) {
maxId = 0;
}
const def_value = {
...defValue,
id: maxId + 1
};
const newList = [...value, def_value];
onChange(newList);
setTab(newList[newList.length - 1].id);
};
const removeTab = (tab_id) => {
if (!window.confirm("\u0412\u044B \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043B\u044C\u043D\u043E \u0445\u043E\u0442\u0438\u0442\u0435 \u0443\u0434\u0430\u043B\u0438\u0442\u044C?")) {
return;
}
const new_value = value.filter((tab2) => tab2.id !== tab_id);
onChange(new_value);
onTabRemove(tab_id);
if (tab === tab_id) {
if (new_value[0]) {
setTab(new_value[0].id);
} else {
setTab(1);
}
}
};
const sortTabs = (event) => {
const { active, over } = event;
if (active && over && active.id !== over.id) {
if (over.id === "trash") {
removeTab(active.id);
return;
}
const oldIndex = value.findIndex(
(_i) => _i.id === active.id
);
const newIndex = value.findIndex(
(_i) => _i.id === over.id
);
onChange((0, import_arrayMove.arrayMoveImmutable)(value, oldIndex, newIndex));
}
};
return {
tab,
setTab,
addTab,
removeTab,
sortTabs
};
};
var useTabs_default = useTabs;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {});