Skip to content

Commit

Permalink
feat: Added new DataListTextType to render HTML input with their da…
Browse files Browse the repository at this point in the history
…talist.
  • Loading branch information
ambroisemaupate committed Jan 25, 2024
1 parent eea6399 commit 5316137
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
43 changes: 43 additions & 0 deletions lib/RoadizCoreBundle/src/Form/DataListTextType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace RZ\Roadiz\CoreBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;

class DataListTextType extends AbstractType
{
public function configureOptions(OptionsResolver $resolver): void
{
parent::configureOptions($resolver);

$resolver->setRequired('listName');
$resolver->setAllowedTypes('listName', 'string');
$resolver->setRequired('list');
$resolver->setAllowedTypes('list', 'array');
}

public function buildView(FormView $view, FormInterface $form, array $options): void
{
parent::buildView($view, $form, $options);

$view->vars['listName'] = $options['listName'];
$view->vars['list'] = $options['list'];
}


public function getBlockPrefix(): string
{
return 'data_list_text';
}

public function getParent(): string
{
return TextType::class;
}
}
17 changes: 17 additions & 0 deletions lib/Rozier/src/Resources/views/forms.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,23 @@
{%- endif -%}
{%- endblock date_widget -%}

{%- block data_list_text_widget -%}
{%- set type = type|default('text') -%}
{%- if type == 'range' or type == 'color' -%}
{# Attribute "required" is not supported #}
{%- set required = false -%}
{%- endif -%}

<input list="datalist-{{ form.vars.listName }}" type="{{ type }}" {{ block('widget_attributes') }}{% if value is not empty %} value="{{ value }}"{% endif %}>
{% if form.vars.list %}
<datalist id="datalist-{{ form.vars.listName }}">
{% for choice in form.vars.list %}
<option value="{{ choice }}"></option>
{% endfor %}
</datalist>
{% endif %}
{%- endblock data_list_text_widget -%}

{% block markdown_widget %}
{% apply spaceless %}
<div class="markdown-editor-wrapper">
Expand Down

0 comments on commit 5316137

Please sign in to comment.