@snack-uikit/stepper 0.5.9 → 0.5.10-preview-e59b2871.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/README.md +1 -1
- package/dist/StepperContext/StepperContext.js +5 -0
- package/dist/components/Stepper/Stepper.d.ts +1 -1
- package/dist/components/Stepper/Stepper.js +22 -6
- package/dist/types.d.ts +4 -0
- package/package.json +2 -2
- package/src/StepperContext/{StepperContext.ts → StepperContext.tsx} +4 -0
- package/src/components/Stepper/Stepper.tsx +26 -9
- package/src/types.ts +6 -0
package/README.md
CHANGED
|
@@ -48,7 +48,7 @@ return (
|
|
|
48
48
|
### Props
|
|
49
49
|
| name | type | default value | description |
|
|
50
50
|
|------|------|---------------|-------------|
|
|
51
|
-
| children* | `({stepper, ...api}) => ReactElement` | - | Render function. Принимает аргументы: `stepper` - JSX-элемент степпера, `goNext` - перейти на след. шаг, `goPrev` - перейти на пред. шаг, `resetValidation` - сбросить состояние валидации для текущего шага, `isCompleted` - окончен ли процесс, `currentStepIndex` - индекс текущего шага, `stepCount` - кол-во шагов. |
|
|
51
|
+
| children* | `({stepper, ...api}) => ReactElement` | - | Render function. Принимает аргументы: `stepper` - JSX-элемент степпера, `goNext` - перейти на след. шаг, `goPrev` - перейти на пред. шаг, `resetValidation` - сбросить состояние валидации для текущего шага, `setValidator` добавляет функцию-валидатор, которая принимает в параметры индекс текущего шага, `isCompleted` - окончен ли процесс, `currentStepIndex` - индекс текущего шага, `stepCount` - кол-во шагов. |
|
|
52
52
|
| steps* | `StepData[]` | - | Массив шагов |
|
|
53
53
|
| defaultCurrentStepIndex | `number` | - | Индекс текущего шага по-дефолту |
|
|
54
54
|
| className | `string` | - | CSS-класс |
|
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
|
|
1
2
|
import { createContext, useContext } from 'react';
|
|
2
3
|
export const StepperContext = createContext({
|
|
4
|
+
stepper: _jsx(_Fragment, {}),
|
|
3
5
|
stepCount: 0,
|
|
4
6
|
isCompleted: false,
|
|
5
7
|
currentStepIndex: 0,
|
|
@@ -12,5 +14,8 @@ export const StepperContext = createContext({
|
|
|
12
14
|
resetValidation() {
|
|
13
15
|
/* stub */
|
|
14
16
|
},
|
|
17
|
+
setValidator() {
|
|
18
|
+
/* stub */
|
|
19
|
+
},
|
|
15
20
|
});
|
|
16
21
|
export const useStepperApi = () => useContext(StepperContext);
|
|
@@ -13,7 +13,7 @@ export type StepperProps = WithSupportProps<{
|
|
|
13
13
|
className?: string;
|
|
14
14
|
/**
|
|
15
15
|
* Render function. Принимает аргументы: `stepper` - JSX-элемент степпера,
|
|
16
|
-
* `goNext` - перейти на след. шаг, `goPrev` - перейти на пред. шаг, `resetValidation` - сбросить состояние валидации для текущего шага, `isCompleted` - окончен ли процесс, `currentStepIndex` - индекс текущего шага, `stepCount` - кол-во шагов.
|
|
16
|
+
* `goNext` - перейти на след. шаг, `goPrev` - перейти на пред. шаг, `resetValidation` - сбросить состояние валидации для текущего шага, `setValidator` добавляет функцию-валидатор, которая принимает в параметры индекс текущего шага, `isCompleted` - окончен ли процесс, `currentStepIndex` - индекс текущего шага, `stepCount` - кол-во шагов.
|
|
17
17
|
* @type ({stepper, ...api}) => ReactElement
|
|
18
18
|
*/
|
|
19
19
|
children: (params: StepperState) => ReactElement;
|
|
@@ -23,6 +23,7 @@ export function Stepper(_a) {
|
|
|
23
23
|
const [currentStepState, setCurrentStepState] = useState(isCompletedByDefault ? STEP_STATE.Completed : STEP_STATE.Current);
|
|
24
24
|
const [currentStepIndex, setCurrentStepIndexValue] = useState(defaultCurrentStepIndex);
|
|
25
25
|
const [isCompleted, setIsCompleted] = useState(isCompletedByDefault);
|
|
26
|
+
const [stepsValidator, setStepsValidator] = useState();
|
|
26
27
|
useEffect(() => {
|
|
27
28
|
onCompleteChange === null || onCompleteChange === void 0 ? void 0 : onCompleteChange(isCompleted);
|
|
28
29
|
}, [isCompleted, onCompleteChange]);
|
|
@@ -35,14 +36,20 @@ export function Stepper(_a) {
|
|
|
35
36
|
});
|
|
36
37
|
}, [onChangeCurrentStep]);
|
|
37
38
|
const goNext = useCallback(() => {
|
|
38
|
-
var _a, _b;
|
|
39
|
+
var _a, _b, _c;
|
|
39
40
|
if (currentStepIndex >= steps.length || isCompleted) {
|
|
40
41
|
return;
|
|
41
42
|
}
|
|
42
43
|
const currentStep = steps[currentStepIndex];
|
|
43
|
-
const stepperValidPromise = (_b = (_a = currentStep.validation) === null || _a === void 0 ? void 0 : _a.call(currentStep)) !== null && _b !== void 0 ? _b : Promise.resolve(STEP_STATE.Completed);
|
|
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(STEP_STATE.Completed),
|
|
47
|
+
(_c = stepsValidator === null || stepsValidator === void 0 ? void 0 : stepsValidator(currentStepIndex)) !== null && _c !== void 0 ? _c : Promise.resolve(STEP_STATE.Completed),
|
|
48
|
+
])
|
|
49
|
+
.then(results => results.every(result => result === STEP_VALIDATION_RESULT.Completed)
|
|
50
|
+
? STEP_VALIDATION_RESULT.Completed
|
|
51
|
+
: STEP_VALIDATION_RESULT.Rejected)
|
|
52
|
+
.catch(() => STEP_VALIDATION_RESULT.Rejected)
|
|
46
53
|
.then(validationResult => {
|
|
47
54
|
switch (validationResult) {
|
|
48
55
|
case STEP_VALIDATION_RESULT.Completed:
|
|
@@ -64,7 +71,7 @@ export function Stepper(_a) {
|
|
|
64
71
|
}
|
|
65
72
|
})
|
|
66
73
|
.catch(() => setCurrentStepState(STEP_STATE.Rejected));
|
|
67
|
-
}, [currentStepIndex, isCompleted, setCurrentStepIndex, steps]);
|
|
74
|
+
}, [currentStepIndex, isCompleted, setCurrentStepIndex, steps, stepsValidator]);
|
|
68
75
|
const goPrev = useCallback((index = currentStepIndex - 1) => {
|
|
69
76
|
if (currentStepIndex === 0 || index < 0 || index > currentStepIndex) {
|
|
70
77
|
return;
|
|
@@ -97,6 +104,15 @@ export function Stepper(_a) {
|
|
|
97
104
|
}
|
|
98
105
|
}, [currentStepState]);
|
|
99
106
|
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))) })));
|
|
100
|
-
const stepperApi = {
|
|
101
|
-
|
|
107
|
+
const stepperApi = {
|
|
108
|
+
goNext,
|
|
109
|
+
goPrev,
|
|
110
|
+
currentStepIndex,
|
|
111
|
+
isCompleted,
|
|
112
|
+
resetValidation,
|
|
113
|
+
stepCount: steps.length,
|
|
114
|
+
stepper,
|
|
115
|
+
setValidator: setStepsValidator,
|
|
116
|
+
};
|
|
117
|
+
return _jsx(StepperContext.Provider, { value: stepperApi, children: children(stepperApi) });
|
|
102
118
|
}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
import { JSX } from 'react';
|
|
1
2
|
import { ValueOf } from '@snack-uikit/utils';
|
|
2
3
|
import { STEP_STATE, STEP_VALIDATION_RESULT } from './constants';
|
|
3
4
|
export type StepState = ValueOf<typeof STEP_STATE>;
|
|
4
5
|
export type StepValidationResult = ValueOf<typeof STEP_VALIDATION_RESULT>;
|
|
6
|
+
export type StepsValidator = (stepIndex: number) => Promise<StepValidationResult>;
|
|
5
7
|
export type StepData = {
|
|
6
8
|
title: string;
|
|
7
9
|
description?: string;
|
|
@@ -13,9 +15,11 @@ export type StepViewData = StepData & {
|
|
|
13
15
|
number: number;
|
|
14
16
|
};
|
|
15
17
|
export type StepperApi = {
|
|
18
|
+
stepper: JSX.Element;
|
|
16
19
|
goNext(): void;
|
|
17
20
|
goPrev(): void;
|
|
18
21
|
resetValidation(): void;
|
|
22
|
+
setValidator(validator: StepsValidator): void;
|
|
19
23
|
isCompleted: boolean;
|
|
20
24
|
currentStepIndex: number;
|
|
21
25
|
stepCount: number;
|
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
6
|
"title": "Stepper",
|
|
7
|
-
"version": "0.5.
|
|
7
|
+
"version": "0.5.10-preview-e59b2871.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": "1f6f70cea8ded89a952f288fefe362231ea3404b"
|
|
43
43
|
}
|
|
@@ -5,6 +5,7 @@ import { StepperApi } from '../types';
|
|
|
5
5
|
export type StepperContextValue = StepperApi;
|
|
6
6
|
|
|
7
7
|
export const StepperContext = createContext<StepperApi>({
|
|
8
|
+
stepper: <></>,
|
|
8
9
|
stepCount: 0,
|
|
9
10
|
isCompleted: false,
|
|
10
11
|
currentStepIndex: 0,
|
|
@@ -17,6 +18,9 @@ export const StepperContext = createContext<StepperApi>({
|
|
|
17
18
|
resetValidation() {
|
|
18
19
|
/* stub */
|
|
19
20
|
},
|
|
21
|
+
setValidator() {
|
|
22
|
+
/* stub */
|
|
23
|
+
},
|
|
20
24
|
});
|
|
21
25
|
|
|
22
26
|
export const useStepperApi = () => useContext(StepperContext);
|
|
@@ -6,7 +6,7 @@ import { extractSupportProps, WithSupportProps } from '@snack-uikit/utils';
|
|
|
6
6
|
import { STEP_STATE, STEP_VALIDATION_RESULT } from '../../constants';
|
|
7
7
|
import { Step } from '../../helperComponents';
|
|
8
8
|
import { StepperContext } from '../../StepperContext';
|
|
9
|
-
import { StepData, StepperApi, StepState, StepViewData } from '../../types';
|
|
9
|
+
import { StepData, StepperApi, StepState, StepsValidator, StepViewData } from '../../types';
|
|
10
10
|
import styles from './styles.module.scss';
|
|
11
11
|
|
|
12
12
|
export type StepperState = StepperApi & {
|
|
@@ -22,7 +22,7 @@ export type StepperProps = WithSupportProps<{
|
|
|
22
22
|
className?: string;
|
|
23
23
|
/**
|
|
24
24
|
* Render function. Принимает аргументы: `stepper` - JSX-элемент степпера,
|
|
25
|
-
* `goNext` - перейти на след. шаг, `goPrev` - перейти на пред. шаг, `resetValidation` - сбросить состояние валидации для текущего шага, `isCompleted` - окончен ли процесс, `currentStepIndex` - индекс текущего шага, `stepCount` - кол-во шагов.
|
|
25
|
+
* `goNext` - перейти на след. шаг, `goPrev` - перейти на пред. шаг, `resetValidation` - сбросить состояние валидации для текущего шага, `setValidator` добавляет функцию-валидатор, которая принимает в параметры индекс текущего шага, `isCompleted` - окончен ли процесс, `currentStepIndex` - индекс текущего шага, `stepCount` - кол-во шагов.
|
|
26
26
|
* @type ({stepper, ...api}) => ReactElement
|
|
27
27
|
*/
|
|
28
28
|
children: (params: StepperState) => ReactElement;
|
|
@@ -47,6 +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
51
|
|
|
51
52
|
useEffect(() => {
|
|
52
53
|
onCompleteChange?.(isCompleted);
|
|
@@ -71,11 +72,18 @@ export function Stepper({
|
|
|
71
72
|
|
|
72
73
|
const currentStep = steps[currentStepIndex];
|
|
73
74
|
|
|
74
|
-
const stepperValidPromise = currentStep.validation?.() ?? Promise.resolve(STEP_STATE.Completed);
|
|
75
|
-
|
|
76
75
|
setCurrentStepState(STEP_STATE.Loading);
|
|
77
76
|
|
|
78
|
-
|
|
77
|
+
Promise.all([
|
|
78
|
+
currentStep.validation?.() ?? Promise.resolve(STEP_STATE.Completed),
|
|
79
|
+
stepsValidator?.(currentStepIndex) ?? Promise.resolve(STEP_STATE.Completed),
|
|
80
|
+
])
|
|
81
|
+
.then(results =>
|
|
82
|
+
results.every(result => result === STEP_VALIDATION_RESULT.Completed)
|
|
83
|
+
? STEP_VALIDATION_RESULT.Completed
|
|
84
|
+
: STEP_VALIDATION_RESULT.Rejected,
|
|
85
|
+
)
|
|
86
|
+
.catch(() => STEP_VALIDATION_RESULT.Rejected)
|
|
79
87
|
.then(validationResult => {
|
|
80
88
|
switch (validationResult) {
|
|
81
89
|
case STEP_VALIDATION_RESULT.Completed:
|
|
@@ -96,7 +104,7 @@ export function Stepper({
|
|
|
96
104
|
}
|
|
97
105
|
})
|
|
98
106
|
.catch(() => setCurrentStepState(STEP_STATE.Rejected));
|
|
99
|
-
}, [currentStepIndex, isCompleted, setCurrentStepIndex, steps]);
|
|
107
|
+
}, [currentStepIndex, isCompleted, setCurrentStepIndex, steps, stepsValidator]);
|
|
100
108
|
|
|
101
109
|
const goPrev = useCallback(
|
|
102
110
|
(index: number = currentStepIndex - 1) => {
|
|
@@ -159,7 +167,16 @@ export function Stepper({
|
|
|
159
167
|
</div>
|
|
160
168
|
);
|
|
161
169
|
|
|
162
|
-
const stepperApi = {
|
|
163
|
-
|
|
164
|
-
|
|
170
|
+
const stepperApi = {
|
|
171
|
+
goNext,
|
|
172
|
+
goPrev,
|
|
173
|
+
currentStepIndex,
|
|
174
|
+
isCompleted,
|
|
175
|
+
resetValidation,
|
|
176
|
+
stepCount: steps.length,
|
|
177
|
+
stepper,
|
|
178
|
+
setValidator: setStepsValidator,
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
return <StepperContext.Provider value={stepperApi}>{children(stepperApi)}</StepperContext.Provider>;
|
|
165
182
|
}
|
package/src/types.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { JSX } from 'react';
|
|
2
|
+
|
|
1
3
|
import { ValueOf } from '@snack-uikit/utils';
|
|
2
4
|
|
|
3
5
|
import { STEP_STATE, STEP_VALIDATION_RESULT } from './constants';
|
|
@@ -6,6 +8,8 @@ export type StepState = ValueOf<typeof STEP_STATE>;
|
|
|
6
8
|
|
|
7
9
|
export type StepValidationResult = ValueOf<typeof STEP_VALIDATION_RESULT>;
|
|
8
10
|
|
|
11
|
+
export type StepsValidator = (stepIndex: number) => Promise<StepValidationResult>;
|
|
12
|
+
|
|
9
13
|
export type StepData = {
|
|
10
14
|
title: string;
|
|
11
15
|
description?: string;
|
|
@@ -19,9 +23,11 @@ export type StepViewData = StepData & {
|
|
|
19
23
|
};
|
|
20
24
|
|
|
21
25
|
export type StepperApi = {
|
|
26
|
+
stepper: JSX.Element;
|
|
22
27
|
goNext(): void;
|
|
23
28
|
goPrev(): void;
|
|
24
29
|
resetValidation(): void;
|
|
30
|
+
setValidator(validator: StepsValidator): void;
|
|
25
31
|
isCompleted: boolean;
|
|
26
32
|
currentStepIndex: number;
|
|
27
33
|
stepCount: number;
|