-
Notifications
You must be signed in to change notification settings - Fork 98
Tony.busa/stepper convert to tsx #4137
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
base: release-23.x
Are you sure you want to change the base?
Changes from all commits
9d0e76d
9e7dc01
dd89f18
7889f2e
e4efd63
0f1c3ee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,31 @@ | ||
| import React, { useContext } from 'react'; | ||
| import PropTypes from 'prop-types'; | ||
| import { StepperContext } from './StepperContext'; | ||
| import ActionRow from '../ActionRow'; | ||
|
|
||
| export interface StepperActionRowProps { | ||
| /** Specifies the content of the `ActionRow`. */ | ||
| children: React.ReactNode; | ||
| /** | ||
| * An identifier of the `ActionRow`. When `activeKey` on the | ||
| * `Stepper` equals to the `eventKey`, the `ActionRow` will be displayed. | ||
| */ | ||
| eventKey: string; | ||
| /** Specifies the base element */ | ||
| as?: React.ElementType; | ||
| [key: string]: any; // For additional props passed through | ||
|
Comment on lines
+14
to
+15
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This came up in the |
||
| } | ||
|
|
||
| interface StepperActionRowComponent { | ||
| (props: StepperActionRowProps): JSX.Element | null; | ||
| Spacer: typeof ActionRow.Spacer; | ||
| } | ||
|
|
||
| function StepperActionRow({ | ||
| as, | ||
| as = ActionRow, | ||
| children, | ||
| eventKey, | ||
| ...props | ||
| }) { | ||
| }: StepperActionRowProps) { | ||
| const { activeKey } = useContext(StepperContext); | ||
| const isActive = activeKey === eventKey; | ||
|
|
||
|
|
@@ -19,22 +36,6 @@ function StepperActionRow({ | |
| return React.createElement(as, props, children); | ||
| } | ||
|
|
||
| StepperActionRow.propTypes = { | ||
| /** Specifies the content of the `ActionRow`. */ | ||
| children: PropTypes.node.isRequired, | ||
| /** | ||
| * An identifier of the `ActionRow`. When `activeKey` on the | ||
| * `Stepper` equals to the `eventKey`, the `ActionRow` will be displayed. | ||
| */ | ||
| eventKey: PropTypes.string.isRequired, | ||
| /** Specifies the base element */ | ||
| as: PropTypes.elementType, | ||
| }; | ||
|
|
||
| StepperActionRow.defaultProps = { | ||
| as: ActionRow, | ||
| }; | ||
|
|
||
| StepperActionRow.Spacer = ActionRow.Spacer; | ||
| (StepperActionRow as StepperActionRowComponent).Spacer = ActionRow.Spacer; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same thing here as https://github.com/openedx/paragon/pull/4137/changes#r3174512641, following the pattern from const Stepper: StepperComponent = function Stepper({ children, activeKey }: StepperProps) { |
||
|
|
||
| export default StepperActionRow; | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,50 +1,86 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import React, { useContext } from 'react'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import PropTypes from 'prop-types'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import classNames from 'classnames'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import StepperHeaderStep from './StepperHeaderStep'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { StepperContext } from './StepperContext'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { StepperContext, Step } from './StepperContext'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import useWindowSize from '../hooks/useWindowSizeHook'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import breakpoints, { Size } from '../utils/breakpoints'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function StepListSeparator() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return <li aria-hidden="true" className="pgn__stepper-header-line" />; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function StepList({ steps, activeKey }) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export interface StepListProps { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| steps: Step[]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| activeKey: string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function StepList({ steps, activeKey }: StepListProps) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <ul className="pgn__stepper-header-step-list"> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {steps.map(({ label, ...stepProps }, index) => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {steps.map(({ title: label, ...stepProps }, index) => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <React.Fragment key={stepProps.eventKey}> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {index !== 0 && <StepListSeparator />} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <StepperHeaderStep | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {...stepProps} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| title={label} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| index={index} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| isActive={activeKey === stepProps.eventKey} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| > | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {label} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </StepperHeaderStep> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </React.Fragment> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ))} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| </ul> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+17
to
33
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like With that in mind, we can simplify this to
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const PageCount = ({ activeStepIndex, totalSteps }) => `Step ${activeStepIndex + 1} of ${totalSteps}`; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export interface PageCountProps { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| activeStepIndex: number; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| totalSteps: number; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function PageCount({ activeStepIndex, totalSteps }: PageCountProps) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return <>Step {activeStepIndex + 1} of {totalSteps}</>; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export interface StepperHeaderProps { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** Specifies class name to append to the base element. */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| className?: string | null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** A component that receives `activeStepIndex` and `totalSteps` props to display them. */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| PageCountComponent?: React.ComponentType<PageCountProps>; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** The max width in which the compact view of the header will switch to display the step number that is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * currently in progress. Options include 'xs', 'sm', 'md', 'lg', 'xl', and 'xxl'. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| compactWidth?: 'xs' | 'sm' | 'md' | 'lg' | 'xl' | 'xxl'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| interface StepperHeaderComponent { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (props: StepperHeaderProps): JSX.Element | null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Step: typeof StepperHeaderStep; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function StepperHeader({ className, PageCountComponent, compactWidth }) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function StepperHeader({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| className = null, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| PageCountComponent = PageCount, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| compactWidth = 'sm', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }: StepperHeaderProps) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { steps, activeKey } = useContext(StepperContext); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const windowDimensions = useWindowSize(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const size = Size[compactWidth] || 'small'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const breakpointWidth = breakpoints[size].maxWidth || Infinity; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const isCompactView = windowDimensions.width < breakpointWidth; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const isCompactView = (windowDimensions.width ?? 0) < breakpointWidth; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is changing the behavior slightly. When We can replicate the original behavior with
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (isCompactView) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const activeStepIndex = steps.findIndex(step => step.eventKey === activeKey); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const activeStep = steps[activeStepIndex]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!activeStep) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <div className={classNames('pgn__stepper-header', className)}> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <StepperHeaderStep | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {...activeStep} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| title={activeStep.title} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not clear to me why we need to specify this here. We're spreading the |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| index={activeStepIndex} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| isActive | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -67,37 +103,6 @@ function StepperHeader({ className, PageCountComponent, compactWidth }) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| StepperHeader.propTypes = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** Specifies class name to append to the base element. */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| className: PropTypes.string, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** A component that receives `activeStepIndex` and `totalSteps` props to display them. */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| PageCountComponent: PropTypes.elementType, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** The max width in which the compact view of the header will switch to display the step number that is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * currently in progress. Options include 'xs', 'sm', 'md', 'lg', 'xl', and 'xxl'. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| compactWidth: PropTypes.oneOf(['xs', 'sm', 'md', 'lg', 'xl', 'xxl']), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| StepperHeader.defaultProps = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| className: null, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| PageCountComponent: PageCount, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| compactWidth: 'sm', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| StepList.propTypes = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| steps: PropTypes.arrayOf(PropTypes.shape({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| eventKey: PropTypes.string, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| title: PropTypes.string, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| description: PropTypes.string, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| hasError: PropTypes.bool, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| })), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| activeKey: PropTypes.string.isRequired, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| StepList.defaultProps = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| steps: [], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| StepperHeader.Step = StepperHeaderStep; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (StepperHeader as StepperHeaderComponent).Step = StepperHeaderStep; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This cast feels like it could cause some problems. In this PR's const Stepper: StepperComponent = function Stepper({ children, activeKey }: StepperProps) {it'd be great to follow that pattern here too. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export default StepperHeader; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume we can just revert these changes and things will still work, I know the package lock has been updated quite a bit since this PR was opened.