1 | import React from 'react';
|
2 | import PropTypes from 'prop-types';
|
3 | import classNames from 'classnames';
|
4 | import { mapToCssModules, tagPropType, isObject } from './utils';
|
5 |
|
6 | const colWidths = ['xs', 'sm', 'md', 'lg', 'xl', 'xxl'];
|
7 |
|
8 | const stringOrNumberProp = PropTypes.oneOfType([
|
9 | PropTypes.number,
|
10 | PropTypes.string,
|
11 | ]);
|
12 |
|
13 | const columnProps = PropTypes.oneOfType([
|
14 | PropTypes.bool,
|
15 | PropTypes.string,
|
16 | PropTypes.number,
|
17 | PropTypes.shape({
|
18 | size: stringOrNumberProp,
|
19 | order: stringOrNumberProp,
|
20 | offset: stringOrNumberProp,
|
21 | }),
|
22 | ]);
|
23 |
|
24 | const propTypes = {
|
25 | children: PropTypes.node,
|
26 | hidden: PropTypes.bool,
|
27 | check: PropTypes.bool,
|
28 | size: PropTypes.string,
|
29 | for: PropTypes.string,
|
30 | tag: tagPropType,
|
31 | className: PropTypes.string,
|
32 | cssModule: PropTypes.object,
|
33 | xs: columnProps,
|
34 | sm: columnProps,
|
35 | md: columnProps,
|
36 | lg: columnProps,
|
37 | xl: columnProps,
|
38 | xxl: columnProps,
|
39 | widths: PropTypes.array,
|
40 | };
|
41 |
|
42 | const getColumnSizeClass = (isXs, colWidth, colSize) => {
|
43 | if (colSize === true || colSize === '') {
|
44 | return isXs ? 'col' : `col-${colWidth}`;
|
45 | }
|
46 | if (colSize === 'auto') {
|
47 | return isXs ? 'col-auto' : `col-${colWidth}-auto`;
|
48 | }
|
49 |
|
50 | return isXs ? `col-${colSize}` : `col-${colWidth}-${colSize}`;
|
51 | };
|
52 |
|
53 | function Label(props) {
|
54 | const {
|
55 | className,
|
56 | cssModule,
|
57 | hidden,
|
58 | widths = colWidths,
|
59 | tag: Tag = 'label',
|
60 | check,
|
61 | size,
|
62 | for: htmlFor,
|
63 | ...attributes
|
64 | } = props;
|
65 |
|
66 | const colClasses = [];
|
67 |
|
68 | widths.forEach((colWidth, i) => {
|
69 | let columnProp = props[colWidth];
|
70 |
|
71 | delete attributes[colWidth];
|
72 |
|
73 | if (!columnProp && columnProp !== '') {
|
74 | return;
|
75 | }
|
76 |
|
77 | const isXs = !i;
|
78 | let colClass;
|
79 |
|
80 | if (isObject(columnProp)) {
|
81 | const colSizeInterfix = isXs ? '-' : `-${colWidth}-`;
|
82 | colClass = getColumnSizeClass(isXs, colWidth, columnProp.size);
|
83 |
|
84 | colClasses.push(
|
85 | mapToCssModules(
|
86 | classNames({
|
87 | [colClass]: columnProp.size || columnProp.size === '',
|
88 | [`order${colSizeInterfix}${columnProp.order}`]:
|
89 | columnProp.order || columnProp.order === 0,
|
90 | [`offset${colSizeInterfix}${columnProp.offset}`]:
|
91 | columnProp.offset || columnProp.offset === 0,
|
92 | }),
|
93 | ),
|
94 | cssModule,
|
95 | );
|
96 | } else {
|
97 | colClass = getColumnSizeClass(isXs, colWidth, columnProp);
|
98 | colClasses.push(colClass);
|
99 | }
|
100 | });
|
101 |
|
102 | const colFormLabel = size || colClasses.length;
|
103 | const formLabel = !(check || colFormLabel);
|
104 |
|
105 | const classes = mapToCssModules(
|
106 | classNames(
|
107 | className,
|
108 | hidden ? 'visually-hidden' : false,
|
109 | check ? 'form-check-label' : false,
|
110 | size ? `col-form-label-${size}` : false,
|
111 | colClasses,
|
112 | colFormLabel ? 'col-form-label' : false,
|
113 | formLabel ? 'form-label' : false,
|
114 | ),
|
115 | cssModule,
|
116 | );
|
117 |
|
118 | return <Tag htmlFor={htmlFor} {...attributes} className={classes} />;
|
119 | }
|
120 |
|
121 | Label.propTypes = propTypes;
|
122 |
|
123 | export default Label;
|
124 |
|
\ | No newline at end of file |