Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Commit

Permalink
feat: integrate pattern list search
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexpeschel committed Dec 12, 2017
1 parent bb3b30e commit a710ec4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
24 changes: 21 additions & 3 deletions src/component/container/pattern_list.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -11,15 +13,27 @@ export interface PatternListProps {

@observer
export class PatternList extends React.Component<PatternListProps> {
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 (
<div>
<Input handleChange={this.handleSearchInputChange}/>
<List headline="Patterns" items={this.items} />
</div>
);
return <List headline="Patterns" items={items} />;
}

public createItemsFromFolder(folder: PatternFolder): ListPropsListItem[] {
Expand All @@ -39,4 +53,8 @@ export class PatternList extends React.Component<PatternListProps> {

return result;
}
@action
protected handleSearchInputChange(evt: React.ChangeEvent<HTMLInputElement>): void {
this.props.store.setPatternSearchTerm(evt.target.value);
}
}
4 changes: 2 additions & 2 deletions src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, Project> = new Map();
@MobX.observable private patternRoot: PatternFolder;
@MobX.observable private selectedElement?: PageElement;
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions src/store/pattern/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit a710ec4

Please sign in to comment.