-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathenter-handler.ts
More file actions
56 lines (46 loc) · 1.65 KB
/
Copy pathenter-handler.ts
File metadata and controls
56 lines (46 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { KeyboardEvent } from 'react';
import selectNext, { NextStep } from './select-next';
import { AnyObject } from '@data-driven-forms/react-form-renderer';
interface FormOptions {
valid: boolean;
getState: () => AnyObject & { validating: boolean; values: AnyObject };
getRegisteredFields: () => AnyObject;
}
interface Step {
nextStep?: NextStep;
buttons?: unknown;
}
type HandleNext = (nextStep: string, getRegisteredFields: () => AnyObject) => void;
type HandleSubmit = () => void;
type FindCurrentStep = (activeStep: string) => Step;
const enterHandler = (
e: KeyboardEvent,
formOptions: FormOptions,
activeStep: string,
findCurrentStep: FindCurrentStep,
handleNext: HandleNext,
handleSubmit: HandleSubmit
): void => {
if (e.key === 'Enter' && !e.shiftKey && !e.ctrlKey) {
const target = e.target as HTMLElement;
const isNotButton = target && 'type' in target && target.type !== 'button';
if (isNotButton) {
e.preventDefault();
const currentStep = findCurrentStep(activeStep);
const schemaNextStep = currentStep.nextStep;
const hasCustomButtons = currentStep.buttons;
let nextStep;
if (schemaNextStep) {
const result = selectNext(schemaNextStep, formOptions.getState);
nextStep = typeof result === 'string' ? result : undefined;
}
const canContinue = formOptions.valid && !formOptions.getState().validating;
if (canContinue && nextStep && !hasCustomButtons) {
handleNext(nextStep, formOptions.getRegisteredFields);
} else if (canContinue && !schemaNextStep && !hasCustomButtons) {
handleSubmit();
}
}
}
};
export default enterHandler;