♻️(react) update Pagination behavior

We want to make the Pagination more intuitive and fluent. Now it displays
the current page all the time, auto select, auto submits on blur.
This commit is contained in:
Nathan Vasse
2023-09-04 15:42:02 +02:00
committed by NathanVss
parent c90f66f0cd
commit 09e474c8c1
3 changed files with 14 additions and 4 deletions

View File

@@ -0,0 +1,5 @@
---
"@openfun/cunningham-react": minor
---
improve Pagination behavior

View File

@@ -1,4 +1,4 @@
import React, { Fragment, useState } from "react"; import React, { Fragment, useEffect, useState } from "react";
import { Button } from ":/components/Button"; import { Button } from ":/components/Button";
import { Input } from ":/components/Forms/Input"; import { Input } from ":/components/Forms/Input";
import { useCunningham } from ":/components/Provider"; import { useCunningham } from ":/components/Provider";
@@ -47,6 +47,10 @@ export const Pagination = ({
const { t } = useCunningham(); const { t } = useCunningham();
const [gotoValue, setGotoValue] = useState(""); const [gotoValue, setGotoValue] = useState("");
useEffect(() => {
setGotoValue(page + "");
}, [page]);
if (pagesCount <= 1) { if (pagesCount <= 1) {
return null; return null;
} }
@@ -83,14 +87,13 @@ export const Pagination = ({
const gotoPage = () => { const gotoPage = () => {
let value = +gotoValue; let value = +gotoValue;
if (value < 0) { if (value < 0 || !value) {
value = 1; value = 1;
} }
if (value > pagesCount) { if (value > pagesCount) {
value = pagesCount; value = pagesCount;
} }
onPageChange(value); onPageChange(value);
setGotoValue("");
}; };
const canPrevious = page > 1; const canPrevious = page > 1;
@@ -161,6 +164,8 @@ export const Pagination = ({
onChange={(e) => setGotoValue(e.target.value)} onChange={(e) => setGotoValue(e.target.value)}
min={1} min={1}
max={pagesCount} max={pagesCount}
onBlur={() => gotoPage()}
onFocus={(e) => e.target.select()}
/> />
</form> </form>
</div> </div>

View File

@@ -1,7 +1,7 @@
{ {
"components": { "components": {
"pagination": { "pagination": {
"goto_label": "Go to", "goto_label": "Go to page",
"goto_label_aria": "Go to any page", "goto_label_aria": "Go to any page",
"next_aria": "Go to next page", "next_aria": "Go to next page",
"previous_aria": "Go to previous page", "previous_aria": "Go to previous page",