Skip to content

Commit

Permalink
fix: refactor hooks: useForkRef and useFileInput
Browse files Browse the repository at this point in the history
  • Loading branch information
NikitaCG committed Oct 6, 2023
1 parent 57123c2 commit 54c0827
Show file tree
Hide file tree
Showing 20 changed files with 71 additions and 25 deletions.
1 change: 1 addition & 0 deletions CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
/src/components/theme @resure

/src/hooks/useActionHandlers @ogonkov
/src/hooks/useFileInput @korvin89
/src/hooks/useOutsideClick @NikitaCG
/src/hooks/useUniqId @ValeraS

Expand Down
2 changes: 1 addition & 1 deletion src/components/Popup/Popup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import React from 'react';

import {CSSTransition} from 'react-transition-group';

import {useForkRef} from '../../hooks';
import {Portal} from '../Portal';
import type {DOMProps, QAProps} from '../types';
import {FocusTrap, useParentFocusTrap} from '../utils/FocusTrap';
import {block} from '../utils/cn';
import {getCSSTransitionClassNames} from '../utils/transition';
import {useForkRef} from '../utils/useForkRef';
import {useLayer} from '../utils/useLayer';
import type {LayerExtendableProps} from '../utils/useLayer';
import {usePopper} from '../utils/usePopper';
Expand Down
3 changes: 1 addition & 2 deletions src/components/Select/Select.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import React from 'react';

import {KeyCode} from '../../constants';
import {useUniqId} from '../../hooks';
import {useForkRef, useUniqId} from '../../hooks';
import type {List} from '../List';
import {useMobile} from '../mobile';
import type {CnMods} from '../utils/cn';
import {useFocusWithin} from '../utils/interactions';
import {useForkRef} from '../utils/useForkRef';
import {useSelect} from '../utils/useSelect';

import {EmptyOptions, SelectControl, SelectFilter, SelectList, SelectPopup} from './components';
Expand Down
2 changes: 1 addition & 1 deletion src/components/Tooltip/Tooltip.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React from 'react';

import {KeyCode} from '../../constants';
import {useForkRef} from '../../hooks';
import {Popup} from '../Popup';
import type {PopupPlacement} from '../Popup';
import type {DOMProps} from '../types';
import {block} from '../utils/cn';
import {useBoolean} from '../utils/useBoolean';
import {useForkRef} from '../utils/useForkRef';

import './Tooltip.scss';

Expand Down
3 changes: 1 addition & 2 deletions src/components/controls/TextArea/TextArea.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import React from 'react';

import {useUniqId} from '../../../hooks';
import {useForkRef, useUniqId} from '../../../hooks';
import {blockNew} from '../../utils/cn';
import {useForkRef} from '../../utils/useForkRef';
import {ClearButton, mapTextInputSizeToButtonSize} from '../common';
import {OuterAdditionalContent} from '../common/OuterAdditionalContent/OuterAdditionalContent';
import type {
Expand Down
2 changes: 1 addition & 1 deletion src/components/controls/TextArea/TextAreaControl.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';

import {useForkRef} from '../../../hooks';
import {blockNew} from '../../utils/cn';
import {useForkRef} from '../../utils/useForkRef';

import type {TextAreaProps} from './TextArea';

Expand Down
3 changes: 1 addition & 2 deletions src/components/controls/TextInput/TextInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ import React from 'react';

import {TriangleExclamation} from '@gravity-ui/icons';

import {useUniqId} from '../../../hooks';
import {useForkRef, useUniqId} from '../../../hooks';
import {Icon} from '../../Icon';
import {Popover} from '../../Popover';
import {block} from '../../utils/cn';
import {useElementSize} from '../../utils/useElementSize';
import {useForkRef} from '../../utils/useForkRef';
import {ClearButton, mapTextInputSizeToButtonSize} from '../common';
import {OuterAdditionalContent} from '../common/OuterAdditionalContent/OuterAdditionalContent';
import type {
Expand Down
3 changes: 0 additions & 3 deletions src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,7 @@ export * from './utils/useLayer';
export {Lang, configure} from './utils/configure';
export * from './utils/useSelect';
export * from './utils/useListNavigation';
export * from './utils/useForkRef';
export * from './utils/setRef';
export {useOnFocusOutside} from './utils/useOnFocusOutside';
export * from './utils/interactions';
export * from './utils/xpath';
export * from './utils/useFileInput/useFileInput';
export {getLayersCount} from './utils/LayerManager';
4 changes: 1 addition & 3 deletions src/components/utils/FocusTrap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import React from 'react';
import {createFocusTrap} from 'focus-trap';
import type {FocusTrap as FocusTrapInstance} from 'focus-trap';

import {useUniqId} from '../../hooks';

import {useForkRef} from './useForkRef';
import {useForkRef, useUniqId} from '../../hooks';

interface FocusTrapContext {
addNode: (id: string, node: HTMLElement) => void;
Expand Down
2 changes: 1 addition & 1 deletion src/components/utils/useCheckbox.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import React from 'react';

import {useForkRef} from '../../hooks';
import type {ControlProps} from '../types';

import {eventBroker} from './event-broker';
import {useForkRef} from './useForkRef';

export function useCheckbox({
name,
Expand Down
3 changes: 1 addition & 2 deletions src/components/utils/useRadio.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import React from 'react';

import {useUniqId} from '../../hooks';
import {useForkRef, useUniqId} from '../../hooks';
import type {ControlProps} from '../types';

import {eventBroker} from './event-broker';
import {useForkRef} from './useForkRef';

export function useRadio({
name,
Expand Down
2 changes: 2 additions & 0 deletions src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export * from './useActionHandlers';
export * from './useBodyScrollLock';
export * from './useFileInput';
export * from './useForkRef';
export * from './useOutsideClick';
export * from './usePortalContainer';
export * from './useVirtualElementRef';
Expand Down
23 changes: 23 additions & 0 deletions src/hooks/useFileInput/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!--GITHUB_BLOCK-->

# useFileInput

<!--/GITHUB_BLOCK-->

```tsx
import {useFileInput} from '@gravity-ui/uikit';
```

The `useFileInput` hook used to shape props for input with type "file"

## Properties

| Name | Description | Type | Default |
| :------- | :------------------- | :----------------------------------: | :-----: |
| onUpdate | Update file callback | `(files: File[]) => void` | |
| onChange | On change callback | `(event: React.ChangeEvent) => void` | |

## Result

- controlProps - props for the input with type 'file' `React.DetailedHTMLProps`.
- triggerProps - props for the interactive element that, when interacted with, should open a dialog window for file selection `{onClick: () => void;}`.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';

import type {Meta, StoryFn} from '@storybook/react';

import {Button} from '../../../Button';
import {Button} from '../../../components/Button';
import {useFileInput} from '../useFileInput';

export default {title: 'Hooks/useFileInput'} as Meta;
Expand Down
2 changes: 2 additions & 0 deletions src/hooks/useFileInput/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export {useFileInput} from './useFileInput';
export type {UseFileInputProps, UseFileInputOutput, UseFileInputResult} from './useFileInput';
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,20 @@ export type UseFileInputOutput = {
};
};

export type UseFileInputResult = UseFileInputOutput;

/**
* Used to shape props for input with type "file".
*
*
* Usage example:
```tsx
import React from 'react';
import {Button, useFileInput} from '@gravity-ui/uikit';
const Component = () => {
const onUpdate = React.useCallback((files: File[]) => console.log(files), []);
const {controlProps, triggerProps} = useFileInput({onUpdate});
return (
<React.Fragment>
<input {...controlProps} />
Expand Down
21 changes: 21 additions & 0 deletions src/hooks/useForkRef/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!--GITHUB_BLOCK-->

# useForkRef

<!--/GITHUB_BLOCK-->

```tsx
import {useForkRef} from '@gravity-ui/uikit';
```

The `useForkRef` hook that can combine refs into a single Ref

## Properties

| Name | Description | Type | Default |
| :--- | :-------------- | :-----------------: | :-----: |
| refs | ref-links array | `React.RefObject[]` | |

## Result

Combined ref. `React.RefCallback`
3 changes: 3 additions & 0 deletions src/hooks/useForkRef/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export {useForkRef} from './useForkRef';
export {setRef} from './setRef';
export type {UseForkRefProps, UseForkRefResult} from './useForkRef';
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import React from 'react';

import {setRef} from './setRef';

export function useForkRef<T>(
...refs: Array<React.Ref<T> | undefined>
): React.RefCallback<T> | null {
export type UseForkRefProps<K> = Array<React.Ref<K> | undefined>;
export type UseForkRefResult<W> = React.RefCallback<W> | null;

export function useForkRef<T>(...refs: UseForkRefProps<T>): UseForkRefResult<T> {
return React.useMemo(() => {
if (refs.every((ref) => ref === null || ref === undefined)) {
return null;
Expand Down

0 comments on commit 54c0827

Please sign in to comment.