File size: 1,190 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import clsx from 'clsx';
import { useTranslate } from 'i18n-calypso';
import { Children, forwardRef } from 'react';

import './style.scss';

type FormLabelProps = {
	optional?: boolean;
	required?: boolean;
	hasCoreStyles?: boolean;
	hasCoreStylesNoCaps?: boolean;
} & JSX.IntrinsicElements[ 'label' ];

const FormLabel = forwardRef< HTMLLabelElement, FormLabelProps >(
	(
		{ children, required, optional, className, hasCoreStyles, hasCoreStylesNoCaps, ...labelProps },
		ref
	) => {
		const translate = useTranslate();
		const hasChildren: boolean = Children.count( children ) > 0;

		return (
			<label
				{ ...labelProps }
				className={ clsx( className, 'form-label', {
					'form-label-core-styles': hasCoreStyles || hasCoreStylesNoCaps,
					'form-label-core-styles-no-caps': hasCoreStylesNoCaps,
				} ) }
				ref={ ref }
			>
				{ children }
				{ hasChildren && required && (
					<small className="form-label__required">{ translate( 'Required' ) }</small>
				) }
				{ hasChildren && optional && (
					<small className="form-label__optional">{ translate( 'Optional' ) }</small>
				) }
			</label>
		);
	}
);

FormLabel.displayName = 'FormLabel';

export default FormLabel;