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

PLASMA-4117: PaginationDots api & example #1714

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
import React from 'react';
import cls from 'classnames';
import { styled } from '@linaria/react';

interface Props {
size?: 's' | 'm';
/**
* Отображение активного элемента
* @default 'dot'
*/
activeElementView?: 'dot' | 'line';
/**
* Ориентация элементов
* @default `horizontal`
*/
orientation?: 'horizontal' | 'vertical';
/**
* Растягивается на всю доступную ширину
* @default true
*/
stretch?: boolean;
/**
* Выбранная страница
*/
value?: number;
/**
* Количество страниц
*/
count?: number;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. А у пользователя не должно быть возможности кликать на эти точки чтобы изменить активное состояние?
  2. А если будет условно 90 элементов, тогда все 90 точек будем выводить? Для такого подхода не должен быть предусмотрен механизм "сжатия" или это на стороне пользователя?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ещё кажется, что не хватает такого свойства

image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ещё кажется, что не хватает такого свойства

это для сторибука

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

про onClick спрошу у дизайна

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

onClick опционально будет


const Dot = styled.div`
padding: 0 0.375rem;

&:before {
content: '';
display: block;
border-radius: 50%;

background-color: #0808081f;
}

&.active {
&:before {
background-color: #080808;
}
}

&[data-size='xs'] {
&:before {
width: 0.375rem;
height: 0.375rem;
}
}

&[data-size='s'] {
&:before {
width: 0.5rem;
height: 0.5rem;
}
}

&[data-size='m'] {
&:before {
width: 0.75rem;
height: 0.75rem;
}
}
`;

const Line = styled(Dot)`
&[data-size='s'] {
&:before {
width: 2rem;
height: 0.5rem;
border-radius: 0.375rem;
}
}

&[data-size='m'] {
&:before {
width: 3rem;
height: 0.75rem;
border-radius: 0.375rem;
}
}

&.vertical {
&[data-size='s'] {
&:before {
height: 2rem;
width: 0.5rem;
}
}

&[data-size='m'] {
&:before {
height: 3rem;
width: 0.75rem;
}
}
}
`;

const Wrapper = styled.div`
display: flex;
align-items: center;

&.stretch {
width: 100%;
justify-content: space-between;
}

&.vertical {
flex-direction: column;

${Dot} {
padding: 0.375rem 0;
}

&.stretch {
width: auto;
height: 100%;
}
}
`;

const PaginationDots: React.FC<Props> = ({
size = 's',
activeElementView = 'dot',
orientation = 'horizontal',
stretch = true,
value = 0,
count,
}) => {
const dots = new Array(count).fill(null);

const isVertical = orientation === 'vertical';

return (
<Wrapper className={cls({ size, stretch, vertical: isVertical })}>
{dots.map((_, index) => {
if (index === value) {
const ActiveElement = activeElementView === 'dot' ? Dot : Line;
return <ActiveElement key={index} data-size={size}
className={cls({ active: true, vertical: isVertical })} />;
}

const isCurrent = index === value;

const isFirst = !isCurrent && count > 1 && (index === 0 || index === count - 1);
const isSecond = !isCurrent && count > 2 && (index === 1 || index === count - 2);

const getSize = () => {
if (size === 's') {
return isFirst ? 'xs' : 's';
}

if (isFirst) {
return 'xs';
}

if (isSecond) {
return 's';
}

return 'm';
};

const dotSize = getSize();

return <Dot key={index} data-size={dotSize} />;
})}
</Wrapper>
);
};

export const App = () => {
return (
<div style={{ display: 'grid', gap: '1rem' }}>
<PaginationDots value={0} count={9} stretch={false} />
<PaginationDots value={1} count={9} stretch={false} />
<PaginationDots value={2} count={9} stretch={false} />
<PaginationDots value={3} count={9} stretch={false} />
<PaginationDots value={8} count={9} stretch={false} />
<PaginationDots value={0} count={9} stretch={false} activeElementView="line" />
<PaginationDots value={1} count={9} stretch={false} activeElementView="line" />
<PaginationDots value={2} count={9} stretch={false} activeElementView="line" />
<PaginationDots value={3} count={9} stretch={false} activeElementView="line" />
<PaginationDots value={8} count={9} stretch={false} activeElementView="line" />
<PaginationDots value={4} count={9} stretch={false} activeElementView="line" />
<PaginationDots value={4} count={9} stretch={false} size="m" />
<PaginationDots value={4} count={9} stretch={false} size="m" activeElementView="line" />
<PaginationDots value={4} count={9} stretch={true} />

<hr />
<div style={{ display: 'flex', height: '300px', gap: '1rem' }}>
<PaginationDots value={4} count={9} stretch={false} orientation="vertical" />
<PaginationDots value={4} count={9} stretch={true} orientation="vertical" />

<PaginationDots value={4} count={9} stretch={false} orientation="vertical" activeElementView="line" />
<PaginationDots value={4} count={9} stretch={true} orientation="vertical" activeElementView="line" />
</div>
</div>
);
};
Loading