From 2f3906769a6ae60255e5ed18af8a191cefb4e0e1 Mon Sep 17 00:00:00 2001 From: Xavier Balloy Date: Mon, 11 May 2020 15:08:02 +0200 Subject: [PATCH] feat(react-toolkit-table): allow to customize labels of Pager --- packages/table/src/Pager/Pager.spec.tsx | 18 +++++ packages/table/src/Pager/Pager.tsx | 40 +++++++--- .../Pager/__snapshots__/Pager.spec.tsx.snap | 80 +++++++++++++++++++ .../packages/table/src/Pager/Pager.stories.js | 18 +++-- 4 files changed, 137 insertions(+), 19 deletions(-) diff --git a/packages/table/src/Pager/Pager.spec.tsx b/packages/table/src/Pager/Pager.spec.tsx index 7e8a6bd85..47853fd6f 100644 --- a/packages/table/src/Pager/Pager.spec.tsx +++ b/packages/table/src/Pager/Pager.spec.tsx @@ -13,4 +13,22 @@ describe('Table.Pager', () => { expect(component).toMatchSnapshot(); }); + + it('renders correctly with custom label', () => { + const handleOnChangeMock = jest.fn(); + handleOnChangeMock.mockImplementationOnce(() => 'My mock is called'); + + const component = create( + + ); + + expect(component).toMatchSnapshot(); + }); }); diff --git a/packages/table/src/Pager/Pager.tsx b/packages/table/src/Pager/Pager.tsx index 3f6d88e6f..d5f0d0353 100644 --- a/packages/table/src/Pager/Pager.tsx +++ b/packages/table/src/Pager/Pager.tsx @@ -1,11 +1,11 @@ import * as React from 'react'; import { - WithClassModifierOptions, - withClassModifier, - withClassDefault, + compose, OnChangeCustomEvent, + withClassDefault, + withClassModifier, + WithClassModifierOptions, WithOnChangeEvent, - compose, } from '@axa-fr/react-toolkit-core'; import Modes from './Modes'; import Previous from './Previous'; @@ -16,21 +16,35 @@ import Li from './Li'; const DEFAULT_CLASSNAME = 'af-pager'; export type PagerComponentProps = { - numberItems?: number; numberPages?: number; - currentPage: number; + currentPage?: number; mode?: Modes; + previousLabel?: React.ReactNode; + nextLabel?: React.ReactNode; + ofLabel?: React.ReactNode; } & Pick, 'className'> & WithOnChangeEvent; const defaultProps: Partial = { - numberItems: 10, numberPages: 1, currentPage: 1, + mode: Modes.default, + previousLabel: '« Précédent', + nextLabel: 'Suivant »', + ofLabel: 'sur', }; -const Pager: React.SFC = props => { - const { numberPages, currentPage, onChange, mode, className } = props; +const Pager = (props: PagerComponentProps) => { + const { + numberPages, + currentPage, + onChange, + mode, + className, + previousLabel, + nextLabel, + ofLabel, + } = props; const hasNext = currentPage < numberPages; const hasPrevious = currentPage > 1; @@ -50,7 +64,7 @@ const Pager: React.SFC = props => { /> - {currentPage} sur {numberPages} + {currentPage} {ofLabel} {numberPages} = props => { value={currentPage - 1} active={hasPrevious} isVisible> - « Précédent + {previousLabel}
  • = props => { value={currentPage + 1} active={hasNext} isVisible> - Suivant » + {nextLabel} @@ -126,6 +140,6 @@ const enhance = compose( withClassDefault(DEFAULT_CLASSNAME), withClassModifier ); +Pager.defaultProps = defaultProps; const Enhance = enhance(Pager); -Enhance.defaultProps = defaultProps; export default Enhance; diff --git a/packages/table/src/Pager/__snapshots__/Pager.spec.tsx.snap b/packages/table/src/Pager/__snapshots__/Pager.spec.tsx.snap index 1c54c0b2f..174720342 100644 --- a/packages/table/src/Pager/__snapshots__/Pager.spec.tsx.snap +++ b/packages/table/src/Pager/__snapshots__/Pager.spec.tsx.snap @@ -79,3 +79,83 @@ exports[`Table.Pager renders correctly 1`] = ` `; + +exports[`Table.Pager renders correctly with custom label 1`] = ` + +`; diff --git a/storybook/storybook/src/packages/table/src/Pager/Pager.stories.js b/storybook/storybook/src/packages/table/src/Pager/Pager.stories.js index 37f179b05..41a914a11 100644 --- a/storybook/storybook/src/packages/table/src/Pager/Pager.stories.js +++ b/storybook/storybook/src/packages/table/src/Pager/Pager.stories.js @@ -7,23 +7,29 @@ import readme from '@axa-fr/react-toolkit-table/src/Pager/README.md'; const PagerStory = () => ( ); const LightStory = () => ( );