Skip to content

Commit 952be20

Browse files
authored
Merge pull request #11 from devrnt/chore/logging
Chore/logging
2 parents 4616e98 + 9baec06 commit 952be20

File tree

10 files changed

+67
-29
lines changed

10 files changed

+67
-29
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
};

global.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
declare var __DEV__: boolean;

jest.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
module.exports = {
22
// https://github.com/testing-library/jest-native/issues/46#issuecomment-748674706
33
setupFilesAfterEnv: ['<rootDir>/test/setup.ts'],
4+
globals: {
5+
__DEV__: 'development',
6+
},
47
};

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/reducer.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/types.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,7 @@ export type WizardValues = {
3434
isFirstStep: boolean;
3535
/** Indicate if the current step is the last step (aka no next step) */
3636
isLastStep: boolean;
37-
} | null;
37+
};
38+
39+
/** Console log levels */
40+
export type LogLevel = 'info' | 'error' | 'warn';

src/useWizard.ts

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

3+
import { WizardValues } from './types';
34
import WizardContext from './wizardContext';
45

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

8-
if (!context) {
9-
throw Error('Wrap your component with `Wizard`');
9+
if (!context && __DEV__) {
10+
throw Error('Wrap your step with `Wizard`');
1011
} else {
11-
return context;
12+
return context as WizardValues;
1213
}
1314
};
1415

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 (header && !React.isValidElement(header)) {
83+
logger.log('error', 'Invalid header passed to <Wizard>');
84+
}
85+
// Invalid footer element
86+
if (footer && !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}>

src/wizardContext.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as React from 'react';
22

33
import { WizardValues } from './types';
44

5-
const WizardContext = React.createContext<WizardValues>(null);
5+
const WizardContext = React.createContext<WizardValues | null>(null);
66
WizardContext.displayName = 'WizardContext';
77

88
export default WizardContext;

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
// see https://www.typescriptlang.org/tsconfig to better understand tsconfigs
3-
"include": ["src", "types"],
3+
"include": ["src", "global.d.ts"],
44
"compilerOptions": {
55
"module": "esnext",
66
"lib": ["dom", "esnext"],

0 commit comments

Comments
 (0)