Skip to content
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

Support for typescript #31

Closed
BenevidesLecontes opened this issue May 6, 2019 · 5 comments · Fixed by #70
Closed

Support for typescript #31

BenevidesLecontes opened this issue May 6, 2019 · 5 comments · Fixed by #70

Comments

@BenevidesLecontes
Copy link

Hi, thank you for your work. Can you provide types for typescript?

@dmk99
Copy link

dmk99 commented Jun 25, 2019

Hi,

If still relevant you can use the following type declaration in your code. I have it under src/@types/react-step-wizard.

declare module 'react-step-wizard' {
    import * as React from "react";

    interface StepWizardProps {
        className: string;

        hashKey: string;
        initialStep: number;
        instance: (wizard: StepWizardProps) => void;
        isHashEnabled: boolean;
        isLazyMount: boolean;
        nav: JSX.Element;

        onStepChange: ({
                            previousStep: number,
                            activeStep: number
                        }) => void;

        transitions: {
            enterRight?: string,
            enterLeft?: string,
            exitRight?: string,
            exitLeft?: string
        }
    }

    export interface StepWizardChildProps {
        isActive: boolean;
        currentStep: number;
        totalSteps: number;
        firstStep: () => void;
        lastStep: () => void;
        nextStep: () => void;
        previousStep: () => void;
        goToStep: (step: number) => void;
    }

    export default class StepWizard extends React.PureComponent<Partial<StepWizardProps>>{};
}

Then in an example. Define the wizard:

<StepWizard>
      <Step1/>
      <div>Another Step</div>
</StepWizard>

And the Step1 component would look something like:

import {StepWizardChildProps} from "react-step-wizard";
import * as React from "react";

export default class Step1 extends React.PureComponent<Partial<StepWizardChildProps>> {
    render() {
        return (
            <div>
                {this.props.currentStep} / {this.props.totalSteps}
            </div>
        )
    }
}

@JGrzybowski
Copy link

@dmk99 maybe you can just create a pull request for that?

@spencerpauly
Copy link

Would love official TS support!

@LukasDeco
Copy link

LukasDeco commented Apr 29, 2020

Also really voting for official TS support!

Additionally, with the provided solution, it looks like there is some issues when you try to call one of the child prop functions. Since wrapping the child props in Partial means they might be undefined, you have to call functions like this:

if(goToStep) {
    goToStep(1)
}

Anyone know a solution for this? How can the child components be properly typed with these functions?

@RadekGluchowski
Copy link

RadekGluchowski commented Sep 14, 2021

Function component example:

import { StepWizardChildProps } from "react-step-wizard";
import * as React from "react";

export const StepOne: React.FC<Partial<StepWizardChildProps>> = ({ ...props }) => {
    return (
        <div>
            <h2>Step {props.currentStep}</h2>
            <p><button onClick={props.nextStep}>Next Step</button></p>
        </div>
    )
}

Or u can destructure props

import { StepWizardChildProps } from "react-step-wizard";
import * as React from "react";

export const StepOne: React.FC<Partial<StepWizardChildProps>> = ({ currentStep, nextStep }) => {
    return (
        <div>
            <h2>Step {currentStep}</h2>
            <p><button onClick={nextStep}>Next Step</button></p>
        </div>
    )
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants