@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
41 lines • 1.05 kB
JavaScript
import { useCallback } from 'react';
import { useTranslation } from "../../hooks/index.js";
import { useSharedState } from "../../../../shared/helpers/useSharedState.js";
export default function useArrayLimit(path) {
const sharedState = useSharedState(path + '-iterate-limit');
const {
set,
update,
extend,
data
} = sharedState || {};
const {
limit,
total,
show
} = data || {};
const setLimitProps = useCallback(props => {
if (props.total !== total) {
update(props);
} else {
set(props);
}
}, [set, total, update]);
const setShowStatus = useCallback(show => {
extend({
show
});
}, [extend]);
const hasReachedLimit = typeof limit === 'number' && total >= limit;
const {
itemsLimitReached
} = useTranslation().IteratePushContainer;
const error = show ? new Error(itemsLimitReached.replace('{limit}', String(limit))) : undefined;
return {
setShowStatus,
setLimitProps,
error,
hasReachedLimit
};
}
//# sourceMappingURL=useArrayLimit.js.map