Skip to content

Commit 73a55a4

Browse files
committed
feat: add logging
1 parent 011ae3e commit 73a55a4

File tree

4 files changed

+55
-7
lines changed

4 files changed

+55
-7
lines changed

.eslintrc.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module.exports = {
1515
rules: {
1616
'simple-import-sort/imports': 'error',
1717
'sort-imports': 'off',
18-
'import/order': 'off'
18+
'import/order': 'off',
19+
'jsdoc/require-param-type': 'off',
1920
}
2021
};

src/logger.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { LogLevel } from './types';
2+
/**
3+
* Log messages in the console with a corresponding urgency
4+
*
5+
* @param level The urgency of the message
6+
* @param message The message to log in the console
7+
*/
8+
export const log = (level: LogLevel, message: string) => {
9+
if (__DEV__) {
10+
const packageName = '[react-use-wizard]';
11+
12+
switch (level) {
13+
case 'warn':
14+
console.warn(`${packageName} ${message}`);
15+
break;
16+
case 'error':
17+
console.error(`${packageName} ${message}`);
18+
break;
19+
default:
20+
console.log(`${packageName} ${message}`);
21+
}
22+
}
23+
};

src/useWizard.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import WizardContext from './wizardContext';
55
const useWizard = () => {
66
const context = React.useContext(WizardContext);
77

8-
if (!context) {
9-
throw Error('Wrap your component with `Wizard`');
8+
if (!context && __DEV__) {
9+
throw Error('Wrap your step with `Wizard`');
1010
} else {
1111
return context;
1212
}

src/wizard.tsx

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as React from 'react';
22

3+
import * as logger from './logger';
34
import { Handler, WizardProps } from './types';
45
import WizardContext from './wizardContext';
56

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

65-
const activeStepContent = React.useMemo(
66-
() => React.Children.toArray(children)[activeStep],
67-
[activeStep, children],
68-
);
66+
const activeStepContent = React.useMemo(() => {
67+
const reactChildren = React.Children.toArray(children);
68+
69+
if (__DEV__) {
70+
// No steps passed
71+
if (reactChildren.length === 0) {
72+
logger.log(
73+
'warn',
74+
'Make sure to pass your steps as children in your <Wizard>',
75+
);
76+
}
77+
// The passed start index is invalid
78+
if (activeStep > reactChildren.length) {
79+
logger.log('warn', 'An invalid startIndex is passed to <Wizard>');
80+
}
81+
// Invalid header element
82+
if (!React.isValidElement(header)) {
83+
logger.log('error', 'Invalid header passed to <Wizard>');
84+
}
85+
// Invalid footer element
86+
if (!React.isValidElement(footer)) {
87+
logger.log('error', 'Invalid footer passed to <Wizard>');
88+
}
89+
}
90+
91+
return reactChildren[activeStep];
92+
}, [activeStep, children, header, footer]);
6993

7094
return (
7195
<WizardContext.Provider value={wizardValue}>

0 commit comments

Comments
 (0)