-
Notifications
You must be signed in to change notification settings - Fork 429
/
Copy pathlabel.jsx
69 lines (62 loc) · 1.61 KB
/
label.jsx
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
/* eslint-disable react/jsx-curly-brace-presence */
/* Copyright (c) 2015-present, salesforce.com, inc. All rights reserved */
/* Licensed under BSD 3-Clause - see LICENSE.txt or git.io/sfdc-license */
import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
const propTypes = {
/*
* Assistive Text to use instead of a visible label
*/
assistiveText: PropTypes.object,
/*
* Id of the input associated with this label
*/
htmlFor: PropTypes.string,
/*
* Input Label or inner node for formatting purposes
*/
label: PropTypes.oneOfType([PropTypes.node, PropTypes.string]),
/*
* Applies label styling for a required form element
*/
required: PropTypes.bool,
/**
* Changes markup of label.
*/
variant: PropTypes.oneOf(['base', 'static']),
};
/*
* Form label. This returns null if there is no label text (hidden or shown)
*/
const Label = ({
label,
assistiveText,
htmlFor,
required,
variant = 'base',
}) => {
const labelText = label || (assistiveText && assistiveText.label); // One of these is required to pass accessibility tests
const subRenders = {
base: (
<label
className={classNames('slds-form-element__label', {
'slds-assistive-text': assistiveText && !label,
})}
htmlFor={htmlFor}
>
{required && (
<abbr className="slds-required" title="required">
{'*'}
</abbr>
)}
{labelText}
</label>
),
static: <span className="slds-form-element__label">{labelText}</span>,
};
return labelText ? subRenders[variant] : null;
};
Label.displayName = 'Label';
Label.propTypes = propTypes;
export default Label;