-
Notifications
You must be signed in to change notification settings - Fork 3
Fix/bug fixes and updates #80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5536395
fix(web): draggable-list-update-issue
tractorss b6a0fc1
chore: update-documentation
tractorss 483b499
chore: add-ids-accordion
tractorss 82d4aaa
chore: add-aria-label-to-modal-dialog
tractorss a380fc0
feat: field-error-messages
tractorss 08c14de
fix: bignumber-field-scroll-behaviour
tractorss 4a3cea0
chore: make-format-config-local
tractorss 9069e31
chore: move-default-id-generation-to-hook
tractorss 0a33457
fix: show-trash-icon-on-selected
tractorss d0820e6
chore: rabbit-review
tractorss f1234cc
chore: release v3.4.5
tractorss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,103 @@ | ||
import { useCallback, useEffect, useMemo, useState } from "react"; | ||
import _ from "lodash"; | ||
|
||
type Key = string | number; | ||
|
||
export interface ListItem { | ||
id: Key; | ||
name: string; | ||
value: any; | ||
tractorss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
interface UseListOptions { | ||
initialItems: ListItem[]; | ||
onChange?: (updatedItems: ListItem[]) => void; | ||
} | ||
|
||
export function useList({ initialItems, onChange }: UseListOptions) { | ||
const [items, setItems] = useState<ListItem[]>(initialItems); | ||
|
||
// track updates to initialItems | ||
useEffect(() => { | ||
setItems((prevItems) => { | ||
// preventing callback loop | ||
if (_.isEqual(initialItems, prevItems)) return prevItems; | ||
return initialItems; | ||
}); | ||
}, [initialItems]); | ||
|
||
const itemsMap = useMemo(() => { | ||
const map = new Map<Key, ListItem>(); | ||
for (const item of items) { | ||
map.set(item.id, item); | ||
} | ||
return map; | ||
}, [items]); | ||
|
||
const getItem = useCallback((key: Key) => itemsMap.get(key), [itemsMap]); | ||
|
||
const remove = useCallback( | ||
(key: Key) => { | ||
// updateItems(items.filter((item) => key !== item.id)); | ||
setItems((prevItems) => { | ||
const newItems = prevItems.filter((item) => key !== item.id); | ||
onChange?.(newItems); | ||
return newItems; | ||
}); | ||
}, | ||
[onChange], | ||
); | ||
|
||
const moveBefore = useCallback( | ||
(targetKey: Key, keys: Iterable<Key>) => { | ||
setItems((prevItems) => { | ||
const key = Array.from(keys)[0]; | ||
if (key === targetKey) return prevItems; | ||
|
||
const indexFrom = prevItems.findIndex((item) => item.id === key); | ||
const indexTo = prevItems.findIndex((item) => item.id === targetKey); | ||
if (indexFrom === -1 || indexTo === -1) return prevItems; | ||
|
||
const reordered = [...prevItems]; | ||
const [movedItem] = reordered.splice(indexFrom, 1); | ||
reordered.splice(indexTo, 0, movedItem); | ||
onChange?.(reordered); | ||
|
||
return reordered; | ||
}); | ||
}, | ||
[onChange], | ||
); | ||
|
||
const moveAfter = useCallback( | ||
(targetKey: Key, keys: Iterable<Key>) => { | ||
setItems((prevItems) => { | ||
const key = Array.from(keys)[0]; | ||
if (key === targetKey) return prevItems; | ||
|
||
const indexFrom = prevItems.findIndex((item) => item.id === key); | ||
const indexTo = prevItems.findIndex((item) => item.id === targetKey); | ||
if (indexFrom === -1 || indexTo === -1) return prevItems; | ||
|
||
const reordered = [...prevItems]; | ||
const [movedItem] = reordered.splice(indexFrom, 1); | ||
|
||
// Adjust if removing item before target index | ||
const insertIndex = indexFrom < indexTo ? indexTo : indexTo + 1; | ||
reordered.splice(insertIndex, 0, movedItem); | ||
onChange?.(reordered); | ||
|
||
return reordered; | ||
}); | ||
}, | ||
[onChange], | ||
); | ||
|
||
return { | ||
items, | ||
getItem, | ||
remove, | ||
moveBefore, | ||
moveAfter, | ||
}; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.