Skip to content
This repository was archived by the owner on Jun 26, 2026. It is now read-only.

Commit 1d9d26b

Browse files
kvapsclaudelexfrei
authored
feat(console): dynamic form dropdowns via x-cozystack-options (DynamicOptionsWidget) (#30)
* feat(console): unify form dropdowns on x-cozystack-options Add a single generic DynamicOptionsWidget that renders any string field carrying the x-cozystack-options schema keyword as a <select>, populated from the cozystack-api Option resource (one source per dropdown). A shared addDynamicOptionWidgets walker binds the widget across nested properties, array items and additionalProperties (so vm-instance disks[].name and kubernetes nodeGroups.* are covered); the AdditionalPropertiesField passes it through to its nested forms. Replaces the bespoke StorageClassWidget, VMDiskWidget and BackupClassWidget (default-StorageClass preselect and disk-size labels are preserved via the server-provided item.default flag and labels), so every form dropdown now uses one mechanism. Co-Authored-By: Claude <[email protected]> Signed-off-by: Andrei Kvapil <[email protected]> * feat(console): migrate backup form dropdowns to x-cozystack-options The standalone backup-resource create pages (Plan, BackupJob, Backup, RestoreJob) maintained a parallel client-side dropdown mechanism: they fetched BackupClasses / Plans / Backups / application kinds and injected them into the CRD schema as static enum arrays via enumMap. The CRD schemas now carry the x-cozystack-options keyword on these fields, so DynamicOptionsWidget renders them automatically from the cozystack-api Option resource. Drop the now-redundant enumMap entries and their backing useK8sList fetches for backupClassName, planRef.name, backupRef.name and applicationRef.kind / targetApplicationRef.kind. Keep applicationRef.name and targetApplicationRef.name on enumMap: they depend on the sibling kind field, a context the Option contract cannot express. Add unit coverage for the addDynamicOptionWidgets walker over the nested backup CRD shape. Co-Authored-By: Claude <[email protected]> Signed-off-by: Andrei Kvapil <[email protected]> * test(forms): cover DynamicOptionsWidget runtime behavior The migration to the generic widget removed the StorageClass/VMDisk/BackupClass widget test suites, dropping coverage of behavior that absorbed several prior bug fixes. Pin it on DynamicOptionsWidget: auto-selecting the server-marked default once, showing an explicit placeholder for a required empty select instead of the first option, keeping a committed value visible while the list loads, emitting undefined (not "") on clear, and resolving items only from the Option whose name matches the field's source. Assisted-By: Claude <[email protected]> Signed-off-by: Aleksei Sviridkin <[email protected]> * test(forms): cover the dynamic-options walker's items and additionalProperties branches The walker recurses into array items and additionalProperties maps — the whole reason it exists over inlining (vm-instance disks[].name, kubernetes nodeGroups.*.instanceType) — but only the properties path was tested. Add cases for both recursion branches. Assisted-By: Claude <[email protected]> Signed-off-by: Aleksei Sviridkin <[email protected]> * docs(forms): correct stale apiGroup-gating comments for the kind dropdown The applicationRef.kind field now resolves through the appkind option source, so it renders as a dropdown for every apiGroup — it is no longer gated to apps.cozystack.io. Only the applicationRef.name enum remains gated (it is served client-side and depends on the chosen kind). Update the comments that still described the old free-text-fallback behavior for the kind field. Assisted-By: Claude <[email protected]> Signed-off-by: Aleksei Sviridkin <[email protected]> * docs: describe the x-cozystack-options widget binding in the RJSF pipeline The forms pipeline section still listed the removed StorageClass/BackupClass/ VMDisk field-name widgets and framed binding as a field-name convention. Update it to describe addDynamicOptionWidgets binding any field carrying x-cozystack-options to DynamicOptionsWidget, recursing into properties, array items and additionalProperties. Assisted-By: Claude <[email protected]> Signed-off-by: Aleksei Sviridkin <[email protected]> * docs(forms): drop stale references to the removed per-field widgets Two comments still pointed at entities this migration removed: the backup-class access hook claimed the dropdown is powered by a tenant list on backupclasses (it is now served by the Option resource), and the SchemaForm default-emit comment cited plansData/backupClassesData as the async-sibling example (both deleted). Correct both to match the code. Assisted-By: Claude <[email protected]> Signed-off-by: Aleksei Sviridkin <[email protected]> * test(forms): pin the dynamic-options walker's oneOf/anyOf/allOf gap The walker recurses into properties/items/additionalProperties but not the composition keywords, mirroring the same intentional limitation in addSensitiveStringWidgets. Pin it with a FIXME so the gap is documented rather than silent. Assisted-By: Claude <[email protected]> Signed-off-by: Aleksei Sviridkin <[email protected]> * test(forms): pin VMDisk no-auto-default and the namespaced Option query The generic widget auto-selects only the server-marked default item, so a default-less source (vmdisk) must not auto-commit on load — the invariant the old VMDiskWidget enforced to avoid dropping the user's disk on a fast submit. Also pin that the Option resource is listed namespaced to the active tenant (querying it cluster-wide would return nothing). Assisted-By: Claude <[email protected]> Signed-off-by: Aleksei Sviridkin <[email protected]> * test(forms): assert the dynamic-options walker does not mutate its input uiSchema The pipeline contract (and the sibling sensitive-fields walker) requires the binders to return a new object and leave the input untouched. Pin it for addDynamicOptionWidgets: a nested input uiSchema stays byte-identical and the result's touched sub-objects are fresh references. Assisted-By: Claude <[email protected]> Signed-off-by: Aleksei Sviridkin <[email protected]> * fix(forms): keep a cleared optional dropdown cleared instead of re-applying the default The select onChange reset the auto-default guard whenever the field was cleared, which re-armed the effect: for an optional field with a server-marked default, clearing emitted undefined, the effect then saw no value and re-selected the default, so the field could never be cleared. Latch the guard after the first auto-default and never reset it — clearing now sticks, matching the comment's stated intent. Required fields were unaffected (their empty option is disabled). Assisted-By: Claude <[email protected]> Signed-off-by: Aleksei Sviridkin <[email protected]> --------- Signed-off-by: Andrei Kvapil <[email protected]> Signed-off-by: Aleksei Sviridkin <[email protected]> Co-authored-by: Claude <[email protected]> Co-authored-by: Aleksei Sviridkin <[email protected]>
1 parent cecb486 commit 1d9d26b

19 files changed

Lines changed: 640 additions & 825 deletions

‎CLAUDE.md‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,8 @@ schema). The pipeline in `apps/console/src/components/SchemaForm.tsx`:
9696
2. `keysOrderToUiSchema` reads `spec.dashboard.keysOrder` and emits per-level
9797
`ui:order` arrays.
9898
3. A chain of `addXxxWidgets(schema, uiSchema)` walks the schema and binds
99-
widgets by **field name** convention:
100-
- `storageClass` → `StorageClassWidget`
101-
- `backupClassName` → `BackupClassWidget`
102-
- `disks[].name` → `VMDiskWidget`
99+
widgets:
100+
- any field carrying the `x-cozystack-options` schema keyword → `DynamicOptionsWidget`, a runtime dropdown populated from the cluster's `Option` resource (`core.cozystack.io`) keyed by the keyword's `source`; `addDynamicOptionWidgets` (`lib/dynamic-options.ts`) recurses into `properties`, array `items` and `additionalProperties`, and replaces the former field-name-bound `StorageClassWidget` / `BackupClassWidget` / `VMDiskWidget`.
103101
- object with `additionalProperties: <schema>` → `AdditionalPropertiesField`
104102
- credential-shaped fields (`password`, `*token`, `*accessKey`, …) →
105103
`SensitiveStringWidget` — see `lib/sensitive-fields.ts` and its tests for

‎apps/console/src/components/AdditionalPropertiesField.tsx‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,24 @@ import type { FieldProps, RJSFSchema, TemplatesType } from "@rjsf/utils"
33
import Form from "@rjsf/core"
44
import validator from "@rjsf/validator-ajv8"
55
import { customTemplates, customWidgets } from "./rjsf-templates.tsx"
6+
import { addDynamicOptionWidgets } from "../lib/dynamic-options.ts"
67

78
export function AdditionalPropertiesField(props: FieldProps) {
89
const { schema, formData, onChange, readonly, disabled, name, required } = props
910
const [newKey, setNewKey] = useState("")
1011

1112
// Get the schema for items from additionalProperties
12-
const itemSchema = (schema.additionalProperties as RJSFSchema) || {}
13+
const itemSchema = useMemo(
14+
() => (schema.additionalProperties as RJSFSchema) || {},
15+
[schema.additionalProperties],
16+
)
1317
const keys = Object.keys(formData || {})
1418

19+
// The nested <Form> renders its own subtree, so it needs its own uiSchema to
20+
// bind x-cozystack-options fields (e.g. nodeGroups.*.instanceType) to the
21+
// DynamicOptionsWidget — the parent SchemaForm's uiSchema does not reach here.
22+
const itemUiSchema = useMemo(() => addDynamicOptionWidgets(itemSchema), [itemSchema])
23+
1524
// Create templates without submit button for nested forms
1625
const templatesWithoutSubmit = useMemo<Partial<TemplatesType>>(() => {
1726
return {
@@ -79,6 +88,7 @@ export function AdditionalPropertiesField(props: FieldProps) {
7988
<Form
8089
tagName="div"
8190
schema={itemSchema}
91+
uiSchema={itemUiSchema}
8292
formData={formData[key]}
8393
validator={validator}
8494
templates={templatesWithoutSubmit}

‎apps/console/src/components/BackupClassWidget.test.tsx‎

Lines changed: 0 additions & 110 deletions
This file was deleted.

‎apps/console/src/components/BackupClassWidget.tsx‎

Lines changed: 0 additions & 61 deletions
This file was deleted.

0 commit comments

Comments
 (0)