1.2.0 • Published 5 months ago

@marianmeres/selection-store v1.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

@marianmeres/selection-store

Simple store utility for marking objects in collection as selected.

Install

$ npm i @marianmeres/selection-store

Example

// factory
const store = createSelectionStore<T>(
    // initial collection of arbitrary objects
    items: T[] = [],  // e.g. [{ id: 'a' }, { id: 'b' }, { id: 'c' }],

    // initial set of selected indexes
    selected: number[] = [],

    // allow multiple selection flag
    multiple: boolean = false
);

//
store.subscribe((v) => {
    // full collection of objects
    // v.items: T[];

    // currently selected indexes
    // v.selected: number[];

    // current selection of objects (mapped indexes)
    // v.selection: T[];
});

// `resetSelected` flag empties the selection before applying the current one
// (makes sense only for multiple, since non multiple are always reset)
store.select(indexOrItem, resetSelected = true);
store.select([indexOrItem, indexOrItem2, ...], resetSelected = true);

// "unselect"
store.select([], resetSelected = true);
store.unselect(); // will unselect all
store.unselect(indexOrItem);
store.unselect([indexOrItem, indexOrItem2, ...]);

// helper to find index
store.findIndexBy(propName, propValue);
// for cases like:
store.select(store.findIndexBy('id', 'a'));

// resets selection and internal collection of objects
store.reset(items = []);
1.2.0

5 months ago

1.1.0

5 months ago

1.0.1

1 year ago