Skip to content
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

chore(pagination): refactor storybook #430

Merged
merged 3 commits into from
Mar 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions packages/pagination/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ npm install @zendeskgarden/container-pagination

## Usage

For live examples check out our [storybook](https://zendeskgarden.github.io/react-containers).
Check out [storybook](https://zendeskgarden.github.io/react-containers) for live
examples.

### usePagination

The `usePagination` hook is wrapper on top of the `useSelection` hook with
specific prop getters for pagination.

```jsx static
```jsx
import { createRef, useRef } from 'react';
import { usePagination } from '@zendeskgarden/container-pagination';

Expand Down Expand Up @@ -97,7 +98,7 @@ const Pagination = () => {

### PaginationContainer

```jsx static
```jsx
import { createRef, useRef } from 'react';
import { PaginationContainer } from '@zendeskgarden/container-pagination';

Expand Down
6 changes: 6 additions & 0 deletions packages/pagination/demo/#readme.stories.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Meta, Description } from '@storybook/addon-docs';
import README from '../README.md';

<Meta title="Packages/Pagination/README" />

<Description>{README}</Description>
44 changes: 44 additions & 0 deletions packages/pagination/demo/pagination.stories.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Meta, ArgsTable, Canvas, Story } from '@storybook/addon-docs';
import { useArgs } from '@storybook/client-api';
import { PaginationContainer } from '@zendeskgarden/container-pagination';
import { PaginationStory } from './stories/PaginationStory';

<Meta title="Packages/Pagination" component={PaginationContainer} />

# API

<ArgsTable />

# Demo

<Canvas>
<Story
name="Pagination"
args={{ as: 'hook', pages: 7, selectedItem: '0' }}
argTypes={{
as: { options: ['container', 'hook'], control: 'radio', table: { category: 'Story' } },
pages: { control: { type: 'range', min: 1, max: 10 }, table: { category: 'Story' } }
}}
>
{args => {
const updateArgs = useArgs()[1];
const handleSelect = selectedItem => {
if (selectedItem === 'next') {
const currentPage = parseInt(args['selectedItem']);
if (currentPage < args.pages - 1) {
updateArgs({ selectedItem: (currentPage + 1).toString() });
}
} else if (selectedItem === 'prev') {
const currentPage = parseInt(args['selectedItem']);
if (currentPage > 0) {
updateArgs({ selectedItem: (currentPage - 1).toString() });
}
} else {
updateArgs({ selectedItem });
}
};
const handleFocus = focusedItem => updateArgs({ focusedItem });
return <PaginationStory {...args} onSelect={handleSelect} onFocus={handleFocus} />;
}}
</Story>
</Canvas>
110 changes: 110 additions & 0 deletions packages/pagination/demo/stories/PaginationStory.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/**
* Copyright Zendesk, Inc.
*
* Use of this source code is governed under the Apache License, Version 2.0
* found at http://www.apache.org/licenses/LICENSE-2.0.
*/

import React, { createRef, RefObject, useRef } from 'react';
import { Story } from '@storybook/react';
import {
IPaginationContainerProps,
IUsePaginationReturnValue,
PaginationContainer,
usePagination
} from '@zendeskgarden/container-pagination';
import classNames from 'classnames';

interface IComponentProps extends IUsePaginationReturnValue<any> {
pages: Record<string, RefObject<any>>;
}

const Component = ({
pages,
getContainerProps,
getPreviousPageProps,
getPageProps,
getNextPageProps,
selectedItem
}: IComponentProps) => {
const previousRef = useRef();
const nextRef = useRef();
const className = 'border border-solid cursor-pointer px-3 py-1 select-none';
const keys = Object.keys(pages);

return (
<nav aria-label="pagination">
<ul className="flex" {...getContainerProps()}>
<li
className={classNames(className, { 'text-grey-400': selectedItem === keys[0] })}
{...getPreviousPageProps({ item: 'prev', focusRef: previousRef })}
>
&lt;
</li>
{keys.map((key, index) => {
const current = key === selectedItem;

return (
<li
key={key}
className={classNames(className, { 'bg-blue-300': current })}
{...getPageProps({
item: key,
focusRef: pages[key],
page: index + 1,
current
})}
>
{index + 1}
</li>
);
})}
<li
className={classNames(className, {
'text-grey-400': selectedItem === keys[keys.length - 1]
})}
{...getNextPageProps({ item: 'next', focusRef: nextRef })}
>
&gt;
</li>
</ul>
</nav>
);
};

interface IProps extends IPaginationContainerProps<any> {
pages: Record<string, RefObject<any>>;
}

const Container = ({ pages, ...props }: IProps) => (
<PaginationContainer {...props}>
{containerProps => <Component pages={pages} {...containerProps} />}
</PaginationContainer>
);

const Hook = ({ pages, ...props }: IProps) => {
const hookProps = usePagination(props);

return <Component pages={pages} {...hookProps} />;
};

interface IArgs extends IPaginationContainerProps<any> {
as: 'hook' | 'container';
pages: number;
}

export const PaginationStory: Story<IArgs> = ({ as, pages, ...props }) => {
const _pages = Array.from({ length: pages }, (_, index) => index).reduce(
(previous, current) => ({ ...previous, [current]: createRef() }),
{}
);

switch (as) {
case 'container':
return <Container pages={_pages} {...props} />;

case 'hook':
default:
return <Hook pages={_pages} {...props} />;
}
};
7 changes: 4 additions & 3 deletions packages/pagination/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
*/

/* Hooks */
export {
usePagination,
export { usePagination } from './usePagination';
export type {
IUsePaginationProps,
IUsePaginationReturnValue,
IGetContainerProps,
IGetPageProps
} from './usePagination';

/* Render-props */
export { PaginationContainer, IPaginationContainerProps } from './PaginationContainer';
export { PaginationContainer } from './PaginationContainer';
export type { IPaginationContainerProps } from './PaginationContainer';