-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
94 additions
and
50 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { NodeObject } from "../nodes/types"; | ||
|
||
export function matchesStringProperties<T>(node: NodeObject<T>, term: string) { | ||
const haystack = Array.from(Object.values(node.sourceData as any)) | ||
.filter((value) => typeof value === "string") | ||
.join(" ") | ||
.toLocaleLowerCase(); | ||
|
||
return haystack.includes(term.toLocaleLowerCase()); | ||
} |
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,43 @@ | ||
import { NodeObject } from "../nodes/types"; | ||
import { matchesStringProperties } from "./match"; | ||
import { FilterOptions } from "./use-filter"; | ||
|
||
type BoolMap = Record<string, boolean>; | ||
|
||
export class TreeFilter<T> { | ||
term: string; | ||
isMatch: (node: NodeObject<T>, term: string) => boolean; | ||
|
||
constructor(opts: FilterOptions<T>) { | ||
this.term = (opts.term || "").trim(); | ||
this.isMatch = opts.isMatch || matchesStringProperties; | ||
} | ||
|
||
get isPresent() { | ||
return this.term.length > 0; | ||
} | ||
|
||
getVisibility(nodes: NodeObject<T>[], value: BoolMap = {}) { | ||
for (const node of nodes) { | ||
if (this.isMatch(node, this.term)) { | ||
for (const id of [node.id, ...this.ancestorIds(node)]) { | ||
value[id] = true; | ||
} | ||
} else { | ||
value[node.id] = false; | ||
} | ||
if (node.children) this.getVisibility(node.children, value); | ||
} | ||
return value; | ||
} | ||
|
||
ancestorIds(node: NodeObject<T>) { | ||
const ids = []; | ||
let parent = node.parent; | ||
while (parent) { | ||
ids.push(parent.id); | ||
parent = parent.parent; | ||
} | ||
return ids; | ||
} | ||
} |
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,35 @@ | ||
import { useState } from "react"; | ||
import { OpensOnChangeEvent, OpensState } from "../opens/types"; | ||
import { TreeFilter } from "./tree-filter"; | ||
import { NodeObject } from "../nodes/types"; | ||
|
||
export type FilterOptions<T> = { | ||
term?: string; | ||
isMatch?: (node: NodeObject<T>, term: string) => boolean; | ||
openByDefault?: (isFiltered: boolean) => boolean; | ||
}; | ||
|
||
export function useFilter<T>( | ||
nodes: NodeObject<T>[], | ||
options: FilterOptions<T> = {}, | ||
) { | ||
const filter = new TreeFilter(options); | ||
const [opens, setOpens] = useState<OpensState>({}); | ||
const [filteredOpens, setFilteredOpens] = useState<OpensState>({}); | ||
|
||
return { | ||
visible: { | ||
value: filter.isPresent ? filter.getVisibility(nodes) : {}, | ||
onChange: () => {}, | ||
}, | ||
opens: { | ||
value: filter.isPresent ? filteredOpens : opens, | ||
onChange: (e: OpensOnChangeEvent) => { | ||
filter.isPresent ? setFilteredOpens(e.value) : setOpens(e.value); | ||
}, | ||
}, | ||
openByDefault: options.openByDefault | ||
? options.openByDefault(filter.isPresent) | ||
: true, | ||
}; | ||
} |
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 was deleted.
Oops, something went wrong.
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