-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from smithki/development
v0.5.0
- Loading branch information
Showing
20 changed files
with
2,345 additions
and
1,820 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,17 @@ | ||
{ | ||
"extends": ["@ikscodes/eslint-config"], | ||
"parserOptions": { | ||
"project": "./tsconfig.json" | ||
}, | ||
"rules": { | ||
"import/extensions": 0, | ||
"@typescript-eslint/no-empty-function": 0 | ||
}, | ||
"settings": { | ||
"import/resolver": { | ||
"typescript": { | ||
"directory": ["tsconfig.json"] | ||
} | ||
} | ||
} | ||
} |
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 @@ | ||
module.exports = require('@ikscodes/prettier-config'); |
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,6 +1,6 @@ | ||
{ | ||
"name": "usable-react", | ||
"version": "0.4.1", | ||
"version": "0.5.0", | ||
"description": "Basic React hooks to get any project off the ground.", | ||
"author": "Ian K Smith <[email protected]>", | ||
"license": "MIT", | ||
|
@@ -19,37 +19,44 @@ | |
"scripts": { | ||
"dev": "npm-run-all -s clean:dist -p compile_watch", | ||
"build": "npm-run-all -s clean:dist -p compile", | ||
"lint": "tslint --fix -p .", | ||
"lint": "eslint --fix src/**/*.{ts,tsx}", | ||
"compile": "microbundle build src/index.ts --name UsableReact --target web --external react", | ||
"compile_watch": "microbundle watch src/index.ts --name UsableReact --target web --external react", | ||
"clean": "npm-run-all -s clean:*", | ||
"clean:dist": "rimraf dist", | ||
"clean:test-dist": "rimraf test-dist", | ||
"clean:cache": "rimraf .rts2_cache_*", | ||
"test": "npm-run-all -s clean:* test:*", | ||
"test:compile": "tsc -p ./tsconfig.test.json", | ||
"test:run": "alsatian ./test-dist/**/*.spec.js", | ||
"test_watch": "npm-run-all -s test:compile -p test_watch:*", | ||
"test_watch:compile": "tsc -w -p ./tsconfig.test.json", | ||
"test_watch:run": "chokidar \"./test-dist/**/*.spec.js\" -c \"npm run test:run\" --initial \"npm run test:run\"" | ||
"test": "echo \"No tests to run!\"" | ||
}, | ||
"devDependencies": { | ||
"@ikscodes/tslint-config": "^5.3.1", | ||
"@ikscodes/eslint-config": "^6.2.0", | ||
"@ikscodes/prettier-config": "^0.1.0", | ||
"@types/node": "^12.11.5", | ||
"@types/object-hash": "^1.3.1", | ||
"@types/react": "^16.9.9", | ||
"@typescript-eslint/eslint-plugin": "^2.15.0", | ||
"alsatian": "^2.4.0", | ||
"atob": "^2.1.2", | ||
"btoa": "^1.2.1", | ||
"microbundle": "^0.8.4", | ||
"eslint": "^6.7.2", | ||
"eslint-import-resolver-typescript": "^2.0.0", | ||
"eslint-plugin-import": "^2.18.2", | ||
"eslint-plugin-jsx-a11y": "^6.2.3", | ||
"eslint-plugin-prettier": "^3.1.2", | ||
"eslint-plugin-react": "^7.15.1", | ||
"eslint-plugin-react-hooks": "^1.7.0", | ||
"microbundle": "next", | ||
"mock-browser": "^0.92.14", | ||
"npm-run-all": "^4.1.5", | ||
"prettier": "^1.14.0", | ||
"prettier": "^1.19.1", | ||
"react": "^16.11.0", | ||
"rimraf": "^2.6.1", | ||
"tslint": "^5.11.0", | ||
"typescript": "^3.0.3" | ||
}, | ||
"peerDependencies": { | ||
"react": "^16.11.0" | ||
}, | ||
"dependencies": { | ||
"fuse.js": "^3.4.6", | ||
"object-hash": "^2.0.1" | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import Fuse from 'fuse.js'; | ||
import { useEffect, useMemo, useState } from 'react'; | ||
import { useCompare } from './useCompare'; | ||
import { useHash } from './useHash'; | ||
import { useTimer } from './useTimer'; | ||
|
||
interface UseFilterOptions<TData> { | ||
needle?: string; | ||
haystack?: TData[]; | ||
debounce?: number; | ||
/* Fuse.js options -> see https://fusejs.io/ */ | ||
searchOptions?: Fuse.FuseOptions<TData>; | ||
} | ||
|
||
/** | ||
* Peform a fuzzy search on a dataset (`haystack`), returning the results that | ||
* match most closely to the given `needle` | ||
*/ | ||
export function useFilter<TData = any>({ needle, haystack, debounce, searchOptions }: UseFilterOptions<TData> = {}) { | ||
const [results, setResults] = useState<TData[]>([]); | ||
const cooldownTimer = useTimer({ length: 0, tick: 100, autoStart: false }); | ||
|
||
const optionsWithDefaults: Fuse.FuseOptions<TData> = useMemo( | ||
() => ({ | ||
keys: [], | ||
...searchOptions, | ||
}), | ||
[searchOptions], | ||
); | ||
|
||
const haystackHash = useHash(haystack); | ||
const optionsHash = useHash(optionsWithDefaults); | ||
|
||
const didNeedleChange = useCompare(needle); | ||
const didHaystackChange = useCompare(haystackHash); | ||
const didOptionsChange = useCompare(optionsHash); | ||
|
||
// Execute a search if the needle/haystack changes. | ||
useEffect(() => { | ||
if (!cooldownTimer.isRunning && (didNeedleChange || didHaystackChange || didOptionsChange)) { | ||
cooldownTimer.reset(debounce); | ||
cooldownTimer.start(); | ||
|
||
const fuse = new Fuse<TData, Fuse.FuseOptions<TData>>(haystack || [], optionsWithDefaults); | ||
if (needle) setResults(fuse.search(needle) as TData[]); | ||
} | ||
}, [needle, haystackHash, debounce, optionsHash]); | ||
|
||
return results; | ||
} |
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,15 @@ | ||
import createHash from 'object-hash'; | ||
import { useEffect, useState } from 'react'; | ||
|
||
/** | ||
* Returns a hash of the given `value`. | ||
*/ | ||
export function useHash<T>(value: T) { | ||
const [hash, setHash] = useState<string>(createHash(typeof value === 'undefined' ? null : '')); | ||
useEffect(() => { | ||
if (value) { | ||
setHash(createHash(value)); | ||
} | ||
}, [value]); | ||
return hash; | ||
} |
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,12 @@ | ||
import { useCompare } from './useCompare'; | ||
import { useHash } from './useHash'; | ||
|
||
/** | ||
* Returns a `boolean` indicating whether the given `value` has changed since | ||
* the previous update based on a hash of its contents. | ||
*/ | ||
export function useHashCompare<T = any>(value: T) { | ||
const hash = useHash(value); | ||
const didHashChange = useCompare(hash); | ||
return didHashChange; | ||
} |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.