Skip to content

Support for "additionalProperties" in string with choices #183

@rptmat57

Description

@rptmat57

Hi,
I was wondering if there was a way to have additional properties or a custom input option for the string type with choices.
This would be useful when we want to offer a list of options but also let the user enter their own (without using a separate field)

Technically speaking, I believe this could be done by using the HTML input datalist attribute? https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/datalist

here is a Widget implementation I've used in the past:

class DatalistWidget(forms.TextInput):
    def __init__(self, attrs=None):
        if attrs is not None:
            attrs = attrs.copy()
            self.choices = attrs.pop("choices", [])
        super().__init__(attrs)

    def render(self, name, value, attrs=None, renderer=None):
        if attrs is None:
            attrs = {}
        attrs["list"] = f"{name}_datalist"
        options = self.choices
        datalist = f'<datalist id="{name}_datalist">'
        for option_value, option_label in options:
            datalist += f'<option value="{option_value}">{option_label}</option>'
        datalist += "</datalist>"
        return super().render(name, value, attrs, renderer) + datalist

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions