UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

53 lines (52 loc) 1.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Count = Count; exports.count = count; exports.useCount = useCount; var _react = require("react"); var _index = _interopRequireDefault(require("../../utils/json-pointer/index.js")); var _index2 = require("../../Form/index.js"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } function Count(props) { const { data } = (0, _index2.useData)(props.id); return countData(data, props); } function countData(data, { path, filter }) { if (_index.default.has(data, path)) { const value = _index.default.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; } function count(props) { const { data } = (0, _index2.getData)(props.id); return countData(data, props); } function useCount(id = undefined) { const { data } = (0, _index2.useData)(id); const count = (0, _react.useCallback)((path, filter) => { return countData(data, { path, filter }); }, [data]); return { count }; } //# sourceMappingURL=Count.js.map