Skip to content

Commit 651de2c

Browse files
authored
feat(orchestrator): a wizard step is skipped if both empty and set to 'hidden' (#1236)
Signed-off-by: Marek Libra <marek.libra@gmail.com>
1 parent b3a2059 commit 651de2c

3 files changed

Lines changed: 54 additions & 3 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@red-hat-developer-hub/backstage-plugin-orchestrator-form-react': patch
3+
---
4+
5+
A wizard step is not rendered if it's properites are both empty and "ui:widget" is set to "hidden".

workspaces/orchestrator/plugins/orchestrator-form-react/report.api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { OrchestratorFormContextProps } from '@red-hat-developer-hub/backstage-p
1010
import { default as React_2 } from 'react';
1111

1212
// @public
13-
export const OrchestratorForm: ({ schema, updateSchema, handleExecute, isExecuting, initialFormData, isDataReadonly, setAuthTokenDescriptors, }: OrchestratorFormProps) => React_2.JSX.Element;
13+
export const OrchestratorForm: ({ schema: rawSchema, updateSchema, handleExecute, isExecuting, initialFormData, isDataReadonly, setAuthTokenDescriptors, }: OrchestratorFormProps) => React_2.JSX.Element;
1414

1515
// @public
1616
export type OrchestratorFormProps = {

workspaces/orchestrator/plugins/orchestrator-form-react/src/components/OrchestratorForm.tsx

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ import React, { Fragment } from 'react';
1919
import { JsonObject } from '@backstage/types';
2020

2121
import { UiSchema } from '@rjsf/utils';
22-
import type { JSONSchema7 } from 'json-schema';
22+
import type { JSONSchema7, JSONSchema7Definition } from 'json-schema';
23+
import cloneDeep from 'lodash/cloneDeep';
24+
import get from 'lodash/get';
2325

2426
import { OrchestratorFormContextProps } from '@red-hat-developer-hub/backstage-plugin-orchestrator-form-api';
2527

@@ -51,12 +53,54 @@ export type OrchestratorFormProps = {
5153
isDataReadonly?: boolean;
5254
};
5355

56+
/**
57+
* Remove hidden steps from the schema.
58+
*
59+
* A wizard step is removed when
60+
* "type": "object"
61+
* "ui:widget": "hidden"
62+
* and properties are empty ("properties": {})
63+
*
64+
* @param schema - The schema to remove hidden steps from.
65+
* @returns The schema with hidden steps removed.
66+
*/
67+
const removeHiddenSteps = (schema: JSONSchema7): JSONSchema7 => {
68+
if (typeof schema.properties === 'object') {
69+
const hiddenSteps = Object.entries(schema.properties)
70+
.map(([key, value]: [string, JSONSchema7Definition]) => {
71+
const uiWidget = get(value, 'ui:widget');
72+
if (
73+
typeof value !== 'boolean' &&
74+
value.type === 'object' &&
75+
uiWidget === 'hidden' &&
76+
value.properties &&
77+
Object.keys(value.properties).length === 0
78+
) {
79+
return key;
80+
}
81+
return undefined;
82+
})
83+
.filter(Boolean) as string[];
84+
85+
if (hiddenSteps.length > 0) {
86+
const newSchema = cloneDeep(schema);
87+
hiddenSteps.forEach(step => {
88+
delete newSchema.properties?.[step];
89+
});
90+
91+
return newSchema;
92+
}
93+
}
94+
95+
return schema;
96+
};
97+
5498
/**
5599
* @public
56100
* The component contains the react-json-schema-form and serves as an extensible form. It allows loading a custom plugin decorator to override the default react-json-schema-form properties.
57101
*/
58102
const OrchestratorForm = ({
59-
schema,
103+
schema: rawSchema,
60104
updateSchema,
61105
handleExecute,
62106
isExecuting,
@@ -69,6 +113,8 @@ const OrchestratorForm = ({
69113
initialFormData ? () => structuredClone(initialFormData) : {},
70114
);
71115

116+
const schema = React.useMemo(() => removeHiddenSteps(rawSchema), [rawSchema]);
117+
72118
const numStepsInMultiStepSchema = React.useMemo(
73119
() => getNumSteps(schema),
74120
[schema],

0 commit comments

Comments
 (0)