-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathLabel.js
59 lines (53 loc) · 1.65 KB
/
Label.js
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
import {FormLabel, Paragraph} from '@utrecht/component-library-react';
import classNames from 'classnames';
import PropTypes from 'prop-types';
import React, {useContext} from 'react';
import {FormattedMessage} from 'react-intl';
import {ConfigContext} from 'Context';
export const LabelContent = ({id, disabled = false, isRequired = false, type, children}) => {
const {requiredFieldsWithAsterisk} = useContext(ConfigContext);
return (
<FormLabel
htmlFor={id}
disabled={disabled}
className={classNames({
'utrecht-form-label--openforms-required': isRequired && requiredFieldsWithAsterisk,
[`utrecht-form-label--${type}`]: type,
})}
>
{isRequired ? (
<>{children}</>
) : (
<FormattedMessage
description="Form field label, field not required"
defaultMessage="{withAsterisk, select, true {<label></label>} other {<label></label> (not required)}}"
values={{
withAsterisk: requiredFieldsWithAsterisk,
label: () => children,
}}
/>
)}
</FormLabel>
);
};
LabelContent.propTypes = {
id: PropTypes.string,
isRequired: PropTypes.bool,
disabled: PropTypes.bool,
children: PropTypes.node,
type: PropTypes.string,
};
const Label = ({id, isRequired = false, disabled = false, children}) => (
<Paragraph className="utrecht-form-field__label">
<LabelContent id={id} isRequired={isRequired} disabled={disabled}>
{children}
</LabelContent>
</Paragraph>
);
Label.propTypes = {
id: PropTypes.string,
isRequired: PropTypes.bool,
disabled: PropTypes.bool,
children: PropTypes.node,
};
export default Label;