All files / src/components/zd-list list-group.ts

100% Statements 18/18
100% Branches 2/2
100% Functions 4/4
100% Lines 16/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53  93x 93x         93x                 6x         6x     6x 6x 6x 6x 6x 6x             2x                 3x 2x 1x   1x      
import { IListGroup, IListItem } from './interfaces';
import { ItemNotFoundError } from './item-not-found';
import { ListItem } from './list-item';
 
/**
 * Base class for ListGroup component.
 */
export class ListGroup extends ListItem implements IListGroup {
	/**
	 * List items structure.
	 */
	public items!: IListItem[];
 
	/**
	 * Content will be loaded only when the group opens.
	 */
	public lazyLoad = false;
 
	/**
	 * Group state.
	 */
	public opened = false;
 
	constructor(props: IListGroup) {
		super(props);
		this.items = this.getInitValue('items', props.items, this.items);
		this.lazyLoad = this.getInitValue('lazyLoad', props.lazyLoad, this.lazyLoad);
		this.opened = this.getInitValue('opened', props.opened, this.opened);
		if (this.appendIcon === undefined) this.appendIcon = 'expand';
		this.createAccessors();
	}
 
	/**
	 * Toggle group state.
	 */
	public toggle() {
		this.opened = !this.opened;
	}
 
	/**
	 * Get item by name
	 * @param name name property of the item
	 * @throws ItemNotFoundError
	 */
	public getItem(name: string) {
		const item = this.items.find((listItem: IListItem) => listItem.name === name);
		if (item) {
			return item;
		}
		throw new ItemNotFoundError(name, this.name);
	}
}