Skip to content

[PANGOO-2945][BpkPageIndicator]Bpk component page indicator ts support #3783

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

Merged
merged 25 commits into from
Apr 7, 2025
Merged
Show file tree
Hide file tree
Changes from 12 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
28 changes: 28 additions & 0 deletions @types/jest-axe.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Backpack - Skyscanner's Design System
*
* Copyright 2022 Skyscanner Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

declare module 'jest-axe' {
import { AxeResults, RunOptions } from 'axe-core';

export function axe(
html: string | HTMLElement,
options?: RunOptions,
): Promise<AxeResults>;

export function toHaveNoViolations(): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ const image =
'https://content.skyscnr.com/m/50d9dff3186775ad/original/Condor-Homepage-Hero-Option-3.png';
const imageWidth = 400;
const imageHeight = 90;
const PageIndicatorContainer = (props) => {
type Variant = typeof VARIANT[keyof typeof VARIANT];
const PageIndicatorContainer = (props: { totalIndicators: number; showNav?: boolean; variant?: Variant; className?: string}) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
type Variant = typeof VARIANT[keyof typeof VARIANT];
const PageIndicatorContainer = (props: { totalIndicators: number; showNav?: boolean; variant?: Variant; className?: string}) => {
type PageIndicatorContainerProps = Omit<BpkPageIndicatorProps, 'currentIndex'>;
const PageIndicatorContainer = (props: PageIndicatorContainerProps) => {

const [currentIndex, setCurrentIndex] = useState(0);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@ export const ThreePages = ThreePagesExample;
export const OverImage = OverImageExample;
export const WithNav = WithNavExample;
export const VisualTest = VisualTestExample;
export const VisualTestWithZoom = VisualTest.bind({});
VisualTestWithZoom.args = {
zoomEnabled: true
};
// export const VisualTestWithZoom = VisualTest.bind({});
// VisualTestWithZoom.args = {
// zoomEnabled: true
// };
export const VisualTestWithZoom = {
render: VisualTest,
args: {
zoomEnabled: true,
}
};
1 change: 0 additions & 1 deletion packages/bpk-component-carousel/src/BpkCarousel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import { useRef, useState } from 'react';

// @ts-expect-error Untyped import. See `decisions/imports-ts-suppressions.md`.
import BpkPageIndicator, { VARIANT } from '../../bpk-component-page-indicator';
import { cssModules } from '../../bpk-react-utils';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
/* @flow strict */

import BpkPageIndicator, {
type Props as BpkPageIndicatorProps,
VARIANT,
} from './src/BpkPageIndicator';
import { DIRECTIONS } from './src/NavButton';

import type {
Props as BpkPageIndicatorProps} from './src/BpkPageIndicator';

export type { BpkPageIndicatorProps };
export { DIRECTIONS, VARIANT };
export default BpkPageIndicator;
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,18 @@ import '@testing-library/jest-dom';

import BpkPageIndicator from './BpkPageIndicator';

let props;

describe('BpkPageIndicator', () => {
let props: {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it would be better if we reuse the type defined in packages/bpk-component-carousel/src/BpkCarousel.tsx

currentIndex: number;
totalIndicators: number;
indicatorLabel?: string;
prevNavLabel?: string;
nextNavLabel?: string;
onClick?: jest.Mock;
className?: string;
showNav?: boolean;
};

beforeEach(() => {
props = {
currentIndex: 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
*/
/* @flow strict */

import PropTypes from 'prop-types';

import { cssModules } from '../../bpk-react-utils';

import NavButton, { DIRECTIONS } from './NavButton';
Expand All @@ -33,30 +31,37 @@ const START_SCROLL_INDEX = Math.floor(DISPLAYED_TOTAL / 2);
export const VARIANT = {
default: 'default',
overImage: 'overImage',
};
} as const;

export type Props = {
className: ?string,
showNav: ?boolean,
type Variant = typeof VARIANT[keyof typeof VARIANT];
type Direction = typeof DIRECTIONS[keyof typeof DIRECTIONS];

export interface Props {
indicatorLabel?: string,
prevNavLabel?: string,
nextNavLabel?: string,
currentIndex: number,
totalIndicators: number,
onClick: ?() => void,
indicatorLabel: string,
prevNavLabel: string,
nextNavLabel: string,
variant: ?$Keys<typeof VARIANT>,
};
variant?: Variant,
onClick?: (
event: React.MouseEvent<HTMLButtonElement>,
newIndex: number,
direction: Direction,
) => void,
className?: string,
showNav?: boolean,
}

const BpkPageIndicator = ({
className,
const BpkPageIndicator: React.FC<Props> = ({
className = undefined,
currentIndex,
indicatorLabel,
nextNavLabel,
onClick,
onClick = () => {},
prevNavLabel,
showNav,
showNav = false,
totalIndicators,
variant,
variant = VARIANT.default,
}: Props) => {
/**
* This validation is used to avoid an a11y issue when onClick isn't available.
Expand Down Expand Up @@ -93,14 +98,14 @@ const BpkPageIndicator = ({
style={
currentIndex > START_SCROLL_INDEX
? {
'--scroll-index':
totalIndicators > DISPLAYED_TOTAL
? Math.min(
currentIndex - START_SCROLL_INDEX,
totalIndicators - DISPLAYED_TOTAL,
)
: 0,
}
'--scroll-index':
totalIndicators > DISPLAYED_TOTAL
? Math.min(
currentIndex - START_SCROLL_INDEX,
totalIndicators - DISPLAYED_TOTAL,
)
: 0,
} as React.CSSProperties
Copy link
Contributor

Choose a reason for hiding this comment

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

As I remember in banana, we are not recommending to use type assertion, therefore, i would prefer

import type { CSSProperties } from 'react';
...
type CustomCSSProperties = CSSProperties & {
  '--scroll-index'?: number;
}
  const customStyle: CustomCSSProperties = {
    '--scroll-index':
      totalIndicators > DISPLAYED_TOTAL
        ? Math.min(
          currentIndex - START_SCROLL_INDEX,
          totalIndicators - DISPLAYED_TOTAL,
        )
        : 0,
  }

      <div
        className={getClassName('bpk-page-indicator__indicators-container')}
        style={
          currentIndex > START_SCROLL_INDEX
            ? customStyle
            : undefined
        }
      >
...


: undefined
}
>
Expand Down Expand Up @@ -149,23 +154,4 @@ const BpkPageIndicator = ({
)
};

BpkPageIndicator.propTypes = {
indicatorLabel: PropTypes.string,
prevNavLabel: PropTypes.string,
nextNavLabel: PropTypes.string,
currentIndex: PropTypes.number.isRequired,
totalIndicators: PropTypes.number.isRequired,
variant: PropTypes.oneOf(Object.keys(VARIANT)),
onClick: PropTypes.func,
className: PropTypes.string,
showNav: PropTypes.bool,
};

BpkPageIndicator.defaultProps = {
onClick: null,
className: null,
showNav: false,
variant: VARIANT.default,
};

export default BpkPageIndicator;
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
*/
/* @flow strict */

import PropTypes from 'prop-types';

import {BUTTON_TYPES, BpkButtonV2} from '../../bpk-component-button';
import { withButtonAlignment, withRtlSupport } from '../../bpk-component-icon';
import LeftArrowIcon from '../../bpk-component-icon/lg/chevron-left';
Expand All @@ -28,24 +26,34 @@ export const DIRECTIONS = {
PREV: 'PREV',
INDICATORS: 'INDICATORS',
NEXT: 'NEXT',
};
} as const;

type Props = {
direction: $Keys<typeof DIRECTIONS>,
disabled?: boolean,
type Direction = typeof DIRECTIONS[keyof typeof DIRECTIONS];

interface Props {
ariaLabel: string | undefined,
currentIndex: number,
onClick: ?() => void,
ariaLabel: string,
direction: Direction,
disabled?: boolean,
onClick?: (
event: React.MouseEvent<HTMLButtonElement>,
newIndex: number,
direction: Direction,
) => void,
};

const AlignedLeftArrowIcon = withButtonAlignment(withRtlSupport(LeftArrowIcon));
const AlignedRightArrowIcon = withButtonAlignment(
withRtlSupport(RightArrowIcon),
);

const NavButton = (props: Props) => {
const { ariaLabel, currentIndex, direction, disabled, onClick } = props;
return (
const NavButton: React.FC<Props> = ({
ariaLabel,
currentIndex,
direction,
disabled = false,
onClick = () => {},
}: Props) => (
<BpkButtonV2
iconOnly
type={BUTTON_TYPES.link}
Expand All @@ -66,19 +74,5 @@ const NavButton = (props: Props) => {
)}
</BpkButtonV2>
);
};

NavButton.propTypes = {
direction: PropTypes.oneOf(Object.keys(DIRECTIONS)).isRequired,
disabled: PropTypes.bool,
ariaLabel: PropTypes.string.isRequired,
currentIndex: PropTypes.number.isRequired,
onClick: PropTypes.func,
};

NavButton.defaultProps = {
disabled: false,
onClick: null,
};

export default NavButton;
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"dom.iterable",
"esnext"
],
"typeRoots": ["./node_modules/@types", "./types"],
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
Expand Down
Loading