This repository has been archived by the owner on Feb 10, 2024. It is now read-only.
/
FormControls.tsx
110 lines (93 loc) · 3.16 KB
/
FormControls.tsx
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright (c) 2022. Heusala Group Oy <info@heusalagroup.fi>. All rights reserved.
import {
ReactNode,
useCallback
} from "react";
import { VoidCallback } from "../../../core/interfaces/callbacks";
import { Button } from "../button/Button";
import { LogService } from "../../../core/LogService";
import { FORM_CONTROLS_CLASS_NAME } from "../../constants/hgClassName";
import { TranslationFunction } from "../../../core/types/TranslationFunction";
import "./FormControls.scss";
const LOG = LogService.createLogger('FormControls');
export interface FormControlsProps {
readonly t : TranslationFunction;
readonly hasErrors?: boolean;
readonly className?: string;
readonly cancelLabel?: string;
readonly cancel?: VoidCallback;
readonly submitLabel?: string;
readonly submit?: VoidCallback;
readonly children ?: ReactNode;
}
export function FormControls (props: FormControlsProps) {
const t = props?.t;
const className = props?.className;
const propsSubmit = props?.submit;
const propsCancel = props?.cancel;
const hasErrors: boolean = !!props?.hasErrors;
const submitLabel: string = props?.submitLabel ?? 'common.submitLabel';
const cancelLabel: string = props?.cancelLabel ?? 'common.cancelLabel';
const hasSubmit: boolean = !!propsSubmit
const hasCancel: boolean = !!propsCancel;
const onCancelCallback = useCallback(
() => {
try {
if ( propsCancel ) {
propsCancel();
} else {
LOG.error(`onCancel: No cancel prop defined`);
}
} catch (err) {
LOG.error(`onCancel: Error: `, err);
}
},
[
]
);
const onSubmitCallback = useCallback(
() => {
try {
if ( propsSubmit ) {
propsSubmit();
} else {
LOG.error(`onSubmit: No submit prop defined`);
}
} catch (err) {
LOG.error(`onSubmit: Error: `, err);
}
},
[
propsSubmit
]
);
return (
<div
className={
FORM_CONTROLS_CLASS_NAME
+ (className ? ` ${className}` : '')
+ (hasErrors ? ` ${FORM_CONTROLS_CLASS_NAME}-with-errors` : '')
}
>
<div className={FORM_CONTROLS_CLASS_NAME + '-content'}>{props?.children}</div>
{hasCancel ? (
<Button
className={
FORM_CONTROLS_CLASS_NAME + '-button '
+ FORM_CONTROLS_CLASS_NAME + '-cancel-button'
}
click={onCancelCallback}
>{t(cancelLabel)}</Button>
) : null}
{hasSubmit ? (
<Button
className={
FORM_CONTROLS_CLASS_NAME + '-button '
+ FORM_CONTROLS_CLASS_NAME + '-submit-button'
}
click={onSubmitCallback}
>{t(submitLabel)}</Button>
) : null}
</div>
);
}