Skip to content

Commit

Permalink
fix: add settings context to add a tag for pagination (#47)
Browse files Browse the repository at this point in the history
* fix: add settings context to add <a> tag in paginationItem

* fix: add settings context to add <a> tag in paginationItem

* fix: add settings context to add <a> tag in paginationItem

* add readme about SettingsContext and change <> to <Fragment>
  • Loading branch information
VitaliiDC8 authored May 16, 2023
1 parent 4d1533c commit 0856675
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 10 deletions.
1 change: 0 additions & 1 deletion src/components/Paginator/Paginator.scss
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ $itemDimensions: 44px;

&__link {
text-decoration: none;
pointer-events: none;
color: var(--yc-color-text-primary);
}

Expand Down
30 changes: 21 additions & 9 deletions src/components/Paginator/components/PaginatorItem.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React, {useContext} from 'react';
import React, {Fragment, useContext} from 'react';

import {Button} from '@gravity-ui/uikit';

import {LocaleContext} from '../../../contexts/LocaleContext';
import {SettingsContext} from '../../../contexts/SettingsContext';
import {block} from '../../../utils/cn';
import {getBlogPath} from '../../../utils/common';
import {ArrowType, PaginatorItemProps} from '../types';
Expand All @@ -20,27 +21,38 @@ export const PaginatorItem = ({
index,
}: PaginatorItemProps) => {
const {locale} = useContext(LocaleContext);
const {addNavigationLinkForPages} = useContext(SettingsContext);
const urlPath = getBlogPath(locale?.pathPrefix || '');

const itemKey = Number(dataKey) > 0 ? Number(dataKey) : (dataKey as ArrowType);
const navTag = index > 1 ? `?page=${index}` : '';
const navigationLink = `${urlPath || ''}${navTag}`;

return (
const renderButton = (
<Button
view="flat"
size="xl"
className={b('item', mods)}
onClick={() => onClick?.(itemKey)}
loading={loading && Boolean(mods.active)}
>
<a
href={navigationLink}
className={b('link')}
onClick={(event) => event.preventDefault()}
>
{content}
</a>
{content}
</Button>
);

return (
<Fragment>
{addNavigationLinkForPages ? (
<a
href={navigationLink}
className={b('link')}
onClick={(event) => event.preventDefault()}
>
{renderButton}
</a>
) : (
<Fragment>{renderButton}</Fragment>
)}
</Fragment>
);
};
4 changes: 4 additions & 0 deletions src/constructor/BlogConstructorProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {DeviceContext, DeviceContextProps} from '../contexts/DeviceContext';
import {LocaleContext} from '../contexts/LocaleContext';
import {MobileContext} from '../contexts/MobileContext';
import {RouterContext, RouterContextProps} from '../contexts/RouterContext';
import {SettingsContext, SettingsContextProps} from '../contexts/SettingsContext';
import {ThemeValueContext, ThemeValueType} from '../contexts/theme/ThemeValueContext';
import {Locale} from '../models/locale';

Expand All @@ -17,6 +18,7 @@ export interface BlogConstructorProviderProps {
theme?: ThemeValueType;
device?: DeviceContextProps;
analytics?: AnalyticsContextProps;
settings?: SettingsContextProps;
children?: React.ReactNode;
}

Expand All @@ -27,6 +29,7 @@ export const BlogConstructorProvider: React.FC<BlogConstructorProviderProps> = (
theme = DEFAULT_THEME,
device = {},
analytics = {},
settings = {},
children,
}) => {
const context = [
Expand All @@ -35,6 +38,7 @@ export const BlogConstructorProvider: React.FC<BlogConstructorProviderProps> = (
<RouterContext.Provider value={router} key="router-context" />,
<MobileContext.Provider value={Boolean(isMobile)} key="is-mobile-context" />,
<DeviceContext.Provider value={device} key="device-context" />,
<SettingsContext.Provider value={settings} key="settings-context" />,
<AnalyticsContext.Provider value={analytics} key="analytics-context" />,
].reduceRight((prev, provider) => React.cloneElement(provider, {}, prev), children);

Expand Down
11 changes: 11 additions & 0 deletions src/constructor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ interface BlogConstructorProviderProps {
theme?: ThemeValueType;
device?: DeviceContextProps;
analytics?: AnalyticsContextProps;
settings?: SettingsContextProps;
children?: React.ReactNode;
}
```
Expand Down Expand Up @@ -77,4 +78,14 @@ interface AnalyticsContextProps {

**!!! Important thing** - We throw analytics settings in blog constructor provide, if we need analytics from only-blog components. If we need analytics in page-constructor blocks we need to throw analytics settings in [page settings props](../containers/BlogPage/README.md)

### `Settings` - blog settings

```jsx
interface SettingsContextProps {
addNavigationLinkForPages?: boolean;
}
```

**!!! Most important thing** - `addNavigationLinkForPages` is option to cover pagination buttons with a `<a>` tag and add a `href` link for page navigation

### `Children` - children react component
7 changes: 7 additions & 0 deletions src/contexts/SettingsContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React from 'react';

export interface SettingsContextProps {
addNavigationLinkForPages?: boolean;
}

export const SettingsContext = React.createContext<SettingsContextProps>({});

0 comments on commit 0856675

Please sign in to comment.