|
|
|
@ -175,7 +175,7 @@ export const useForceUpdateWithCallback = callback => {
|
|
|
|
|
}).current;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const usePrevious = value => {
|
|
|
|
|
export const usePreviousProps = value => {
|
|
|
|
|
/*
|
|
|
|
|
* The ref object is a generic container whose current property is mutable
|
|
|
|
|
* and can hold any value, similar to an instance property on a class.
|
|
|
|
@ -225,15 +225,23 @@ export const useToggle = (initialState = false) => {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param {*} param
|
|
|
|
|
* @returns
|
|
|
|
|
* Filters data based on a query, with the option to filter by keys and
|
|
|
|
|
* includes some other options.
|
|
|
|
|
* @param {object} filter Filter parameters
|
|
|
|
|
* @param {Array<string>} filter.data Data
|
|
|
|
|
* @param {Array<string>|string} [filter.keys] Keys in the data to process
|
|
|
|
|
* @param {object} [filter.options] Extra filter options
|
|
|
|
|
* @param {boolean} [options.fuzzy] Whether or not the search should be fuzzy (return results that do not match exactly)
|
|
|
|
|
* @param {boolean} [options.caseSensitive] Whether or not the search should be case sensitive
|
|
|
|
|
* @param {boolean} [options.sortByRelevance] Whether or not the search should be sorted by relevance (only applicable if fuzzy option is true)
|
|
|
|
|
* @param {boolean} [options.exactMatch] Whether or not the search should return exact matches only
|
|
|
|
|
* @example
|
|
|
|
|
* ```
|
|
|
|
|
* const App = () => {
|
|
|
|
|
* const { inputText, setInputText, filtered } = useFilter({
|
|
|
|
|
* const [ inputText, setInputText, filtered ] = useFilter({
|
|
|
|
|
* keys: [ 'user.name', 'subject', 'dest.name' ],
|
|
|
|
|
* data: sampleData,
|
|
|
|
|
* options:
|
|
|
|
|
* });
|
|
|
|
|
*
|
|
|
|
|
* return (
|
|
|
|
@ -249,44 +257,16 @@ export const useToggle = (initialState = false) => {
|
|
|
|
|
* };
|
|
|
|
|
* ```
|
|
|
|
|
*/
|
|
|
|
|
export const useFilter = ({ keys, data }) => {
|
|
|
|
|
export const useFilter = ({ data, keys, options }) => {
|
|
|
|
|
const [ query, setQuery ] = useState('');
|
|
|
|
|
const filtered = useMemo(
|
|
|
|
|
() => data.filter(createFilter(query, keys)),
|
|
|
|
|
[ data, query ]
|
|
|
|
|
);
|
|
|
|
|
const filtered = useMemo(() => data.filter(createFilter(query, keys)), [ data, query, options ]);
|
|
|
|
|
return [ query, setQuery, filtered ];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const useSticky = ({ defaultSticky = false }) => {
|
|
|
|
|
const [ sticky, setSticky ] = useState(defaultSticky);
|
|
|
|
|
const tableRef = useRef(null);
|
|
|
|
|
const toggleSticky = useCallback(
|
|
|
|
|
({ top, bottom }) => {
|
|
|
|
|
if (top <= 0 && bottom > 2 * 68) {
|
|
|
|
|
!sticky && setSticky(true);
|
|
|
|
|
} else {
|
|
|
|
|
sticky && setSticky(false);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
[ sticky ]
|
|
|
|
|
);
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const handleScroll = () => {
|
|
|
|
|
toggleSticky(tableRef.current.getBoundingClientRect());
|
|
|
|
|
};
|
|
|
|
|
window.addEventListener('scroll', handleScroll);
|
|
|
|
|
return () => {
|
|
|
|
|
window.removeEventListener('scroll', handleScroll);
|
|
|
|
|
};
|
|
|
|
|
}, [ toggleSticky ]);
|
|
|
|
|
return { sticky, setSticky };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* Sourced from @see {@link https://codesandbox.io/s/animate-height-framer-motion-yn59l?file=/src/use-callback-ref.js:48-209}
|
|
|
|
|
* @returns
|
|
|
|
|
* @returns
|
|
|
|
|
*/
|
|
|
|
|
export const useCallbackRef = () => {
|
|
|
|
|
const [ ref, setRef ] = useState(null);
|
|
|
|
@ -297,16 +277,16 @@ export const useCallbackRef = () => {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* Sourced from @see {@link https://codesandbox.io/s/animate-height-framer-motion-yn59l?file=/src/use-measure.js}
|
|
|
|
|
* @returns
|
|
|
|
|
* @returns
|
|
|
|
|
*/
|
|
|
|
|
export const useMeasure = ref => {
|
|
|
|
|
export const useMeasure = ref => {
|
|
|
|
|
const [ element, attachRef ] = useCallbackRef();
|
|
|
|
|
const [ bounds, setBounds ] = useState({});
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
function onResize([ entry ]) {
|
|
|
|
|
function onResize ([ entry ]) {
|
|
|
|
|
setBounds({
|
|
|
|
|
height: entry.contentRect.height
|
|
|
|
|
});
|
|
|
|
@ -314,7 +294,7 @@ export const useCallbackRef = () => {
|
|
|
|
|
|
|
|
|
|
const observer = new ResizeObserver(onResize);
|
|
|
|
|
|
|
|
|
|
if (element && element.current) {
|
|
|
|
|
if (element?.current) {
|
|
|
|
|
observer.observe(element.current);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|