Skip to content

Commit f3dabc0

Browse files
committed
feat(carbon): implemented optional isRequired for all fields
1 parent f2f4531 commit f3dabc0

File tree

12 files changed

+61
-58
lines changed

12 files changed

+61
-58
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import React from 'react';
2+
import { childrenPropTypes } from '@data-driven-forms/common/src/prop-types-templates';
3+
4+
import './is-required.scss';
5+
6+
const IsRequired = ({ children }) => (
7+
<React.Fragment>
8+
<span className="ddorg__carbon-component-mapper_is-required" aria-hidden="true">
9+
*
10+
</span>
11+
{children}
12+
</React.Fragment>
13+
);
14+
15+
IsRequired.propTypes = {
16+
children: childrenPropTypes
17+
};
18+
19+
export default IsRequired;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.ddorg__carbon-component-mapper_is-required {
2+
color: #E0182D;
3+
margin-right: 4px;
4+
}
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import React from 'react';
22
import WithDescription from './with-description';
3+
import IsRequired from './is-required';
34

4-
const prepareProps = ({ isDisabled, isReadOnly, label, description, ...props }) => ({
5+
export const buildLabel = (label, isRequired) => (label && (isRequired ? <IsRequired>{label}</IsRequired> : label)) || undefined;
6+
7+
const prepareProps = ({ isDisabled, isReadOnly, isRequired = false, label, description, ...props }) => ({
58
disabled: isDisabled,
6-
labelText: label,
9+
labelText: buildLabel(label, isRequired),
710
readOnly: isReadOnly,
811
...props,
9-
...(description ? { labelText: <WithDescription description={description} labelText={label || props.labelText} /> } : {})
12+
...(description ? { labelText: <WithDescription description={description} labelText={buildLabel(label || props.labelText, isRequired)} /> } : {})
1013
});
1114

1215
export default prepareProps;

packages/carbon-component-mapper/src/files/checkbox.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ import MultipleChoiceListCommon from '@data-driven-forms/common/src/multiple-cho
66
import { Checkbox as CarbonCheckbox, FormGroup } from 'carbon-components-react';
77

88
import WithDescription from '../common/with-description';
9-
import prepareProps from '../common/prepare-props';
9+
import prepareProps, { buildLabel } from '../common/prepare-props';
1010
import HelperTextBlock from '../common/helper-text-block';
1111

12-
const Wrapper = ({ label, description, children, helperText, error, showError }) => (
13-
<FormGroup legendText={description ? <WithDescription labelText={label} description={description} /> : label}>
12+
const Wrapper = ({ label, description, children, helperText, error, showError, isRequired }) => (
13+
<FormGroup
14+
legendText={description ? <WithDescription labelText={buildLabel(label, isRequired)} description={description} /> : buildLabel(label, isRequired)}
15+
>
1416
{children}
1517
<HelperTextBlock helperText={helperText} errorText={showError && error} />
1618
</FormGroup>
@@ -22,7 +24,8 @@ Wrapper.propTypes = {
2224
description: PropTypes.node,
2325
helperText: PropTypes.node,
2426
error: PropTypes.node,
25-
showError: PropTypes.bool
27+
showError: PropTypes.bool,
28+
isRequired: PropTypes.bool
2629
};
2730

2831
const SingleCheckbox = (props) => {
@@ -44,6 +47,7 @@ SingleCheckboxInCommon.propTypes = {
4447
label: PropTypes.node,
4548
input: PropTypes.object,
4649
isDisabled: PropTypes.bool,
50+
isRequired: PropTypes.bool,
4751
name: PropTypes.string,
4852
id: PropTypes.string,
4953
WrapperProps: PropTypes.object

packages/carbon-component-mapper/src/files/date-picker.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const DatePicker = (props) => {
2424

2525
DatePicker.propTypes = {
2626
isDisabled: PropTypes.bool,
27+
isRequired: PropTypes.bool,
2728
datePickerType: PropTypes.string,
2829
DatePickerProps: PropTypes.object,
2930
WrapperProps: PropTypes.object

packages/carbon-component-mapper/src/files/dual-list-select.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
StructuredListCell
1414
} from 'carbon-components-react/lib/components/StructuredList/StructuredList';
1515

16+
import { buildLabel } from '../common/prepare-props';
1617
import './dual-list-select.scss';
1718

1819
const EmptyList = ({ message }) => (
@@ -118,6 +119,7 @@ const DualListSelectInner = ({
118119
moveAllLeftTitle,
119120
moveAllRightTitle,
120121
label,
122+
isRequired,
121123
filterOptionsTitle,
122124
filterValuesTitle,
123125
sortOptionsTitle,
@@ -145,7 +147,7 @@ const DualListSelectInner = ({
145147
RightListProps,
146148
RightBodyProps
147149
}) => (
148-
<FormGroup legendText={label || ''} {...FormGroupProps}>
150+
<FormGroup legendText={buildLabel(label || '', isRequired)} {...FormGroupProps}>
149151
<Grid {...GridProps}>
150152
<Row condensed {...RowProps}>
151153
<Column sm={4} md={8} lg={5} {...OptionsColumnProps}>
@@ -248,6 +250,7 @@ DualListSelectInner.propTypes = {
248250
moveAllLeftTitle: PropTypes.node,
249251
moveAllRightTitle: PropTypes.node,
250252
label: PropTypes.node,
253+
isRequired: PropTypes.bool,
251254
filterOptionsTitle: PropTypes.string,
252255
filterValuesTitle: PropTypes.string,
253256
sortOptionsTitle: PropTypes.string,

packages/carbon-component-mapper/src/files/field-array.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ FieldArray.propTypes = {
140140
ArrayItemProps: PropTypes.object,
141141
RemoveButtonProps: PropTypes.object,
142142
defaultItem: PropTypes.any,
143+
isRequired: PropTypes.bool,
143144
fields: PropTypes.array
144145
};
145146

packages/carbon-component-mapper/src/files/radio.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Radio.propTypes = {
3030
FormGroupProps: PropTypes.object,
3131
isDisabled: PropTypes.bool,
3232
label: PropTypes.node,
33+
isRequired: PropTypes.bool,
3334
options: PropTypes.arrayOf(
3435
PropTypes.shape({
3536
label: PropTypes.node,

packages/carbon-component-mapper/src/files/select.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { useState, useEffect } from 'react';
2-
import PropTypes, { bool } from 'prop-types';
2+
import PropTypes from 'prop-types';
33
import { useFieldApi } from '@data-driven-forms/react-form-renderer';
44

55
import DataDrivenSelect from '@data-driven-forms/common/src/select';
@@ -63,6 +63,7 @@ ClearedMultiSelectFilterable.propTypes = {
6363
originalOnChange: PropTypes.func,
6464
carbonLabel: PropTypes.node,
6565
placeholder: PropTypes.node,
66+
isRequired: PropTypes.bool,
6667
isDisabled: PropTypes.bool
6768
};
6869

@@ -113,6 +114,7 @@ ClearedMultiSelect.propTypes = {
113114
originalOnChange: PropTypes.func,
114115
carbonLabel: PropTypes.node,
115116
placeholder: PropTypes.node,
117+
isRequired: PropTypes.bool,
116118
isDisabled: PropTypes.bool
117119
};
118120

@@ -158,8 +160,9 @@ ClearedSelect.propTypes = {
158160
carbonLabel: PropTypes.node,
159161
placeholder: PropTypes.node,
160162
isDisabled: PropTypes.bool,
161-
isSearchable: bool,
162-
isClearable: bool
163+
isRequired: PropTypes.bool,
164+
isSearchable: PropTypes.bool,
165+
isClearable: PropTypes.bool
163166
};
164167

165168
const Select = (props) => {
@@ -194,6 +197,7 @@ const Select = (props) => {
194197

195198
Select.propTypes = {
196199
isDisabled: PropTypes.bool,
200+
isRequired: PropTypes.bool,
197201
options: PropTypes.arrayOf(
198202
PropTypes.shape({
199203
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),

packages/carbon-component-mapper/src/files/switch.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const Switch = (props) => {
2323
Switch.propTypes = {
2424
isDisabled: PropTypes.bool,
2525
isReadOnly: PropTypes.bool,
26+
isRequired: PropTypes.bool,
2627
label: PropTypes.node,
2728
labelText: PropTypes.node,
2829
description: PropTypes.node,

0 commit comments

Comments
 (0)