-
-
Notifications
You must be signed in to change notification settings - Fork 390
refactor: add new useVisibleItems composable #2395
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
+203
−17
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import { computed, shallowRef, toValue } from 'vue' | ||
| import type { MaybeRefOrGetter } from 'vue' | ||
|
|
||
| export function useVisibleItems<T>(items: MaybeRefOrGetter<T[]>, limit: number) { | ||
| const showAll = shallowRef(false) | ||
|
|
||
| const visibleItems = computed(() => { | ||
| const list = toValue(items) | ||
| return showAll.value ? list : list.slice(0, limit) | ||
| }) | ||
|
|
||
| const hiddenCount = computed(() => | ||
| showAll.value ? 0 : Math.max(0, toValue(items).length - limit), | ||
| ) | ||
|
|
||
| const hasMore = computed(() => !showAll.value && toValue(items).length > limit) | ||
|
|
||
| const expand = () => { | ||
| showAll.value = true | ||
| } | ||
| const collapse = () => { | ||
| showAll.value = false | ||
| } | ||
| const toggle = () => { | ||
| showAll.value = !showAll.value | ||
| } | ||
|
|
||
| return { visibleItems, hiddenCount, hasMore, showAll, expand, collapse, toggle } | ||
| } | ||
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,147 @@ | ||
| import { describe, expect, it } from 'vitest' | ||
| import { computed, ref } from 'vue' | ||
| import { useVisibleItems } from '~/composables/useVisibleItems' | ||
|
|
||
| describe('useVisibleItems', () => { | ||
| describe('initial state', () => { | ||
| it('returns all items when list is within limit', () => { | ||
| const { visibleItems, hasMore, hiddenCount } = useVisibleItems(['a', 'b', 'c'], 5) | ||
|
|
||
| expect(visibleItems.value).toEqual(['a', 'b', 'c']) | ||
| expect(hasMore.value).toBe(false) | ||
| expect(hiddenCount.value).toBe(0) | ||
| }) | ||
|
|
||
| it('returns exactly limit items when list exceeds limit', () => { | ||
| const items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | ||
| const { visibleItems, hasMore, hiddenCount } = useVisibleItems(items, 5) | ||
|
|
||
| expect(visibleItems.value).toEqual([1, 2, 3, 4, 5]) | ||
| expect(hasMore.value).toBe(true) | ||
| expect(hiddenCount.value).toBe(5) | ||
| }) | ||
|
|
||
| it('returns all items when list length equals limit exactly', () => { | ||
| const items = [1, 2, 3] | ||
| const { visibleItems, hasMore, hiddenCount } = useVisibleItems(items, 3) | ||
|
|
||
| expect(visibleItems.value).toEqual([1, 2, 3]) | ||
| expect(hasMore.value).toBe(false) | ||
| expect(hiddenCount.value).toBe(0) | ||
| }) | ||
|
|
||
| it('handles empty list', () => { | ||
| const { visibleItems, hasMore, hiddenCount } = useVisibleItems([], 5) | ||
|
|
||
| expect(visibleItems.value).toEqual([]) | ||
| expect(hasMore.value).toBe(false) | ||
| expect(hiddenCount.value).toBe(0) | ||
| }) | ||
| }) | ||
|
|
||
| describe('expand()', () => { | ||
| it('shows all items after expand', () => { | ||
| const items = [1, 2, 3, 4, 5, 6] | ||
| const { visibleItems, hasMore, hiddenCount, expand } = useVisibleItems(items, 3) | ||
|
|
||
| expect(visibleItems.value).toEqual([1, 2, 3]) | ||
|
|
||
| expand() | ||
|
|
||
| expect(visibleItems.value).toEqual([1, 2, 3, 4, 5, 6]) | ||
| expect(hasMore.value).toBe(false) | ||
| expect(hiddenCount.value).toBe(0) | ||
| }) | ||
| }) | ||
|
|
||
| describe('collapse()', () => { | ||
| it('hides items again after collapse', () => { | ||
| const items = [1, 2, 3, 4, 5, 6] | ||
| const { visibleItems, hasMore, expand, collapse } = useVisibleItems(items, 3) | ||
|
|
||
| expect(visibleItems.value).toEqual([1, 2, 3]) | ||
|
|
||
| expand() | ||
|
|
||
| expect(visibleItems.value).toEqual([1, 2, 3, 4, 5, 6]) | ||
|
|
||
| collapse() | ||
|
|
||
| expect(visibleItems.value).toEqual([1, 2, 3]) | ||
| expect(hasMore.value).toBe(true) | ||
| }) | ||
| }) | ||
|
|
||
| describe('toggle()', () => { | ||
| it('expands on first toggle', () => { | ||
| const items = [1, 2, 3, 4, 5] | ||
| const { visibleItems, toggle } = useVisibleItems(items, 3) | ||
|
|
||
| expect(visibleItems.value).toEqual([1, 2, 3]) | ||
|
|
||
| toggle() | ||
|
|
||
| expect(visibleItems.value).toEqual([1, 2, 3, 4, 5]) | ||
| }) | ||
|
|
||
| it('collapses on second toggle', () => { | ||
| const items = [1, 2, 3, 4, 5] | ||
| const { visibleItems, toggle } = useVisibleItems(items, 3) | ||
|
|
||
| expect(visibleItems.value).toEqual([1, 2, 3]) | ||
|
|
||
| toggle() | ||
|
|
||
| expect(visibleItems.value).toEqual([1, 2, 3, 4, 5]) | ||
|
|
||
| toggle() | ||
|
|
||
| expect(visibleItems.value).toEqual([1, 2, 3]) | ||
| }) | ||
| }) | ||
|
|
||
| describe('reactivity', () => { | ||
| it('reacts to ref source changes', () => { | ||
| const items = ref([1, 2, 3]) | ||
| const { visibleItems, hasMore, hiddenCount } = useVisibleItems(items, 2) | ||
|
|
||
| expect(visibleItems.value).toEqual([1, 2]) | ||
| expect(hasMore.value).toBe(true) | ||
| expect(hiddenCount.value).toBe(1) | ||
|
|
||
| items.value = [1, 2] | ||
|
|
||
| expect(visibleItems.value).toEqual([1, 2]) | ||
| expect(hasMore.value).toBe(false) | ||
| expect(hiddenCount.value).toBe(0) | ||
| }) | ||
|
|
||
| it('reacts to computed source changes', () => { | ||
| const source = ref([1, 2, 3, 4, 5]) | ||
| const filtered = computed(() => source.value.filter(n => n % 2 === 0)) | ||
| const { visibleItems, hasMore } = useVisibleItems(filtered, 3) | ||
|
|
||
| expect(visibleItems.value).toEqual([2, 4]) | ||
| expect(hasMore.value).toBe(false) | ||
|
|
||
| source.value = [1, 2, 3, 4, 5, 6, 7, 8] | ||
|
|
||
| expect(visibleItems.value).toEqual([2, 4, 6]) | ||
| expect(hasMore.value).toBe(true) | ||
| }) | ||
|
|
||
| it('reacts to getter function source changes', () => { | ||
| const count = ref(2) | ||
| const { visibleItems } = useVisibleItems( | ||
| () => Array.from({ length: count.value }, (_, i) => i), | ||
| 3, | ||
| ) | ||
|
|
||
| expect(visibleItems.value).toEqual([0, 1]) | ||
|
|
||
| count.value = 5 | ||
|
|
||
| expect(visibleItems.value).toEqual([0, 1, 2]) | ||
| }) | ||
| }) | ||
| }) |
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.