@cloudscape-design/collection-hooks
Advanced tools
Comparing version 1.0.56 to 1.0.57
@@ -5,4 +5,5 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.compareTimestamps = exports.compareDates = void 0; | ||
var parse_iso_date_js_1 = require("./parse-iso-date.js"); | ||
exports.compareDates = compareDates; | ||
exports.compareTimestamps = compareTimestamps; | ||
const parse_iso_date_js_1 = require("./parse-iso-date.js"); | ||
/** | ||
@@ -20,3 +21,2 @@ * Compares dates up to a day. | ||
} | ||
exports.compareDates = compareDates; | ||
/** | ||
@@ -34,7 +34,6 @@ * Compares dates up to a millisecond. | ||
} | ||
exports.compareTimestamps = compareTimestamps; | ||
function startOfDay(date) { | ||
var copy = new Date(date.getTime()); | ||
const copy = new Date(date.getTime()); | ||
copy.setHours(0, 0, 0, 0); | ||
return copy; | ||
} |
@@ -5,3 +5,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.parseIsoDate = void 0; | ||
exports.parseIsoDate = parseIsoDate; | ||
function parseIsoDate(isoDate) { | ||
@@ -18,2 +18,1 @@ if (typeof isoDate === 'string') { | ||
} | ||
exports.parseIsoDate = parseIsoDate; |
@@ -5,8 +5,8 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.warnOnce = void 0; | ||
var isDevelopment = process.env.NODE_ENV !== 'production'; | ||
var messageCache = new Set(); | ||
exports.warnOnce = warnOnce; | ||
const isDevelopment = process.env.NODE_ENV !== 'production'; | ||
const messageCache = new Set(); | ||
function warnOnce(message) { | ||
if (isDevelopment) { | ||
var warning = "[AwsUi] collection-hooks ".concat(message); | ||
const warning = `[AwsUi] collection-hooks ${message}`; | ||
if (!messageCache.has(warning)) { | ||
@@ -18,2 +18,1 @@ messageCache.add(warning); | ||
} | ||
exports.warnOnce = warnOnce; |
@@ -5,12 +5,7 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.composeFilters = void 0; | ||
function composeFilters() { | ||
var predicates = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
predicates[_i] = arguments[_i]; | ||
} | ||
exports.composeFilters = composeFilters; | ||
function composeFilters(...predicates) { | ||
return predicates.some(Boolean) | ||
? function (item) { | ||
for (var _i = 0, predicates_1 = predicates; _i < predicates_1.length; _i++) { | ||
var predicate = predicates_1[_i]; | ||
? item => { | ||
for (const predicate of predicates) { | ||
if (predicate && !predicate(item)) { | ||
@@ -24,2 +19,1 @@ return false; | ||
} | ||
exports.composeFilters = composeFilters; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createFilterPredicate = void 0; | ||
exports.createFilterPredicate = createFilterPredicate; | ||
function defaultFilteringFunction(item, filteringText, filteringFields) { | ||
@@ -9,18 +9,14 @@ if (filteringText.length === 0) { | ||
filteringFields = filteringFields || Object.keys(item); | ||
var lowFilteringText = filteringText.toLowerCase(); | ||
return filteringFields.some(function (key) { | ||
return String(item[key]) | ||
.toLowerCase() | ||
.indexOf(lowFilteringText) > -1; | ||
}); | ||
const lowFilteringText = filteringText.toLowerCase(); | ||
return filteringFields.some(key => String(item[key]) | ||
.toLowerCase() | ||
.indexOf(lowFilteringText) > -1); | ||
} | ||
function createFilterPredicate(filtering, filteringText) { | ||
function createFilterPredicate(filtering, filteringText = '') { | ||
var _a; | ||
if (filteringText === void 0) { filteringText = ''; } | ||
if (!filtering) { | ||
return null; | ||
} | ||
var filteringFunction = (_a = filtering.filteringFunction) !== null && _a !== void 0 ? _a : defaultFilteringFunction; | ||
return function (item) { return filteringFunction(item, filteringText, filtering.fields); }; | ||
const filteringFunction = (_a = filtering.filteringFunction) !== null && _a !== void 0 ? _a : defaultFilteringFunction; | ||
return item => filteringFunction(item, filteringText, filtering.fields); | ||
} | ||
exports.createFilterPredicate = createFilterPredicate; |
@@ -12,3 +12,3 @@ import { UseCollectionOptions, CollectionState, TrackBy } from '../interfaces'; | ||
export declare const getTrackableValue: <T>(trackBy: TrackBy<T> | undefined, item: T) => string | T; | ||
export declare const processSelectedItems: <T>(items: readonly T[], selectedItems: readonly T[], trackBy?: TrackBy<T> | undefined) => T[]; | ||
export declare const itemsAreEqual: <T>(items1: readonly T[], items2: readonly T[], trackBy?: TrackBy<T> | undefined) => boolean; | ||
export declare const processSelectedItems: <T>(items: ReadonlyArray<T>, selectedItems: ReadonlyArray<T>, trackBy?: TrackBy<T>) => T[]; | ||
export declare const itemsAreEqual: <T>(items1: ReadonlyArray<T>, items2: ReadonlyArray<T>, trackBy?: TrackBy<T>) => boolean; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.itemsAreEqual = exports.processSelectedItems = exports.getTrackableValue = exports.processItems = void 0; | ||
var filter_js_1 = require("./filter.js"); | ||
var property_filter_js_1 = require("./property-filter.js"); | ||
var sort_js_1 = require("./sort.js"); | ||
var pagination_js_1 = require("./pagination.js"); | ||
var items_tree_js_1 = require("./items-tree.js"); | ||
var compose_filters_js_1 = require("./compose-filters.js"); | ||
function processItems(items, _a, _b) { | ||
var filteringText = _a.filteringText, sortingState = _a.sortingState, currentPageIndex = _a.currentPageIndex, propertyFilteringQuery = _a.propertyFilteringQuery; | ||
var filtering = _b.filtering, sorting = _b.sorting, pagination = _b.pagination, propertyFiltering = _b.propertyFiltering, expandableRows = _b.expandableRows; | ||
var itemsTree = new items_tree_js_1.ItemsTree(items, expandableRows); | ||
var filterPredicate = (0, compose_filters_js_1.composeFilters)((0, property_filter_js_1.createPropertyFilterPredicate)(propertyFiltering, propertyFilteringQuery), (0, filter_js_1.createFilterPredicate)(filtering, filteringText)); | ||
exports.itemsAreEqual = exports.processSelectedItems = exports.getTrackableValue = void 0; | ||
exports.processItems = processItems; | ||
const filter_js_1 = require("./filter.js"); | ||
const property_filter_js_1 = require("./property-filter.js"); | ||
const sort_js_1 = require("./sort.js"); | ||
const pagination_js_1 = require("./pagination.js"); | ||
const items_tree_js_1 = require("./items-tree.js"); | ||
const compose_filters_js_1 = require("./compose-filters.js"); | ||
function processItems(items, { filteringText, sortingState, currentPageIndex, propertyFilteringQuery }, { filtering, sorting, pagination, propertyFiltering, expandableRows }) { | ||
const itemsTree = new items_tree_js_1.ItemsTree(items, expandableRows); | ||
const filterPredicate = (0, compose_filters_js_1.composeFilters)((0, property_filter_js_1.createPropertyFilterPredicate)(propertyFiltering, propertyFilteringQuery), (0, filter_js_1.createFilterPredicate)(filtering, filteringText)); | ||
if (filterPredicate) { | ||
itemsTree.filter(filterPredicate); | ||
} | ||
var comparator = (0, sort_js_1.createComparator)(sorting, sortingState); | ||
const comparator = (0, sort_js_1.createComparator)(sorting, sortingState); | ||
if (comparator) { | ||
itemsTree.sort(comparator); | ||
} | ||
var allPageItems = itemsTree.getItems(); | ||
var filteredItemsCount = filterPredicate ? itemsTree.getSize() : undefined; | ||
var pageProps = (0, pagination_js_1.createPageProps)(pagination, currentPageIndex, allPageItems); | ||
const allPageItems = itemsTree.getItems(); | ||
const filteredItemsCount = filterPredicate ? itemsTree.getSize() : undefined; | ||
const pageProps = (0, pagination_js_1.createPageProps)(pagination, currentPageIndex, allPageItems); | ||
if (pageProps) { | ||
@@ -29,6 +28,6 @@ return { | ||
allPageItems: allPageItems, | ||
filteredItemsCount: filteredItemsCount, | ||
filteredItemsCount, | ||
pagesCount: pageProps === null || pageProps === void 0 ? void 0 : pageProps.pagesCount, | ||
actualPageIndex: pageProps === null || pageProps === void 0 ? void 0 : pageProps.pageIndex, | ||
itemsTree: itemsTree, | ||
itemsTree, | ||
}; | ||
@@ -39,10 +38,9 @@ } | ||
allPageItems: allPageItems, | ||
filteredItemsCount: filteredItemsCount, | ||
filteredItemsCount, | ||
pagesCount: undefined, | ||
actualPageIndex: undefined, | ||
itemsTree: itemsTree, | ||
itemsTree, | ||
}; | ||
} | ||
exports.processItems = processItems; | ||
var getTrackableValue = function (trackBy, item) { | ||
const getTrackableValue = (trackBy, item) => { | ||
if (!trackBy) { | ||
@@ -57,16 +55,16 @@ return item; | ||
exports.getTrackableValue = getTrackableValue; | ||
var processSelectedItems = function (items, selectedItems, trackBy) { | ||
var selectedSet = new Set(); | ||
selectedItems.forEach(function (item) { return selectedSet.add((0, exports.getTrackableValue)(trackBy, item)); }); | ||
return items.filter(function (item) { return selectedSet.has((0, exports.getTrackableValue)(trackBy, item)); }); | ||
const processSelectedItems = (items, selectedItems, trackBy) => { | ||
const selectedSet = new Set(); | ||
selectedItems.forEach(item => selectedSet.add((0, exports.getTrackableValue)(trackBy, item))); | ||
return items.filter(item => selectedSet.has((0, exports.getTrackableValue)(trackBy, item))); | ||
}; | ||
exports.processSelectedItems = processSelectedItems; | ||
var itemsAreEqual = function (items1, items2, trackBy) { | ||
const itemsAreEqual = (items1, items2, trackBy) => { | ||
if (items1.length !== items2.length) { | ||
return false; | ||
} | ||
var set1 = new Set(); | ||
items1.forEach(function (item) { return set1.add((0, exports.getTrackableValue)(trackBy, item)); }); | ||
return items2.every(function (item) { return set1.has((0, exports.getTrackableValue)(trackBy, item)); }); | ||
const set1 = new Set(); | ||
items1.forEach(item => set1.add((0, exports.getTrackableValue)(trackBy, item))); | ||
return items2.every(item => set1.has((0, exports.getTrackableValue)(trackBy, item))); | ||
}; | ||
exports.itemsAreEqual = itemsAreEqual; |
@@ -6,5 +6,4 @@ "use strict"; | ||
exports.ItemsTree = void 0; | ||
var ItemsTree = /** @class */ (function () { | ||
function ItemsTree(items, treeProps) { | ||
var _this = this; | ||
class ItemsTree { | ||
constructor(items, treeProps) { | ||
var _a; | ||
@@ -15,55 +14,54 @@ this.size = 0; | ||
this.idToChildren = new Map(); | ||
this.filter = function (predicate) { | ||
if (!_this.hasNesting) { | ||
_this.items = _this.items.filter(predicate); | ||
_this.size = _this.items.length; | ||
this.filter = (predicate) => { | ||
if (!this.hasNesting) { | ||
this.items = this.items.filter(predicate); | ||
this.size = this.items.length; | ||
} | ||
else { | ||
_this.filterTree(predicate); | ||
this.filterTree(predicate); | ||
} | ||
return _this; | ||
return this; | ||
}; | ||
this.sort = function (comparator) { | ||
if (!_this.hasNesting) { | ||
_this.items = _this.items.slice().sort(comparator); | ||
this.sort = (comparator) => { | ||
if (!this.hasNesting) { | ||
this.items = this.items.slice().sort(comparator); | ||
} | ||
else { | ||
_this.sortTree(comparator); | ||
this.sortTree(comparator); | ||
} | ||
return _this; | ||
return this; | ||
}; | ||
this.getChildren = function (item) { | ||
this.getChildren = (item) => { | ||
var _a; | ||
return (_a = (_this.treeProps && _this.idToChildren.get(_this.treeProps.getId(item)))) !== null && _a !== void 0 ? _a : []; | ||
return (_a = (this.treeProps && this.idToChildren.get(this.treeProps.getId(item)))) !== null && _a !== void 0 ? _a : []; | ||
}; | ||
this.getItems = function () { | ||
if (_this.hasNesting) { | ||
return _this.roots; | ||
this.getItems = () => { | ||
if (this.hasNesting) { | ||
return this.roots; | ||
} | ||
return _this.items; | ||
return this.items; | ||
}; | ||
this.getSize = function () { | ||
return _this.size; | ||
this.getSize = () => { | ||
return this.size; | ||
}; | ||
this.filterTree = function (predicate) { | ||
var filterNode = function (item) { | ||
var children = _this.getChildren(item); | ||
var filteredChildren = children.filter(filterNode); | ||
_this.size -= children.length - filteredChildren.length; | ||
_this.setChildren(item, filteredChildren); | ||
this.filterTree = (predicate) => { | ||
const filterNode = (item) => { | ||
const children = this.getChildren(item); | ||
const filteredChildren = children.filter(filterNode); | ||
this.size -= children.length - filteredChildren.length; | ||
this.setChildren(item, filteredChildren); | ||
return predicate(item) || filteredChildren.length > 0; | ||
}; | ||
var roots = _this.roots; | ||
_this.roots = _this.roots.filter(filterNode); | ||
_this.size -= roots.length - _this.roots.length; | ||
const roots = this.roots; | ||
this.roots = this.roots.filter(filterNode); | ||
this.size -= roots.length - this.roots.length; | ||
}; | ||
this.sortTree = function (comparator) { | ||
var sortLevel = function (items) { | ||
this.sortTree = (comparator) => { | ||
const sortLevel = (items) => { | ||
items.sort(comparator); | ||
for (var _i = 0, items_2 = items; _i < items_2.length; _i++) { | ||
var item = items_2[_i]; | ||
sortLevel(_this.getChildren(item)); | ||
for (const item of items) { | ||
sortLevel(this.getChildren(item)); | ||
} | ||
}; | ||
sortLevel(_this.roots); | ||
sortLevel(this.roots); | ||
}; | ||
@@ -77,5 +75,4 @@ this.size = items.length; | ||
// Assign item children. | ||
for (var _i = 0, items_1 = items; _i < items_1.length; _i++) { | ||
var item = items_1[_i]; | ||
var parentId = treeProps.getParentId(item); | ||
for (const item of items) { | ||
const parentId = treeProps.getParentId(item); | ||
if (parentId === null) { | ||
@@ -85,3 +82,3 @@ this.roots.push(item); | ||
else { | ||
var children = (_a = this.idToChildren.get(parentId)) !== null && _a !== void 0 ? _a : []; | ||
const children = (_a = this.idToChildren.get(parentId)) !== null && _a !== void 0 ? _a : []; | ||
children.push(item); | ||
@@ -93,19 +90,16 @@ this.idToChildren.set(parentId, children); | ||
// Assign item levels. | ||
var traverse = function (item, level) { | ||
const traverse = (item, level = 1) => { | ||
var _a; | ||
if (level === void 0) { level = 1; } | ||
for (var _i = 0, _b = (_a = _this.idToChildren.get(treeProps.getId(item))) !== null && _a !== void 0 ? _a : []; _i < _b.length; _i++) { | ||
var child = _b[_i]; | ||
for (const child of (_a = this.idToChildren.get(treeProps.getId(item))) !== null && _a !== void 0 ? _a : []) { | ||
traverse(child, level + 1); | ||
} | ||
}; | ||
this.roots.forEach(function (root) { return traverse(root); }); | ||
this.roots.forEach(root => traverse(root)); | ||
} | ||
ItemsTree.prototype.setChildren = function (item, children) { | ||
setChildren(item, children) { | ||
if (this.treeProps) { | ||
this.idToChildren.set(this.treeProps.getId(item), children); | ||
} | ||
}; | ||
return ItemsTree; | ||
}()); | ||
} | ||
} | ||
exports.ItemsTree = ItemsTree; |
@@ -5,4 +5,4 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createPageProps = void 0; | ||
var DEFAULT_PAGE_SIZE = 10; | ||
exports.createPageProps = createPageProps; | ||
const DEFAULT_PAGE_SIZE = 10; | ||
function createPageProps(pagination, currentPageIndex, items) { | ||
@@ -13,10 +13,9 @@ var _a; | ||
} | ||
var pageSize = (_a = pagination.pageSize) !== null && _a !== void 0 ? _a : DEFAULT_PAGE_SIZE; | ||
var pagesCount = Math.ceil(items.length / pageSize); | ||
var pageIndex = currentPageIndex !== null && currentPageIndex !== void 0 ? currentPageIndex : 1; | ||
const pageSize = (_a = pagination.pageSize) !== null && _a !== void 0 ? _a : DEFAULT_PAGE_SIZE; | ||
const pagesCount = Math.ceil(items.length / pageSize); | ||
let pageIndex = currentPageIndex !== null && currentPageIndex !== void 0 ? currentPageIndex : 1; | ||
if (pageIndex < 1 || pageIndex > pagesCount || Number.isNaN(pageIndex)) { | ||
pageIndex = 1; | ||
} | ||
return { pageSize: pageSize, pagesCount: pagesCount, pageIndex: pageIndex }; | ||
return { pageSize, pagesCount, pageIndex }; | ||
} | ||
exports.createPageProps = createPageProps; |
import { PropertyFilterQuery, UseCollectionOptions } from '../interfaces'; | ||
import { Predicate } from './compose-filters'; | ||
export declare function createPropertyFilterPredicate<T>(propertyFiltering: UseCollectionOptions<T>['propertyFiltering'], query?: PropertyFilterQuery): null | Predicate<T>; | ||
export declare const fixupFalsyValues: <T>(value: T) => string | T; | ||
export declare const fixupFalsyValues: <T>(value: T) => T | string; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.fixupFalsyValues = exports.createPropertyFilterPredicate = void 0; | ||
var compare_dates_js_1 = require("../date-utils/compare-dates.js"); | ||
var logging_js_1 = require("../logging.js"); | ||
var filterUsingOperator = function (itemValue, _a) { | ||
var tokenValue = _a.tokenValue, _b = _a.operator, operator = _b.operator, match = _b.match, tokenType = _b.tokenType; | ||
exports.fixupFalsyValues = void 0; | ||
exports.createPropertyFilterPredicate = createPropertyFilterPredicate; | ||
const compare_dates_js_1 = require("../date-utils/compare-dates.js"); | ||
const logging_js_1 = require("../logging.js"); | ||
const filterUsingOperator = (itemValue, { tokenValue, operator: { operator, match, tokenType }, }) => { | ||
// For match="date" or match="datetime" we expect the value to be a Date object. | ||
@@ -12,3 +12,3 @@ // The token value is expected to be an ISO date- or datetime string, example: | ||
if (match === 'date' || match === 'datetime') { | ||
return matchDateValue({ tokenValue: tokenValue, itemValue: itemValue, operator: operator, match: match }); | ||
return matchDateValue({ tokenValue, itemValue, operator, match }); | ||
} | ||
@@ -27,8 +27,7 @@ // For custom match functions there is no expectation to value or token type: the function is supposed | ||
// match(operator="=", token=["A", "B"], value="A") == true | ||
return matchPrimitiveValue({ tokenValue: tokenValue, itemValue: itemValue, operator: operator, tokenType: tokenType }); | ||
return matchPrimitiveValue({ tokenValue, itemValue, operator, tokenType }); | ||
}; | ||
function matchDateValue(_a) { | ||
var tokenValue = _a.tokenValue, itemValue = _a.itemValue, operator = _a.operator, match = _a.match; | ||
var comparator = match === 'date' ? compare_dates_js_1.compareDates : compare_dates_js_1.compareTimestamps; | ||
var comparisonResult = comparator(itemValue, tokenValue); | ||
function matchDateValue({ tokenValue, itemValue, operator, match, }) { | ||
const comparator = match === 'date' ? compare_dates_js_1.compareDates : compare_dates_js_1.compareTimestamps; | ||
const comparisonResult = comparator(itemValue, tokenValue); | ||
switch (operator) { | ||
@@ -48,8 +47,7 @@ case '<': | ||
default: | ||
(0, logging_js_1.warnOnce)("Unsupported operator \"".concat(operator, "\" given for match=\"").concat(match, "\".")); | ||
(0, logging_js_1.warnOnce)(`Unsupported operator "${operator}" given for match="${match}".`); | ||
return false; | ||
} | ||
} | ||
function matchPrimitiveValue(_a) { | ||
var tokenValue = _a.tokenValue, itemValue = _a.itemValue, operator = _a.operator, tokenType = _a.tokenType; | ||
function matchPrimitiveValue({ tokenValue, itemValue, operator, tokenType, }) { | ||
if (tokenType === 'enum') { | ||
@@ -66,3 +64,3 @@ if (!tokenValue || !Array.isArray(tokenValue)) { | ||
default: | ||
(0, logging_js_1.warnOnce)("Unsupported operator \"".concat(operator, "\" given for tokenType==\"enum\".")); | ||
(0, logging_js_1.warnOnce)(`Unsupported operator "${operator}" given for tokenType=="enum".`); | ||
return false; | ||
@@ -103,10 +101,10 @@ } | ||
// If the operator is a negation, we want none of the properties of the object to match. | ||
var isNegation = operator.startsWith('!'); | ||
return Object.keys(filteringPropertiesMap)[isNegation ? 'every' : 'some'](function (propertyKey) { | ||
var operators = filteringPropertiesMap[propertyKey].operators; | ||
var propertyOperator = operators[operator]; | ||
const isNegation = operator.startsWith('!'); | ||
return Object.keys(filteringPropertiesMap)[isNegation ? 'every' : 'some'](propertyKey => { | ||
const { operators } = filteringPropertiesMap[propertyKey]; | ||
const propertyOperator = operators[operator]; | ||
if (!propertyOperator) { | ||
return isNegation; | ||
} | ||
return filterUsingOperator(item[propertyKey], { tokenValue: tokenValue, operator: propertyOperator }); | ||
return filterUsingOperator(item[propertyKey], { tokenValue, operator: propertyOperator }); | ||
}); | ||
@@ -121,5 +119,5 @@ } | ||
} | ||
var property = filteringPropertiesMap[token.propertyKey]; | ||
var operator = property.operators[token.operator]; | ||
var itemValue = (operator === null || operator === void 0 ? void 0 : operator.match) | ||
const property = filteringPropertiesMap[token.propertyKey]; | ||
const operator = property.operators[token.operator]; | ||
const itemValue = (operator === null || operator === void 0 ? void 0 : operator.match) | ||
? item[token.propertyKey] | ||
@@ -135,9 +133,8 @@ : (0, exports.fixupFalsyValues)(item[token.propertyKey]); | ||
function defaultFilteringFunction(filteringPropertiesMap) { | ||
return function (item, query) { | ||
return (item, query) => { | ||
var _a; | ||
function evaluate(tokenOrGroup) { | ||
if ('operation' in tokenOrGroup) { | ||
var result = tokenOrGroup.operation === 'and' ? true : !tokenOrGroup.tokens.length; | ||
for (var _i = 0, _a = tokenOrGroup.tokens; _i < _a.length; _i++) { | ||
var group = _a[_i]; | ||
let result = tokenOrGroup.operation === 'and' ? true : !tokenOrGroup.tokens.length; | ||
for (const group of tokenOrGroup.tokens) { | ||
result = tokenOrGroup.operation === 'and' ? result && evaluate(group) : result || evaluate(group); | ||
@@ -157,12 +154,9 @@ } | ||
} | ||
function createPropertyFilterPredicate(propertyFiltering, query) { | ||
if (query === void 0) { query = { tokens: [], operation: 'and' }; } | ||
function createPropertyFilterPredicate(propertyFiltering, query = { tokens: [], operation: 'and' }) { | ||
if (!propertyFiltering) { | ||
return null; | ||
} | ||
var filteringPropertiesMap = propertyFiltering.filteringProperties.reduce(function (acc, _a) { | ||
var _b; | ||
var key = _a.key, operators = _a.operators, defaultOperator = _a.defaultOperator; | ||
var operatorMap = (_b = {}, _b[defaultOperator !== null && defaultOperator !== void 0 ? defaultOperator : '='] = { operator: defaultOperator !== null && defaultOperator !== void 0 ? defaultOperator : '=' }, _b); | ||
operators === null || operators === void 0 ? void 0 : operators.forEach(function (op) { | ||
const filteringPropertiesMap = propertyFiltering.filteringProperties.reduce((acc, { key, operators, defaultOperator }) => { | ||
const operatorMap = { [defaultOperator !== null && defaultOperator !== void 0 ? defaultOperator : '=']: { operator: defaultOperator !== null && defaultOperator !== void 0 ? defaultOperator : '=' } }; | ||
operators === null || operators === void 0 ? void 0 : operators.forEach(op => { | ||
if (typeof op === 'string') { | ||
@@ -178,7 +172,6 @@ operatorMap[op] = { operator: op }; | ||
}, {}); | ||
var filteringFunction = propertyFiltering.filteringFunction || defaultFilteringFunction(filteringPropertiesMap); | ||
return function (item) { return filteringFunction(item, query); }; | ||
const filteringFunction = propertyFiltering.filteringFunction || defaultFilteringFunction(filteringPropertiesMap); | ||
return item => filteringFunction(item, query); | ||
} | ||
exports.createPropertyFilterPredicate = createPropertyFilterPredicate; | ||
var fixupFalsyValues = function (value) { | ||
const fixupFalsyValues = (value) => { | ||
if (typeof value === 'boolean') { | ||
@@ -185,0 +178,0 @@ return value + ''; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createComparator = void 0; | ||
exports.createComparator = createComparator; | ||
function getSorter(sortingField) { | ||
@@ -8,3 +8,3 @@ if (!sortingField) { | ||
} | ||
return function (row1, row2) { | ||
return (row1, row2) => { | ||
var _a, _b; | ||
@@ -14,4 +14,4 @@ // Use empty string as a default value, because it works well to compare with both strings and numbers: | ||
// sometimes it is NaN. | ||
var value1 = (_a = row1[sortingField]) !== null && _a !== void 0 ? _a : ''; | ||
var value2 = (_b = row2[sortingField]) !== null && _b !== void 0 ? _b : ''; | ||
const value1 = (_a = row1[sortingField]) !== null && _a !== void 0 ? _a : ''; | ||
const value2 = (_b = row2[sortingField]) !== null && _b !== void 0 ? _b : ''; | ||
if (typeof value1 === 'string' && typeof value2 === 'string') { | ||
@@ -30,6 +30,5 @@ return value1.localeCompare(value2); | ||
} | ||
var direction = state.isDescending ? -1 : 1; | ||
var comparator = (_a = state.sortingColumn.sortingComparator) !== null && _a !== void 0 ? _a : getSorter(state.sortingColumn.sortingField); | ||
return comparator ? function (a, b) { return comparator(a, b) * direction; } : null; | ||
const direction = state.isDescending ? -1 : 1; | ||
const comparator = (_a = state.sortingColumn.sortingComparator) !== null && _a !== void 0 ? _a : getSorter(state.sortingColumn.sortingField); | ||
return comparator ? (a, b) => comparator(a, b) * direction : null; | ||
} | ||
exports.createComparator = createComparator; |
@@ -1,3 +0,2 @@ | ||
/// <reference types="react" /> | ||
import { UseCollectionOptions, CollectionState, CollectionRef, CollectionActions } from './interfaces'; | ||
export declare function useCollectionState<T>(options: UseCollectionOptions<T>, collectionRef: React.RefObject<CollectionRef>): readonly [CollectionState<T>, CollectionActions<T>]; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.useCollectionState = void 0; | ||
exports.useCollectionState = useCollectionState; | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
var react_1 = require("react"); | ||
var utils_js_1 = require("./utils.js"); | ||
const react_1 = require("react"); | ||
const utils_js_1 = require("./utils.js"); | ||
function useCollectionState(options, collectionRef) { | ||
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l; | ||
var _m = (0, react_1.useReducer)(utils_js_1.collectionReducer, { | ||
const [state, dispatch] = (0, react_1.useReducer)(utils_js_1.collectionReducer, { | ||
selectedItems: (_b = (_a = options.selection) === null || _a === void 0 ? void 0 : _a.defaultSelectedItems) !== null && _b !== void 0 ? _b : [], | ||
@@ -17,11 +17,10 @@ expandedItems: (_d = (_c = options.expandableRows) === null || _c === void 0 ? void 0 : _c.defaultExpandedItems) !== null && _d !== void 0 ? _d : [], | ||
propertyFilteringQuery: (_l = (_k = options.propertyFiltering) === null || _k === void 0 ? void 0 : _k.defaultQuery) !== null && _l !== void 0 ? _l : { tokens: [], operation: 'and' }, | ||
}), state = _m[0], dispatch = _m[1]; | ||
}); | ||
return [ | ||
state, | ||
(0, utils_js_1.createActions)({ | ||
dispatch: dispatch, | ||
collectionRef: collectionRef, | ||
dispatch, | ||
collectionRef, | ||
}), | ||
]; | ||
} | ||
exports.useCollectionState = useCollectionState; |
"use strict"; | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.useCollection = void 0; | ||
exports.useCollection = useCollection; | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
var react_1 = require("react"); | ||
var index_js_1 = require("./operations/index.js"); | ||
var utils_js_1 = require("./utils.js"); | ||
var use_collection_state_js_1 = require("./use-collection-state.js"); | ||
const react_1 = require("react"); | ||
const index_js_1 = require("./operations/index.js"); | ||
const utils_js_1 = require("./utils.js"); | ||
const use_collection_state_js_1 = require("./use-collection-state.js"); | ||
function useCollection(allItems, options) { | ||
var collectionRef = (0, react_1.useRef)(null); | ||
var _a = (0, use_collection_state_js_1.useCollectionState)(options, collectionRef), state = _a[0], actions = _a[1]; | ||
var _b = (0, index_js_1.processItems)(allItems, state, options), items = _b.items, allPageItems = _b.allPageItems, pagesCount = _b.pagesCount, filteredItemsCount = _b.filteredItemsCount, actualPageIndex = _b.actualPageIndex, itemsTree = _b.itemsTree; | ||
var expandedItemsSet = new Set(); | ||
const collectionRef = (0, react_1.useRef)(null); | ||
const [state, actions] = (0, use_collection_state_js_1.useCollectionState)(options, collectionRef); | ||
const { items, allPageItems, pagesCount, filteredItemsCount, actualPageIndex, itemsTree } = (0, index_js_1.processItems)(allItems, state, options); | ||
const expandedItemsSet = new Set(); | ||
if (options.expandableRows) { | ||
for (var _i = 0, _c = state.expandedItems; _i < _c.length; _i++) { | ||
var item = _c[_i]; | ||
for (const item of state.expandedItems) { | ||
expandedItemsSet.add(options.expandableRows.getId(item)); | ||
} | ||
} | ||
var visibleItems = items; | ||
let visibleItems = items; | ||
if (options.expandableRows) { | ||
var flatItems_1 = new Array(); | ||
var getId_1 = options.expandableRows.getId; | ||
var traverse_1 = function (items) { | ||
for (var _i = 0, items_1 = items; _i < items_1.length; _i++) { | ||
var item = items_1[_i]; | ||
flatItems_1.push(item); | ||
if (expandedItemsSet.has(getId_1(item))) { | ||
traverse_1(itemsTree.getChildren(item)); | ||
const flatItems = new Array(); | ||
const getId = options.expandableRows.getId; | ||
const traverse = (items) => { | ||
for (const item of items) { | ||
flatItems.push(item); | ||
if (expandedItemsSet.has(getId(item))) { | ||
traverse(itemsTree.getChildren(item)); | ||
} | ||
} | ||
}; | ||
traverse_1(items); | ||
visibleItems = flatItems_1; | ||
traverse(items); | ||
visibleItems = flatItems; | ||
} | ||
// Removing selected items that are no longer present in items unless keepSelection=true. | ||
if (options.selection && !options.selection.keepSelection) { | ||
var newSelectedItems = (0, index_js_1.processSelectedItems)(visibleItems, state.selectedItems, options.selection.trackBy); | ||
const newSelectedItems = (0, index_js_1.processSelectedItems)(visibleItems, state.selectedItems, options.selection.trackBy); | ||
if (!(0, index_js_1.itemsAreEqual)(newSelectedItems, state.selectedItems, options.selection.trackBy)) { | ||
@@ -59,3 +46,3 @@ // This is a recommended pattern for how to handle the state, dependent on the incoming props | ||
if (options.expandableRows) { | ||
var newExpandedItems = visibleItems.filter(function (item) { return expandedItemsSet.has(options.expandableRows.getId(item)); }); | ||
const newExpandedItems = visibleItems.filter(item => expandedItemsSet.has(options.expandableRows.getId(item))); | ||
if (!(0, index_js_1.itemsAreEqual)(newExpandedItems, state.expandedItems, options.expandableRows.getId)) { | ||
@@ -65,10 +52,12 @@ actions.setExpandedItems(newExpandedItems); | ||
} | ||
return __assign({ items: items, allPageItems: allPageItems, filteredItemsCount: filteredItemsCount, actions: actions }, (0, utils_js_1.createSyncProps)(options, state, actions, collectionRef, { | ||
actualPageIndex: actualPageIndex, | ||
pagesCount: pagesCount, | ||
allItems: allItems, | ||
allPageItems: allPageItems, | ||
itemsTree: itemsTree, | ||
return Object.assign({ items, | ||
allPageItems, | ||
filteredItemsCount, | ||
actions }, (0, utils_js_1.createSyncProps)(options, state, actions, collectionRef, { | ||
actualPageIndex, | ||
pagesCount, | ||
allItems, | ||
allPageItems, | ||
itemsTree, | ||
})); | ||
} | ||
exports.useCollection = useCollection; |
123
cjs/utils.js
"use strict"; | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { | ||
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { | ||
if (ar || !(i in from)) { | ||
if (!ar) ar = Array.prototype.slice.call(from, 0, i); | ||
ar[i] = from[i]; | ||
} | ||
} | ||
return to.concat(ar || Array.prototype.slice.call(from)); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createSyncProps = exports.createActions = exports.collectionReducer = void 0; | ||
var property_filter_js_1 = require("./operations/property-filter.js"); | ||
exports.collectionReducer = collectionReducer; | ||
exports.createActions = createActions; | ||
exports.createSyncProps = createSyncProps; | ||
const property_filter_js_1 = require("./operations/property-filter.js"); | ||
function collectionReducer(state, action) { | ||
var newState = __assign({}, state); | ||
const newState = Object.assign({}, state); | ||
switch (action.type) { | ||
@@ -52,36 +34,31 @@ case 'selection': | ||
} | ||
exports.collectionReducer = collectionReducer; | ||
function createActions(_a) { | ||
var dispatch = _a.dispatch, collectionRef = _a.collectionRef; | ||
function createActions({ dispatch, collectionRef, }) { | ||
return { | ||
setFiltering: function (filteringText) { | ||
dispatch({ type: 'filtering', filteringText: filteringText }); | ||
setFiltering(filteringText) { | ||
dispatch({ type: 'filtering', filteringText }); | ||
collectionRef.current && collectionRef.current.scrollToTop(); | ||
}, | ||
setSorting: function (state) { | ||
setSorting(state) { | ||
dispatch({ type: 'sorting', sortingState: state }); | ||
collectionRef.current && collectionRef.current.scrollToTop(); | ||
}, | ||
setCurrentPage: function (pageIndex) { | ||
dispatch({ type: 'pagination', pageIndex: pageIndex }); | ||
setCurrentPage(pageIndex) { | ||
dispatch({ type: 'pagination', pageIndex }); | ||
collectionRef.current && collectionRef.current.scrollToTop(); | ||
}, | ||
setSelectedItems: function (selectedItems) { | ||
dispatch({ type: 'selection', selectedItems: selectedItems }); | ||
setSelectedItems(selectedItems) { | ||
dispatch({ type: 'selection', selectedItems }); | ||
}, | ||
setPropertyFiltering: function (query) { | ||
dispatch({ type: 'property-filtering', query: query }); | ||
setPropertyFiltering(query) { | ||
dispatch({ type: 'property-filtering', query }); | ||
collectionRef.current && collectionRef.current.scrollToTop(); | ||
}, | ||
setExpandedItems: function (expandedItems) { | ||
dispatch({ type: 'expansion', expandedItems: expandedItems }); | ||
setExpandedItems(expandedItems) { | ||
dispatch({ type: 'expansion', expandedItems }); | ||
}, | ||
}; | ||
} | ||
exports.createActions = createActions; | ||
function createSyncProps(options, _a, actions, collectionRef, _b) { | ||
var _c, _d, _e, _f, _g; | ||
var filteringText = _a.filteringText, sortingState = _a.sortingState, selectedItems = _a.selectedItems, expandedItems = _a.expandedItems, currentPageIndex = _a.currentPageIndex, propertyFilteringQuery = _a.propertyFilteringQuery; | ||
var pagesCount = _b.pagesCount, actualPageIndex = _b.actualPageIndex, allItems = _b.allItems, allPageItems = _b.allPageItems, itemsTree = _b.itemsTree; | ||
var empty = options.filtering | ||
function createSyncProps(options, { filteringText, sortingState, selectedItems, expandedItems, currentPageIndex, propertyFilteringQuery, }, actions, collectionRef, { pagesCount, actualPageIndex, allItems, allPageItems, itemsTree, }) { | ||
var _a, _b, _c, _d, _e; | ||
let empty = options.filtering | ||
? allItems.length | ||
@@ -96,12 +73,12 @@ ? options.filtering.noMatch | ||
: empty; | ||
var filteringOptions = options.propertyFiltering | ||
? options.propertyFiltering.filteringProperties.reduce(function (acc, property) { | ||
Object.keys(allItems.reduce(function (acc, item) { | ||
const filteringOptions = options.propertyFiltering | ||
? options.propertyFiltering.filteringProperties.reduce((acc, property) => { | ||
Object.keys(allItems.reduce((acc, item) => { | ||
acc['' + (0, property_filter_js_1.fixupFalsyValues)(item[property.key])] = true; | ||
return acc; | ||
}, {})).forEach(function (value) { | ||
}, {})).forEach(value => { | ||
if (value !== '') { | ||
acc.push({ | ||
propertyKey: property.key, | ||
value: value, | ||
value, | ||
}); | ||
@@ -114,6 +91,5 @@ } | ||
return { | ||
collectionProps: __assign(__assign(__assign(__assign(__assign({ empty: empty }, (options.sorting | ||
collectionProps: Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({ empty }, (options.sorting | ||
? { | ||
onSortingChange: function (_a) { | ||
var detail = _a.detail; | ||
onSortingChange: ({ detail }) => { | ||
actions.setSorting(detail); | ||
@@ -127,15 +103,13 @@ }, | ||
expandableRows: { | ||
getItemChildren: function (item) { | ||
getItemChildren(item) { | ||
return itemsTree.getChildren(item); | ||
}, | ||
isItemExpandable: function (item) { | ||
isItemExpandable(item) { | ||
return itemsTree.getChildren(item).length > 0; | ||
}, | ||
expandedItems: expandedItems, | ||
onExpandableItemToggle: function (_a) { | ||
var _b = _a.detail, item = _b.item, expanded = _b.expanded; | ||
var getId = options.expandableRows.getId; | ||
expandedItems, | ||
onExpandableItemToggle: ({ detail: { item, expanded } }) => { | ||
const getId = options.expandableRows.getId; | ||
if (expanded) { | ||
for (var _i = 0, expandedItems_1 = expandedItems; _i < expandedItems_1.length; _i++) { | ||
var stateItem = expandedItems_1[_i]; | ||
for (const stateItem of expandedItems) { | ||
if (getId(stateItem) === getId(item)) { | ||
@@ -145,6 +119,6 @@ return; | ||
} | ||
actions.setExpandedItems(__spreadArray(__spreadArray([], expandedItems, true), [item], false)); | ||
actions.setExpandedItems([...expandedItems, item]); | ||
} | ||
else { | ||
actions.setExpandedItems(expandedItems.filter(function (stateItem) { return getId(stateItem) !== getId(item); })); | ||
actions.setExpandedItems(expandedItems.filter(stateItem => getId(stateItem) !== getId(item))); | ||
} | ||
@@ -161,10 +135,9 @@ }, | ||
? { | ||
onSelectionChange: function (_a) { | ||
var selectedItems = _a.detail.selectedItems; | ||
onSelectionChange: ({ detail: { selectedItems } }) => { | ||
actions.setSelectedItems(selectedItems); | ||
}, | ||
selectedItems: selectedItems, | ||
trackBy: (_c = options.selection.trackBy) !== null && _c !== void 0 ? _c : (_d = options.expandableRows) === null || _d === void 0 ? void 0 : _d.getId, | ||
selectedItems, | ||
trackBy: (_a = options.selection.trackBy) !== null && _a !== void 0 ? _a : (_b = options.expandableRows) === null || _b === void 0 ? void 0 : _b.getId, | ||
} | ||
: {})), { ref: collectionRef, firstIndex: 1, totalItemsCount: allPageItems.length }), (((_e = options.pagination) === null || _e === void 0 ? void 0 : _e.pageSize) | ||
: {})), { ref: collectionRef, firstIndex: 1, totalItemsCount: allPageItems.length }), (((_c = options.pagination) === null || _c === void 0 ? void 0 : _c.pageSize) | ||
? { | ||
@@ -175,5 +148,4 @@ firstIndex: ((actualPageIndex !== null && actualPageIndex !== void 0 ? actualPageIndex : currentPageIndex) - 1) * options.pagination.pageSize + 1, | ||
filterProps: { | ||
filteringText: filteringText, | ||
onChange: function (_a) { | ||
var filteringText = _a.detail.filteringText; | ||
filteringText, | ||
onChange: ({ detail: { filteringText } }) => { | ||
actions.setFiltering(filteringText); | ||
@@ -184,9 +156,8 @@ }, | ||
query: propertyFilteringQuery, | ||
onChange: function (_a) { | ||
var query = _a.detail; | ||
onChange: ({ detail: query }) => { | ||
actions.setPropertyFiltering(query); | ||
}, | ||
filteringProperties: ((_f = options.propertyFiltering) === null || _f === void 0 ? void 0 : _f.filteringProperties) || [], | ||
filteringOptions: filteringOptions, | ||
freeTextFiltering: (_g = options.propertyFiltering) === null || _g === void 0 ? void 0 : _g.freeTextFiltering, | ||
filteringProperties: ((_d = options.propertyFiltering) === null || _d === void 0 ? void 0 : _d.filteringProperties) || [], | ||
filteringOptions, | ||
freeTextFiltering: (_e = options.propertyFiltering) === null || _e === void 0 ? void 0 : _e.freeTextFiltering, | ||
}, | ||
@@ -197,4 +168,3 @@ paginationProps: { | ||
pagesCount: pagesCount, | ||
onChange: function (_a) { | ||
var currentPageIndex = _a.detail.currentPageIndex; | ||
onChange: ({ detail: { currentPageIndex } }) => { | ||
actions.setCurrentPage(currentPageIndex); | ||
@@ -205,2 +175,1 @@ }, | ||
} | ||
exports.createSyncProps = createSyncProps; |
{ | ||
"commit": "7af5676e47a404d25e387d9cdeca3d055c767fed" | ||
"commit": "5ae4cc5fbf010b7ffc69b9d9e2a4e37213a51ca1" | ||
} |
@@ -29,5 +29,5 @@ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
function startOfDay(date) { | ||
var copy = new Date(date.getTime()); | ||
const copy = new Date(date.getTime()); | ||
copy.setHours(0, 0, 0, 0); | ||
return copy; | ||
} |
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
var isDevelopment = process.env.NODE_ENV !== 'production'; | ||
var messageCache = new Set(); | ||
const isDevelopment = process.env.NODE_ENV !== 'production'; | ||
const messageCache = new Set(); | ||
export function warnOnce(message) { | ||
if (isDevelopment) { | ||
var warning = "[AwsUi] collection-hooks ".concat(message); | ||
const warning = `[AwsUi] collection-hooks ${message}`; | ||
if (!messageCache.has(warning)) { | ||
@@ -9,0 +9,0 @@ messageCache.add(warning); |
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
export function composeFilters() { | ||
var predicates = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
predicates[_i] = arguments[_i]; | ||
} | ||
export function composeFilters(...predicates) { | ||
return predicates.some(Boolean) | ||
? function (item) { | ||
for (var _i = 0, predicates_1 = predicates; _i < predicates_1.length; _i++) { | ||
var predicate = predicates_1[_i]; | ||
? item => { | ||
for (const predicate of predicates) { | ||
if (predicate && !predicate(item)) { | ||
@@ -13,0 +8,0 @@ return false; |
@@ -6,17 +6,14 @@ function defaultFilteringFunction(item, filteringText, filteringFields) { | ||
filteringFields = filteringFields || Object.keys(item); | ||
var lowFilteringText = filteringText.toLowerCase(); | ||
return filteringFields.some(function (key) { | ||
return String(item[key]) | ||
.toLowerCase() | ||
.indexOf(lowFilteringText) > -1; | ||
}); | ||
const lowFilteringText = filteringText.toLowerCase(); | ||
return filteringFields.some(key => String(item[key]) | ||
.toLowerCase() | ||
.indexOf(lowFilteringText) > -1); | ||
} | ||
export function createFilterPredicate(filtering, filteringText) { | ||
export function createFilterPredicate(filtering, filteringText = '') { | ||
var _a; | ||
if (filteringText === void 0) { filteringText = ''; } | ||
if (!filtering) { | ||
return null; | ||
} | ||
var filteringFunction = (_a = filtering.filteringFunction) !== null && _a !== void 0 ? _a : defaultFilteringFunction; | ||
return function (item) { return filteringFunction(item, filteringText, filtering.fields); }; | ||
const filteringFunction = (_a = filtering.filteringFunction) !== null && _a !== void 0 ? _a : defaultFilteringFunction; | ||
return item => filteringFunction(item, filteringText, filtering.fields); | ||
} |
@@ -12,3 +12,3 @@ import { UseCollectionOptions, CollectionState, TrackBy } from '../interfaces'; | ||
export declare const getTrackableValue: <T>(trackBy: TrackBy<T> | undefined, item: T) => string | T; | ||
export declare const processSelectedItems: <T>(items: readonly T[], selectedItems: readonly T[], trackBy?: TrackBy<T> | undefined) => T[]; | ||
export declare const itemsAreEqual: <T>(items1: readonly T[], items2: readonly T[], trackBy?: TrackBy<T> | undefined) => boolean; | ||
export declare const processSelectedItems: <T>(items: ReadonlyArray<T>, selectedItems: ReadonlyArray<T>, trackBy?: TrackBy<T>) => T[]; | ||
export declare const itemsAreEqual: <T>(items1: ReadonlyArray<T>, items2: ReadonlyArray<T>, trackBy?: TrackBy<T>) => boolean; |
@@ -7,17 +7,15 @@ import { createFilterPredicate } from './filter.js'; | ||
import { composeFilters } from './compose-filters.js'; | ||
export function processItems(items, _a, _b) { | ||
var filteringText = _a.filteringText, sortingState = _a.sortingState, currentPageIndex = _a.currentPageIndex, propertyFilteringQuery = _a.propertyFilteringQuery; | ||
var filtering = _b.filtering, sorting = _b.sorting, pagination = _b.pagination, propertyFiltering = _b.propertyFiltering, expandableRows = _b.expandableRows; | ||
var itemsTree = new ItemsTree(items, expandableRows); | ||
var filterPredicate = composeFilters(createPropertyFilterPredicate(propertyFiltering, propertyFilteringQuery), createFilterPredicate(filtering, filteringText)); | ||
export function processItems(items, { filteringText, sortingState, currentPageIndex, propertyFilteringQuery }, { filtering, sorting, pagination, propertyFiltering, expandableRows }) { | ||
const itemsTree = new ItemsTree(items, expandableRows); | ||
const filterPredicate = composeFilters(createPropertyFilterPredicate(propertyFiltering, propertyFilteringQuery), createFilterPredicate(filtering, filteringText)); | ||
if (filterPredicate) { | ||
itemsTree.filter(filterPredicate); | ||
} | ||
var comparator = createComparator(sorting, sortingState); | ||
const comparator = createComparator(sorting, sortingState); | ||
if (comparator) { | ||
itemsTree.sort(comparator); | ||
} | ||
var allPageItems = itemsTree.getItems(); | ||
var filteredItemsCount = filterPredicate ? itemsTree.getSize() : undefined; | ||
var pageProps = createPageProps(pagination, currentPageIndex, allPageItems); | ||
const allPageItems = itemsTree.getItems(); | ||
const filteredItemsCount = filterPredicate ? itemsTree.getSize() : undefined; | ||
const pageProps = createPageProps(pagination, currentPageIndex, allPageItems); | ||
if (pageProps) { | ||
@@ -27,6 +25,6 @@ return { | ||
allPageItems: allPageItems, | ||
filteredItemsCount: filteredItemsCount, | ||
filteredItemsCount, | ||
pagesCount: pageProps === null || pageProps === void 0 ? void 0 : pageProps.pagesCount, | ||
actualPageIndex: pageProps === null || pageProps === void 0 ? void 0 : pageProps.pageIndex, | ||
itemsTree: itemsTree, | ||
itemsTree, | ||
}; | ||
@@ -37,9 +35,9 @@ } | ||
allPageItems: allPageItems, | ||
filteredItemsCount: filteredItemsCount, | ||
filteredItemsCount, | ||
pagesCount: undefined, | ||
actualPageIndex: undefined, | ||
itemsTree: itemsTree, | ||
itemsTree, | ||
}; | ||
} | ||
export var getTrackableValue = function (trackBy, item) { | ||
export const getTrackableValue = (trackBy, item) => { | ||
if (!trackBy) { | ||
@@ -53,14 +51,14 @@ return item; | ||
}; | ||
export var processSelectedItems = function (items, selectedItems, trackBy) { | ||
var selectedSet = new Set(); | ||
selectedItems.forEach(function (item) { return selectedSet.add(getTrackableValue(trackBy, item)); }); | ||
return items.filter(function (item) { return selectedSet.has(getTrackableValue(trackBy, item)); }); | ||
export const processSelectedItems = (items, selectedItems, trackBy) => { | ||
const selectedSet = new Set(); | ||
selectedItems.forEach(item => selectedSet.add(getTrackableValue(trackBy, item))); | ||
return items.filter(item => selectedSet.has(getTrackableValue(trackBy, item))); | ||
}; | ||
export var itemsAreEqual = function (items1, items2, trackBy) { | ||
export const itemsAreEqual = (items1, items2, trackBy) => { | ||
if (items1.length !== items2.length) { | ||
return false; | ||
} | ||
var set1 = new Set(); | ||
items1.forEach(function (item) { return set1.add(getTrackableValue(trackBy, item)); }); | ||
return items2.every(function (item) { return set1.has(getTrackableValue(trackBy, item)); }); | ||
const set1 = new Set(); | ||
items1.forEach(item => set1.add(getTrackableValue(trackBy, item))); | ||
return items2.every(item => set1.has(getTrackableValue(trackBy, item))); | ||
}; |
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
var ItemsTree = /** @class */ (function () { | ||
function ItemsTree(items, treeProps) { | ||
var _this = this; | ||
export class ItemsTree { | ||
constructor(items, treeProps) { | ||
var _a; | ||
@@ -11,55 +10,54 @@ this.size = 0; | ||
this.idToChildren = new Map(); | ||
this.filter = function (predicate) { | ||
if (!_this.hasNesting) { | ||
_this.items = _this.items.filter(predicate); | ||
_this.size = _this.items.length; | ||
this.filter = (predicate) => { | ||
if (!this.hasNesting) { | ||
this.items = this.items.filter(predicate); | ||
this.size = this.items.length; | ||
} | ||
else { | ||
_this.filterTree(predicate); | ||
this.filterTree(predicate); | ||
} | ||
return _this; | ||
return this; | ||
}; | ||
this.sort = function (comparator) { | ||
if (!_this.hasNesting) { | ||
_this.items = _this.items.slice().sort(comparator); | ||
this.sort = (comparator) => { | ||
if (!this.hasNesting) { | ||
this.items = this.items.slice().sort(comparator); | ||
} | ||
else { | ||
_this.sortTree(comparator); | ||
this.sortTree(comparator); | ||
} | ||
return _this; | ||
return this; | ||
}; | ||
this.getChildren = function (item) { | ||
this.getChildren = (item) => { | ||
var _a; | ||
return (_a = (_this.treeProps && _this.idToChildren.get(_this.treeProps.getId(item)))) !== null && _a !== void 0 ? _a : []; | ||
return (_a = (this.treeProps && this.idToChildren.get(this.treeProps.getId(item)))) !== null && _a !== void 0 ? _a : []; | ||
}; | ||
this.getItems = function () { | ||
if (_this.hasNesting) { | ||
return _this.roots; | ||
this.getItems = () => { | ||
if (this.hasNesting) { | ||
return this.roots; | ||
} | ||
return _this.items; | ||
return this.items; | ||
}; | ||
this.getSize = function () { | ||
return _this.size; | ||
this.getSize = () => { | ||
return this.size; | ||
}; | ||
this.filterTree = function (predicate) { | ||
var filterNode = function (item) { | ||
var children = _this.getChildren(item); | ||
var filteredChildren = children.filter(filterNode); | ||
_this.size -= children.length - filteredChildren.length; | ||
_this.setChildren(item, filteredChildren); | ||
this.filterTree = (predicate) => { | ||
const filterNode = (item) => { | ||
const children = this.getChildren(item); | ||
const filteredChildren = children.filter(filterNode); | ||
this.size -= children.length - filteredChildren.length; | ||
this.setChildren(item, filteredChildren); | ||
return predicate(item) || filteredChildren.length > 0; | ||
}; | ||
var roots = _this.roots; | ||
_this.roots = _this.roots.filter(filterNode); | ||
_this.size -= roots.length - _this.roots.length; | ||
const roots = this.roots; | ||
this.roots = this.roots.filter(filterNode); | ||
this.size -= roots.length - this.roots.length; | ||
}; | ||
this.sortTree = function (comparator) { | ||
var sortLevel = function (items) { | ||
this.sortTree = (comparator) => { | ||
const sortLevel = (items) => { | ||
items.sort(comparator); | ||
for (var _i = 0, items_2 = items; _i < items_2.length; _i++) { | ||
var item = items_2[_i]; | ||
sortLevel(_this.getChildren(item)); | ||
for (const item of items) { | ||
sortLevel(this.getChildren(item)); | ||
} | ||
}; | ||
sortLevel(_this.roots); | ||
sortLevel(this.roots); | ||
}; | ||
@@ -73,5 +71,4 @@ this.size = items.length; | ||
// Assign item children. | ||
for (var _i = 0, items_1 = items; _i < items_1.length; _i++) { | ||
var item = items_1[_i]; | ||
var parentId = treeProps.getParentId(item); | ||
for (const item of items) { | ||
const parentId = treeProps.getParentId(item); | ||
if (parentId === null) { | ||
@@ -81,3 +78,3 @@ this.roots.push(item); | ||
else { | ||
var children = (_a = this.idToChildren.get(parentId)) !== null && _a !== void 0 ? _a : []; | ||
const children = (_a = this.idToChildren.get(parentId)) !== null && _a !== void 0 ? _a : []; | ||
children.push(item); | ||
@@ -89,19 +86,15 @@ this.idToChildren.set(parentId, children); | ||
// Assign item levels. | ||
var traverse = function (item, level) { | ||
const traverse = (item, level = 1) => { | ||
var _a; | ||
if (level === void 0) { level = 1; } | ||
for (var _i = 0, _b = (_a = _this.idToChildren.get(treeProps.getId(item))) !== null && _a !== void 0 ? _a : []; _i < _b.length; _i++) { | ||
var child = _b[_i]; | ||
for (const child of (_a = this.idToChildren.get(treeProps.getId(item))) !== null && _a !== void 0 ? _a : []) { | ||
traverse(child, level + 1); | ||
} | ||
}; | ||
this.roots.forEach(function (root) { return traverse(root); }); | ||
this.roots.forEach(root => traverse(root)); | ||
} | ||
ItemsTree.prototype.setChildren = function (item, children) { | ||
setChildren(item, children) { | ||
if (this.treeProps) { | ||
this.idToChildren.set(this.treeProps.getId(item), children); | ||
} | ||
}; | ||
return ItemsTree; | ||
}()); | ||
export { ItemsTree }; | ||
} | ||
} |
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
var DEFAULT_PAGE_SIZE = 10; | ||
const DEFAULT_PAGE_SIZE = 10; | ||
export function createPageProps(pagination, currentPageIndex, items) { | ||
@@ -9,9 +9,9 @@ var _a; | ||
} | ||
var pageSize = (_a = pagination.pageSize) !== null && _a !== void 0 ? _a : DEFAULT_PAGE_SIZE; | ||
var pagesCount = Math.ceil(items.length / pageSize); | ||
var pageIndex = currentPageIndex !== null && currentPageIndex !== void 0 ? currentPageIndex : 1; | ||
const pageSize = (_a = pagination.pageSize) !== null && _a !== void 0 ? _a : DEFAULT_PAGE_SIZE; | ||
const pagesCount = Math.ceil(items.length / pageSize); | ||
let pageIndex = currentPageIndex !== null && currentPageIndex !== void 0 ? currentPageIndex : 1; | ||
if (pageIndex < 1 || pageIndex > pagesCount || Number.isNaN(pageIndex)) { | ||
pageIndex = 1; | ||
} | ||
return { pageSize: pageSize, pagesCount: pagesCount, pageIndex: pageIndex }; | ||
return { pageSize, pagesCount, pageIndex }; | ||
} |
import { PropertyFilterQuery, UseCollectionOptions } from '../interfaces'; | ||
import { Predicate } from './compose-filters'; | ||
export declare function createPropertyFilterPredicate<T>(propertyFiltering: UseCollectionOptions<T>['propertyFiltering'], query?: PropertyFilterQuery): null | Predicate<T>; | ||
export declare const fixupFalsyValues: <T>(value: T) => string | T; | ||
export declare const fixupFalsyValues: <T>(value: T) => T | string; |
import { compareDates, compareTimestamps } from '../date-utils/compare-dates.js'; | ||
import { warnOnce } from '../logging.js'; | ||
var filterUsingOperator = function (itemValue, _a) { | ||
var tokenValue = _a.tokenValue, _b = _a.operator, operator = _b.operator, match = _b.match, tokenType = _b.tokenType; | ||
const filterUsingOperator = (itemValue, { tokenValue, operator: { operator, match, tokenType }, }) => { | ||
// For match="date" or match="datetime" we expect the value to be a Date object. | ||
@@ -9,3 +8,3 @@ // The token value is expected to be an ISO date- or datetime string, example: | ||
if (match === 'date' || match === 'datetime') { | ||
return matchDateValue({ tokenValue: tokenValue, itemValue: itemValue, operator: operator, match: match }); | ||
return matchDateValue({ tokenValue, itemValue, operator, match }); | ||
} | ||
@@ -24,8 +23,7 @@ // For custom match functions there is no expectation to value or token type: the function is supposed | ||
// match(operator="=", token=["A", "B"], value="A") == true | ||
return matchPrimitiveValue({ tokenValue: tokenValue, itemValue: itemValue, operator: operator, tokenType: tokenType }); | ||
return matchPrimitiveValue({ tokenValue, itemValue, operator, tokenType }); | ||
}; | ||
function matchDateValue(_a) { | ||
var tokenValue = _a.tokenValue, itemValue = _a.itemValue, operator = _a.operator, match = _a.match; | ||
var comparator = match === 'date' ? compareDates : compareTimestamps; | ||
var comparisonResult = comparator(itemValue, tokenValue); | ||
function matchDateValue({ tokenValue, itemValue, operator, match, }) { | ||
const comparator = match === 'date' ? compareDates : compareTimestamps; | ||
const comparisonResult = comparator(itemValue, tokenValue); | ||
switch (operator) { | ||
@@ -45,8 +43,7 @@ case '<': | ||
default: | ||
warnOnce("Unsupported operator \"".concat(operator, "\" given for match=\"").concat(match, "\".")); | ||
warnOnce(`Unsupported operator "${operator}" given for match="${match}".`); | ||
return false; | ||
} | ||
} | ||
function matchPrimitiveValue(_a) { | ||
var tokenValue = _a.tokenValue, itemValue = _a.itemValue, operator = _a.operator, tokenType = _a.tokenType; | ||
function matchPrimitiveValue({ tokenValue, itemValue, operator, tokenType, }) { | ||
if (tokenType === 'enum') { | ||
@@ -63,3 +60,3 @@ if (!tokenValue || !Array.isArray(tokenValue)) { | ||
default: | ||
warnOnce("Unsupported operator \"".concat(operator, "\" given for tokenType==\"enum\".")); | ||
warnOnce(`Unsupported operator "${operator}" given for tokenType=="enum".`); | ||
return false; | ||
@@ -100,10 +97,10 @@ } | ||
// If the operator is a negation, we want none of the properties of the object to match. | ||
var isNegation = operator.startsWith('!'); | ||
return Object.keys(filteringPropertiesMap)[isNegation ? 'every' : 'some'](function (propertyKey) { | ||
var operators = filteringPropertiesMap[propertyKey].operators; | ||
var propertyOperator = operators[operator]; | ||
const isNegation = operator.startsWith('!'); | ||
return Object.keys(filteringPropertiesMap)[isNegation ? 'every' : 'some'](propertyKey => { | ||
const { operators } = filteringPropertiesMap[propertyKey]; | ||
const propertyOperator = operators[operator]; | ||
if (!propertyOperator) { | ||
return isNegation; | ||
} | ||
return filterUsingOperator(item[propertyKey], { tokenValue: tokenValue, operator: propertyOperator }); | ||
return filterUsingOperator(item[propertyKey], { tokenValue, operator: propertyOperator }); | ||
}); | ||
@@ -118,5 +115,5 @@ } | ||
} | ||
var property = filteringPropertiesMap[token.propertyKey]; | ||
var operator = property.operators[token.operator]; | ||
var itemValue = (operator === null || operator === void 0 ? void 0 : operator.match) | ||
const property = filteringPropertiesMap[token.propertyKey]; | ||
const operator = property.operators[token.operator]; | ||
const itemValue = (operator === null || operator === void 0 ? void 0 : operator.match) | ||
? item[token.propertyKey] | ||
@@ -132,9 +129,8 @@ : fixupFalsyValues(item[token.propertyKey]); | ||
function defaultFilteringFunction(filteringPropertiesMap) { | ||
return function (item, query) { | ||
return (item, query) => { | ||
var _a; | ||
function evaluate(tokenOrGroup) { | ||
if ('operation' in tokenOrGroup) { | ||
var result = tokenOrGroup.operation === 'and' ? true : !tokenOrGroup.tokens.length; | ||
for (var _i = 0, _a = tokenOrGroup.tokens; _i < _a.length; _i++) { | ||
var group = _a[_i]; | ||
let result = tokenOrGroup.operation === 'and' ? true : !tokenOrGroup.tokens.length; | ||
for (const group of tokenOrGroup.tokens) { | ||
result = tokenOrGroup.operation === 'and' ? result && evaluate(group) : result || evaluate(group); | ||
@@ -154,12 +150,9 @@ } | ||
} | ||
export function createPropertyFilterPredicate(propertyFiltering, query) { | ||
if (query === void 0) { query = { tokens: [], operation: 'and' }; } | ||
export function createPropertyFilterPredicate(propertyFiltering, query = { tokens: [], operation: 'and' }) { | ||
if (!propertyFiltering) { | ||
return null; | ||
} | ||
var filteringPropertiesMap = propertyFiltering.filteringProperties.reduce(function (acc, _a) { | ||
var _b; | ||
var key = _a.key, operators = _a.operators, defaultOperator = _a.defaultOperator; | ||
var operatorMap = (_b = {}, _b[defaultOperator !== null && defaultOperator !== void 0 ? defaultOperator : '='] = { operator: defaultOperator !== null && defaultOperator !== void 0 ? defaultOperator : '=' }, _b); | ||
operators === null || operators === void 0 ? void 0 : operators.forEach(function (op) { | ||
const filteringPropertiesMap = propertyFiltering.filteringProperties.reduce((acc, { key, operators, defaultOperator }) => { | ||
const operatorMap = { [defaultOperator !== null && defaultOperator !== void 0 ? defaultOperator : '=']: { operator: defaultOperator !== null && defaultOperator !== void 0 ? defaultOperator : '=' } }; | ||
operators === null || operators === void 0 ? void 0 : operators.forEach(op => { | ||
if (typeof op === 'string') { | ||
@@ -175,6 +168,6 @@ operatorMap[op] = { operator: op }; | ||
}, {}); | ||
var filteringFunction = propertyFiltering.filteringFunction || defaultFilteringFunction(filteringPropertiesMap); | ||
return function (item) { return filteringFunction(item, query); }; | ||
const filteringFunction = propertyFiltering.filteringFunction || defaultFilteringFunction(filteringPropertiesMap); | ||
return item => filteringFunction(item, query); | ||
} | ||
export var fixupFalsyValues = function (value) { | ||
export const fixupFalsyValues = (value) => { | ||
if (typeof value === 'boolean') { | ||
@@ -181,0 +174,0 @@ return value + ''; |
@@ -5,3 +5,3 @@ function getSorter(sortingField) { | ||
} | ||
return function (row1, row2) { | ||
return (row1, row2) => { | ||
var _a, _b; | ||
@@ -11,4 +11,4 @@ // Use empty string as a default value, because it works well to compare with both strings and numbers: | ||
// sometimes it is NaN. | ||
var value1 = (_a = row1[sortingField]) !== null && _a !== void 0 ? _a : ''; | ||
var value2 = (_b = row2[sortingField]) !== null && _b !== void 0 ? _b : ''; | ||
const value1 = (_a = row1[sortingField]) !== null && _a !== void 0 ? _a : ''; | ||
const value2 = (_b = row2[sortingField]) !== null && _b !== void 0 ? _b : ''; | ||
if (typeof value1 === 'string' && typeof value2 === 'string') { | ||
@@ -27,5 +27,5 @@ return value1.localeCompare(value2); | ||
} | ||
var direction = state.isDescending ? -1 : 1; | ||
var comparator = (_a = state.sortingColumn.sortingComparator) !== null && _a !== void 0 ? _a : getSorter(state.sortingColumn.sortingField); | ||
return comparator ? function (a, b) { return comparator(a, b) * direction; } : null; | ||
const direction = state.isDescending ? -1 : 1; | ||
const comparator = (_a = state.sortingColumn.sortingComparator) !== null && _a !== void 0 ? _a : getSorter(state.sortingColumn.sortingField); | ||
return comparator ? (a, b) => comparator(a, b) * direction : null; | ||
} |
@@ -1,3 +0,2 @@ | ||
/// <reference types="react" /> | ||
import { UseCollectionOptions, CollectionState, CollectionRef, CollectionActions } from './interfaces'; | ||
export declare function useCollectionState<T>(options: UseCollectionOptions<T>, collectionRef: React.RefObject<CollectionRef>): readonly [CollectionState<T>, CollectionActions<T>]; |
@@ -7,3 +7,3 @@ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l; | ||
var _m = useReducer(collectionReducer, { | ||
const [state, dispatch] = useReducer(collectionReducer, { | ||
selectedItems: (_b = (_a = options.selection) === null || _a === void 0 ? void 0 : _a.defaultSelectedItems) !== null && _b !== void 0 ? _b : [], | ||
@@ -15,10 +15,10 @@ expandedItems: (_d = (_c = options.expandableRows) === null || _c === void 0 ? void 0 : _c.defaultExpandedItems) !== null && _d !== void 0 ? _d : [], | ||
propertyFilteringQuery: (_l = (_k = options.propertyFiltering) === null || _k === void 0 ? void 0 : _k.defaultQuery) !== null && _l !== void 0 ? _l : { tokens: [], operation: 'and' }, | ||
}), state = _m[0], dispatch = _m[1]; | ||
}); | ||
return [ | ||
state, | ||
createActions({ | ||
dispatch: dispatch, | ||
collectionRef: collectionRef, | ||
dispatch, | ||
collectionRef, | ||
}), | ||
]; | ||
} |
@@ -1,12 +0,1 @@ | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
@@ -19,31 +8,29 @@ // SPDX-License-Identifier: Apache-2.0 | ||
export function useCollection(allItems, options) { | ||
var collectionRef = useRef(null); | ||
var _a = useCollectionState(options, collectionRef), state = _a[0], actions = _a[1]; | ||
var _b = processItems(allItems, state, options), items = _b.items, allPageItems = _b.allPageItems, pagesCount = _b.pagesCount, filteredItemsCount = _b.filteredItemsCount, actualPageIndex = _b.actualPageIndex, itemsTree = _b.itemsTree; | ||
var expandedItemsSet = new Set(); | ||
const collectionRef = useRef(null); | ||
const [state, actions] = useCollectionState(options, collectionRef); | ||
const { items, allPageItems, pagesCount, filteredItemsCount, actualPageIndex, itemsTree } = processItems(allItems, state, options); | ||
const expandedItemsSet = new Set(); | ||
if (options.expandableRows) { | ||
for (var _i = 0, _c = state.expandedItems; _i < _c.length; _i++) { | ||
var item = _c[_i]; | ||
for (const item of state.expandedItems) { | ||
expandedItemsSet.add(options.expandableRows.getId(item)); | ||
} | ||
} | ||
var visibleItems = items; | ||
let visibleItems = items; | ||
if (options.expandableRows) { | ||
var flatItems_1 = new Array(); | ||
var getId_1 = options.expandableRows.getId; | ||
var traverse_1 = function (items) { | ||
for (var _i = 0, items_1 = items; _i < items_1.length; _i++) { | ||
var item = items_1[_i]; | ||
flatItems_1.push(item); | ||
if (expandedItemsSet.has(getId_1(item))) { | ||
traverse_1(itemsTree.getChildren(item)); | ||
const flatItems = new Array(); | ||
const getId = options.expandableRows.getId; | ||
const traverse = (items) => { | ||
for (const item of items) { | ||
flatItems.push(item); | ||
if (expandedItemsSet.has(getId(item))) { | ||
traverse(itemsTree.getChildren(item)); | ||
} | ||
} | ||
}; | ||
traverse_1(items); | ||
visibleItems = flatItems_1; | ||
traverse(items); | ||
visibleItems = flatItems; | ||
} | ||
// Removing selected items that are no longer present in items unless keepSelection=true. | ||
if (options.selection && !options.selection.keepSelection) { | ||
var newSelectedItems = processSelectedItems(visibleItems, state.selectedItems, options.selection.trackBy); | ||
const newSelectedItems = processSelectedItems(visibleItems, state.selectedItems, options.selection.trackBy); | ||
if (!itemsAreEqual(newSelectedItems, state.selectedItems, options.selection.trackBy)) { | ||
@@ -57,3 +44,3 @@ // This is a recommended pattern for how to handle the state, dependent on the incoming props | ||
if (options.expandableRows) { | ||
var newExpandedItems = visibleItems.filter(function (item) { return expandedItemsSet.has(options.expandableRows.getId(item)); }); | ||
const newExpandedItems = visibleItems.filter(item => expandedItemsSet.has(options.expandableRows.getId(item))); | ||
if (!itemsAreEqual(newExpandedItems, state.expandedItems, options.expandableRows.getId)) { | ||
@@ -63,9 +50,12 @@ actions.setExpandedItems(newExpandedItems); | ||
} | ||
return __assign({ items: items, allPageItems: allPageItems, filteredItemsCount: filteredItemsCount, actions: actions }, createSyncProps(options, state, actions, collectionRef, { | ||
actualPageIndex: actualPageIndex, | ||
pagesCount: pagesCount, | ||
allItems: allItems, | ||
allPageItems: allPageItems, | ||
itemsTree: itemsTree, | ||
return Object.assign({ items, | ||
allPageItems, | ||
filteredItemsCount, | ||
actions }, createSyncProps(options, state, actions, collectionRef, { | ||
actualPageIndex, | ||
pagesCount, | ||
allItems, | ||
allPageItems, | ||
itemsTree, | ||
})); | ||
} |
114
mjs/utils.js
@@ -1,24 +0,4 @@ | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { | ||
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { | ||
if (ar || !(i in from)) { | ||
if (!ar) ar = Array.prototype.slice.call(from, 0, i); | ||
ar[i] = from[i]; | ||
} | ||
} | ||
return to.concat(ar || Array.prototype.slice.call(from)); | ||
}; | ||
import { fixupFalsyValues } from './operations/property-filter.js'; | ||
export function collectionReducer(state, action) { | ||
var newState = __assign({}, state); | ||
const newState = Object.assign({}, state); | ||
switch (action.type) { | ||
@@ -49,34 +29,31 @@ case 'selection': | ||
} | ||
export function createActions(_a) { | ||
var dispatch = _a.dispatch, collectionRef = _a.collectionRef; | ||
export function createActions({ dispatch, collectionRef, }) { | ||
return { | ||
setFiltering: function (filteringText) { | ||
dispatch({ type: 'filtering', filteringText: filteringText }); | ||
setFiltering(filteringText) { | ||
dispatch({ type: 'filtering', filteringText }); | ||
collectionRef.current && collectionRef.current.scrollToTop(); | ||
}, | ||
setSorting: function (state) { | ||
setSorting(state) { | ||
dispatch({ type: 'sorting', sortingState: state }); | ||
collectionRef.current && collectionRef.current.scrollToTop(); | ||
}, | ||
setCurrentPage: function (pageIndex) { | ||
dispatch({ type: 'pagination', pageIndex: pageIndex }); | ||
setCurrentPage(pageIndex) { | ||
dispatch({ type: 'pagination', pageIndex }); | ||
collectionRef.current && collectionRef.current.scrollToTop(); | ||
}, | ||
setSelectedItems: function (selectedItems) { | ||
dispatch({ type: 'selection', selectedItems: selectedItems }); | ||
setSelectedItems(selectedItems) { | ||
dispatch({ type: 'selection', selectedItems }); | ||
}, | ||
setPropertyFiltering: function (query) { | ||
dispatch({ type: 'property-filtering', query: query }); | ||
setPropertyFiltering(query) { | ||
dispatch({ type: 'property-filtering', query }); | ||
collectionRef.current && collectionRef.current.scrollToTop(); | ||
}, | ||
setExpandedItems: function (expandedItems) { | ||
dispatch({ type: 'expansion', expandedItems: expandedItems }); | ||
setExpandedItems(expandedItems) { | ||
dispatch({ type: 'expansion', expandedItems }); | ||
}, | ||
}; | ||
} | ||
export function createSyncProps(options, _a, actions, collectionRef, _b) { | ||
var _c, _d, _e, _f, _g; | ||
var filteringText = _a.filteringText, sortingState = _a.sortingState, selectedItems = _a.selectedItems, expandedItems = _a.expandedItems, currentPageIndex = _a.currentPageIndex, propertyFilteringQuery = _a.propertyFilteringQuery; | ||
var pagesCount = _b.pagesCount, actualPageIndex = _b.actualPageIndex, allItems = _b.allItems, allPageItems = _b.allPageItems, itemsTree = _b.itemsTree; | ||
var empty = options.filtering | ||
export function createSyncProps(options, { filteringText, sortingState, selectedItems, expandedItems, currentPageIndex, propertyFilteringQuery, }, actions, collectionRef, { pagesCount, actualPageIndex, allItems, allPageItems, itemsTree, }) { | ||
var _a, _b, _c, _d, _e; | ||
let empty = options.filtering | ||
? allItems.length | ||
@@ -91,12 +68,12 @@ ? options.filtering.noMatch | ||
: empty; | ||
var filteringOptions = options.propertyFiltering | ||
? options.propertyFiltering.filteringProperties.reduce(function (acc, property) { | ||
Object.keys(allItems.reduce(function (acc, item) { | ||
const filteringOptions = options.propertyFiltering | ||
? options.propertyFiltering.filteringProperties.reduce((acc, property) => { | ||
Object.keys(allItems.reduce((acc, item) => { | ||
acc['' + fixupFalsyValues(item[property.key])] = true; | ||
return acc; | ||
}, {})).forEach(function (value) { | ||
}, {})).forEach(value => { | ||
if (value !== '') { | ||
acc.push({ | ||
propertyKey: property.key, | ||
value: value, | ||
value, | ||
}); | ||
@@ -109,6 +86,5 @@ } | ||
return { | ||
collectionProps: __assign(__assign(__assign(__assign(__assign({ empty: empty }, (options.sorting | ||
collectionProps: Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({ empty }, (options.sorting | ||
? { | ||
onSortingChange: function (_a) { | ||
var detail = _a.detail; | ||
onSortingChange: ({ detail }) => { | ||
actions.setSorting(detail); | ||
@@ -122,15 +98,13 @@ }, | ||
expandableRows: { | ||
getItemChildren: function (item) { | ||
getItemChildren(item) { | ||
return itemsTree.getChildren(item); | ||
}, | ||
isItemExpandable: function (item) { | ||
isItemExpandable(item) { | ||
return itemsTree.getChildren(item).length > 0; | ||
}, | ||
expandedItems: expandedItems, | ||
onExpandableItemToggle: function (_a) { | ||
var _b = _a.detail, item = _b.item, expanded = _b.expanded; | ||
var getId = options.expandableRows.getId; | ||
expandedItems, | ||
onExpandableItemToggle: ({ detail: { item, expanded } }) => { | ||
const getId = options.expandableRows.getId; | ||
if (expanded) { | ||
for (var _i = 0, expandedItems_1 = expandedItems; _i < expandedItems_1.length; _i++) { | ||
var stateItem = expandedItems_1[_i]; | ||
for (const stateItem of expandedItems) { | ||
if (getId(stateItem) === getId(item)) { | ||
@@ -140,6 +114,6 @@ return; | ||
} | ||
actions.setExpandedItems(__spreadArray(__spreadArray([], expandedItems, true), [item], false)); | ||
actions.setExpandedItems([...expandedItems, item]); | ||
} | ||
else { | ||
actions.setExpandedItems(expandedItems.filter(function (stateItem) { return getId(stateItem) !== getId(item); })); | ||
actions.setExpandedItems(expandedItems.filter(stateItem => getId(stateItem) !== getId(item))); | ||
} | ||
@@ -156,10 +130,9 @@ }, | ||
? { | ||
onSelectionChange: function (_a) { | ||
var selectedItems = _a.detail.selectedItems; | ||
onSelectionChange: ({ detail: { selectedItems } }) => { | ||
actions.setSelectedItems(selectedItems); | ||
}, | ||
selectedItems: selectedItems, | ||
trackBy: (_c = options.selection.trackBy) !== null && _c !== void 0 ? _c : (_d = options.expandableRows) === null || _d === void 0 ? void 0 : _d.getId, | ||
selectedItems, | ||
trackBy: (_a = options.selection.trackBy) !== null && _a !== void 0 ? _a : (_b = options.expandableRows) === null || _b === void 0 ? void 0 : _b.getId, | ||
} | ||
: {})), { ref: collectionRef, firstIndex: 1, totalItemsCount: allPageItems.length }), (((_e = options.pagination) === null || _e === void 0 ? void 0 : _e.pageSize) | ||
: {})), { ref: collectionRef, firstIndex: 1, totalItemsCount: allPageItems.length }), (((_c = options.pagination) === null || _c === void 0 ? void 0 : _c.pageSize) | ||
? { | ||
@@ -170,5 +143,4 @@ firstIndex: ((actualPageIndex !== null && actualPageIndex !== void 0 ? actualPageIndex : currentPageIndex) - 1) * options.pagination.pageSize + 1, | ||
filterProps: { | ||
filteringText: filteringText, | ||
onChange: function (_a) { | ||
var filteringText = _a.detail.filteringText; | ||
filteringText, | ||
onChange: ({ detail: { filteringText } }) => { | ||
actions.setFiltering(filteringText); | ||
@@ -179,9 +151,8 @@ }, | ||
query: propertyFilteringQuery, | ||
onChange: function (_a) { | ||
var query = _a.detail; | ||
onChange: ({ detail: query }) => { | ||
actions.setPropertyFiltering(query); | ||
}, | ||
filteringProperties: ((_f = options.propertyFiltering) === null || _f === void 0 ? void 0 : _f.filteringProperties) || [], | ||
filteringOptions: filteringOptions, | ||
freeTextFiltering: (_g = options.propertyFiltering) === null || _g === void 0 ? void 0 : _g.freeTextFiltering, | ||
filteringProperties: ((_d = options.propertyFiltering) === null || _d === void 0 ? void 0 : _d.filteringProperties) || [], | ||
filteringOptions, | ||
freeTextFiltering: (_e = options.propertyFiltering) === null || _e === void 0 ? void 0 : _e.freeTextFiltering, | ||
}, | ||
@@ -192,4 +163,3 @@ paginationProps: { | ||
pagesCount: pagesCount, | ||
onChange: function (_a) { | ||
var currentPageIndex = _a.detail.currentPageIndex; | ||
onChange: ({ detail: { currentPageIndex } }) => { | ||
actions.setCurrentPage(currentPageIndex); | ||
@@ -196,0 +166,0 @@ }, |
{ | ||
"name": "@cloudscape-design/collection-hooks", | ||
"version": "1.0.56", | ||
"version": "1.0.57", | ||
"license": "Apache-2.0", | ||
@@ -13,2 +13,3 @@ "repository": { | ||
"module": "./mjs/index.js", | ||
"sideEffects": false, | ||
"exports": { | ||
@@ -15,0 +16,0 @@ ".": { |
74
104775
2093