From 0670b0b5cb28a6322364f791560ce8d6a72c27ff Mon Sep 17 00:00:00 2001 From: Maximilian Haye Date: Mon, 16 Dec 2024 13:04:36 +0100 Subject: [PATCH] feat: add GeneratedIdElement See: questionpy-org/questionpy-sdk#140 --- docs/qppe-server.yaml | 12 ++++++++++++ questionpy_common/elements.py | 11 +++++++++++ 2 files changed, 23 insertions(+) diff --git a/docs/qppe-server.yaml b/docs/qppe-server.yaml index d4bebfa..283fb64 100644 --- a/docs/qppe-server.yaml +++ b/docs/qppe-server.yaml @@ -1429,6 +1429,17 @@ components: minItems: 1 required: [ kind, name, initial_elements, increment, elements ] + GeneratedIdElement: + type: object + description: Generates a unique ID which won't change across form saves. + properties: + kind: + type: string + enum: [ id ] + name: + $ref: "#/components/schemas/FormName" + required: [ kind, name ] + FormElement: oneOf: - $ref: "#/components/schemas/StaticTextElement" @@ -1440,6 +1451,7 @@ components: - $ref: "#/components/schemas/HiddenElement" - $ref: "#/components/schemas/GroupElement" - $ref: "#/components/schemas/RepetitionElement" + - $ref: "#/components/schemas/GeneratedIdElement" FormSection: type: object diff --git a/questionpy_common/elements.py b/questionpy_common/elements.py index c0b5a1e..491cf0a 100644 --- a/questionpy_common/elements.py +++ b/questionpy_common/elements.py @@ -14,6 +14,7 @@ "CheckboxGroupElement", "FormElement", "FormSection", + "GeneratedIdElement", "GroupElement", "HiddenElement", "Option", @@ -167,6 +168,15 @@ class RepetitionElement(_BaseElement): """Elements that will be repeated.""" +class GeneratedIdElement(_BaseElement): + """Generates a unique ID which won't change across form saves. + + Useful to distinguish repetitions without relying on their index, which may change when repetitions are removed. + """ + + kind: Literal["id"] = "id" + + FormElement: TypeAlias = Annotated[ CheckboxElement | CheckboxGroupElement @@ -174,6 +184,7 @@ class RepetitionElement(_BaseElement): | HiddenElement | RadioGroupElement | RepetitionElement + | GeneratedIdElement | SelectElement | StaticTextElement | TextInputElement