Skip to content

Commit

Permalink
chore: improve optional label handling
Browse files Browse the repository at this point in the history
  • Loading branch information
BCerki committed Aug 8, 2022
1 parent 60eefd6 commit b39a204
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
1 change: 1 addition & 0 deletions app/data/jsonSchemaForm/projectEmissionIntensitySchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export const emissionIntensityReportUiSchema = {
calculatedGHGEmissionIntensityPerformance: {
"ui:widget": "ReadOnlyCalculatedValueWidget",
isPercent: true,
hideOptional: true,
},
adjustedGHGEmissionIntensityPerformance: {
"ui:widget": "AdjustableCalculatedValueWidget",
Expand Down
8 changes: 7 additions & 1 deletion app/lib/theme/FieldTemplate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,17 @@ const FieldTemplate: React.FC<FieldTemplateProps> = ({
displayLabel,
required,
id,
uiSchema,
}) => {
return (
<div>
{displayLabel && (
<FieldLabel label={label} required={required} htmlFor={id} />
<FieldLabel
label={label}
required={required}
htmlFor={id}
uiSchema={uiSchema}
/>
)}
{help}
{children}
Expand Down
9 changes: 5 additions & 4 deletions app/lib/theme/widgets/FieldLabel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@ interface Props {
required: boolean;
htmlFor: string;
tagName?: "label" | "dt";
uiSchema?: any;
}

const FieldLabel: React.FC<Props> = ({
label,
required,
htmlFor,
tagName = "label",
uiSchema,
}) => {
if (!label) {
return null;
}

const displayedLabel =
label === "GHG Emission Intensity Performance"
? "GHG Emission Intensity Performance"
: label + (required ? "" : " (optional)") + " ";
const displayedLabel = uiSchema?.hideOptional
? label
: label + (required ? "" : " (optional)") + " ";

if (tagName === "label")
return <label htmlFor={htmlFor}>{displayedLabel}</label>;
Expand Down

0 comments on commit b39a204

Please sign in to comment.