-
-
Notifications
You must be signed in to change notification settings - Fork 394
Add ux collection type component #397
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
alexander-schranz
wants to merge
34
commits into
symfony:2.x
Choose a base branch
from
alexander-schranz:feature/ux-collection-type
base: 2.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
dbba3b8
UX FormCollection
stakovicz 83ec2ea
Remove hard coded property_name __name__
stakovicz d983cf0
PHP CS Fixer
stakovicz a35ff32
First jests
stakovicz e1d7574
Rename CollectionType > UXCollectionType
stakovicz 409c8e7
Rename CollectionType > UXCollectionType
stakovicz a624f25
DependencyInjection Clean
stakovicz f4ba011
Fix .gitattributes
stakovicz 7741907
Move default values
stakovicz 66defab
Predefined theme or not
stakovicz 145b1cb
Update src/FormCollection/README.md
stakovicz 2b900cc
Update src/FormCollection/README.md
stakovicz 25c1454
Update src/FormCollection/README.md
stakovicz d66250d
Update src/FormCollection/Resources/views/form_theme_div.html.twig
stakovicz 2db95e0
Update src/FormCollection/README.md
stakovicz 8f8b5c2
Update src/FormCollection/Resources/views/form_theme_table.html.twig
stakovicz 4a68faa
Split in 4 options
stakovicz a170618
Default startIndex value
stakovicz 13561e7
Update src/FormCollection/Resources/views/form_theme_div.html.twig
stakovicz fb847c6
Update src/FormCollection/Resources/views/form_theme_div.html.twig
stakovicz 740d19b
Update src/FormCollection/Resources/views/form_theme_table.html.twig
stakovicz c57d86e
Update src/FormCollection/Resources/views/form_theme_table.html.twig
stakovicz 0bbac2f
Fix coding-style-js
stakovicz edfe323
Prettier
stakovicz 086c3a3
Rebase and refresh the code
stakovicz 480dbb5
fix TU
stakovicz 3223fae
change buttons attr
stakovicz 911f18f
Move logic from twig files to form types
alexander-schranz 4cfa27d
Fix types and not used imports
alexander-schranz 0976a9e
Allow overriding the data-controller via attr
alexander-schranz dc18974
Simplify controller integration
alexander-schranz 9ee00dc
Fix handling of nested blocks
alexander-schranz d294592
Move docs to index.rst
alexander-schranz 4edfefb
Fix add problem
alexander-schranz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| /.gitattributes export-ignore | ||
| /.gitignore export-ignore | ||
| /Resources/assets/test export-ignore |
32 changes: 32 additions & 0 deletions
32
src/FormCollection/DependencyInjection/FormCollectionExtension.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Symfony package. | ||
| * | ||
| * (c) Fabien Potencier <[email protected]> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Symfony\UX\FormCollection\DependencyInjection; | ||
|
|
||
| use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
| use Symfony\Component\DependencyInjection\Definition; | ||
| use Symfony\Component\HttpKernel\DependencyInjection\Extension; | ||
| use Symfony\UX\FormCollection\Form\UXCollectionType; | ||
|
|
||
| /** | ||
| * @internal | ||
| */ | ||
| class FormCollectionExtension extends Extension | ||
| { | ||
| public function load(array $configs, ContainerBuilder $container) | ||
| { | ||
| $container | ||
| ->setDefinition('form.ux_collection', new Definition(UXCollectionType::class)) | ||
| ->addTag('form.type') | ||
| ->setPublic(false) | ||
| ; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| <?php | ||
|
|
||
| namespace Symfony\UX\FormCollection\Form; | ||
|
|
||
| use Symfony\Component\Form\AbstractType; | ||
| use Symfony\Component\Form\Extension\Core\Type\ButtonType; | ||
| use Symfony\Component\Form\FormBuilderInterface; | ||
| use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
|
||
| /** | ||
| * @final | ||
| * @experimental | ||
| */ | ||
| class UXCollectionEntryToolbarType extends AbstractType | ||
| { | ||
| public function buildForm(FormBuilderInterface $builder, array $options): void | ||
| { | ||
| if ($options['allow_delete']) { | ||
| $builder->add( | ||
| 'uxCollectionEntryDeleteButton', | ||
| ButtonType::class, | ||
| $options['delete_options'], | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| public function configureOptions(OptionsResolver $resolver): void | ||
| { | ||
| $resolver->setDefaults([ | ||
| 'label' => false, | ||
| 'allow_delete' => false, | ||
| 'delete_options' => [], | ||
| ]); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| <?php | ||
|
|
||
| namespace Symfony\UX\FormCollection\Form; | ||
|
|
||
| use Symfony\Component\Form\AbstractType; | ||
| use Symfony\Component\Form\Extension\Core\Type\TextType; | ||
| use Symfony\Component\Form\FormBuilderInterface; | ||
| use Symfony\Component\Form\FormInterface; | ||
| use Symfony\Component\Form\FormView; | ||
| use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
|
||
| /** | ||
| * @final | ||
| * @experimental | ||
| * | ||
| * @internal only for internal usage to add the toolbar to every entry of a collection | ||
| */ | ||
| class UXCollectionEntryType extends AbstractType | ||
| { | ||
| public function buildForm(FormBuilderInterface $builder, array $options): void | ||
| { | ||
| $builder->add( | ||
| 'entry', | ||
| $options['entry_type'], | ||
| $options['entry_options'], | ||
| ); | ||
| } | ||
|
|
||
| public function buildView(FormView $view, FormInterface $form, array $options): void | ||
| { | ||
| $form->add('toolbar', UXCollectionEntryToolbarType::class, [ | ||
| 'allow_delete' => $options['allow_delete'], | ||
| 'delete_options' => $options['delete_options'], | ||
| ]); | ||
| } | ||
|
|
||
| public function configureOptions(OptionsResolver $resolver): void | ||
| { | ||
| $resolver->setDefaults([ | ||
| 'entry_type' => TextType::class, | ||
| 'entry_options' => [], | ||
| 'label' => false, | ||
| 'allow_delete' => false, | ||
| 'delete_options' => [], | ||
| 'row_attr' => [ | ||
| 'data-collection-target' => 'entry', | ||
| ], | ||
| ]); | ||
|
|
||
| $entryOptionsNormalizer = function (OptionsResolver $options, $value) { | ||
| $value['inherit_data'] = true; | ||
|
|
||
| return $value; | ||
| }; | ||
|
|
||
| $resolver->addNormalizer('entry_options', $entryOptionsNormalizer); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| <?php | ||
|
|
||
| namespace Symfony\UX\FormCollection\Form; | ||
|
|
||
| use Symfony\Component\Form\AbstractType; | ||
| use Symfony\Component\Form\Extension\Core\Type\ButtonType; | ||
| use Symfony\Component\Form\FormBuilderInterface; | ||
| use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
|
||
| /** | ||
| * @final | ||
| * @experimental | ||
| */ | ||
| class UXCollectionToolbarType extends AbstractType | ||
| { | ||
| public function buildForm(FormBuilderInterface $builder, array $options): void | ||
| { | ||
| if ($options['allow_add']) { | ||
| $builder->add( | ||
| 'uxCollectionAddButton', | ||
| ButtonType::class, | ||
| $options['add_options'], | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| public function configureOptions(OptionsResolver $resolver): void | ||
| { | ||
| $resolver->setDefaults([ | ||
| 'label' => false, | ||
| 'allow_add' => false, | ||
| 'add_options' => [], | ||
| ]); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| <?php | ||
|
|
||
| namespace Symfony\UX\FormCollection\Form; | ||
|
|
||
| use Symfony\Component\Form\AbstractType; | ||
| use Symfony\Component\Form\Extension\Core\Type\CollectionType; | ||
| use Symfony\Component\Form\Extension\Core\Type\TextType; | ||
| use Symfony\Component\Form\FormInterface; | ||
| use Symfony\Component\Form\FormView; | ||
| use Symfony\Component\OptionsResolver\Options; | ||
| use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
|
||
| /** | ||
| * @final | ||
| * @experimental | ||
| */ | ||
| class UXCollectionType extends AbstractType | ||
| { | ||
| public function getParent(): string | ||
| { | ||
| return CollectionType::class; | ||
| } | ||
|
|
||
| public function buildView(FormView $view, FormInterface $form, array $options): void | ||
| { | ||
| $form->add('toolbar', UXCollectionToolbarType::class, [ | ||
| 'allow_add' => $options['allow_add'], | ||
| 'add_options' => $options['add_options'], | ||
| ]); | ||
| } | ||
|
|
||
| public function configureOptions(OptionsResolver $resolver): void | ||
| { | ||
| $addOptionsNormalizer = function (Options $options, $value) { | ||
| $value['block_name'] = 'add_button'; | ||
| $value['attr'] = \array_merge([ | ||
| 'data-' . $options['attr']['data-controller'] . '-target' => 'addButton', | ||
| 'data-action' => $options['attr']['data-controller'] . '#add', | ||
| ], $value['attr'] ?? []); | ||
|
|
||
| return $value; | ||
| }; | ||
|
|
||
| $deleteOptionsNormalizer = function (Options $options, $value) { | ||
| $value['block_name'] = 'delete_button'; | ||
| $value['attr'] = \array_merge([ | ||
| 'data-' . $options['attr']['data-controller'] . '-target' => 'deleteButton', | ||
| 'data-action' => $options['attr']['data-controller'] . '#delete', | ||
| ], $value['attr'] ?? []); | ||
|
|
||
| return $value; | ||
| }; | ||
|
|
||
| $attrNormalizer = function (Options $options, $value) { | ||
| if (!isset($value['data-controller'])) { | ||
| $value['data-controller'] = 'collection'; | ||
| } | ||
| $value['data-' . $value['data-controller'] . '-prototype-name-value'] = $options['prototype_name']; | ||
|
|
||
| return $value; | ||
| }; | ||
|
|
||
| $entryTypeNormalizer = function (OptionsResolver $options, $value) { | ||
| return UXCollectionEntryType::class; | ||
| }; | ||
|
|
||
| $entryOptionsNormalizer = function (OptionsResolver $options, $value) { | ||
| return [ | ||
| 'row_attr' => [ | ||
| 'data-' . $options['attr']['data-controller'] . '-target' => 'entry', | ||
| ], | ||
| 'label' => false, | ||
| 'allow_delete' => $options['allow_delete'], | ||
| 'delete_options' => $options['delete_options'], | ||
| 'entry_type' => $options['ux_entry_type'] ?? null, | ||
| 'entry_options' => $value, | ||
| ]; | ||
| }; | ||
|
|
||
| $resolver->setDefaults([ | ||
| 'ux_entry_type' => TextType::class, | ||
| 'add_options' => [], | ||
| 'delete_options' => [], | ||
| 'original_entry_type' => [], | ||
| ]); | ||
|
|
||
| $resolver->setNormalizer('add_options', $addOptionsNormalizer); | ||
| $resolver->setNormalizer('delete_options', $deleteOptionsNormalizer); | ||
| $resolver->setNormalizer('attr', $attrNormalizer); | ||
| $resolver->addNormalizer('entry_type', $entryTypeNormalizer); | ||
| $resolver->addNormalizer('entry_options', $entryOptionsNormalizer); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Symfony package. | ||
| * | ||
| * (c) Fabien Potencier <[email protected]> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Symfony\UX\FormCollection; | ||
|
|
||
| use Symfony\Component\HttpKernel\Bundle\Bundle; | ||
|
|
||
| /** | ||
| * @final | ||
| * @experimental | ||
| */ | ||
| class FormCollectionBundle extends Bundle | ||
| { | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| Copyright (c) 2020-2021 Fabien Potencier | ||
|
|
||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is furnished | ||
| to do so, subject to the following conditions: | ||
|
|
||
| The above copyright notice and this permission notice shall be included in all | ||
| copies or substantial portions of the Software. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| THE SOFTWARE. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| # Symfony UX Form Collection | ||
|
|
||
| Symfony UX Form collection is a Symfony bundle provides a Stimulus integration | ||
| for add and remove of for [Symfony Form Collection Type](https://symfony.com/doc/current/form/collection.html). | ||
|
|
||
| **This repository is a READ-ONLY sub-tree split**. See | ||
| https://github.com/symfony/ux to create issues or submit pull requests. | ||
|
|
||
| ## Resources | ||
|
|
||
| - [Documentation](https://symfony.com/bundles/ux-form-collection/current/index.html) | ||
| - [Report issues](https://github.com/symfony/ux/issues) and | ||
| [send Pull Requests](https://github.com/symfony/ux/pulls) | ||
| in the [main Symfony UX repository](https://github.com/symfony/ux) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "presets": ["@babel/env"], | ||
| "plugins": ["@babel/plugin-proposal-class-properties"] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import { Controller } from '@hotwired/stimulus'; | ||
|
|
||
| class default_1 extends Controller { | ||
| constructor() { | ||
| super(...arguments); | ||
| this.index = 0; | ||
| this.controllerName = 'collection'; | ||
| this.entries = []; | ||
| } | ||
| connect() { | ||
| this.controllerName = this.context.scope.identifier; | ||
| this._dispatchEvent('form-collection:pre-connect'); | ||
| this.entries = []; | ||
| this.element.querySelectorAll(':scope > [data-' + this.controllerName + '-target="entry"]').forEach(entry => { | ||
| this.entries.push(entry); | ||
| }); | ||
| this._dispatchEvent('form-collection:connect'); | ||
| } | ||
| add() { | ||
| const prototypeHTML = this.element.dataset.prototype; | ||
| if (!prototypeHTML) { | ||
| throw new Error('A "data-prototype" attribute was expected on data-controller="' + this.controllerName + '" element.'); | ||
| } | ||
| const newEntry = this._textToNode(prototypeHTML.replace(new RegExp('/' + this.prototypeNameValue + '/', 'g'), this.index.toString())); | ||
| this._dispatchEvent('form-collection:pre-add', { | ||
| entry: newEntry, | ||
| index: this.index, | ||
| }); | ||
| if (this.entries.length > 0) { | ||
| this.entries[this.entries.length - 1].after(newEntry); | ||
| } | ||
| else { | ||
| this.element.prepend(newEntry); | ||
| } | ||
| this.entries.push(newEntry); | ||
| this._dispatchEvent('form-collection:add', { | ||
| entry: newEntry, | ||
| index: this.index, | ||
| }); | ||
| this.index++; | ||
| } | ||
| delete(event) { | ||
| const clickTarget = event.target; | ||
| const entry = clickTarget.closest('[data-' + this.controllerName + '-target="entry"]'); | ||
| this._dispatchEvent('form-collection:pre-delete', { | ||
| entry: entry, | ||
| }); | ||
| entry.remove(); | ||
| this.entries = this.entries.filter(currentEntry => currentEntry !== entry); | ||
| this._dispatchEvent('form-collection:delete', { | ||
| entry: entry, | ||
| }); | ||
| } | ||
| _textToNode(text) { | ||
| const template = document.createElement('template'); | ||
| text = text.trim(); | ||
| template.innerHTML = text; | ||
| return template.content.firstChild; | ||
| } | ||
| _dispatchEvent(name, payload = {}) { | ||
| this.element.dispatchEvent(new CustomEvent(name, { detail: payload, bubbles: true })); | ||
| } | ||
| } | ||
| default_1.values = { | ||
| prototypeName: String, | ||
| }; | ||
|
|
||
| export { default_1 as default }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| const config = require('../../../../jest.config.js'); | ||
|
|
||
| config.setupFilesAfterEnv.push('./test/setup.js'); | ||
|
|
||
| module.exports = config; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
UXCollectionEntryToolbarTypeandUXCollectionToolbarTypeallows to add additional buttons later via a form extension. Example can be aCopy & PasteFunctionality.