Skip to content

Commit

Permalink
fixup! feat: refactor CardBase with new RouterLink
Browse files Browse the repository at this point in the history
  • Loading branch information
niktverd committed May 29, 2023
1 parent 788e380 commit 7a4fb6a
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 32 deletions.
16 changes: 7 additions & 9 deletions src/blocks/Header/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, {useContext} from 'react';

import {Button, HTML, Media, RouterLink} from '../../components';
import {Button, HTML, Media} from '../../components';
import HeaderBreadcrumbs from '../../components/HeaderBreadcrumbs/HeaderBreadcrumbs';
import {getMediaImage} from '../../components/Media/Image/utils';
import YFMWrapper from '../../components/YFMWrapper/YFMWrapper';
Expand Down Expand Up @@ -141,14 +141,12 @@ export const HeaderBlock = (props: WithChildren<HeaderBlockFullProps>) => {
<div className={b('buttons')} data-qa="header-buttons">
{buttons &&
buttons.map((button, index) => (
<RouterLink href={button.url} key={index}>
<Button
key={index}
className={b('button')}
size="xl"
{...button}
/>
</RouterLink>
<Button
key={index}
className={b('button')}
size="xl"
{...button}
/>
))}
</div>
)}
Expand Down
3 changes: 3 additions & 0 deletions src/components/Button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React, {useCallback, useContext} from 'react';
import {Button as CommonButton, Icon, Platform, StoreBadge} from '@gravity-ui/uikit';

import {LocaleContext} from '../../context/localeContext/localeContext';
import {LocationContext} from '../../context/locationContext';
import {useAnalytics} from '../../hooks';
import {useMetrika} from '../../hooks/useMetrika';
import {Github} from '../../icons';
Expand All @@ -25,6 +26,7 @@ const b = block('button-block');

const Button = (props: ButtonProps) => {
const handleMetrika = useMetrika();
const {Link} = useContext(LocationContext);
const {lang, tld} = useContext(LocaleContext);
const {
className,
Expand Down Expand Up @@ -95,6 +97,7 @@ const Button = (props: ButtonProps) => {
size={toCommonSize(size as OldButtonSize)}
href={url ? setUrlTld(url, tld) : undefined}
width={width}
component={Link}
{...buttonProps}
>
{icon && buttonImg.position === 'left' ? icon : null}
Expand Down
7 changes: 4 additions & 3 deletions src/components/RouterLink/RouterLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ const RouterLink = ({
}: WithChildren<RouterLinkProps>) => {
const {Link} = useContext(LocationContext);

const isExternal = href.startsWith('http://') || href.startsWith('https://');
const isExternal = href?.startsWith('http://') || href?.startsWith('https://');
const shouldAnchorTag = typeof href === 'string' && (!Link || isExternal || forceAnchor);
const component = shouldAnchorTag ? 'a' : Link;

if (!component) {
return null;
return <div>{JSON.stringify({isExternal, shouldAnchorTag, component})}</div>;
}

const linkProps = {
Expand All @@ -41,7 +41,8 @@ const RouterLink = ({
{
href,
...(shouldAnchorTag ? linkProps : {}),
commonProps,
...commonProps,
area: shouldAnchorTag ? '1' : '2',
},
children,
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';

import {Button, RouterLink} from '../../../../../components';
import {Button} from '../../../../../components';
import {BlockIdContext} from '../../../../../context/blockIdContext';
import {ButtonProps} from '../../../../../models';
import {block} from '../../../../../utils';
Expand All @@ -15,17 +15,11 @@ const ANALYTICS_ID = 'navigation';
type NavigationButtonProps = Pick<NavigationItemProps, 'className'> & ButtonProps;

export const NavigationButton: React.FC<NavigationButtonProps> = (props) => {
const {url, target, className} = props;
const {url, className} = props;
const classes = b(null, className);
return (
<BlockIdContext.Provider value={ANALYTICS_ID}>
{target ? (
<Button className={classes} {...props} url={url} />
) : (
<RouterLink href={url}>
<Button {...props} className={classes} url={url} />
</RouterLink>
)}
<Button {...props} className={classes} url={url} />
</BlockIdContext.Provider>
);
};
20 changes: 9 additions & 11 deletions src/sub-blocks/BannerCard/BannerCard.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, {useContext} from 'react';

import {BackgroundImage, Button, HTML, RouterLink, YFMWrapper} from '../../components';
import {BackgroundImage, Button, HTML, YFMWrapper} from '../../components';
import {ThemeValueContext} from '../../context/theme/ThemeValueContext';
import {BannerCardProps} from '../../models';
import {block, getThemedValue} from '../../utils';
Expand Down Expand Up @@ -42,16 +42,14 @@ export const BannerCard = (props: BannerCardProps) => {
/>
)}
</div>
<RouterLink href={url}>
<Button
className={b('button')}
theme="raised"
size="xl"
text={text}
url={url}
target={target}
/>
</RouterLink>
<Button
className={b('button')}
theme="raised"
size="xl"
text={text}
url={url}
target={target}
/>
</div>
<BackgroundImage
className={b('image')}
Expand Down

0 comments on commit 7a4fb6a

Please sign in to comment.