mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-07-19 10:24:50 +02:00
Implement a user friendly part select element.
This commit is contained in:
parent
c78bc01d23
commit
670dd76ef5
5 changed files with 175 additions and 3 deletions
|
@ -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,
|
||||
])
|
||||
|
||||
|
|
54
src/Form/Type/PartSelectType.php
Normal file
54
src/Form/Type/PartSelectType.php
Normal 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;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue