chore: format all frontend files (#430)
This commit is contained in:
@@ -13,7 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
|
||||
export { default as useHover } from './use-hover';
|
||||
export { default as usePersistCallback } from './use-persist-callback';
|
||||
export { default as useUpdateEffect } from './use-update-effect';
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
|
||||
import { renderHook, act } from '@testing-library/react-hooks';
|
||||
import useBoolean from '../index';
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
|
||||
import { useState, useMemo } from 'react';
|
||||
|
||||
export interface ReturnValue {
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
|
||||
import { act, renderHook } from '@testing-library/react-hooks';
|
||||
|
||||
import useHover from '../index';
|
||||
|
||||
@@ -13,14 +13,14 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import {type DependencyList} from 'react';
|
||||
|
||||
import { type DependencyList } from 'react';
|
||||
import type React from 'react';
|
||||
import { useState, useCallback, useRef, useLayoutEffect } from 'react';
|
||||
|
||||
interface Options {
|
||||
onEnter?: () => void
|
||||
onLeave?: () => void
|
||||
onEnter?: () => void;
|
||||
onLeave?: () => void;
|
||||
}
|
||||
|
||||
const useHover = <T extends HTMLElement = any>(
|
||||
@@ -28,25 +28,31 @@ const useHover = <T extends HTMLElement = any>(
|
||||
options: Options = {},
|
||||
deps: DependencyList = [],
|
||||
): [React.MutableRefObject<T>, boolean] => {
|
||||
const { onEnter, onLeave } = options
|
||||
const { onEnter, onLeave } = options;
|
||||
const ref = useRef<T>();
|
||||
const [isHovered, setIsHovered] = useState(false);
|
||||
|
||||
const handleMouseEnter = useCallback(() => {
|
||||
if (onEnter) {onEnter()}
|
||||
setIsHovered(true)
|
||||
}, [typeof onEnter === 'function']);
|
||||
if (onEnter) {
|
||||
onEnter();
|
||||
}
|
||||
setIsHovered(true);
|
||||
}, [typeof onEnter === 'function']);
|
||||
const handleMouseLeave = useCallback(() => {
|
||||
if (onLeave) {onLeave()}
|
||||
setIsHovered(false)
|
||||
}, [typeof onLeave === 'function']);
|
||||
if (onLeave) {
|
||||
onLeave();
|
||||
}
|
||||
setIsHovered(false);
|
||||
}, [typeof onLeave === 'function']);
|
||||
|
||||
useLayoutEffect(() => {
|
||||
let target = ref.current
|
||||
let target = ref.current;
|
||||
if (el) {
|
||||
target = typeof el === 'function' ? el() : el;
|
||||
}
|
||||
if (!target) {return}
|
||||
if (!target) {
|
||||
return;
|
||||
}
|
||||
target.addEventListener('mouseenter', handleMouseEnter);
|
||||
target.addEventListener('mouseleave', handleMouseLeave);
|
||||
return () => {
|
||||
|
||||
@@ -13,8 +13,12 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { act, renderHook, type RenderHookResult } from '@testing-library/react-hooks';
|
||||
|
||||
import {
|
||||
act,
|
||||
renderHook,
|
||||
type RenderHookResult,
|
||||
} from '@testing-library/react-hooks';
|
||||
import { useState } from 'react';
|
||||
import usePersistCallback from '..';
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
|
||||
import { useRef, useCallback, useMemo } from 'react';
|
||||
|
||||
function usePersistCallback<T extends (...args: any[]) => any>(fn?: T) {
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
|
||||
/* eslint-disable @typescript-eslint/no-unused-vars */
|
||||
import { act, renderHook } from '@testing-library/react-hooks';
|
||||
import useStateRealtime from '../index';
|
||||
|
||||
@@ -13,24 +13,43 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { useState, useRef, type Dispatch, type SetStateAction, useCallback } from 'react';
|
||||
|
||||
import {
|
||||
useState,
|
||||
useRef,
|
||||
type Dispatch,
|
||||
type SetStateAction,
|
||||
useCallback,
|
||||
} from 'react';
|
||||
|
||||
const isFunction = (val: any): val is Function => typeof val === 'function';
|
||||
|
||||
// Get a new state value, compatible with passing values and functions
|
||||
function getStateVal<T>(preState: T, initVal?: SetStateAction<T>): T | undefined {
|
||||
function getStateVal<T>(
|
||||
preState: T,
|
||||
initVal?: SetStateAction<T>,
|
||||
): T | undefined {
|
||||
if (isFunction(initVal)) {
|
||||
return initVal(preState);
|
||||
}
|
||||
return initVal;
|
||||
}
|
||||
|
||||
function useStateRealtime<T>(initialState: T | (() => T)): [T, Dispatch<SetStateAction<T>>, () => T]
|
||||
function useStateRealtime<T = undefined>(): [T | undefined, Dispatch<SetStateAction<T | undefined>>, () => T | undefined]
|
||||
function useStateRealtime<T>(
|
||||
initialState: T | (() => T),
|
||||
): [T, Dispatch<SetStateAction<T>>, () => T];
|
||||
function useStateRealtime<T = undefined>(): [
|
||||
T | undefined,
|
||||
Dispatch<SetStateAction<T | undefined>>,
|
||||
() => T | undefined,
|
||||
];
|
||||
function useStateRealtime<T>(
|
||||
initVal?: T | (() => T),
|
||||
): [T | undefined, Dispatch<SetStateAction<T | undefined>>, () => T | undefined] {
|
||||
): [
|
||||
T | undefined,
|
||||
Dispatch<SetStateAction<T | undefined>>,
|
||||
() => T | undefined,
|
||||
] {
|
||||
const initState = getStateVal(undefined, initVal);
|
||||
const [val, setVal] = useState(initState);
|
||||
const valRef = useRef(initState);
|
||||
@@ -38,8 +57,8 @@ function useStateRealtime<T>(
|
||||
const newState = getStateVal(valRef.current, newVal);
|
||||
valRef.current = newState;
|
||||
setVal(newState);
|
||||
}, [])
|
||||
const getRealState = useCallback(() => valRef.current, [])
|
||||
}, []);
|
||||
const getRealState = useCallback(() => valRef.current, []);
|
||||
return [val, setState, getRealState];
|
||||
}
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
|
||||
import { act, renderHook } from '@testing-library/react-hooks';
|
||||
import useToggle from '../index';
|
||||
|
||||
|
||||
@@ -13,50 +13,52 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { useState, useMemo } from 'react'
|
||||
|
||||
type State = any
|
||||
import { useState, useMemo } from 'react';
|
||||
|
||||
type State = any;
|
||||
|
||||
export interface ReturnValue<T = State> {
|
||||
state: T;
|
||||
toggle: (value?: T) => void;
|
||||
}
|
||||
|
||||
function useToggle<T = boolean>(): ReturnValue<T>
|
||||
function useToggle<T = boolean>(): ReturnValue<T>;
|
||||
|
||||
function useToggle<T = State>(defaultValue: T): ReturnValue<T>
|
||||
function useToggle<T = State>(defaultValue: T): ReturnValue<T>;
|
||||
|
||||
function useToggle<T = State, U = State>(
|
||||
defaultValue: T,
|
||||
reverseValue: U,
|
||||
): ReturnValue<T | U>
|
||||
): ReturnValue<T | U>;
|
||||
|
||||
function useToggle<D extends State = State, R extends State = State>(
|
||||
defaultValue: D = false as D,
|
||||
reverseValue?: R,
|
||||
) {
|
||||
const [state, setState] = useState<D | R>(defaultValue)
|
||||
const [state, setState] = useState<D | R>(defaultValue);
|
||||
|
||||
const actions = useMemo(() => {
|
||||
const reverseValueOrigin = (reverseValue === undefined ? !defaultValue : reverseValue) as D | R
|
||||
const reverseValueOrigin = (
|
||||
reverseValue === undefined ? !defaultValue : reverseValue
|
||||
) as D | R;
|
||||
|
||||
const toggle = (value?: D | R) => {
|
||||
if (value !== undefined) {
|
||||
setState(value)
|
||||
return
|
||||
setState(value);
|
||||
return;
|
||||
}
|
||||
setState((s) => (s === defaultValue ? reverseValueOrigin : defaultValue))
|
||||
}
|
||||
setState(s => (s === defaultValue ? reverseValueOrigin : defaultValue));
|
||||
};
|
||||
return {
|
||||
toggle,
|
||||
}
|
||||
}, [defaultValue, reverseValue])
|
||||
};
|
||||
}, [defaultValue, reverseValue]);
|
||||
|
||||
return {
|
||||
state,
|
||||
...actions,
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export default useToggle
|
||||
export default useToggle;
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
|
||||
import { renderHook } from '@testing-library/react-hooks';
|
||||
import useUpdateEffect from '../index';
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
|
||||
import { useEffect, useRef } from 'react';
|
||||
|
||||
const useUpdateEffect: typeof useEffect = (effect, deps) => {
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
|
||||
import {
|
||||
useEffect,
|
||||
useState,
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
|
||||
import { defineConfig } from '@coze-arch/vitest-config';
|
||||
|
||||
export default defineConfig({
|
||||
|
||||
Reference in New Issue
Block a user