Skip to content
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
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module.exports = {
rules: {
'simple-import-sort/imports': 'error',
'sort-imports': 'off',
'import/order': 'off'
'import/order': 'off',
'jsdoc/require-param-type': 'off',
}
};
1 change: 1 addition & 0 deletions global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare var __DEV__: boolean;
3 changes: 3 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
module.exports = {
// https://github.com/testing-library/jest-native/issues/46#issuecomment-748674706
setupFilesAfterEnv: ['<rootDir>/test/setup.ts'],
globals: {
__DEV__: 'development',
},
};
23 changes: 23 additions & 0 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { LogLevel } from './types';
/**
* Log messages in the console with a corresponding urgency
*
* @param level The urgency of the message
* @param message The message to log in the console
*/
export const log = (level: LogLevel, message: string) => {
if (__DEV__) {
const packageName = '[react-use-wizard]';

switch (level) {
case 'warn':
console.warn(`${packageName} ${message}`);
break;
case 'error':
console.error(`${packageName} ${message}`);
break;
default:
console.log(`${packageName} ${message}`);
}
}
};
18 changes: 0 additions & 18 deletions src/reducer.ts

This file was deleted.

5 changes: 4 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,7 @@ export type WizardValues = {
isFirstStep: boolean;
/** Indicate if the current step is the last step (aka no next step) */
isLastStep: boolean;
} | null;
};

/** Console log levels */
export type LogLevel = 'info' | 'error' | 'warn';
7 changes: 4 additions & 3 deletions src/useWizard.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import * as React from 'react';

import { WizardValues } from './types';
import WizardContext from './wizardContext';

const useWizard = () => {
const context = React.useContext(WizardContext);

if (!context) {
throw Error('Wrap your component with `Wizard`');
if (!context && __DEV__) {
throw Error('Wrap your step with `Wizard`');
} else {
return context;
return context as WizardValues;
}
};

Expand Down
32 changes: 28 additions & 4 deletions src/wizard.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as React from 'react';

import * as logger from './logger';
import { Handler, WizardProps } from './types';
import WizardContext from './wizardContext';

Expand Down Expand Up @@ -62,10 +63,33 @@ const Wizard: React.FC<WizardProps> = React.memo(
[doNextStep, previousStep, isLoading, handleStep, activeStep],
);

const activeStepContent = React.useMemo(
() => React.Children.toArray(children)[activeStep],
[activeStep, children],
);
const activeStepContent = React.useMemo(() => {
const reactChildren = React.Children.toArray(children);

if (__DEV__) {
// No steps passed
if (reactChildren.length === 0) {
logger.log(
'warn',
'Make sure to pass your steps as children in your <Wizard>',
);
}
// The passed start index is invalid
if (activeStep > reactChildren.length) {
logger.log('warn', 'An invalid startIndex is passed to <Wizard>');
}
// Invalid header element
if (header && !React.isValidElement(header)) {
logger.log('error', 'Invalid header passed to <Wizard>');
}
// Invalid footer element
if (footer && !React.isValidElement(footer)) {
logger.log('error', 'Invalid footer passed to <Wizard>');
}
}

return reactChildren[activeStep];
}, [activeStep, children, header, footer]);

return (
<WizardContext.Provider value={wizardValue}>
Expand Down
2 changes: 1 addition & 1 deletion src/wizardContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from 'react';

import { WizardValues } from './types';

const WizardContext = React.createContext<WizardValues>(null);
const WizardContext = React.createContext<WizardValues | null>(null);
WizardContext.displayName = 'WizardContext';

export default WizardContext;
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
// see https://www.typescriptlang.org/tsconfig to better understand tsconfigs
"include": ["src", "types"],
"include": ["src", "global.d.ts"],
"compilerOptions": {
"module": "esnext",
"lib": ["dom", "esnext"],
Expand Down