diff --git a/src/component/container/pattern_list.tsx b/src/component/container/pattern_list.tsx index c4542e77d..0a6637545 100644 --- a/src/component/container/pattern_list.tsx +++ b/src/component/container/pattern_list.tsx @@ -1,5 +1,7 @@ +import Input from '../../lsg/patterns/input/'; import { PatternFolder } from '../../store/pattern/folder'; import { List, ListPropsListItem } from '../presentation/list'; +import { action } from 'mobx'; import { observer } from 'mobx-react'; import { Pattern } from '../../store/pattern'; import * as React from 'react'; @@ -11,15 +13,27 @@ export interface PatternListProps { @observer export class PatternList extends React.Component { + public items: ListPropsListItem[] = []; public constructor(props: PatternListProps) { super(props); + + this.handleSearchInputChange = this.handleSearchInputChange.bind(this); } public render(): JSX.Element { - const items: ListPropsListItem[] = this.createItemsFromFolder( - this.props.store.getPatternRoot() as PatternFolder + if (this.props.store.getPatternSearchTerm() === '') { + this.items = this.createItemsFromFolder( + this.props.store.getPatternRoot() as PatternFolder + ); + } else { + this.items = this.props.store.searchPatterns(this.props.store.getPatternSearchTerm()).map(pattern => ({ value: pattern.getName() })); + } + return ( +
+ + +
); - return ; } public createItemsFromFolder(folder: PatternFolder): ListPropsListItem[] { @@ -39,4 +53,8 @@ export class PatternList extends React.Component { return result; } + @action + protected handleSearchInputChange(evt: React.ChangeEvent): void { + this.props.store.setPatternSearchTerm(evt.target.value); + } } diff --git a/src/store/index.ts b/src/store/index.ts index 545f6009e..36614d607 100644 --- a/src/store/index.ts +++ b/src/store/index.ts @@ -11,7 +11,7 @@ import { Project } from './project'; export class Store { @MobX.observable private currentPage?: Page; - @MobX.observable private patternSearchTerm: string; + @MobX.observable private patternSearchTerm: string = ''; @MobX.observable private projects: Map = new Map(); @MobX.observable private patternRoot: PatternFolder; @MobX.observable private selectedElement?: PageElement; @@ -117,7 +117,7 @@ export class Store { } public searchPatterns(term: string): Pattern[] { - return this.patternRoot.searchPatterns(term); + return this.patternRoot ? this.patternRoot.searchPatterns(term) : []; } public setPageFromJsonInternal(json: JsonObject, projectId: string, pageId: string): void { diff --git a/src/store/pattern/index.ts b/src/store/pattern/index.ts index 22f751958..af27b3ca0 100644 --- a/src/store/pattern/index.ts +++ b/src/store/pattern/index.ts @@ -61,6 +61,9 @@ export class Pattern { } public matches(term: string): boolean { + if (!term || !this.name) { + return false; + } return this.name.toLowerCase().indexOf(term.toLowerCase()) >= 0; }