-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: refactor hooks : moved few hooks in right folder (#1049)
- Loading branch information
Showing
35 changed files
with
250 additions
and
139 deletions.
There are no files selected for viewing
Validating CODEOWNERS rules …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/components/Select/components/SelectList/SelectLoadingIndicator.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<!--GITHUB_BLOCK--> | ||
|
||
# useIntersection | ||
|
||
<!--/GITHUB_BLOCK--> | ||
|
||
```tsx | ||
import {useIntersection} from '@gravity-ui/uikit'; | ||
``` | ||
|
||
The `useIntersection` hook works with the [Intersection Observer API](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API) and calls a callback at the 'isIntersected' event of the observed element | ||
|
||
## Properties | ||
|
||
| Name | Description | Type | Default | | ||
| :---------- | :-------------------------------------------- | :------------------------: | :-----: | | ||
| element | The observed element | `React.RefObject` | | | ||
| options | Intersection observer options | `IntersectionObserverInit` | | | ||
| onIntersect | Callback when observed element is intersected | `() => void` | | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export {useIntersection} from './useIntersection'; | ||
export type {UseIntersection, UseIntersectionProps} from './useIntersection'; |
7 changes: 6 additions & 1 deletion
7
src/components/utils/useIntersection.ts → src/hooks/useIntersection/useIntersection.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<!--GITHUB_BLOCK--> | ||
|
||
# useListNavigation | ||
|
||
<!--/GITHUB_BLOCK--> | ||
|
||
```tsx | ||
import {useListNavigation} from '@gravity-ui/uikit'; | ||
``` | ||
|
||
The `useListNavigation` hook used to navigate through the items in a list | ||
|
||
`ArrowDown` will increase currently active item index | ||
`ArrowUp` will reduce currently active item index | ||
`PageDown` will increase currently active item index by pageSize (works if `pageSize` passed) | ||
`PageUp` will reduce currently active item index by pageSize (works if `pageSize` passed) | ||
`Home` will navigate to the start of the list if `processHomeKey` (disable if you want to move the cursor to the start of active input on `Home` key, for example) | ||
`End` will navigate to the end of the list if `processEndKey` (disable if you want to move the cursor to the end of active input on `End` key, for example) | ||
|
||
For skipping items you should pass `skip` function, which accepts an item and returns boolean value, representing if it's needed to skip the item | ||
|
||
The hook returns the following: | ||
|
||
- `activeItemIndex` - active item index | ||
- `reset` - function, which should be called when you want to reset navigation | ||
|
||
## Examples | ||
|
||
```tsx | ||
const anchorRef = useRef<HTMLButtonElement>(null); | ||
|
||
const items = [ | ||
{ | ||
id: 1, | ||
title: 'Item 1', | ||
}, | ||
{ | ||
id: 2, | ||
title: 'Item 2', | ||
}, | ||
]; | ||
|
||
useListNavigation({ | ||
items, | ||
skip: (item) => item.disabled, | ||
anchorRef, | ||
onAnchorKeydown: (activeItemIndex: number, event: KeyboardEvent) => { | ||
switch (event.key) { | ||
case 'Enter': | ||
case ' ': { | ||
const activeItem = items[activeItemIndex]; | ||
if (activeItem) { | ||
event.preventDefault(); | ||
|
||
console.log(`${activeItem.title} selected`); | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
}, | ||
}); | ||
``` | ||
|
||
## Properties | ||
|
||
| Name | Description | Type | Default | | ||
| :-------------- | :---------------------------------------------------------------------------------------------------- | :------------------------------------------------------------------: | :-----: | | ||
| items | List items. Item can be any object. Also, it can contain `items` property, which represents sub-items | `ItemType[]` | | | ||
| skip | Returns true if the item should not participate in navigation (Called for each item) | `(item: ItemType) => boolean` | | | ||
| pageSize | Items page size, if passed, then navigates by pageSize on PageDown/PageUp | `number` | | | ||
| processHomeKey | Navigate to the start of the list on Home key | `boolean` | `false` | | ||
| processEndKey | Navigate to the end of the list on End key | `boolean` | `false` | | ||
| disabled | Disable navigation | `boolean` | `false` | | ||
| initialValue | Initial active item index | `number` | `-1` | | ||
| anchorRef | HTMLElement reference, the hook will listen keydown event on that element | `RefObject<AnchorType>` | | | ||
| onAnchorKeyDown | Custom keydown handler, if returns false, then the hook won't process keydown | `(activeItemIndex: number, event: KeyboardEvent) => boolean or void` | | | ||
|
||
## Result | ||
|
||
| Name | Description | Type | | ||
| :----------------- | :----------------------- | :-----------------------: | | ||
| activeItemIndex | Index of the active item | `number` | | ||
| setActiveItemIndex | Active item index setter | `(index: number) => void` | | ||
| reset | Resets navigation | `() => void` | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export {useListNavigation} from './useListNavigation'; | ||
export type {UseListNavigationProps, UseListNavigationResult} from './useListNavigation'; |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.