UNPKG

@zohodesk/components

Version:

In this Package, we Provide Some Basic Components to Build Web App

136 lines (122 loc) 4.26 kB
import React, { useRef, useEffect } from 'react'; export function addGlobalId(globalId) { if (Number.isSafeInteger(Number(globalId)) && Number.isSafeInteger(Number(globalId) + 1)) { globalId = Number(globalId) + 1; } else { globalId = `${globalId}1`; } return globalId; } function decreaseGlobalId(number, globalId) { if (`${number}` == `${globalId}`) { if (Number.isSafeInteger(Number(globalId)) && Number.isSafeInteger(Number(globalId) - 1)) { globalId = Number(globalId) - 1; } else { globalId = `${globalId}`; globalId = globalId.substring(0, globalId.length - 1); } return { isDeleted: true, globalId }; } return { isDeleted: false, globalId }; } export function decreaseGlobalIds(numbers, deletedIndexes, globalId) { deletedIndexes = [...deletedIndexes]; [...numbers].reverse().map(number => { let result = decreaseGlobalId(number, globalId); globalId = result.globalId; !result.isDeleted && deletedIndexes.push(number); }); for (; deletedIndexes.indexOf(globalId) != -1;) { let presentHighValue = globalId; let result = decreaseGlobalId(globalId, globalId); globalId = result.globalId; deletedIndexes = deletedIndexes.filter(ele => `${ele}` != `${presentHighValue}`); } return { globalId, deletedIndexes }; } export function useNumberGenerator(_ref) { let { getGlobalId, prefix, getGlobalPrefix, getDeletedIndexes, callback } = _ref; let presentValues = useRef([]), presentIndex = useRef(0); prefix = typeof prefix === 'undefined' ? '' : `${prefix}_`; let globalPrefix = typeof getGlobalPrefix() === 'undefined' ? '' : `${getGlobalPrefix()}_`; function getNextId() { presentIndex.current = presentIndex.current + 1; let currentIndex = presentIndex.current; let isValuePresent = typeof presentValues.current[currentIndex] !== 'undefined'; let result = isValuePresent ? presentValues.current[currentIndex] : presentValues.current[currentIndex] = addGlobalId(getGlobalId()); !isValuePresent && callback({ globalId: result, deletedIndexes: getDeletedIndexes() }); return `${globalPrefix}${prefix}${presentValues.current[currentIndex]}`; } useEffect(() => { presentValues.current = presentValues.current.filter((value, index) => index <= presentIndex.current); }); useEffect(() => () => { callback(decreaseGlobalIds(presentValues.current, getDeletedIndexes(), getGlobalId())); }, []); presentIndex.current = -1; return getNextId; } export function getNumberGenerators(_ref2) { let { Component, prefix, getGlobalPrefix, getGlobalId, getDeletedIndexes, callback } = _ref2; let presentIndex = 0, presentValues = [], { name } = Component.props; let didUpdateRef = Component.componentDidUpdate ? Component.componentDidUpdate.bind(Component) : undefined, renderRef = Component.render.bind(Component), willUnMountRef = Component.componentWillUnmount ? Component.componentWillUnmount.bind(Component) : undefined; prefix = typeof prefix !== 'undefined' ? `${prefix}_` : ''; Component.componentDidUpdate = function () { didUpdateRef && didUpdateRef(...arguments); presentValues = presentValues.filter((value, index) => index <= presentIndex); }; Component.componentWillUnmount = function () { callback(decreaseGlobalIds(presentValues, getDeletedIndexes(), getGlobalId())); willUnMountRef && willUnMountRef(); didUpdateRef = null, renderRef = null, willUnMountRef = null; }; Component.render = function () { presentIndex = -1; return renderRef(); }; function getNextId() { presentIndex += 1; let isValuePresent = typeof presentValues[presentIndex] !== 'undefined'; let result = isValuePresent ? presentValues[presentIndex] : presentValues[presentIndex] = addGlobalId(getGlobalId(getGlobalId())); let globalPrefix = getGlobalPrefix(); globalPrefix = typeof globalPrefix === 'undefined' ? '' : `${globalPrefix}_`; !isValuePresent && callback({ globalId: result, deletedIndexes: getDeletedIndexes() }); return `${globalPrefix}${prefix}${result}`; } return getNextId; }