Skip to content

Commit 5a82ded

Browse files
committed
add required prop to Inputs
closes eclipsesource#1695 Signed-off-by: Lukas Boll <lukas-bool@web.de>
1 parent ba6bdc5 commit 5a82ded

File tree

4 files changed

+36
-20
lines changed

4 files changed

+36
-20
lines changed

packages/core/src/util/renderer.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,19 @@ export const computeLabel = (
9898
return required && !hideRequiredAsterisk ? label + '*' : label;
9999
};
100100

101+
/**
102+
* Decides whether to mark a field as required.
103+
*
104+
* @param {boolean} required whether the label belongs to a control which is required
105+
* @returns {boolean} should the field be marked as required
106+
*/
107+
export const displayRequired = (
108+
required: boolean,
109+
hideRequiredAsterisk: boolean
110+
): boolean => {
111+
return required && !hideRequiredAsterisk;
112+
};
113+
101114
/**
102115
* Create a default value based on the given scheam.
103116
* @param schema the schema for which to create a default value.

packages/material/src/controls/MaterialInputControl.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
*/
2525
import React from 'react';
2626
import {
27-
computeLabel,
27+
displayRequired,
2828
ControlProps,
2929
ControlState,
3030
isDescriptionHidden,
@@ -84,12 +84,10 @@ export abstract class MaterialInputControl extends Control<
8484
<InputLabel
8585
htmlFor={id + '-input'}
8686
error={!isValid}
87+
required={displayRequired(required,
88+
appliedUiSchemaOptions.hideRequiredAsterisk)}
8789
>
88-
{computeLabel(
89-
label,
90-
required,
91-
appliedUiSchemaOptions.hideRequiredAsterisk
92-
)}
90+
{label}
9391
</InputLabel>
9492
<InnerComponent
9593
{...this.props}

packages/material/src/controls/MaterialRadioGroup.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
import merge from 'lodash/merge';
2626
import React from 'react';
2727
import {
28-
computeLabel,
2928
ControlProps,
3029
ControlState,
30+
displayRequired,
3131
isDescriptionHidden,
3232
OwnPropsOfEnum
3333
} from '@jsonforms/core';
@@ -81,12 +81,10 @@ export class MaterialRadioGroup extends Control<
8181
htmlFor={id}
8282
error={!isValid}
8383
component={'legend' as 'label'}
84+
required={displayRequired(required,
85+
appliedUiSchemaOptions.hideRequiredAsterisk)}
8486
>
85-
{computeLabel(
86-
label,
87-
required,
88-
appliedUiSchemaOptions.hideRequiredAsterisk
89-
)}
87+
{label}
9088
</FormLabel>
9189

9290
<RadioGroup

packages/material/src/controls/MaterialSliderControl.tsx

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
*/
2525
import React from 'react';
2626
import {
27-
computeLabel,
2827
ControlProps,
2928
ControlState,
29+
displayRequired,
3030
isDescriptionHidden,
3131
isRangeControl,
3232
RankedTester,
@@ -37,6 +37,7 @@ import { Control, withJsonFormsControlProps } from '@jsonforms/react';
3737
import {
3838
FormControl,
3939
FormHelperText,
40+
FormLabel,
4041
Hidden,
4142
Slider,
4243
Typography
@@ -94,14 +95,20 @@ export class MaterialSliderControl extends Control<ControlProps, ControlState> {
9495
onFocus={this.onFocus}
9596
onBlur={this.onBlur}
9697
id={id}
98+
required={displayRequired(required,
99+
appliedUiSchemaOptions.hideRequiredAsterisk)}
97100
>
98-
<Typography id={id + '-typo'} style={labelStyle} variant='caption'>
99-
{computeLabel(
100-
label,
101-
required,
102-
appliedUiSchemaOptions.hideRequiredAsterisk
103-
)}
104-
</Typography>
101+
<FormLabel
102+
htmlFor={id}
103+
error={!isValid}
104+
component={'legend' as 'label'}
105+
required={displayRequired(required,
106+
appliedUiSchemaOptions.hideRequiredAsterisk)}
107+
>
108+
<Typography id={id + '-typo'} style={labelStyle} variant='caption'>
109+
{label}
110+
</Typography>
111+
</FormLabel>
105112
<div style={rangeContainerStyle}>
106113
<Typography style={rangeItemStyle} variant='caption' align='left'>
107114
{schema.minimum}

0 commit comments

Comments
 (0)