* Улучшен тип 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 { createContext, memo, ReactNode, useCallback, useContext, useEffect, useState } from 'react'
import * as d3 from 'd3' import * as d3 from 'd3'
import '@styles/d3.less'
import { ChartOffset } from './types' import { ChartOffset } from './types'
import '@styles/d3.less'
export type D3MouseState = { export type D3MouseState = {
/** Позиция мыши по оси X */ /** Позиция мыши по оси X */
x: number x: number
@ -13,7 +14,7 @@ export type D3MouseState = {
visible: boolean 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 = { export type D3MouseZoneContext = {
/** Состояние мыши */ /** Состояние мыши */

View File

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

View File

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

View File

@ -1,7 +1,7 @@
import { useCallback, useEffect, useState } from 'react' import { useCallback, useEffect, useState } from 'react'
import { FunctionalValue, useFunctionalValue } from './functionalValue' 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 [value, setValue] = useState<R | undefined>(initialValue)
const [lastUpdate, setLastUpdate] = useState<{ date: number, props: P }>({ date: 0, props: [] as P }) const [lastUpdate, setLastUpdate] = useState<{ date: number, props: P }>({ date: 0, props: [] as P })
const exp = useFunctionalValue(expires) const exp = useFunctionalValue(expires)

View File

@ -1,7 +1,7 @@
import { useMemo } from 'react' import { useMemo } from 'react'
import { FunctionalValue, useFunctionalValue } from './functionalValue' 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 def = useFunctionalValue(defaultValue)
const result: T = useMemo(() => { const result: T = useMemo(() => {