-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(admin): add load more to shipping methods input
- Loading branch information
1 parent
cf6dfe1
commit 7c335f8
Showing
6 changed files
with
71 additions
and
5 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import {computed, type ComputedRef, type MaybeRef, ref, type Ref, toValue} from 'vue'; | ||
import {type ReadonlyOr} from '@myparcel/ts-utils'; | ||
|
||
export type UseLoadMoreOptions<Item = unknown> = { | ||
step?: number; | ||
start?: number; | ||
items: ReadonlyOr<MaybeRef<Item[]>>; | ||
}; | ||
|
||
export type UseLoadMore<Item = unknown> = { | ||
loadMore(): void; | ||
hasMore: ComputedRef<boolean>; | ||
loaded: Ref<number>; | ||
items: ComputedRef<Item[]>; | ||
}; | ||
|
||
export const useLoadMore = <Item = unknown>(options: UseLoadMoreOptions<Item>): UseLoadMore<Item> => { | ||
const loaded = ref(options.start ?? options.step ?? toValue(options.items).length - 1); | ||
|
||
const hasMore = computed(() => loaded.value < toValue(options.items).length - 1); | ||
|
||
const loadMore = () => { | ||
if (!hasMore.value) { | ||
return; | ||
} | ||
|
||
const step = options.step ?? 1; | ||
|
||
const maxLength = toValue(options.items).length; | ||
|
||
if (loaded.value + step > maxLength) { | ||
loaded.value = maxLength; | ||
} else { | ||
loaded.value += step; | ||
} | ||
}; | ||
|
||
const items = computed(() => toValue(options.items).slice(0, loaded.value)); | ||
|
||
return { | ||
items, | ||
loaded, | ||
hasMore, | ||
loadMore, | ||
}; | ||
}; |
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