Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
ae5d325
UX FormCollection
stakovicz May 3, 2021
c305f0c
Remove hard coded property_name __name__
stakovicz May 4, 2021
5e6dd84
PHP CS Fixer
stakovicz May 4, 2021
eea2313
First jests
stakovicz May 4, 2021
f651d90
Rename CollectionType > UXCollectionType
stakovicz May 6, 2021
cc02076
Rename CollectionType > UXCollectionType
stakovicz May 6, 2021
dceb2cf
DependencyInjection Clean
stakovicz May 6, 2021
dfb54f8
Fix .gitattributes
stakovicz May 6, 2021
9300cda
Move default values
stakovicz May 7, 2021
ec4342a
Predefined theme or not
stakovicz May 23, 2021
b4f40cd
Update src/FormCollection/README.md
stakovicz May 24, 2021
0584757
Update src/FormCollection/README.md
stakovicz May 24, 2021
abd2b8f
Update src/FormCollection/README.md
stakovicz May 24, 2021
3996f3b
Update src/FormCollection/Resources/views/form_theme_div.html.twig
stakovicz May 24, 2021
aaa6811
Update src/FormCollection/README.md
stakovicz May 24, 2021
4791ff9
Update src/FormCollection/Resources/views/form_theme_table.html.twig
stakovicz May 24, 2021
8539ff9
Split in 4 options
stakovicz May 24, 2021
2d1ed04
Default startIndex value
stakovicz Jun 6, 2021
1e3105d
Update src/FormCollection/Resources/views/form_theme_div.html.twig
stakovicz Jul 21, 2021
0a9cc54
Update src/FormCollection/Resources/views/form_theme_div.html.twig
stakovicz Jul 21, 2021
94be94c
Update src/FormCollection/Resources/views/form_theme_table.html.twig
stakovicz Jul 21, 2021
c805c0e
Update src/FormCollection/Resources/views/form_theme_table.html.twig
stakovicz Jul 21, 2021
e56d04b
Fix coding-style-js
stakovicz Nov 6, 2021
7467cee
Prettier
stakovicz Nov 6, 2021
a8c730c
Merge branch 'symfony:2.x' into main
stakovicz Jan 19, 2022
ecd774a
Merge branch 'symfony:2.x' into main
stakovicz May 7, 2022
25d8306
Rebase and refresh the code
stakovicz May 21, 2022
8e5cd8a
fix TU
stakovicz May 21, 2022
3b110f9
change buttons attr
stakovicz May 21, 2022
c36157c
Merge branch 'symfony:2.x' into main
stakovicz Jun 14, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Prettier
  • Loading branch information
stakovicz committed Nov 6, 2021
commit 7467cee7fe083f8bd167d29df554840385a1d89c
22 changes: 11 additions & 11 deletions src/FormCollection/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ You need to select the right theme from the one you are using:
```yaml
# config/packages/twig.yaml
twig:
# For bootstrap for example
form_themes: ['@FormCollection/form_theme_div.html.twig']
# For bootstrap for example
form_themes: ['@FormCollection/form_theme_div.html.twig']
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should only have one form theme (form_theme_div.html.twig). If we DID have a 2nd form theme, I would make it a bootstrap theme so that the user could instantly get something nice-looking inside of Bootstrap... but I'm not sure that's necessary because it's really easy to add btn btn-primary classes to the buttons from within the form.

Anyways, if the user needs to heavily customize their buttons, let's let them create their own form theme to override our's. Also, I think we should prevent the user from needing to do this step at all. We can automatically add this form theme for them:

public function prepend(ContainerBuilder $container)
{
// Register the Dropzone form theme if TwigBundle is available
$bundles = $container->getParameter('kernel.bundles');
if (!isset($bundles['TwigBundle'])) {
return;
}
$container->prependExtensionConfig('twig', ['form_themes' => ['@Dropzone/form_theme.html.twig']]);
}
(don't forget the PrependExtensionInterface.

```

There are 2 predefined themes available:

- `@FormCollection/form_theme_div.html.twig`
- `@FormCollection/form_theme_table.html.twig`
- `@FormCollection/form_theme_div.html.twig`
- `@FormCollection/form_theme_table.html.twig`

[Check the Symfony doc](https://symfony.com/doc/4.4/form/form_themes.html) for the different ways to set themes in Symfony.

Expand Down Expand Up @@ -92,11 +92,11 @@ class BlogFormType extends AbstractType
->add('comments', UXCollectionType::class, [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be as helpful as possible, I think we need to show the user a simple CommentType class. And then, use that here with 'entry_type' => CommentType::class`.

Overall, this is how my example looks locally - I think these options are helpful to all show!

'allow_add' => true,
'allow_delete' => true,
'entry_type' => FileType::class,
// ideally UXCollectionType can set this for us
'by_reference' => false,
'entry_options' => [
    // hides the "0", "1", "2" labels on the embedded forms
    'label' => false,
],
'button_add_options' => [
    'label' => '<span class="fa fa-plus"></span>',
    'label_html' => true,
    'class' => 'btn btn-secondary btn-sm'
],
'button_delete_options' => [
    'label' => '<span class="fa fa-times"></span>',
    'label_html' => true,
    'class' => 'btn btn-danger btn-sm'
],

// ...
// Default text for the add button (used by predefined theme)
'button_add_text' => 'Add',
'button_add_text' => 'Add',
// Add HTML classes to the add button (used by predefined theme)
'button_add_class' => 'btn btn-outline-primary',
// Default text for the delete button (used by predefined theme)
'button_delete_text' => 'Remove',
'button_delete_text' => 'Remove',
// Add HTML classes to the add button (used by predefined theme)
'button_delete_class' => 'btn btn-outline-secondary',
])
Expand Down Expand Up @@ -147,7 +147,7 @@ export default class extends Controller {

_onPreConnect(event) {
// The collection is not yet connected
console.log(event.detail.allowAdd); // Access to the allow_add option of the form
console.log(event.detail.allowAdd); // Access to the allow_add option of the form
console.log(event.detail.allowDelete); // Access to the allow_delete option of the form
}

Expand All @@ -156,7 +156,7 @@ export default class extends Controller {
}

_onPreAdd(event) {
console.log(event.detail.index); // Access to the curent index will be added
console.log(event.detail.index); // Access to the curent index will be added
console.log(event.detail.element); // Access to the element will be added
}

Expand All @@ -165,7 +165,7 @@ export default class extends Controller {
}

_onPreDelete(event) {
console.log(event.detail.index); // Access to the index will be removed
console.log(event.detail.index); // Access to the index will be removed
console.log(event.detail.element); // Access to the elemnt will be removed
}

Expand All @@ -183,8 +183,8 @@ Then in your render call, add your controller as an HTML attribute:
->add('comments', UXCollectionType::class, [
// ...
'attr' => [
// Change the controller name
'data-controller' => 'mycollection'
// Change the controller name
'data-controller' => 'mycollection'
]
]);
```
19 changes: 7 additions & 12 deletions src/FormCollection/Resources/assets/src/controller.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
'use strict';

import {Controller} from 'stimulus';
import { Controller } from 'stimulus';

export default class extends Controller {
static targets = [
'entry'
];
static targets = ['entry'];

static values = {
allowAdd: Boolean,
Expand All @@ -14,7 +12,7 @@ export default class extends Controller {
buttonDelete: String,
prototypeName: String,
prototype: String,
startIndex: Number
startIndex: Number,
};

/**
Expand Down Expand Up @@ -63,7 +61,6 @@ export default class extends Controller {
}

add() {

this.index++;

// Compute the new entry
Expand All @@ -82,7 +79,7 @@ export default class extends Controller {

this._dispatchEvent('form-collection:pre-add', {
index: this.index,
element: newEntry
element: newEntry,
});

this.element.append(newEntry);
Expand All @@ -93,7 +90,7 @@ export default class extends Controller {

this._dispatchEvent('form-collection:add', {
index: this.index,
element: entry
element: entry,
});
}

Expand All @@ -102,14 +99,14 @@ export default class extends Controller {

this._dispatchEvent('form-collection:pre-delete', {
index: entry.dataset.indexEntry,
element: entry
element: entry,
});

entry.remove();

this._dispatchEvent('form-collection:delete', {
index: entry.dataset.indexEntry,
element: entry
element: entry,
});
}

Expand All @@ -121,7 +118,6 @@ export default class extends Controller {
* @private
*/
_addDeleteButton(entry, index) {

// link the button and the entry by the data-index-entry attribute
entry.dataset.indexEntry = index;

Expand All @@ -147,7 +143,6 @@ export default class extends Controller {
* @private
*/
_textToNode(text) {

let template = document.createElement('template');
text = text.trim(); // Never return a text node of whitespace as the result
template.innerHTML = text;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,4 @@ describe('FormCollectionController', () => {
await waitFor(() => expect(getByTestId(container, 'container')).toHaveClass('connected'));
await waitFor(() => expect(getByTestId(container, 'container')).toHaveClass('pre-connected'));
});

});