From c8681c64e6c6b547bf30b38366508b876fc66b79 Mon Sep 17 00:00:00 2001 From: cheton Date: Wed, 15 Nov 2023 14:02:32 +0800 Subject: [PATCH] feat: import utility functions from `@tonic-ui/utils` --- packages/react-hooks/src/useEventListener.js | 12 +----------- packages/react-hooks/src/useMediaQuery.js | 4 +--- packages/react-hooks/src/useOutsideClick.js | 9 ++------- 3 files changed, 4 insertions(+), 21 deletions(-) diff --git a/packages/react-hooks/src/useEventListener.js b/packages/react-hooks/src/useEventListener.js index 48ad7d04a8..86ea80c149 100644 --- a/packages/react-hooks/src/useEventListener.js +++ b/packages/react-hooks/src/useEventListener.js @@ -1,17 +1,7 @@ +import { noop, runIfFn } from '@tonic-ui/utils'; import { useEffect } from 'react'; import useEventCallback from './useEventCallback'; -// TODO: move to '@tonic-ui/utils' -const noop = () => {}; - -// TODO: move to '@tonic-ui/utils' -const runIfFn = (valueOrFn, ...args) => { - if (typeof valueOrFn === 'function') { - return valueOrFn(...args); - } - return valueOrFn; -}; - /** * A custom Hook to manage browser event listeners. * diff --git a/packages/react-hooks/src/useMediaQuery.js b/packages/react-hooks/src/useMediaQuery.js index 0f14302118..826acd9324 100644 --- a/packages/react-hooks/src/useMediaQuery.js +++ b/packages/react-hooks/src/useMediaQuery.js @@ -1,8 +1,6 @@ +import { noop } from '@tonic-ui/utils'; import { useEffect, useState } from 'react'; -// TODO: move to '@tonic-ui/utils' -const noop = () => {}; - const getInitialState = (query, defaultValue) => { if (defaultValue !== undefined) { return defaultValue; diff --git a/packages/react-hooks/src/useOutsideClick.js b/packages/react-hooks/src/useOutsideClick.js index 034f7917fb..73d67fb1ab 100644 --- a/packages/react-hooks/src/useOutsideClick.js +++ b/packages/react-hooks/src/useOutsideClick.js @@ -1,12 +1,7 @@ +import { getOwnerDocument, noop } from '@tonic-ui/utils'; import { useEffect } from 'react'; import useEventCallback from './useEventCallback'; -// TODO: move to '@tonic-ui/utils' -const noop = () => {}; - -// TODO: move to '@tonic-ui/utils' -const ownerDocument = (node) => (node?.ownerDocument || document); - const defaultEvents = ['mousedown', 'touchstart']; /** @@ -33,7 +28,7 @@ const useOutsideClick = ( const filteredEvents = (Array.isArray(events) ? events : []) .filter(x => (typeof x === 'string')); - const doc = ownerDocument(ref?.current); + const doc = getOwnerDocument(ref?.current); for (const eventName of filteredEvents) { doc?.addEventListener?.(eventName, savedHandler, true);