├── src
├── view
│ └── frontend
│ │ ├── templates
│ │ ├── magewire
│ │ │ ├── features
│ │ │ │ ├── switch-template
│ │ │ │ │ ├── header.phtml
│ │ │ │ │ ├── a.phtml
│ │ │ │ │ ├── b.phtml
│ │ │ │ │ ├── c.phtml
│ │ │ │ │ └── tools.phtml
│ │ │ │ ├── magic-actions-properties
│ │ │ │ │ ├── emit-global.phtml
│ │ │ │ │ └── emit-specific.phtml
│ │ │ │ ├── data-via-layout.phtml
│ │ │ │ ├── reset.phtml
│ │ │ │ ├── template-methods.phtml
│ │ │ │ ├── wire-ignore.phtml
│ │ │ │ ├── dollar-wire.phtml
│ │ │ │ └── magic-actions-properties.phtml
│ │ │ ├── quick-search.phtml
│ │ │ ├── todo-checklist.phtml
│ │ │ ├── pagination.phtml
│ │ │ ├── dispatch-events.phtml
│ │ │ ├── todo.phtml
│ │ │ ├── form.phtml
│ │ │ ├── list-shuffle.phtml
│ │ │ └── reacticon.phtml
│ │ └── examples.phtml
│ │ └── layout
│ │ ├── magewire_examples_index.xml
│ │ └── magewire_features_index.xml
├── etc
│ ├── module.xml
│ └── frontend
│ │ └── routes.xml
├── registration.php
├── Magewire
│ ├── Features
│ │ ├── MagicActionsProperties.php
│ │ ├── WireIgnore.php
│ │ ├── TemplateMethods.php
│ │ ├── DollarWire.php
│ │ ├── Reset.php
│ │ ├── DataViaLayout.php
│ │ ├── MagicActionsProperties
│ │ │ ├── EmitGlobal.php
│ │ │ └── EmitSpecific.php
│ │ └── SwitchTemplate.php
│ ├── DispatchEvents.php
│ ├── QuickSearch.php
│ ├── ListShuffle.php
│ ├── TodoChecklist.php
│ ├── Form.php
│ ├── Reacticon.php
│ ├── Todo.php
│ ├── Todo
│ │ └── Todo
│ │ │ └── Todo.php
│ └── Pagination.php
└── Controller
│ ├── Examples
│ └── Index.php
│ └── Features
│ └── Index.php
├── composer.json
├── .github
└── FUNDING.yml
├── README.md
└── LICENSE.md
/src/view/frontend/templates/magewire/features/switch-template/header.phtml:
--------------------------------------------------------------------------------
1 |
2 | = __('Switch Template') ?>
3 |
4 |
--------------------------------------------------------------------------------
/src/view/frontend/templates/magewire/features/switch-template/a.phtml:
--------------------------------------------------------------------------------
1 |
2 | = $block->getChildHtml('switch-template.header') ?>
3 |
4 |
Template A
5 |
6 | = $block->getChildHtml('switch-template.tools') ?>
7 |
8 |
--------------------------------------------------------------------------------
/src/view/frontend/templates/magewire/features/switch-template/b.phtml:
--------------------------------------------------------------------------------
1 |
2 | = $block->getChildHtml('switch-template.header') ?>
3 |
4 |
Template B
5 |
6 | = $block->getChildHtml('switch-template.tools') ?>
7 |
8 |
--------------------------------------------------------------------------------
/src/view/frontend/templates/magewire/features/switch-template/c.phtml:
--------------------------------------------------------------------------------
1 |
2 | = $block->getChildHtml('switch-template.header') ?>
3 |
4 |
Template C
5 |
6 | = $block->getChildHtml('switch-template.tools') ?>
7 |
8 |
--------------------------------------------------------------------------------
/src/etc/module.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/src/view/frontend/templates/magewire/features/switch-template/tools.phtml:
--------------------------------------------------------------------------------
1 |
2 | Template A
3 | Template B
4 | Template C
5 |
6 |
--------------------------------------------------------------------------------
/src/registration.php:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "magewirephp/magewire-examples",
3 | "description": "A Magento 2 module including all sorts of Magewire inspired examples",
4 | "version": "1.2.0",
5 | "type": "magento2-module",
6 | "require": {
7 | "magewirephp/magewire": "^1.0"
8 | },
9 | "authors": [
10 | {
11 | "name": "Willem Poortman"
12 | }
13 | ],
14 | "license": "MIT",
15 | "autoload": {
16 | "files": [
17 | "src/registration.php"
18 | ],
19 | "psr-4": {
20 | "Magewirephp\\MagewireExamples\\": "src/"
21 | }
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/view/frontend/templates/examples.phtml:
--------------------------------------------------------------------------------
1 |
2 |
3 | = __('Examples Introduction') ?>
4 |
5 |
6 |
7 | = __('The following examples are for inspiration only. Under no circumstances is it recommended to use these
8 | examples in a production environment. These examples are not optimized for responsiveness. Additional generic
9 | examples can be added via a Github Pull Request.') ?>
10 |
11 |
12 |
--------------------------------------------------------------------------------
/src/Magewire/Features/DollarWire.php:
--------------------------------------------------------------------------------
1 | foo = random_int(0,999);
21 | $this->called++;
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/.github/FUNDING.yml:
--------------------------------------------------------------------------------
1 | github: wpoortman
2 | #patreon: # Replace with a single Patreon username
3 | #open_collective: # Replace with a single Open Collective username
4 | #ko_fi: # Replace with a single Ko-fi username
5 | #tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
6 | #community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
7 | #liberapay: # Replace with a single Liberapay username
8 | #issuehunt: # Replace with a single IssueHunt username
9 | #otechie: # Replace with a single Otechie username
10 | #lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry
11 | #custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
12 |
--------------------------------------------------------------------------------
/src/view/frontend/templates/magewire/features/magic-actions-properties/emit-global.phtml:
--------------------------------------------------------------------------------
1 | getMagewire();
11 | ?>
12 |
13 |
14 | = __('Child Block: Emit Global') ?>
15 |
16 |
17 |
18 | Global emit was triggered: = $magewire->getGlobal() ? 'true' : 'false' ?>
19 |
20 |
21 |
--------------------------------------------------------------------------------
/src/Magewire/DispatchEvents.php:
--------------------------------------------------------------------------------
1 | process = !$this->process;
22 |
23 | $this->dispatchBrowserEvent('process:after', ['show' => $this->process]);
24 |
25 | $this->emitTo(
26 | 'magewire.todo-checklist',
27 | 'todo:finish',
28 | 'Process browser event (' . ($this->process ? 'show' : 'hide') . ')'
29 | );
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/src/Controller/Examples/Index.php:
--------------------------------------------------------------------------------
1 | pageFactory = $pageFactory;
23 | }
24 |
25 | public function execute()
26 | {
27 | $page = $this->pageFactory->create();
28 | $page->getConfig()->getTitle()->set('Magewire - Examples');
29 |
30 | return $page;
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/Controller/Features/Index.php:
--------------------------------------------------------------------------------
1 | pageFactory = $pageFactory;
23 | }
24 |
25 | public function execute()
26 | {
27 | $page = $this->pageFactory->create();
28 | $page->getConfig()->getTitle()->set('Magewire - Features');
29 |
30 | return $page;
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/view/frontend/templates/magewire/features/magic-actions-properties/emit-specific.phtml:
--------------------------------------------------------------------------------
1 | getMagewire();
11 | ?>
12 |
13 |
14 | = __('Child Block: Emit Specific') ?>
15 |
16 |
17 |
18 | Global emit was triggered: = $magewire->getGlobal() ? 'true' : 'false' ?>
19 |
20 |
21 | Specific emit was triggered: = $magewire->getSpecific() ? 'true' : 'false' ?>
22 |
23 |
24 |
--------------------------------------------------------------------------------
/src/view/frontend/templates/magewire/features/data-via-layout.phtml:
--------------------------------------------------------------------------------
1 | getMagewire();
11 | ?>
12 |
13 |
14 | = __('Data via Layout') ?>
15 |
16 |
17 |
18 | getPublicProperties() as $property => $value): ?>
19 | = ucfirst($property) ?>
20 |
21 | Perform Action
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/src/Magewire/Features/Reset.php:
--------------------------------------------------------------------------------
1 | boot = random_int(0,999);
31 | }
32 |
33 | public function resetByProperty(string $property)
34 | {
35 | $this->reset([$property], $property === 'boot');
36 | }
37 |
38 | public function resetAll()
39 | {
40 | $this->reset();
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/src/Magewire/QuickSearch.php:
--------------------------------------------------------------------------------
1 | q = $for;
25 | $this->results = [];
26 |
27 | try {
28 | for ($i = random_int(1, 10); $i <= 10; $i++) {
29 | $this->results[] = substr(str_shuffle(str_repeat($c = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', (int)ceil(10 / strlen($c)))), 1, 10);
30 | }
31 | } catch (Exception $e) {
32 | $this->results[] = __('No results found.');
33 | }
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/src/Magewire/Features/DataViaLayout.php:
--------------------------------------------------------------------------------
1 | foo = strtoupper($this->getFoo());
32 | }
33 |
34 | public function bar()
35 | {
36 | $this->bar = strtolower($this->getBar());
37 | }
38 |
39 | public function toggle()
40 | {
41 | $this->toggle = !$this->getToggle();
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Magewire Examples
2 | 
3 | 
4 |
5 | A Magewire showcase to give easy insights on where Magewire could possibly fit in your project. This
6 | package will grow over time, based on new ideas and/or features introduced into Magewire.
7 |
8 | ## Installation
9 | ```
10 | composer require magewirephp/magewire-examples --dev
11 | ```
12 |
13 | ## Usage
14 | - Navigate to ```{store_url}/magewire/examples``` inside your browser.
15 | - Navigate to ```{store_url}/magewire/features``` inside your browser (WIP).
16 |
17 | ## Security Vulnerabilities
18 | If you discover a security vulnerability within Magewire, please create a PR or send an e-mail to Willem Poortman via [magewirephp@wpoortman.nl](mailto:magewirephp@wpoortman.nl). All security vulnerabilities will be promptly addressed.
19 |
20 | ## License
21 | Copyright © [Willem Poortman](https://github.com/wpoortman)
22 |
23 | Magewire is open-sourced software licensed under the [MIT license](LICENSE.md).
24 |
--------------------------------------------------------------------------------
/LICENSE.md:
--------------------------------------------------------------------------------
1 | ## MIT License
2 |
3 | Copyright © Willem Poortman
4 |
5 | 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:
6 |
7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8 |
9 | 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.
10 |
--------------------------------------------------------------------------------
/src/Magewire/Features/MagicActionsProperties/EmitGlobal.php:
--------------------------------------------------------------------------------
1 | global) {
28 | $this->dispatchNoticeMessage(strtoupper($prefix) . ': Global property value was reset to false');
29 | } else {
30 | $this->dispatchSuccessMessage(strtoupper($prefix) . ': Dispatch message from component ' . get_class($this));
31 | }
32 |
33 | $this->global = !$this->global;
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/src/view/frontend/templates/magewire/features/reset.phtml:
--------------------------------------------------------------------------------
1 | getMagewire();
11 | ?>
12 |
13 |
14 | Reset
15 |
16 |
17 |
18 | getPublicProperties() as $property => $value): ?>
19 | = ucfirst($property) ?>
20 |
21 | Perform Action
22 |
23 |
24 |
25 |
29 | Reset
30 |
31 |
32 |
--------------------------------------------------------------------------------
/src/Magewire/Features/SwitchTemplate.php:
--------------------------------------------------------------------------------
1 | templates = $templates;
21 | $this->setTemplate('ta');
22 | }
23 |
24 | /**
25 | * @param string $key
26 | */
27 | public function setTemplate(string $key): void
28 | {
29 | if (isset($this->templates[$key])) {
30 | if ($key === $this->template) {
31 | $this->dispatchNoticeMessage('Template was already visible.');
32 | }
33 |
34 | $this->template = $key;
35 | $this->switchTemplate($this->templates[$this->template]);
36 |
37 | return;
38 | }
39 |
40 | $this->dispatchErrorMessage('Something went wrong.');
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/src/view/frontend/templates/magewire/features/template-methods.phtml:
--------------------------------------------------------------------------------
1 | getMagewire();
11 | ?>
12 |
33 |
--------------------------------------------------------------------------------
/src/view/frontend/templates/magewire/quick-search.phtml:
--------------------------------------------------------------------------------
1 | getResults();
3 | ?>
4 |
11 |
12 | Quick search (without wire:model)
13 |
14 |
15 |
16 |
23 |
24 |
25 |
26 |
27 | = $result ?>
28 |
29 |
30 |
31 |
= count($results) ?> random result= count($results) === 1 ? '' : 's'?> found.
32 |
33 |
34 |
35 | This example requires AlpineJS .
36 |
37 |
38 |
39 |
--------------------------------------------------------------------------------
/src/Magewire/ListShuffle.php:
--------------------------------------------------------------------------------
1 | 0, 'title' => 'Let\'s'],
26 | ['id' => 1, 'title' => 'Make'],
27 | ['id' => 2, 'title' => 'Magento'],
28 | ['id' => 3, 'title' => 'Even'],
29 | ['id' => 4, 'title' => 'More'],
30 | ['id' => 5, 'title' => 'Awesome'],
31 | ];
32 |
33 | public function shuffle(): void
34 | {
35 | $things = $this->things;
36 | shuffle($things);
37 |
38 | $this->things = $things;
39 | $entities = array_column($things, 'id');
40 |
41 | if ($this->getMarkAsDone()) {
42 | $title = 'List shuffle (' . join(',', $entities) . ')';
43 | $this->emitTo('magewire.todo-checklist', 'todo:finish', $title);
44 | }
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/src/Magewire/TodoChecklist.php:
--------------------------------------------------------------------------------
1 | 'saveTask'];
25 |
26 | /**
27 | * @param string|null $title
28 | */
29 | public function saveTask(string $title = null): void
30 | {
31 | $this->tasks[] = $title;
32 | }
33 |
34 | /**
35 | * @param int $index
36 | * @return FlashMessage|void
37 | */
38 | public function resetTask(int $index)
39 | {
40 | if (!isset($this->tasks[$index])) {
41 | return $this->dispatchErrorMessage(__('Task with ID "%1" doesnt exist', [$index]));
42 | }
43 |
44 | $this->emitTo('magewire.todo', 'todo:reset', [$this->tasks[$index]]);
45 | unset($this->tasks[$index]);
46 | }
47 |
48 | public function clearTasks(): void
49 | {
50 | $this->tasks = [];
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/src/view/frontend/templates/magewire/todo-checklist.phtml:
--------------------------------------------------------------------------------
1 | getMagewire();
11 | ?>
12 |
13 |
14 | = __('Tasks Done') ?> = count($magewire->getTasks()) ?>
15 |
16 |
17 | getTasks())): ?>
18 |
19 | getTasks() as $index => $title): ?>
20 |
21 |
27 | = $title ?>
28 |
29 |
30 |
31 |
32 |
33 |
34 | = __('Clear tasks') ?>
35 |
36 |
37 |
= __('No tasks available.') ?>
38 |
39 |
40 |
--------------------------------------------------------------------------------
/src/view/frontend/templates/magewire/features/wire-ignore.phtml:
--------------------------------------------------------------------------------
1 | getMagewire();
11 | ?>
12 |
13 |
14 | = __('Wire Ignore') ?>
15 |
16 |
17 |
18 |
22 | 0
23 |
24 |
25 |
26 | Just hit the counter button and set it to your favorite number. Because this DOM element is ignored,
27 | Magewire will skip it when fresh HTML comes back from the server. In this case, HTML will be returned
28 | thanks to the "Foo Value" div rendering the value you type in the input below.
29 |
30 |
31 |
32 |
33 | Foo
34 |
35 |
36 |
37 |
38 | = 'Foo Value: ' . ($magewire->hasFoo() ? $magewire->getFoo() : 'None')?>
39 |
40 |
41 |
--------------------------------------------------------------------------------
/src/Magewire/Features/MagicActionsProperties/EmitSpecific.php:
--------------------------------------------------------------------------------
1 | global) {
30 | $this->dispatchNoticeMessage(strtoupper($prefix) . ': Global property value was reset to false');
31 | } else {
32 | $this->dispatchSuccessMessage(strtoupper($prefix) . ': Dispatch message from component ' . get_class($this));
33 | }
34 |
35 | $this->global = !$this->global;
36 | }
37 |
38 | public function specific(string $prefix)
39 | {
40 | if ($this->specific) {
41 | $this->dispatchNoticeMessage(strtoupper($prefix) . ': Specific property value was reset to false');
42 | } else {
43 | $this->dispatchSuccessMessage(strtoupper($prefix) . ': Specific dispatch message from component ' . get_class($this));
44 | }
45 |
46 | $this->specific = !$this->specific;
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/src/view/frontend/templates/magewire/features/dollar-wire.phtml:
--------------------------------------------------------------------------------
1 |
2 |
3 | AlpineJS: $wire
4 |
5 |
6 |
7 |
8 |
20 | Run as a Promise()
21 |
22 |
23 |
24 |
25 | Uses a x-on:click to call the Magewire promise() method who then waits for it's response and calls the
26 | promise() method again where it alerts you afterwards.
27 |
28 |
29 |
30 |
34 | Set Foo with AlpineJS
35 |
36 |
37 |
38 |
39 | Uses a x-on:click to set the "foo" property value to "bar".
40 |
41 |
42 |
43 |
--------------------------------------------------------------------------------
/src/view/frontend/templates/magewire/pagination.phtml:
--------------------------------------------------------------------------------
1 | getMagewire();
11 | ?>
12 |
40 |
--------------------------------------------------------------------------------
/src/Magewire/Form.php:
--------------------------------------------------------------------------------
1 | [
16 | 'firstname' => '',
17 | 'lastname' => '',
18 | 'email' => '',
19 | ]
20 | ];
21 |
22 | // Rules for nested properties.
23 | protected $rules = [
24 | 'customer.credentials.firstname' => 'required|min:2',
25 | 'customer.credentials.lastname' => 'required|min:2',
26 | 'customer.credentials.email' => 'required|email',
27 | ];
28 |
29 | // :attribute & :value are available within each message.
30 | protected $messages = [
31 | 'customer.credentials.firstname:required' => 'Your firstname is required',
32 | 'customer.credentials.firstname:min' => 'Your firstname minimum is 2',
33 |
34 | 'customer.credentials.lastname:required' => 'Your lastname is required',
35 | 'customer.credentials.lastname:min' => 'Your lastname minimum is 2',
36 |
37 | 'customer.credentials.email:required' => 'Your email is required',
38 | 'customer.credentials.email:email' => 'Your email is not valid email',
39 | ];
40 |
41 | public function save()
42 | {
43 | $this->validate();
44 | // Will only dispatch when all customer credentials are valid.
45 | $this->dispatchSuccessMessage('Validation success!');
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/src/Magewire/Reacticon.php:
--------------------------------------------------------------------------------
1 | dispatchBrowserEvent('toggle-button', ['increment' => $value]);
38 | return $value;
39 | }
40 |
41 | /**
42 | * @param bool $value
43 | * @return bool
44 | */
45 | public function updatingDecrement(bool $value): bool
46 | {
47 | $this->dispatchBrowserEvent('toggle-button', ['decrement' => $value]);
48 | return $value;
49 | }
50 |
51 | /**
52 | * @return void
53 | */
54 | public function increment(): void
55 | {
56 | $this->quantity = $this->getQuantity() + $this->getInterval();
57 | }
58 |
59 | public function decrement()
60 | {
61 | $calc = $this->getQuantity() - $this->getInterval();
62 |
63 | if ($calc < 0) {
64 | $this->dispatchNoticeMessage('You can\'t go below zero');
65 | } else {
66 | $this->quantity = $calc;
67 | }
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/src/view/frontend/templates/magewire/dispatch-events.phtml:
--------------------------------------------------------------------------------
1 | getMagewire();
11 | ?>
12 |
13 |
14 | = __('Dispatch Events') ?>
15 |
16 |
17 |
18 | = __('Will trigger a PHP "process" method who on his turn fires a browser event where is being listened to.') ?>
19 |
20 |
21 |
22 |
23 |
24 | = __('At this point the subsequent dispatched a process:after event set within the Magewire
25 | component. It also added a note to the finished list of todo\'s.') ?>
26 |
27 |
28 |
29 |
30 |
31 | Toggle Process
32 |
33 |
34 |
35 |
57 |
--------------------------------------------------------------------------------
/src/view/frontend/templates/magewire/todo.phtml:
--------------------------------------------------------------------------------
1 |
2 |
3 | = __('Tasks Todo') ?> = count($magewire->getTasks()) ?>
4 |
5 |
6 |
48 |
49 |
--------------------------------------------------------------------------------
/src/view/frontend/templates/magewire/form.phtml:
--------------------------------------------------------------------------------
1 | getMagewire();
11 | ?>
12 |
49 |
--------------------------------------------------------------------------------
/src/view/frontend/templates/magewire/features/magic-actions-properties.phtml:
--------------------------------------------------------------------------------
1 | getMagewire();
11 | ?>
12 |
13 |
14 | = __('Magic Actions & Properties') ?>
15 |
16 |
17 |
18 | $toggle
19 |
20 | Perform Action
21 |
22 | $set('foo', 'bar')
23 |
24 | Perform Action
25 |
26 | $set('foo', '$toggle')
27 |
28 | Perform Action
29 |
30 |
31 |
32 | $emit('global', 'message')
33 | Perform Action
34 |
35 |
36 |
37 | $emitTo('magic-actions-properties.emit-specific', 'global', 'message')
38 | Perform Action
39 |
40 |
41 |
42 | = $block->getChildHtml('magic-actions-properties.emit-global') ?>
43 | = $block->getChildHtml('magic-actions-properties.emit-specific') ?>
44 |
45 |
46 |
--------------------------------------------------------------------------------
/src/Magewire/Todo.php:
--------------------------------------------------------------------------------
1 | 'saveTask'];
33 |
34 | /**
35 | * @param string|null $title
36 | * @return FlashMessage|void
37 | */
38 | public function saveTask(string $title = null)
39 | {
40 | $title = ucfirst($title ?? $this->getTask());
41 |
42 | if (empty($title)) {
43 | return $this->dispatchErrorMessage(__('Title can not be empty'));
44 | }
45 | if (in_array($title, $this->tasks) && !$this->hasIndex()) {
46 | return $this->dispatchWarningMessage(__('Task already exists'));
47 | }
48 | if ($this->hasIndex()) {
49 | $this->tasks[$this->getIndex()] = $title;
50 | } else {
51 | $this->tasks[] = $title;
52 | }
53 |
54 | // Notify the customer just once as this is an example
55 | if (count($this->getTasks()) === 1) {
56 | $this->dispatchSuccessMessage(__('Task has been saved successfully.'));
57 | }
58 | // Always empty it's current input title.
59 | $this->task = '';
60 | }
61 |
62 | /**
63 | * @param int $index
64 | * @return FlashMessage|void
65 | */
66 | public function finishTask(int $index)
67 | {
68 | if (!isset($this->tasks[$index])) {
69 | return $this->dispatchErrorMessage(__('Task with ID "%1" doesnt exist', [$index]));
70 | }
71 |
72 | $this->emitTo('magewire.todo-checklist', 'todo:finish', $this->tasks[$index]);
73 | unset($this->tasks[$index]);
74 | }
75 |
76 | public function modifyTask(int $index)
77 | {
78 | if (!isset($this->tasks[$index])) {
79 | return $this->dispatchErrorMessage(__('Task with ID "%1" doesnt exist', [$index]));
80 | }
81 |
82 | $this->index = $index;
83 | $this->task = $this->tasks[$index];
84 | }
85 | }
86 |
--------------------------------------------------------------------------------
/src/Magewire/Todo/Todo/Todo.php:
--------------------------------------------------------------------------------
1 | 'saveTask'];
33 |
34 | /**
35 | * @param string|null $title
36 | * @return FlashMessage|void
37 | */
38 | public function saveTask(string $title = null)
39 | {
40 | $title = ucfirst($title ?? $this->getTask());
41 |
42 | if (empty($title)) {
43 | return $this->dispatchErrorMessage(__('Title can not be empty'));
44 | }
45 | if (in_array($title, $this->tasks) && !$this->hasIndex()) {
46 | return $this->dispatchWarningMessage(__('Task already exists'));
47 | }
48 | if ($this->hasIndex()) {
49 | $this->tasks[$this->getIndex()] = $title;
50 | } else {
51 | $this->tasks[] = $title;
52 | }
53 |
54 | // Notify the customer just once as this is an example
55 | if (count($this->getTasks()) === 1) {
56 | $this->dispatchSuccessMessage(__('Task has been saved successfully.'));
57 | }
58 | // Always empty it's current input title.
59 | $this->task = '';
60 | }
61 |
62 | /**
63 | * @param int $index
64 | * @return FlashMessage|void
65 | */
66 | public function finishTask(int $index)
67 | {
68 | if (!isset($this->tasks[$index])) {
69 | return $this->dispatchErrorMessage(__('Task with ID "%1" doesnt exist', [$index]));
70 | }
71 |
72 | $this->emitTo('magewire.todo-checklist', 'todo:finish', $this->tasks[$index]);
73 | unset($this->tasks[$index]);
74 | }
75 |
76 | public function modifyTask(int $index)
77 | {
78 | if (!isset($this->tasks[$index])) {
79 | return $this->dispatchErrorMessage(__('Task with ID "%1" doesnt exist', [$index]));
80 | }
81 |
82 | $this->index = $index;
83 | $this->task = $this->tasks[$index];
84 | }
85 | }
86 |
--------------------------------------------------------------------------------
/src/Magewire/Pagination.php:
--------------------------------------------------------------------------------
1 | toPage(max($this->getPage() - 1, 1));
21 | }
22 |
23 | /**
24 | * @inheritDoc
25 | */
26 | public function toNextPage(): void
27 | {
28 | $this->toPage($this->getPage() + 1);
29 | }
30 |
31 | /**
32 | * @inheritDoc
33 | */
34 | public function toPage($page): void
35 | {
36 | if ($page === $this->getLastPage()) {
37 | $this->dispatchNoticeMessage(__('You\'ve reached the last page.'));
38 | }
39 | if ($page === 1) {
40 | $this->dispatchNoticeMessage(__('You\'ve reached the first page.'));
41 | }
42 |
43 | $this->page = (int) $page;
44 | }
45 |
46 | /**
47 | * @inheritDoc
48 | */
49 | public function getSize(): int
50 | {
51 | return (int) ceil(count($this->getAllPageItems()) / $this->getPageSize());
52 | }
53 |
54 | /**
55 | * @inheritDoc
56 | */
57 | public function onFirstPage(): bool
58 | {
59 | return $this->getPage() === 1;
60 | }
61 |
62 | /**
63 | * @inheritDoc
64 | */
65 | public function getLastPage(): int
66 | {
67 | return $this->getSize();
68 | }
69 |
70 | /**
71 | * @inheritDoc
72 | */
73 | public function hasPages(): bool
74 | {
75 | return $this->getSize() > 0;
76 | }
77 |
78 | /**
79 | * @inheritDoc
80 | */
81 | public function onLastPage(): bool
82 | {
83 | return $this->getPage() === $this->getLastPage();
84 | }
85 |
86 | /**
87 | * @inheritDoc
88 | */
89 | public function isCurrentPage($page): bool
90 | {
91 | return $this->getPage() === (int) $page;
92 | }
93 |
94 | /**
95 | * @inheritDoc
96 | */
97 | public function hasMorePages(): bool
98 | {
99 | return !$this->onLastPage();
100 | }
101 |
102 | /**
103 | * @inheritDoc
104 | */
105 | public function getPageItems(): array
106 | {
107 | $offset = ($this->getPage() * $this->getPageSize()) - $this->getPageSize();
108 | $length = $this->getPageSize();
109 |
110 | return array_slice($this->getAllPageItems(), $offset, $length, true);
111 | }
112 |
113 | /**
114 | * @inheritDoc
115 | * @throws \Exception
116 | */
117 | public function getAllPageItems(): array
118 | {
119 | $items = [];
120 |
121 | for ($i = 0; $i < 100; $i ++) {
122 | $items[] = [
123 | 'w' => $i,
124 | 'd' => random_int(0, 99),
125 | 's' => random_int(0, 99),
126 | 'a' => random_int(0, 99),
127 | ];
128 | }
129 |
130 | return $items;
131 | }
132 | }
133 |
--------------------------------------------------------------------------------
/src/view/frontend/templates/magewire/list-shuffle.phtml:
--------------------------------------------------------------------------------
1 | getMagewire();
11 | ?>
12 |
13 |
14 | = __('List Shuffle') ?>
15 |
16 |
17 |
18 | getThings() as $thing): ?>
19 |
20 | = $thing['title'] ?>
21 |
22 |
23 |
24 |
25 |
26 | = __('As a final measure, adding wire:key will directly tell Magewire how to keep track of a
27 | DOM element. Over-using this attribute is a smell, but it is very useful and powerful for problems of this
28 | nature.') ?>
29 |
30 |
31 |
34 |
35 |
39 | Mark "Shuffle" task as done.
40 |
41 |
42 |
43 | = __('Awesome, now watch what happens within the "Tasks Done" example when you click Shuffle.') ?>
44 |
45 |
46 |
47 |
Shuffle
48 |
49 |
50 |
88 |
--------------------------------------------------------------------------------
/src/view/frontend/layout/magewire_examples_index.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
8 |
9 |
12 |
13 |
14 |
15 |
16 | \Magewirephp\MagewireExamples\Magewire\Todo\Todo\Todo
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 | \Magewirephp\MagewireExamples\Magewire\TodoChecklist
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 | \Magewirephp\MagewireExamples\Magewire\ListShuffle
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 | \Magewirephp\MagewireExamples\Magewire\DispatchEvents
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 | \Magewirephp\MagewireExamples\Magewire\Pagination
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 | \Magewirephp\MagewireExamples\Magewire\Reacticon
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 | \Magewirephp\MagewireExamples\Magewire\Form
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 | \Magewirephp\MagewireExamples\Magewire\QuickSearch
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
--------------------------------------------------------------------------------
/src/view/frontend/templates/magewire/reacticon.phtml:
--------------------------------------------------------------------------------
1 | getMagewire();
11 | ?>
12 |
13 |
14 | Reacticon
15 |
16 |
17 |
23 |
28 | += $magewire->getInterval() ?>
29 |
30 |
31 |
32 | = $magewire->getQuantity() ?>
33 |
34 |
35 |
40 | −= $magewire->getInterval() ?>
41 |
42 |
43 |
44 |
47 |
48 |
49 | Show configuration
50 |
51 |
52 |
82 |
83 |
84 |
85 | = __('This example was used during my Magewire presentation at Reacticon V4. Go check it out on %1 if you
86 | missed it.', ['YouTube ']) ?>
87 |
88 |
89 |
--------------------------------------------------------------------------------
/src/view/frontend/layout/magewire_features_index.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
8 |
9 |
12 |
13 |
14 | - \Magewirephp\MagewireExamples\Magewire\Features\DataViaLayout
15 | - bar
16 | - FOO
17 | - true
18 |
19 |
20 |
21 |
22 |
25 |
26 |
27 | \Magewirephp\MagewireExamples\Magewire\Features\TemplateMethods
28 |
29 |
30 |
31 |
32 |
35 |
36 |
37 | -
38 | \Magewirephp\MagewireExamples\Magewire\Features\SwitchTemplate
39 |
40 | -
41 |
-
42 | Magewirephp_MagewireExamples::magewire/features/switch-template/a.phtml
43 |
44 | -
45 | Magewirephp_MagewireExamples::magewire/features/switch-template/b.phtml
46 |
47 | -
48 | Magewirephp_MagewireExamples::magewire/features/switch-template/c.phtml
49 |
50 |
51 |
52 |
53 |
54 |
56 |
58 |
59 |
60 |
63 |
64 |
65 | \Magewirephp\MagewireExamples\Magewire\Features\WireIgnore
66 |
67 |
68 |
69 |
70 |
73 |
74 |
75 | \Magewirephp\MagewireExamples\Magewire\Features\MagicActionsProperties
76 |
77 |
78 |
79 |
82 |
83 |
84 | \Magewirephp\MagewireExamples\Magewire\Features\MagicActionsProperties\EmitGlobal
85 |
86 |
87 |
88 |
89 |
92 |
93 |
94 | \Magewirephp\MagewireExamples\Magewire\Features\MagicActionsProperties\EmitSpecific
95 |
96 |
97 |
98 |
99 |
100 |
103 |
104 |
105 | - \Magewirephp\MagewireExamples\Magewire\Features\Reset
106 | - Bar
107 | - Foo
108 |
109 |
110 |
111 |
112 |
115 |
116 |
117 | \Magewirephp\MagewireExamples\Magewire\Features\DollarWire
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
--------------------------------------------------------------------------------