forked from primer/react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_InputValidation.tsx
53 lines (47 loc) · 1.46 KB
/
_InputValidation.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
import {AlertFillIcon, CheckCircleFillIcon, IconProps} from '@primer/octicons-react'
import React from 'react'
import {Box, Text} from '.'
import {SxProp} from './sx'
import {FormValidationStatus} from './utils/types/FormValidationStatus'
type Props = {
id: string
validationStatus?: FormValidationStatus
} & SxProp
const validationIconMap: Record<NonNullable<Props['validationStatus']>, React.ComponentType<IconProps>> = {
success: CheckCircleFillIcon,
error: AlertFillIcon,
warning: AlertFillIcon
}
const validationColorMap: Record<NonNullable<Props['validationStatus']>, string> = {
success: 'success.fg',
error: 'danger.fg',
warning: 'attention.fg'
}
const InputValidation: React.FC<Props> = ({children, id, validationStatus, sx}) => {
const IconComponent = validationStatus ? validationIconMap[validationStatus] : undefined
const fgColor = validationStatus ? validationColorMap[validationStatus] : undefined
return (
<Text
sx={{
fontSize: 0,
fontWeight: 'bold',
alignItems: 'center',
color: fgColor,
display: 'flex',
a: {
color: 'currentColor',
textDecoration: 'underline'
},
...sx
}}
>
{IconComponent && (
<Box as="span" mr={1} sx={{display: 'flex'}} aria-hidden="true">
<IconComponent size={12} fill="currentColor" />
</Box>
)}
<span id={id}>{children}</span>
</Text>
)
}
export default InputValidation