Implement a user friendly part select element.

This commit is contained in:
Jan Böhmer 2022-12-24 14:04:46 +01:00
parent c78bc01d23
commit 670dd76ef5
5 changed files with 175 additions and 3 deletions

View file

@ -4,6 +4,7 @@ namespace App\Form\ProjectSystem;
use App\Entity\Parts\Part;
use App\Entity\ProjectSystem\ProjectBOMEntry;
use App\Form\Type\PartSelectType;
use Svg\Tag\Text;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
@ -23,9 +24,7 @@ class ProjectBOMEntryType extends AbstractType
'label' => 'project.bom.quantity',
])
->add('part', EntityType::class, [
'class' => Part::class,
'choice_label' => 'name',
->add('part', PartSelectType::class, [
'required' => false,
])

View file

@ -0,0 +1,54 @@
<?php
namespace App\Form\Type;
use App\Entity\Parts\Part;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\ChoiceList\ChoiceList;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class PartSelectType extends AbstractType
{
private UrlGeneratorInterface $urlGenerator;
public function __construct(UrlGeneratorInterface $urlGenerator)
{
$this->urlGenerator = $urlGenerator;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'class' => Part::class,
'choice_label' => 'name',
'placeholder' => 'None'
]);
$resolver->setDefaults([
'attr' => [
'data-controller' => 'elements--part-select',
'data-autocomplete' => $this->urlGenerator->generate('typeahead_parts', ['query' => '__QUERY__']),
//Disable browser autocomplete
'autocomplete' => 'off',
],
]);
$resolver->setDefaults(['choices' => []]);
$resolver->setDefaults([
'choice_attr' => ChoiceList::attr($this, function (?Part $part) {
return $part ? [
//'data-description' => $part->getDescription(),
//'data-category' => $part->getCategory() ? $part->getCategory()->getName() : '',
] : [];
})
]);
}
public function getParent()
{
return EntityType::class;
}
}