@snack-uikit/stepper 0.5.10-preview-8e2ed733.0 → 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 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` - перейти на пред. шаг, `setValidation` - установить состояние валидации для текущего шага, `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,
@@ -9,7 +11,10 @@ export const StepperContext = createContext({
9
11
  goPrev() {
10
12
  /* stub */
11
13
  },
12
- setValidation() {
14
+ resetValidation() {
15
+ /* stub */
16
+ },
17
+ setValidator() {
13
18
  /* stub */
14
19
  },
15
20
  });
@@ -13,7 +13,7 @@ export type StepperProps = WithSupportProps<{
13
13
  className?: string;
14
14
  /**
15
15
  * Render function. Принимает аргументы: `stepper` - JSX-элемент степпера,
16
- * `goNext` - перейти на след. шаг, `goPrev` - перейти на пред. шаг, `setValidation` - установить состояние валидации для текущего шага, `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
- stepperValidPromise
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;
@@ -91,16 +98,21 @@ export function Stepper(_a) {
91
98
  }
92
99
  return Object.assign(Object.assign({}, step), { number, state: STEP_STATE.Waiting });
93
100
  }), [steps, currentStepIndex, goPrev, currentStepState, isCompleted, goNext]);
94
- const setValidation = useCallback((isValid) => {
95
- const setValidationValue = (isValid) => setCurrentStepState(isValid ? STEP_STATE.Current : STEP_STATE.Rejected);
96
- if (isValid instanceof Promise) {
97
- setCurrentStepState(STEP_STATE.Loading);
98
- isValid.then(setValidationValue).catch(() => setValidationValue(false));
99
- return;
101
+ const resetValidation = useCallback(() => {
102
+ if (currentStepState === STEP_STATE.Rejected) {
103
+ setCurrentStepState(STEP_STATE.Current);
100
104
  }
101
- setValidationValue(isValid);
102
- }, []);
105
+ }, [currentStepState]);
103
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))) })));
104
- const stepperApi = { goNext, goPrev, currentStepIndex, isCompleted, setValidation, stepCount: steps.length };
105
- return _jsx(StepperContext.Provider, { value: stepperApi, children: children(Object.assign({ stepper }, stepperApi)) });
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) });
106
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
- setValidation(isValid: boolean | Promise<boolean>): void;
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.10-preview-8e2ed733.0",
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": "98c8bfc998da448c39972953e87601d5174e1e23"
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,
@@ -14,7 +15,10 @@ export const StepperContext = createContext<StepperApi>({
14
15
  goPrev() {
15
16
  /* stub */
16
17
  },
17
- setValidation() {
18
+ resetValidation() {
19
+ /* stub */
20
+ },
21
+ setValidator() {
18
22
  /* stub */
19
23
  },
20
24
  });
@@ -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` - перейти на пред. шаг, `setValidation` - установить состояние валидации для текущего шага, `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
- stepperValidPromise
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) => {
@@ -140,18 +148,11 @@ export function Stepper({
140
148
  [steps, currentStepIndex, goPrev, currentStepState, isCompleted, goNext],
141
149
  );
142
150
 
143
- const setValidation = useCallback((isValid: boolean | Promise<boolean>) => {
144
- const setValidationValue = (isValid: boolean) =>
145
- setCurrentStepState(isValid ? STEP_STATE.Current : STEP_STATE.Rejected);
146
-
147
- if (isValid instanceof Promise) {
148
- setCurrentStepState(STEP_STATE.Loading);
149
- isValid.then(setValidationValue).catch(() => setValidationValue(false));
150
- return;
151
+ const resetValidation = useCallback(() => {
152
+ if (currentStepState === STEP_STATE.Rejected) {
153
+ setCurrentStepState(STEP_STATE.Current);
151
154
  }
152
-
153
- setValidationValue(isValid);
154
- }, []);
155
+ }, [currentStepState]);
155
156
 
156
157
  const stepper = (
157
158
  <div className={cn(styles.stepper, className)} {...extractSupportProps(props)}>
@@ -166,7 +167,16 @@ export function Stepper({
166
167
  </div>
167
168
  );
168
169
 
169
- const stepperApi = { goNext, goPrev, currentStepIndex, isCompleted, setValidation, stepCount: steps.length };
170
-
171
- return <StepperContext.Provider value={stepperApi}>{children({ stepper, ...stepperApi })}</StepperContext.Provider>;
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>;
172
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
- setValidation(isValid: boolean | Promise<boolean>): void;
29
+ resetValidation(): void;
30
+ setValidator(validator: StepsValidator): void;
25
31
  isCompleted: boolean;
26
32
  currentStepIndex: number;
27
33
  stepCount: number;