-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added dbxListItemIsSelectedModifier
- added IsSelectedDecisionFunctionFactory
- Loading branch information
Showing
11 changed files
with
151 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
packages/dbx-web/src/lib/layout/list/list.view.value.modifier.selection.directive.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { Directive, Input, OnDestroy } from '@angular/core'; | ||
import { Maybe, ArrayOrValue, Modifier, DecisionFunction } from '@dereekb/util'; | ||
import { BehaviorSubject, Observable, map } from 'rxjs'; | ||
import { DbxValueListItem, DbxValueListItemDecisionFunction } from './list.view.value'; | ||
import { AbstractDbxValueListItemModifierDirective } from './list.view.value.modifier.directive'; | ||
|
||
export const DBX_LIST_ITEM_IS_SELECTED_ITEM_MODIFIER_KEY = 'is_selected_item_modifier'; | ||
|
||
export const DEFAULT_DBX_LIST_ITEM_IS_SELECTED_FUNCTION: DbxValueListItemDecisionFunction<unknown> = <T>(item: DbxValueListItem<T>) => { | ||
return item.selected ?? false; | ||
}; | ||
|
||
@Directive({ | ||
selector: '[dbxListItemIsSelectedModifier]' | ||
}) | ||
export class DbxListItemIsSelectedModifierDirective<T> extends AbstractDbxValueListItemModifierDirective<T> implements OnDestroy { | ||
private _listItemIsSelected = new BehaviorSubject<DbxValueListItemDecisionFunction<T>>(DEFAULT_DBX_LIST_ITEM_IS_SELECTED_FUNCTION); | ||
|
||
readonly modifiers$: Observable<Maybe<ArrayOrValue<Modifier<DbxValueListItem<T>>>>> = this._listItemIsSelected.pipe( | ||
map((listItemIsSelected) => { | ||
const modifiers: Modifier<DbxValueListItem<T>> = { | ||
key: DBX_LIST_ITEM_IS_SELECTED_ITEM_MODIFIER_KEY, | ||
modify: (x: DbxValueListItem<T>) => { | ||
x.selected = listItemIsSelected(x); | ||
} | ||
}; | ||
|
||
return modifiers; | ||
}) | ||
); | ||
|
||
override ngOnDestroy(): void { | ||
super.ngOnDestroy(); | ||
this._listItemIsSelected.complete(); | ||
} | ||
|
||
@Input('dbxListItemIsSelectedModifier') | ||
set listItemIsSelected(listItemIsSelected: Maybe<DbxValueListItemDecisionFunction<T>>) { | ||
this._listItemIsSelected.next(listItemIsSelected ?? DEFAULT_DBX_LIST_ITEM_IS_SELECTED_FUNCTION); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * from './set.allowed'; | ||
export * from './set.hashset'; | ||
export * from './set.maybe'; | ||
export * from './set.selection'; | ||
export * from './set'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { readIndexNumber } from '../value/indexed'; | ||
import { isSelectedDecisionFunctionFactory } from './set.selection'; | ||
|
||
describe('isSelectedDecisionFunctionFactory()', () => { | ||
describe('factory', () => { | ||
const factory = isSelectedDecisionFunctionFactory({ | ||
readKey: readIndexNumber | ||
}); | ||
|
||
describe('function', () => { | ||
it('should return true for the selected numbers.', () => { | ||
const selected = [0, 1, 2]; | ||
const fn = factory(selected); | ||
|
||
selected.forEach((i) => expect(fn({ i }))); | ||
expect(fn({ i: -1 })).toBe(false); | ||
expect(fn({ i: 4 })).toBe(false); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { iterableToArray } from '../iterable'; | ||
import { PrimativeKey, ReadKeyFunction } from '../key'; | ||
import { DecisionFunction } from '../value'; | ||
|
||
/** | ||
* Factory that creates a DecisionFunction using the input "selected" values. That function returns true if the value's key is considered to be included in the selected values. | ||
*/ | ||
export type IsSelectedDecisionFunctionFactory<T, K extends PrimativeKey = PrimativeKey> = (selectedValues: Iterable<K>) => DecisionFunction<T>; | ||
|
||
export interface IsSelectedDecisionFunctionConfig<T, K extends PrimativeKey = PrimativeKey> { | ||
/** | ||
* Reads the key from the input value. | ||
*/ | ||
readKey: ReadKeyFunction<T, K>; | ||
/** | ||
* Default value to use if readKey returns no key. | ||
* | ||
* Defaults to false. | ||
*/ | ||
defaultIfKeyNull?: boolean; | ||
} | ||
|
||
/** | ||
* Creates a IsSelectedDecisionFunctionFactory | ||
* | ||
* @param config | ||
* @returns | ||
*/ | ||
export function isSelectedDecisionFunctionFactory<T, K extends PrimativeKey = PrimativeKey>(config: IsSelectedDecisionFunctionConfig<T, K>): IsSelectedDecisionFunctionFactory<T, K> { | ||
const { readKey, defaultIfKeyNull = false } = config; | ||
|
||
return (selectedValues: Iterable<K>) => { | ||
const selectedValuesSet = new Set(selectedValues); | ||
|
||
return (value: T) => { | ||
const key = readKey(value); | ||
return key != null ? selectedValuesSet.has(key) : defaultIfKeyNull; | ||
}; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters