UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

46 lines 1.12 kB
import { useCallback } from 'react'; import pointer from '../../utils/json-pointer'; import { useData, getData } from '../../Form'; export function Count(props) { const { data } = useData(props.id); return countData(data, props); } function countData(data, _ref) { let { path, filter } = _ref; 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() { let id = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : undefined; const { data } = useData(id); const count = useCallback((path, filter) => { return countData(data, { path, filter }); }, [data]); return { count }; } //# sourceMappingURL=Count.js.map