* Улучшен тип SubscribeFunction

* Исправлены ошибки FunctionalValue
This commit is contained in:
Александр Сироткин 2022-07-31 01:24:53 +05:00
parent c9a17b8de9
commit de729dc2ff
5 changed files with 9 additions and 8 deletions

View File

@ -1,9 +1,10 @@
import { createContext, memo, ReactNode, useCallback, useContext, useEffect, useState } from 'react'
import * as d3 from 'd3'
import '@styles/d3.less'
import { ChartOffset } from './types'
import '@styles/d3.less'
export type D3MouseState = {
/** Позиция мыши по оси X */
x: number
@ -13,7 +14,7 @@ export type D3MouseState = {
visible: boolean
}
type SubscribeFunction = (name: keyof SVGElementEventMap, handler: EventListenerOrEventListenerObject) => null | (() => boolean)
type SubscribeFunction = <K extends keyof SVGElementEventMap>(name: K, handler: (this: SVGRectElement, ev: SVGElementEventMap[K]) => any) => null | (() => boolean)
export type D3MouseZoneContext = {
/** Состояние мыши */

View File

@ -92,8 +92,8 @@ const _D3HorizontalCursor = <DataType,>({
const getXLine = useMemo(() => zone ? (() => zone().select('.tooltip-x-line')) : null, [zone])
useEffect(() => {
const onMiddleClick = (e: Event) => {
if ((e as MouseEvent).button === 1)
const onMiddleClick = (e: MouseEvent) => {
if (e.button === 1)
setFixed((prev) => !prev)
}

View File

@ -98,8 +98,8 @@ function _D3Tooltip<DataType extends Record<string, unknown>>({
const tooltipRef = useRef<HTMLDivElement>(null)
useEffect(() => {
const onMiddleClick = (e: Event) => {
if ((e as MouseEvent).button === 1 && visible)
const onMiddleClick = (e: MouseEvent) => {
if (e.button === 1 && visible)
setFixed((prev) => !prev)
}

View File

@ -1,7 +1,7 @@
import { useCallback, useEffect, useState } from 'react'
import { FunctionalValue, useFunctionalValue } from './functionalValue'
export const useCachedMethod = <P extends [], R>(method: (...props: P) => R, expires: FunctionalValue<number> = 60, initialValue?: R | (() => R), storeValue: boolean = false) => {
export const useCachedMethod = <P extends [], R>(method: (...props: P) => R, expires: FunctionalValue<() => number> = 60, initialValue?: R | (() => R), storeValue: boolean = false) => {
const [value, setValue] = useState<R | undefined>(initialValue)
const [lastUpdate, setLastUpdate] = useState<{ date: number, props: P }>({ date: 0, props: [] as P })
const exp = useFunctionalValue(expires)

View File

@ -1,7 +1,7 @@
import { useMemo } from 'react'
import { FunctionalValue, useFunctionalValue } from './functionalValue'
export const usePartialProps = <T,>(prop: Partial<T> | null | undefined, defaultValue: FunctionalValue<T>): T => {
export const usePartialProps = <T,>(prop: Partial<T> | null | undefined, defaultValue: FunctionalValue<() => T>): T => {
const def = useFunctionalValue(defaultValue)
const result: T = useMemo(() => {