Skip to content

Commit

Permalink
allow setting chevron optionally in buttonLink
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoskolodny committed Jul 6, 2023
1 parent 65952ce commit fb1a9bf
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 18 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 29 additions & 12 deletions src/__screenshot_tests__/button-screenshot-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,34 @@ test.each(DEVICES)('Buttons - ellipsis (%s)', async (device) => {
expect(image).toMatchImageSnapshot();
});

test('Buttons - Link button - href and no chevron', async () => {
await openStoryPage({
id: 'components-buttons--link-button',
device: 'MOBILE_IOS',
args: {
noChevron: true,
},
});
const BUTTON_LINK_ACTIONS = ['href', 'to', 'onPress'];
const BUTTON_LINK_CHEVRON_OPTIONS = ['default', 'true', 'false'];

const story = await screen.findByTestId('content');
const getLinkWithChevronCases = () => {
const cases = [];
for (const action of BUTTON_LINK_ACTIONS) {
for (const withChevron of BUTTON_LINK_CHEVRON_OPTIONS) {
cases.push([action, withChevron]);
}
}
return cases;
};

const image = await story.screenshot();
expect(image).toMatchImageSnapshot();
});
test.each(getLinkWithChevronCases())(
'Buttons - Link button - %s (chevron = %s)',
async (action, withChevron) => {
await openStoryPage({
id: 'components-buttons--link-button',
device: 'MOBILE_IOS',
args: {
action,
withChevron,
},
});

const story = await screen.findByTestId('content');

const image = await story.screenshot();
expect(image).toMatchImageSnapshot();
}
);
11 changes: 8 additions & 3 deletions src/__stories__/button-story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,18 +126,20 @@ export const DangerButton: StoryComponent<Args> = ({isInverse, text, icon, actio
);
};

export const LinkButton: StoryComponent<Omit<Args, 'small'> & {noChevron: boolean}> = ({
export const LinkButton: StoryComponent<Omit<Args, 'small'> & {withChevron: string}> = ({
isInverse,
text,
icon,
action,
newTab,
withChevron,
...props
}) => {
return (
<ButtonBackgroundContainer isInverse={isInverse}>
<ButtonLink
{...props}
withChevron={withChevron === 'default' ? undefined : withChevron === 'true'}
{...getButtonActionProps(action, newTab)}
StartIcon={icon === 'left' ? IconPhotoCameraRegular : undefined}
EndIcon={icon === 'right' ? IconPhotoCameraRegular : undefined}
Expand Down Expand Up @@ -176,13 +178,16 @@ SecondaryButton.args = defaultArgs;
DangerButton.args = defaultArgs;
LinkButton.args = {
...(({small, ...o}) => o)(defaultArgs),
noChevron: false,
withChevron: 'default',
};

primaryButton.argTypes = defaultArgTypes;
SecondaryButton.argTypes = defaultArgTypes;
DangerButton.argTypes = defaultArgTypes;
LinkButton.argTypes = {
...defaultArgTypes,
noChevron: {if: {arg: 'action', neq: 'onPress'}},
withChevron: {
options: ['default', 'true', 'false'],
control: {type: 'select'},
},
};
6 changes: 3 additions & 3 deletions src/button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@ interface ButtonLinkCommonProps {
loadingText?: string;
StartIcon?: React.FC<IconProps>;
EndIcon?: React.FC<IconProps>;
withChevron?: boolean;
}
interface ButtonLinkOnPressProps extends ButtonLinkCommonProps {
onPress: (event: React.MouseEvent<HTMLElement>) => void | undefined | Promise<void>;
Expand All @@ -452,14 +453,12 @@ interface ButtonLinkHrefProps extends ButtonLinkCommonProps {
newTab?: boolean;
onPress?: undefined;
to?: undefined;
noChevron?: boolean;
}
interface ButtonLinkToProps extends ButtonLinkCommonProps {
to: string;
fullPageOnWebView?: boolean;
onPress?: undefined;
href?: undefined;
noChevron?: boolean;
}

export type ButtonLinkProps = ButtonLinkOnPressProps | ButtonLinkHrefProps | ButtonLinkToProps;
Expand All @@ -474,6 +473,7 @@ export const ButtonLink = React.forwardRef<TouchableElement, ButtonLinkProps>((p
const [isOnPressPromiseResolving, setIsOnPressPromiseResolving] = React.useState(false);

const showSpinner = props.showSpinner || isOnPressPromiseResolving;
const showChevron = props.withChevron ?? (!!props.href || !!props.to);

// This state is needed to not render the spinner when hidden (because it causes high CPU usage
// specially in iPhone). But we want the spinner to be visible during the show/hide animation.
Expand Down Expand Up @@ -528,7 +528,7 @@ export const ButtonLink = React.forwardRef<TouchableElement, ButtonLinkProps>((p
textContentStyle: styles.textContentLink,
StartIcon: props.StartIcon,
EndIcon: props.EndIcon,
withChevron: (!!props.to || !!props.href) && !props.noChevron,
withChevron: showChevron,
}),
disabled: props.disabled || showSpinner || isFormSending,
};
Expand Down

0 comments on commit fb1a9bf

Please sign in to comment.