This repository was archived by the owner on Jan 9, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 189
/
Copy pathLabel.tsx
67 lines (66 loc) · 1.71 KB
/
Label.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
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { createElement, CSSProperties } from 'react'
import FormLabel from 'react-bootstrap/FormLabel'
interface Props {
/** Text to display in label */
text: string
/** Title of the label. */
title?: string // Use on required input labels to override default required title
/** Ties label to input */
htmlFor?: string
/** Defines whether input is required. */
isRequired?: boolean
/**
* Defines the class of the label.
*/
className?: string
/**
* Defines the style of the label.
*/
style?: CSSProperties
}
/**
* Svg instead of asterisk to avoid asterisk being read by screenreaders
* hidden text to be read explaing the input is required incase the title attribute
* is not supported by the screen reader
*/
const asterisk = createElement('i', { style: { color: 'red' } }, [
<FontAwesomeIcon
icon="asterisk"
key="asterisk"
style={{ height: '7px', verticalAlign: 'top', marginLeft: '2px' }}
/>,
])
/**
* Labels are used to display text
*/
const Label = (props: Props) => {
const { text, htmlFor, isRequired, title, className, style } = props
/** Form label for required inputs */
if (isRequired) {
return (
<div>
<FormLabel
htmlFor={htmlFor}
title={title || 'This is a required input'}
className={className}
style={style}
>
{text}
{asterisk}
</FormLabel>
</div>
)
}
/** Default form label */
return (
<FormLabel htmlFor={htmlFor} title={title} className={className} style={style}>
{text}
</FormLabel>
)
}
Label.defaultProps = {
title: undefined,
htmlFor: undefined,
}
export { Label }