generated from vtex-apps/react-app-template
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathFormSubmit.tsx
64 lines (58 loc) · 1.62 KB
/
FormSubmit.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
import React, { useContext } from 'react'
import { defineMessages } from 'react-intl'
import { Button, Alert } from 'vtex.styleguide'
import { useCssHandles } from 'vtex.css-handles'
import { IOMessage } from 'vtex.native-types'
import { SubmitContext } from './logic/formState'
export type FormSubmitProps = {
label?: string
}
const CSS_HANDLES = [
'formSubmitContainer',
'formSubmitButton',
'formErrorServer',
'formErrorUserInput',
] as const
const messages = defineMessages({
submitButton: {
id: 'store/form.submit.buttonLabel',
defaultMessage: '',
},
userInputError: {
id: 'store/form.submit.error.userInputError',
defaultMessage: '',
},
serverError: {
id: 'store/form.submit.error.serverError',
defaultMessage: '',
},
})
export default function FormSubmit({
label = messages.submitButton.id,
}: FormSubmitProps) {
const { loading, userInputError, serverError } = useContext(SubmitContext)
const handles = useCssHandles(CSS_HANDLES)
return (
<div className={handles.formSubmitContainer}>
<div className={handles.formSubmitButton}>
<Button type="submit" isLoading={loading}>
<IOMessage id={label} />
</Button>
</div>
<div className={handles.formErrorUserInput}>
{userInputError && (
<Alert type="error">
<IOMessage id={messages.userInputError.id} />
</Alert>
)}
</div>
<div className={handles.formErrorServer}>
{serverError && (
<Alert type="error">
<IOMessage id={messages.serverError.id} />
</Alert>
)}
</div>
</div>
)
}