@snack-uikit/stepper 0.5.10-preview-e59b2871.0 → 0.5.10-preview-96ca8003.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/Stepper/Stepper.js +23 -28
- package/dist/constants.d.ts +0 -4
- package/dist/constants.js +0 -4
- package/dist/types.d.ts +3 -4
- package/package.json +2 -2
- package/src/components/Stepper/Stepper.tsx +25 -30
- package/src/constants.ts +0 -5
- package/src/types.ts +3 -5
|
@@ -13,7 +13,7 @@ import { jsx as _jsx } from "react/jsx-runtime";
|
|
|
13
13
|
import cn from 'classnames';
|
|
14
14
|
import { useCallback, useEffect, useMemo, useState } from 'react';
|
|
15
15
|
import { extractSupportProps } from '@snack-uikit/utils';
|
|
16
|
-
import { STEP_STATE
|
|
16
|
+
import { STEP_STATE } from '../../constants';
|
|
17
17
|
import { Step } from '../../helperComponents';
|
|
18
18
|
import { StepperContext } from '../../StepperContext';
|
|
19
19
|
import styles from './styles.module.css';
|
|
@@ -43,34 +43,26 @@ export function Stepper(_a) {
|
|
|
43
43
|
const currentStep = steps[currentStepIndex];
|
|
44
44
|
setCurrentStepState(STEP_STATE.Loading);
|
|
45
45
|
Promise.all([
|
|
46
|
-
(_b = (_a = currentStep.validation) === null || _a === void 0 ? void 0 : _a.call(currentStep)) !== null && _b !== void 0 ? _b : Promise.resolve(
|
|
47
|
-
(_c = stepsValidator === null || stepsValidator === void 0 ? void 0 : stepsValidator(currentStepIndex)) !== null && _c !== void 0 ? _c : Promise.resolve(
|
|
46
|
+
(_b = (_a = currentStep.validation) === null || _a === void 0 ? void 0 : _a.call(currentStep)) !== null && _b !== void 0 ? _b : Promise.resolve(true),
|
|
47
|
+
(_c = stepsValidator === null || stepsValidator === void 0 ? void 0 : stepsValidator.value(currentStepIndex)) !== null && _c !== void 0 ? _c : Promise.resolve(true),
|
|
48
48
|
])
|
|
49
|
-
.then(results => results.every(
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
case STEP_VALIDATION_RESULT.Completed:
|
|
56
|
-
if (currentStepIndex === steps.length - 1) {
|
|
57
|
-
// завершился последний шаг
|
|
58
|
-
setCurrentStepState(STEP_STATE.Completed);
|
|
59
|
-
setIsCompleted(true);
|
|
60
|
-
}
|
|
61
|
-
else {
|
|
62
|
-
setCurrentStepIndex(currentStepIndex + 1);
|
|
63
|
-
setCurrentStepState(STEP_STATE.Current);
|
|
64
|
-
}
|
|
65
|
-
return;
|
|
66
|
-
case STEP_VALIDATION_RESULT.Rejected:
|
|
67
|
-
setCurrentStepState(STEP_STATE.Rejected);
|
|
68
|
-
return;
|
|
69
|
-
default:
|
|
70
|
-
setCurrentStepState(STEP_STATE.Current);
|
|
49
|
+
.then(results => results.every(isValid => isValid))
|
|
50
|
+
.catch(() => false)
|
|
51
|
+
.then(isValid => {
|
|
52
|
+
if (!isValid) {
|
|
53
|
+
setCurrentStepState(STEP_STATE.Rejected);
|
|
54
|
+
return;
|
|
71
55
|
}
|
|
72
|
-
|
|
73
|
-
|
|
56
|
+
if (currentStepIndex === steps.length - 1) {
|
|
57
|
+
// завершился последний шаг
|
|
58
|
+
setCurrentStepState(STEP_STATE.Completed);
|
|
59
|
+
setIsCompleted(true);
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
setCurrentStepIndex(currentStepIndex + 1);
|
|
63
|
+
setCurrentStepState(STEP_STATE.Current);
|
|
64
|
+
}
|
|
65
|
+
});
|
|
74
66
|
}, [currentStepIndex, isCompleted, setCurrentStepIndex, steps, stepsValidator]);
|
|
75
67
|
const goPrev = useCallback((index = currentStepIndex - 1) => {
|
|
76
68
|
if (currentStepIndex === 0 || index < 0 || index > currentStepIndex) {
|
|
@@ -103,6 +95,9 @@ export function Stepper(_a) {
|
|
|
103
95
|
setCurrentStepState(STEP_STATE.Current);
|
|
104
96
|
}
|
|
105
97
|
}, [currentStepState]);
|
|
98
|
+
const setValidator = useCallback((validator) => {
|
|
99
|
+
setStepsValidator({ value: validator });
|
|
100
|
+
}, []);
|
|
106
101
|
const stepper = (_jsx("div", Object.assign({ className: cn(styles.stepper, className) }, extractSupportProps(props), { children: stepsView.map((step, index) => (_jsx(Step, { step: step, "data-test-id": props['data-test-id'], hideTailLine: index === steps.length - 1 }, step.title + index))) })));
|
|
107
102
|
const stepperApi = {
|
|
108
103
|
goNext,
|
|
@@ -112,7 +107,7 @@ export function Stepper(_a) {
|
|
|
112
107
|
resetValidation,
|
|
113
108
|
stepCount: steps.length,
|
|
114
109
|
stepper,
|
|
115
|
-
setValidator
|
|
110
|
+
setValidator,
|
|
116
111
|
};
|
|
117
112
|
return _jsx(StepperContext.Provider, { value: stepperApi, children: children(stepperApi) });
|
|
118
113
|
}
|
package/dist/constants.d.ts
CHANGED
package/dist/constants.js
CHANGED
package/dist/types.d.ts
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
import { JSX } from 'react';
|
|
2
2
|
import { ValueOf } from '@snack-uikit/utils';
|
|
3
|
-
import { STEP_STATE
|
|
3
|
+
import { STEP_STATE } from './constants';
|
|
4
4
|
export type StepState = ValueOf<typeof STEP_STATE>;
|
|
5
|
-
export type
|
|
6
|
-
export type StepsValidator = (stepIndex: number) => Promise<StepValidationResult>;
|
|
5
|
+
export type StepsValidator = (stepIndex: number) => Promise<boolean>;
|
|
7
6
|
export type StepData = {
|
|
8
7
|
title: string;
|
|
9
8
|
description?: string;
|
|
10
|
-
validation?(): Promise<
|
|
9
|
+
validation?(): Promise<boolean>;
|
|
11
10
|
};
|
|
12
11
|
export type StepViewData = StepData & {
|
|
13
12
|
onClick?(): void;
|
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
6
|
"title": "Stepper",
|
|
7
|
-
"version": "0.5.10-preview-
|
|
7
|
+
"version": "0.5.10-preview-96ca8003.0",
|
|
8
8
|
"sideEffects": [
|
|
9
9
|
"*.css",
|
|
10
10
|
"*.woff",
|
|
@@ -39,5 +39,5 @@
|
|
|
39
39
|
"@snack-uikit/utils": "3.2.0",
|
|
40
40
|
"classnames": "2.3.2"
|
|
41
41
|
},
|
|
42
|
-
"gitHead": "
|
|
42
|
+
"gitHead": "f34f4124d15e2215a4eca379bb530b21cf63b87b"
|
|
43
43
|
}
|
|
@@ -3,7 +3,7 @@ import { ReactElement, useCallback, useEffect, useMemo, useState } from 'react';
|
|
|
3
3
|
|
|
4
4
|
import { extractSupportProps, WithSupportProps } from '@snack-uikit/utils';
|
|
5
5
|
|
|
6
|
-
import { STEP_STATE
|
|
6
|
+
import { STEP_STATE } from '../../constants';
|
|
7
7
|
import { Step } from '../../helperComponents';
|
|
8
8
|
import { StepperContext } from '../../StepperContext';
|
|
9
9
|
import { StepData, StepperApi, StepState, StepsValidator, StepViewData } from '../../types';
|
|
@@ -47,7 +47,7 @@ export function Stepper({
|
|
|
47
47
|
);
|
|
48
48
|
const [currentStepIndex, setCurrentStepIndexValue] = useState(defaultCurrentStepIndex);
|
|
49
49
|
const [isCompleted, setIsCompleted] = useState(isCompletedByDefault);
|
|
50
|
-
const [stepsValidator, setStepsValidator] = useState<StepsValidator>();
|
|
50
|
+
const [stepsValidator, setStepsValidator] = useState<{ value: StepsValidator }>();
|
|
51
51
|
|
|
52
52
|
useEffect(() => {
|
|
53
53
|
onCompleteChange?.(isCompleted);
|
|
@@ -75,35 +75,26 @@ export function Stepper({
|
|
|
75
75
|
setCurrentStepState(STEP_STATE.Loading);
|
|
76
76
|
|
|
77
77
|
Promise.all([
|
|
78
|
-
currentStep.validation?.() ?? Promise.resolve(
|
|
79
|
-
stepsValidator?.(currentStepIndex) ?? Promise.resolve(
|
|
78
|
+
currentStep.validation?.() ?? Promise.resolve(true),
|
|
79
|
+
stepsValidator?.value(currentStepIndex) ?? Promise.resolve(true),
|
|
80
80
|
])
|
|
81
|
-
.then(results =>
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
.then(validationResult => {
|
|
88
|
-
switch (validationResult) {
|
|
89
|
-
case STEP_VALIDATION_RESULT.Completed:
|
|
90
|
-
if (currentStepIndex === steps.length - 1) {
|
|
91
|
-
// завершился последний шаг
|
|
92
|
-
setCurrentStepState(STEP_STATE.Completed);
|
|
93
|
-
setIsCompleted(true);
|
|
94
|
-
} else {
|
|
95
|
-
setCurrentStepIndex(currentStepIndex + 1);
|
|
96
|
-
setCurrentStepState(STEP_STATE.Current);
|
|
97
|
-
}
|
|
98
|
-
return;
|
|
99
|
-
case STEP_VALIDATION_RESULT.Rejected:
|
|
100
|
-
setCurrentStepState(STEP_STATE.Rejected);
|
|
101
|
-
return;
|
|
102
|
-
default:
|
|
103
|
-
setCurrentStepState(STEP_STATE.Current);
|
|
81
|
+
.then(results => results.every(isValid => isValid))
|
|
82
|
+
.catch(() => false)
|
|
83
|
+
.then(isValid => {
|
|
84
|
+
if (!isValid) {
|
|
85
|
+
setCurrentStepState(STEP_STATE.Rejected);
|
|
86
|
+
return;
|
|
104
87
|
}
|
|
105
|
-
|
|
106
|
-
|
|
88
|
+
|
|
89
|
+
if (currentStepIndex === steps.length - 1) {
|
|
90
|
+
// завершился последний шаг
|
|
91
|
+
setCurrentStepState(STEP_STATE.Completed);
|
|
92
|
+
setIsCompleted(true);
|
|
93
|
+
} else {
|
|
94
|
+
setCurrentStepIndex(currentStepIndex + 1);
|
|
95
|
+
setCurrentStepState(STEP_STATE.Current);
|
|
96
|
+
}
|
|
97
|
+
});
|
|
107
98
|
}, [currentStepIndex, isCompleted, setCurrentStepIndex, steps, stepsValidator]);
|
|
108
99
|
|
|
109
100
|
const goPrev = useCallback(
|
|
@@ -154,6 +145,10 @@ export function Stepper({
|
|
|
154
145
|
}
|
|
155
146
|
}, [currentStepState]);
|
|
156
147
|
|
|
148
|
+
const setValidator = useCallback((validator: StepsValidator) => {
|
|
149
|
+
setStepsValidator({ value: validator });
|
|
150
|
+
}, []);
|
|
151
|
+
|
|
157
152
|
const stepper = (
|
|
158
153
|
<div className={cn(styles.stepper, className)} {...extractSupportProps(props)}>
|
|
159
154
|
{stepsView.map((step, index) => (
|
|
@@ -175,7 +170,7 @@ export function Stepper({
|
|
|
175
170
|
resetValidation,
|
|
176
171
|
stepCount: steps.length,
|
|
177
172
|
stepper,
|
|
178
|
-
setValidator
|
|
173
|
+
setValidator,
|
|
179
174
|
};
|
|
180
175
|
|
|
181
176
|
return <StepperContext.Provider value={stepperApi}>{children(stepperApi)}</StepperContext.Provider>;
|
package/src/constants.ts
CHANGED
package/src/types.ts
CHANGED
|
@@ -2,18 +2,16 @@ import { JSX } from 'react';
|
|
|
2
2
|
|
|
3
3
|
import { ValueOf } from '@snack-uikit/utils';
|
|
4
4
|
|
|
5
|
-
import { STEP_STATE
|
|
5
|
+
import { STEP_STATE } from './constants';
|
|
6
6
|
|
|
7
7
|
export type StepState = ValueOf<typeof STEP_STATE>;
|
|
8
8
|
|
|
9
|
-
export type
|
|
10
|
-
|
|
11
|
-
export type StepsValidator = (stepIndex: number) => Promise<StepValidationResult>;
|
|
9
|
+
export type StepsValidator = (stepIndex: number) => Promise<boolean>;
|
|
12
10
|
|
|
13
11
|
export type StepData = {
|
|
14
12
|
title: string;
|
|
15
13
|
description?: string;
|
|
16
|
-
validation?(): Promise<
|
|
14
|
+
validation?(): Promise<boolean>;
|
|
17
15
|
};
|
|
18
16
|
|
|
19
17
|
export type StepViewData = StepData & {
|