Skip to content

feat(pf4): introduce StepTemplate #648

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 3 commits into from
Jul 14, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion packages/pf4-component-mapper/demo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const fieldArrayState = { schema: arraySchemaDDF, additionalOptions: {
class App extends React.Component {
constructor(props) {
super(props);
this.state = {schema: selectSchema, additionalOptions: {}}
this.state = {schema: wizardSchema, additionalOptions: {}}
}

render() {
Expand Down
12 changes: 12 additions & 0 deletions packages/pf4-component-mapper/src/files/wizard.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ export interface SubstepOfObject {
title?: ReactNode;
}

export interface StepTemplateProps {
title?: ReactNode;
formFields: ReactNode;
showTitles?: boolean;
showTitle?: boolean;
Copy link
Member

Choose a reason for hiding this comment

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

What is the difference between showTitle and showTitles? Maybe we could use different names?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ShowTitles is on wizard, ShowTitle is on step

Copy link
Member

Choose a reason for hiding this comment

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

Maybe rename the showTitle to something like show header? The difference is so tiny that I had to read it 3 times to notice the plural.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It doesn't matter. showTitles is set in wizard definition. showTitle in wizard step.

Renaming it would be a breaking change.

Copy link
Member

Choose a reason for hiding this comment

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

Ah. Well, that is a bummer. I was not aware it was used already. Nothing we can to at this point.

customTitle?: ReactNode;
fields: Field[];
}

export interface WizardField {
name: string | number;
fields: Field[];
Expand All @@ -62,6 +71,8 @@ export interface WizardField {
customTitle?: ReactNode;
disableForwardJumping?: boolean;
buttons?: ReactNode | React.ComponentType<WizardButtonsProps>;
StepTemplate?: React.ComponentType<StepTemplateProps>;
hasNoBodyPadding?: boolean;
}

export interface WizardProps {
Expand All @@ -80,6 +91,7 @@ export interface WizardProps {
closeButtonAriaLabel?: string;
hasNoBodyPadding?: boolean;
navAriaLabel?: string;
StepTemplate?: React.ComponentType<StepTemplateProps>;
}

declare const Wizard: React.ComponentType<WizardProps>;
Expand Down
11 changes: 7 additions & 4 deletions packages/pf4-component-mapper/src/files/wizard.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const WizardInternal = ({
closeButtonAriaLabel,
hasNoBodyPadding,
navAriaLabel,
StepTemplate,
...rest
}) => {
const {
Expand Down Expand Up @@ -122,15 +123,16 @@ const WizardInternal = ({
</FormSpy>
</WizardNav>
<WizardStep
{...currentStep}
formOptions={formOptions}
buttonLabels={buttonLabels}
buttonsClassName={buttonsClassName}
showTitles={showTitles}
hasNoBodyPadding={hasNoBodyPadding}
StepTemplate={StepTemplate}
{...currentStep}
formOptions={formOptions}
handleNext={(nextStep) => handleNext(nextStep)}
handlePrev={handlePrev}
disableBack={activeStepIndex === 0}
hasNoBodyPadding={hasNoBodyPadding}
/>
</div>
</div>
Expand All @@ -157,7 +159,8 @@ WizardInternal.propTypes = {
closeButtonAriaLabel: PropTypes.string,
hasNoBodyPadding: PropTypes.bool,
navAriaLabel: PropTypes.string,
container: PropTypes.instanceOf(Element)
container: PropTypes.instanceOf(Element),
StepTemplate: PropTypes.elementType
};

const defaultLabels = {
Expand Down
60 changes: 53 additions & 7 deletions packages/pf4-component-mapper/src/files/wizard/wizard-step.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,65 @@ RenderTitle.propTypes = {
customTitle: PropTypes.node
};

const WizardStep = ({ name, title, description, fields, formOptions, showTitles, showTitle, customTitle, hasNoBodyPadding, ...rest }) => {
const DefaultStepTemplate = ({ formFields, formRef, title, customTitle, showTitle, showTitles }) => (
<div ref={formRef} className="pf-c-form">
{((showTitles && showTitle !== false) || showTitle) && <RenderTitle title={title} customTitle={customTitle} />}
{formFields}
</div>
);

DefaultStepTemplate.propTypes = {
title: PropTypes.node,
formFields: PropTypes.array.isRequired,
formOptions: PropTypes.shape({
renderForm: PropTypes.func.isRequired
}).isRequired,
showTitles: PropTypes.bool,
showTitle: PropTypes.bool,
customTitle: PropTypes.node,
formRef: PropTypes.oneOfType([PropTypes.func, PropTypes.shape({ current: PropTypes.instanceOf(Element) })])
};

const WizardStep = ({
name,
title,
description,
fields,
formOptions,
showTitles,
showTitle,
customTitle,
hasNoBodyPadding,
StepTemplate,
...rest
}) => {
const formRef = useRef();

useEffect(() => {
// HACK: I can not pass ref to WizardBody because it is not
// wrapped by forwardRef. However, the step body (the one that overflows)
// is the grand parent of the form element.
const stepBody = formRef.current && formRef.current.parentNode.parentNode;
stepBody.scrollTo({ top: 0, left: 0, behavior: 'smooth' });
stepBody && stepBody.scrollTo({ top: 0, left: 0, behavior: 'smooth' });
}, [name]);

return (
<Fragment>
<WizardBody hasNoBodyPadding={hasNoBodyPadding}>
<div ref={formRef} className="pf-c-form">
{((showTitles && showTitle !== false) || showTitle) && <RenderTitle title={title} customTitle={customTitle} />}
{fields.map((item) => formOptions.renderForm([item], formOptions))}
</div>
<StepTemplate
formFields={fields.map((item) => formOptions.renderForm([item], formOptions))}
name={name}
title={title}
description={description}
formOptions={formOptions}
showTitles={showTitles}
showTitle={showTitle}
customTitle={customTitle}
hasNoBodyPadding={hasNoBodyPadding}
formRef={formRef}
fields={fields}
{...rest}
/>
</WizardBody>
<WizardStepButtons formOptions={formOptions} {...rest} />
</Fragment>
Expand All @@ -52,7 +93,12 @@ WizardStep.propTypes = {
showTitle: PropTypes.bool,
customTitle: PropTypes.node,
name: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
hasNoBodyPadding: PropTypes.bool
hasNoBodyPadding: PropTypes.bool,
StepTemplate: PropTypes.elementType
};

WizardStep.defaultProps = {
StepTemplate: DefaultStepTemplate
};

export default WizardStep;
Loading