Add multiple option to text selector (#104635)

Co-authored-by: Robert Resch <robert@resch.dev>
This commit is contained in:
Paul Bottein 2023-11-29 18:32:32 +01:00 committed by GitHub
parent dfed10420c
commit 38eda9f46e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 3 deletions

View File

@ -1206,6 +1206,7 @@ class TextSelectorConfig(TypedDict, total=False):
suffix: str
type: TextSelectorType
autocomplete: str
multiple: bool
class TextSelectorType(StrEnum):
@ -1243,6 +1244,7 @@ class TextSelector(Selector[TextSelectorConfig]):
vol.Coerce(TextSelectorType), lambda val: val.value
),
vol.Optional("autocomplete"): str,
vol.Optional("multiple", default=False): bool,
}
)
@ -1250,10 +1252,14 @@ class TextSelector(Selector[TextSelectorConfig]):
"""Instantiate a selector."""
super().__init__(config)
def __call__(self, data: Any) -> str:
def __call__(self, data: Any) -> str | list[str]:
"""Validate the passed selection."""
text: str = vol.Schema(str)(data)
return text
if not self.config["multiple"]:
text: str = vol.Schema(str)(data)
return text
if not isinstance(data, list):
raise vol.Invalid("Value should be a list")
return [vol.Schema(str)(val) for val in data]
class ThemeSelectorConfig(TypedDict):

View File

@ -832,6 +832,7 @@ def test_selector_in_serializer() -> None:
"selector": {
"text": {
"multiline": False,
"multiple": False,
}
}
}

View File

@ -602,6 +602,11 @@ def test_object_selector_schema(schema, valid_selections, invalid_selections) ->
({"multiline": True}, (), ()),
({"multiline": False, "type": "email"}, (), ()),
({"prefix": "before", "suffix": "after"}, (), ()),
(
{"multiple": True},
(["abc123", "def456"],),
("abc123", None, ["abc123", None]),
),
),
)
def test_text_selector_schema(schema, valid_selections, invalid_selections) -> None: