useClamp doesn't return min value if max is less than min value
Describe the bug
if we use:
const min = ref(1);
const max = ref(0);
const value = useClamp(0, min, max);
it should return 1 because that is minimum of both 1 & 0
Due to this bug/issue useOffsetPagination is returning currentPage as 0 instead of 1 when total is 0.
Reproduction
https://stackblitz.com/edit/vitejs-vite-yy2zvs/?file=src%2FApp.vue
System Info
System:
OS: Linux 5.15 Ubuntu 22.04 LTS 22.04 LTS (Jammy Jellyfish)
CPU: (16) x64 AMD Ryzen 7 3700X 8-Core Processor
Memory: 7.24 GB / 15.57 GB
Container: Yes
Shell: 0.12.1 - /home/jd/.xonsh/.venv/bin/xonsh
Binaries:
Node: 16.14.2 - ~/.nvm/versions/node/v16.14.2/bin/node
Yarn: 1.22.18 - ~/.nvm/versions/node/v16.14.2/bin/yarn
npm: 8.5.0 - ~/.nvm/versions/node/v16.14.2/bin/npm
Browsers:
Chrome: 101.0.4951.64
npmPackages:
@vueuse/core: ^8.7.5 => 8.7.5
vue: ^3.2.37 => 3.2.37
Used Package Manager
pnpm
Validations
- [X] Follow our Code of Conduct
- [X] Read the Contributing Guidelines.
- [X] Read the docs.
- [X] Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- [X] Make sure this is a VueUse issue and not a framework-specific issue. For example, if it's a Vue SFC related bug, it should likely be reported to https://github.com/vuejs/core instead.
- [X] Check that this is a concrete bug. For Q&A open a GitHub Discussion.
- [X] The provided reproduction is a minimal reproducible example of the bug.
I don't know. This is more like an undefined behavior. If we return 1 I can also argue that it does not meet the requirement of max 0. I would not consider this a bug but something we should better documented.
What about useOffsetPagination where we get currentPage is 0 when total is 0.
How about providing option like { respectMin: boolean } or some better name to respect either min or max.
Or if we don't do anything we can throw an error in the console regarding this.
As per the clamp spec in the math extensions proposal, the new Math.clamp function would be defined as clamp(x, lower, upper) = min(max(x, lower), upper).
With this definition, the upper bound is always returned if the lower bound is greater than the upper bound.
VueUse's clamp function (used by useClamp) follows this spec, so you could consider this to be "standard behavior".
I do agree that something could be added to the docs, though, and an option to prefer min over max would also be nice.
This seems like it should rather be fixed in useOffsetPagination.
If total is 0, return 1, bypassing the clamp entirely.
I think transforming below code:
https://github.com/vueuse/vueuse/blob/f8962f93688347bab70bddc93b7158163d832785/packages/core/useOffsetPagination/index.ts#L69
Into:
const currentPage = computed(() => useClamp(page, 1, pageCount).value || 1)
will fix it.
As this is correct than @antfu can edit and push right away or I will make PR.
I guess this is solved now but still useOffsetPagination isn't working correctly
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.