Skip to content

Commit 6afcd61

Browse files
committed
feature #378 Model rendering overhaul (weaverryan)
This PR was squashed before being merged into the 2.x branch. Discussion ---------- Model rendering overhaul | Q | A | ------------- | --- | Bug fix? | yes | New feature? | yes | Tickets | None | License | MIT Hi! tl;dr ```twig BEFORE: <input data-model="firstName" data-action="live#update" > AFTER: <input data-model="firstName" > ``` This PR makes it so that you *only* need to use `data-model` to bind an input to a `LiveProp` on your component object. This also introduces: A) **model modifiers** (e.g. `data-model="on(change)|norender|firstName"` to update the model on change and skip re-rendering) B) **A new way to "opt in" to the `name` attribute being used in place of `data-model`**: you now add a `data-model` to the `form` tag surrounding your fields: `<form data-model"*">`. All of this is explained more in the CHANGELOG and documentation. The motivation behind this change was actually to fix a bug. If you typed into field (A), then quickly typed into field (B) before the re-render Ajax request finished from changing field (A), when that Ajax call *did* finish, it would "replace" your custom changes to field (B). Basically, what you just typed disappears. Fixing this meant making `live_controller.ts` listen to the `input` event in all situations and track "unsynced inputs". And since we were now listening to the `input` event all the time, it seemed logical to no longer require the user to do this manually with `data-action="live#update"`. This PR also overhauls the test suite to make it *much* more readable and user friends 🌞 and also completes the conversion to TypeScript by removing all of the TS warnings. Cheers! Commits ------- d3aaf4f Model rendering overhaul
2 parents 92bc2cf + d3aaf4f commit 6afcd61

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+3305
-1785
lines changed

src/Autocomplete/assets/dist/controller.js

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
import { Controller } from '@hotwired/stimulus';
22
import TomSelect from 'tom-select';
33

4-
/******************************************************************************
5-
Copyright (c) Microsoft Corporation.
6-
7-
Permission to use, copy, modify, and/or distribute this software for any
8-
purpose with or without fee is hereby granted.
9-
10-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
11-
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
12-
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
13-
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
14-
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
15-
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16-
PERFORMANCE OF THIS SOFTWARE.
17-
***************************************************************************** */
18-
19-
function __classPrivateFieldGet(receiver, state, kind, f) {
20-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
21-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
22-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
4+
/******************************************************************************
5+
Copyright (c) Microsoft Corporation.
6+
7+
Permission to use, copy, modify, and/or distribute this software for any
8+
purpose with or without fee is hereby granted.
9+
10+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
11+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
12+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
13+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
14+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
15+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16+
PERFORMANCE OF THIS SOFTWARE.
17+
***************************************************************************** */
18+
19+
function __classPrivateFieldGet(receiver, state, kind, f) {
20+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
21+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
22+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
2323
}
2424

2525
var _instances, _getCommonConfig, _createAutocomplete, _createAutocompleteWithHtmlContents, _createAutocompleteWithRemoteData, _stripTags, _mergeObjects, _createTomSelect, _dispatchEvent;

src/Chartjs/Twig/ChartExtension.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\UX\Chartjs\Twig;
1313

1414
use Symfony\UX\Chartjs\Model\Chart;
15+
use Symfony\WebpackEncoreBundle\Dto\StimulusControllersDto;
1516
use Symfony\WebpackEncoreBundle\Twig\StimulusTwigExtension;
1617
use Twig\Environment;
1718
use Twig\Extension\AbstractExtension;
@@ -49,7 +50,16 @@ public function renderChart(Environment $env, Chart $chart, array $attributes =
4950
}
5051
$controllers['@symfony/ux-chartjs/chart'] = ['view' => $chart->createView()];
5152

52-
$html = '<canvas '.$this->stimulus->renderStimulusController($env, $controllers).' ';
53+
if (class_exists(StimulusControllersDto::class)) {
54+
$dto = new StimulusControllersDto($env);
55+
foreach ($controllers as $name => $controllerValues) {
56+
$dto->addController($name, $controllerValues);
57+
}
58+
59+
$html = '<canvas '.$dto.' ';
60+
} else {
61+
$html = '<canvas '.$this->stimulus->renderStimulusController($env, $controllers).' ';
62+
}
5363

5464
foreach ($chart->getAttributes() as $name => $value) {
5565
if ('data-controller' === $name) {

src/LiveComponent/CHANGELOG.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,68 @@
11
# CHANGELOG
22

3+
## 2.3.0
4+
5+
- [BC BREAK] The `data-action="live#update"` attribute must now be
6+
removed from **nearly** all elements. This is because LiveComponents
7+
now automatically listens to the `input` event on all elements
8+
with a `data-model` attribute and updates the data. If you previously
9+
used `data-action="change->live#update"` to list on the `change`
10+
event, now you should use the `on(change)` modifier inside `data-model`.
11+
12+
```twig
13+
<!-- BEFORE -->
14+
<input
15+
data-model="max"
16+
data-action="change->live#update"
17+
>
18+
19+
<!-- AFTER -->
20+
<input
21+
data-model="on(change)|max"
22+
>
23+
```
24+
25+
- [BC BREAK] The `live#updateDefer` action was removed entirely.
26+
Now, to update a model without triggering a re-render, use the
27+
`norender` modifier for `data-model`:
28+
29+
```twig
30+
<!-- BEFORE -->
31+
<input
32+
data-model="max"
33+
data-action="live#updateDefer"
34+
>
35+
36+
<!-- AFTER -->
37+
<input
38+
data-model="norender|max"
39+
>
40+
```
41+
42+
- [BC BREAK] The `name` attribute is no longer automatically used to
43+
update a model when a parent component has `data-action="change->live#update"`.
44+
To make a form's fields behave like "model" fields (but using the
45+
`name` attribute instead of `data-model`) you need to add a `data-model`
46+
attribute to the `<form>` element around your fields (NOTE: the
47+
new attribute is automatically added to your `form` element when
48+
using `ComponentWithFormTrait`):
49+
50+
```twig
51+
<!-- BEFORE -->
52+
<form data-action="change->live#update">
53+
<input
54+
name="max"
55+
>
56+
</form>
57+
58+
<!-- AFTER -->
59+
<form data-model="on(change)|*">
60+
<input
61+
name="max"
62+
>
63+
</form>
64+
```
65+
366
## 2.2.0
467

568
- The bundle now properly exposes a `live` controller, which can be

0 commit comments

Comments
 (0)