diff --git a/.changeset/cyan-baboons-applaud.md b/.changeset/cyan-baboons-applaud.md new file mode 100644 index 00000000000..6093faf2007 --- /dev/null +++ b/.changeset/cyan-baboons-applaud.md @@ -0,0 +1,9 @@ +--- +"@aws-amplify/ui-react-core-auth": patch +"@aws-amplify/ui-react-core": patch +"@aws-amplify/ui-react-storage": patch +"@aws-amplify/ui-react": patch +"@aws-amplify/ui": patch +--- + +chore(utils): prefer isFunction diff --git a/packages/react-core-auth/src/hooks/useHandleCopy/useHandleCopy.ts b/packages/react-core-auth/src/hooks/useHandleCopy/useHandleCopy.ts index c4562dbaa12..cf0656ff45e 100644 --- a/packages/react-core-auth/src/hooks/useHandleCopy/useHandleCopy.ts +++ b/packages/react-core-auth/src/hooks/useHandleCopy/useHandleCopy.ts @@ -1,6 +1,6 @@ import React from 'react'; -import { isString, isTypedFunction } from '@aws-amplify/ui'; +import { isString, isFunction } from '@aws-amplify/ui'; import { useTimeout } from '@aws-amplify/ui-react-core'; interface UseHandleCopyParams { @@ -52,7 +52,7 @@ export default function useHandleCopy({ (v?: string) => { // prefer `v` passed to callback over `target` const copyValue = v ?? target; - if (isTypedFunction(copyHandler) && isString(copyValue)) { + if (isFunction(copyHandler) && isString(copyValue)) { copyHandler(copyValue); setValue(copyValue); } diff --git a/packages/react-core/src/components/FormCore/useControlledField.ts b/packages/react-core/src/components/FormCore/useControlledField.ts index a6b5060eb12..97d922d3feb 100644 --- a/packages/react-core/src/components/FormCore/useControlledField.ts +++ b/packages/react-core/src/components/FormCore/useControlledField.ts @@ -6,7 +6,7 @@ import { UseControlledField, UseControlledFieldParams, } from './types'; -import { isTypedFunction } from '@aws-amplify/ui'; +import { isFunction } from '@aws-amplify/ui'; export const DEFAULT_ERROR_MESSAGE = '`useControlledField` must be used within a `FormProvider`'; @@ -44,7 +44,7 @@ export default function useControlledField({ const hasError = !!errorMessage; const handleBlur = (event: Parameters[0]) => { - if (isTypedFunction(_onBlur)) { + if (isFunction(_onBlur)) { _onBlur(event); } // `useController.onBlur` does not receive params @@ -52,7 +52,7 @@ export default function useControlledField({ }; const handleChangeText = (event: string) => { - if (isTypedFunction(_onChangeText)) { + if (isFunction(_onChangeText)) { _onChangeText(event); } onChangeText(event); diff --git a/packages/react-core/src/hooks/useTimeout.ts b/packages/react-core/src/hooks/useTimeout.ts index 8874cea6d89..92f2422f03c 100644 --- a/packages/react-core/src/hooks/useTimeout.ts +++ b/packages/react-core/src/hooks/useTimeout.ts @@ -1,5 +1,5 @@ import React from 'react'; -import { isTypedFunction } from '@aws-amplify/ui'; +import { isFunction } from '@aws-amplify/ui'; export default function useTimeout({ callback, @@ -15,7 +15,7 @@ export default function useTimeout({ }, [callback]); React.useEffect(() => { - if (!isTypedFunction(storedCallback.current) || !delay) { + if (!isFunction(storedCallback.current) || !delay) { return; } diff --git a/packages/react-storage/src/components/FileUploader/utils/getInput.ts b/packages/react-storage/src/components/FileUploader/utils/getInput.ts index 7a6f29b6d19..f57fa028d21 100644 --- a/packages/react-storage/src/components/FileUploader/utils/getInput.ts +++ b/packages/react-storage/src/components/FileUploader/utils/getInput.ts @@ -2,7 +2,7 @@ import { fetchAuthSession } from 'aws-amplify/auth'; import { StorageAccessLevel } from '@aws-amplify/core'; import { UploadDataWithPathInput, UploadDataInput } from 'aws-amplify/storage'; -import { isString, isTypedFunction } from '@aws-amplify/ui'; +import { isString, isFunction } from '@aws-amplify/ui'; import { ProcessFile } from '../types'; import { resolveFile } from './resolveFile'; @@ -28,7 +28,7 @@ export const getInput = ({ useAccelerateEndpoint, }: GetInputParams) => { return async (): Promise => { - const hasCallbackPath = isTypedFunction(path); + const hasCallbackPath = isFunction(path); const hasStringPath = isString(path); const hasKeyInput = !!accessLevel && !hasCallbackPath; diff --git a/packages/react/src/primitives/Accordion/AccordionTrigger.tsx b/packages/react/src/primitives/Accordion/AccordionTrigger.tsx index 4a6e32f3052..149fa9653a2 100644 --- a/packages/react/src/primitives/Accordion/AccordionTrigger.tsx +++ b/packages/react/src/primitives/Accordion/AccordionTrigger.tsx @@ -1,7 +1,7 @@ import * as React from 'react'; import { classNames } from '@aws-amplify/ui'; -import { ComponentClassName, isTypedFunction } from '@aws-amplify/ui'; +import { ComponentClassName, isFunction } from '@aws-amplify/ui'; import { primitiveWithForwardRef } from '../utils/primitiveWithForwardRef'; import { ElementType, PrimitiveProps, BaseViewProps } from '../types/view'; import { ForwardRefPrimitive, Primitive } from '../types/view'; @@ -20,7 +20,7 @@ const AccordionTriggerPrimitive: Primitive = ( const context = React.useContext(AccordionContext); const value = React.useContext(AccordionItemContext); const handleOnClick = (e: React.MouseEvent) => { - if (isTypedFunction(rest.onClick)) { + if (isFunction(rest.onClick)) { rest.onClick(e); } diff --git a/packages/react/src/primitives/Tabs/TabsItem.tsx b/packages/react/src/primitives/Tabs/TabsItem.tsx index 645fe0c987e..9219e80d84e 100644 --- a/packages/react/src/primitives/Tabs/TabsItem.tsx +++ b/packages/react/src/primitives/Tabs/TabsItem.tsx @@ -4,7 +4,7 @@ import { classNames } from '@aws-amplify/ui'; import { ComponentClassName, classNameModifierByFlag, - isTypedFunction, + isFunction, } from '@aws-amplify/ui'; import { ForwardRefPrimitive, Primitive } from '../types'; @@ -25,7 +25,7 @@ const TabsItemPrimitive: Primitive = ( } const isActive = activeTab === value; const handleOnClick = (e: React.MouseEvent) => { - if (isTypedFunction(onClick)) { + if (isFunction(onClick)) { onClick?.(e); } setActiveTab(value); diff --git a/packages/ui/src/utils/__tests__/index.test.ts b/packages/ui/src/utils/__tests__/index.test.ts index 85ddb576e5f..b2b897534c7 100644 --- a/packages/ui/src/utils/__tests__/index.test.ts +++ b/packages/ui/src/utils/__tests__/index.test.ts @@ -12,7 +12,6 @@ import { isObject, isSet, isString, - isTypedFunction, isUndefined, sanitizeNamespaceImport, templateJoin, @@ -317,19 +316,6 @@ describe('classNameModifierByFlag', () => { }); }); -describe('isTypedFunction', () => { - it('returns `true` when the value param is a function', () => { - expect(isTypedFunction(() => null)).toBe(true); - }); - - it.each(['string', null, undefined, true, false])( - 'returns `false` when the value param is %s', - (value) => { - expect(isTypedFunction(value)).toBe(false); - } - ); -}); - describe('templateJoin', () => { it('returns the expected value', () => { const output = templateJoin(['one', 'two'], (value) => `^${value}^`); diff --git a/packages/ui/src/utils/utils.ts b/packages/ui/src/utils/utils.ts index 1b9d05c5b9c..ba304515b6c 100644 --- a/packages/ui/src/utils/utils.ts +++ b/packages/ui/src/utils/utils.ts @@ -207,18 +207,6 @@ export const classNameModifierByFlag = ( return flag ? `${base}--${modifier}` : ''; }; -/** - * `isFunction` but types the param with its function signature - * - * @param {unknown} value param to check - * @returns {boolean} whether `value` is a function - */ -export function isTypedFunction any>( - value: unknown -): value is T { - return isFunction(value); -} - /** * Similar to `Array.join`, with an optional callback/template param * for formatting returned string values