Skip to content

Add required prop to Inputs #1798

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions packages/core/src/util/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ const isRequired = (
*
* @param {string} label the label string
* @param {boolean} required whether the label belongs to a control which is required
* @param {boolean} hideRequiredAsterisk applied UI Schema option
* @returns {string} the label string
*/
export const computeLabel = (
Expand All @@ -98,6 +99,20 @@ export const computeLabel = (
return required && !hideRequiredAsterisk ? label + '*' : label;
};

/**
* Indicates whether to mark a field as required.
*
* @param {boolean} required whether the label belongs to a control which is required
* @param {boolean} hideRequiredAsterisk applied UI Schema option
* @returns {boolean} should the field be marked as required
*/
export const showAsRequired = (
required: boolean,
hideRequiredAsterisk: boolean
): boolean => {
return required && !hideRequiredAsterisk;
};

/**
* Create a default value based on the given scheam.
* @param schema the schema for which to create a default value.
Expand Down
10 changes: 4 additions & 6 deletions packages/material/src/controls/MaterialInputControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/
import React from 'react';
import {
computeLabel,
showAsRequired,
ControlProps,
ControlState,
isDescriptionHidden,
Expand Down Expand Up @@ -84,12 +84,10 @@ export abstract class MaterialInputControl extends Control<
<InputLabel
htmlFor={id + '-input'}
error={!isValid}
required={showAsRequired(required,
appliedUiSchemaOptions.hideRequiredAsterisk)}
>
{computeLabel(
label,
required,
appliedUiSchemaOptions.hideRequiredAsterisk
)}
{label}
</InputLabel>
<InnerComponent
{...this.props}
Expand Down
10 changes: 4 additions & 6 deletions packages/material/src/controls/MaterialNativeControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
*/
import React from 'react';
import {
computeLabel,
ControlProps,
ControlState,
showAsRequired,
isDateControl,
isDescriptionHidden,
isTimeControl,
Expand Down Expand Up @@ -73,12 +73,10 @@ export class MaterialNativeControl extends Control<ControlProps, ControlState> {
return (
<Hidden xsUp={!visible}>
<TextField
required={showAsRequired(required,
appliedUiSchemaOptions.hideRequiredAsterisk)}
id={id + '-input'}
label={computeLabel(
label,
required,
appliedUiSchemaOptions.hideRequiredAsterisk
)}
label={label}
type={fieldType}
error={!isValid}
disabled={!enabled}
Expand Down
10 changes: 4 additions & 6 deletions packages/material/src/controls/MaterialRadioGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
import merge from 'lodash/merge';
import React from 'react';
import {
computeLabel,
ControlProps,
ControlState,
showAsRequired,
isDescriptionHidden,
OwnPropsOfEnum
} from '@jsonforms/core';
Expand Down Expand Up @@ -81,12 +81,10 @@ export class MaterialRadioGroup extends Control<
htmlFor={id}
error={!isValid}
component={'legend' as 'label'}
required={showAsRequired(required,
appliedUiSchemaOptions.hideRequiredAsterisk)}
>
{computeLabel(
label,
required,
appliedUiSchemaOptions.hideRequiredAsterisk
)}
{label}
</FormLabel>

<RadioGroup
Expand Down
21 changes: 13 additions & 8 deletions packages/material/src/controls/MaterialSliderControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
*/
import React from 'react';
import {
computeLabel,
ControlProps,
ControlState,
showAsRequired,
isDescriptionHidden,
isRangeControl,
RankedTester,
Expand All @@ -37,6 +37,7 @@ import { Control, withJsonFormsControlProps } from '@jsonforms/react';
import {
FormControl,
FormHelperText,
FormLabel,
Hidden,
Slider,
Typography
Expand Down Expand Up @@ -95,13 +96,17 @@ export class MaterialSliderControl extends Control<ControlProps, ControlState> {
onBlur={this.onBlur}
id={id}
>
<Typography id={id + '-typo'} style={labelStyle} variant='caption'>
{computeLabel(
label,
required,
appliedUiSchemaOptions.hideRequiredAsterisk
)}
</Typography>
<FormLabel
htmlFor={id}
error={!isValid}
component={'legend' as 'label'}
required={showAsRequired(required,
appliedUiSchemaOptions.hideRequiredAsterisk)}
>
<Typography id={id + '-typo'} style={labelStyle} variant='caption'>
{label}
</Typography>
</FormLabel>
<div style={rangeContainerStyle}>
<Typography style={rangeItemStyle} variant='caption' align='left'>
{schema.minimum}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ describe('Material input control', () => {
</JsonFormsStateProvider>
);
const label = wrapper.find('label').first();
expect(label.text()).toBe('Date Cell*');
expect(label.text()).toBe('Date Cell*');
});

it('should not display a marker for a non-required prop', () => {
Expand Down