@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
44 lines • 1.03 kB
JavaScript
import { useCallback } from 'react';
import pointer from "../../utils/json-pointer/index.js";
import { useData, getData } from "../../Form/index.js";
export function Count(props) {
const {
data
} = useData(props.id);
return countData(data, props);
}
function countData(data, {
path,
filter
}) {
if (pointer.has(data, path)) {
const value = pointer.get(data, path);
if (Array.isArray(value)) {
return filter ? value.filter(filter).length : value.length;
} else if (typeof value === 'object' && value) {
return filter ? Object.entries(value).filter(filter).length : Object.keys(value).length;
}
}
return NaN;
}
export function count(props) {
const {
data
} = getData(props.id);
return countData(data, props);
}
export function useCount(id = undefined) {
const {
data
} = useData(id);
const count = useCallback((path, filter) => {
return countData(data, {
path,
filter
});
}, [data]);
return {
count
};
}
//# sourceMappingURL=Count.js.map