@nolikein/types-livewire3 1.0.4 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +144 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -4,7 +4,150 @@ Allows you to use the power of TypeScript with Livewire 3.
|
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
7
|
-
npm i @nolikein/types-livewire3
|
|
7
|
+
npm i -D @nolikein/types-livewire3
|
|
8
|
+
|
|
9
|
+
## Usage
|
|
10
|
+
|
|
11
|
+
### Declare Livewire and AlpineJs into your project
|
|
12
|
+
|
|
13
|
+
First you need to install the alpineJs TypeScript definition.
|
|
14
|
+
|
|
15
|
+
npm i -D @types/alpinejs
|
|
16
|
+
|
|
17
|
+
Look where is stored your main ts file (your entrypoint) and create at the same place a `livewire.d.ts` file.
|
|
18
|
+
```ts
|
|
19
|
+
// Note: "@/your_livewire_path" is an alias to the "vendor/livewire/livewire/dist/livewire.esm.js" file
|
|
20
|
+
declare module '@/your_livewire_path' {
|
|
21
|
+
export const Livewire: import('@nolikein/types-livewire3').Livewire.Livewire;
|
|
22
|
+
export const Alpine: import('@types/alpinejs').Alpine
|
|
23
|
+
}
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Now you are able to use Livewire and Alpine inside your project. Here is an example with an index.ts file
|
|
27
|
+
```ts
|
|
28
|
+
import { Livewire, Alpine } from '@/your_livewire_path';
|
|
29
|
+
|
|
30
|
+
Alpine.data('YourCOmponent', () => {});
|
|
31
|
+
Livewire.start();
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### Create an Alpine component that use a $wire instance
|
|
35
|
+
|
|
36
|
+
#### Use my dedicated package
|
|
37
|
+
Why complicating things when i created the easiest way to do it ? Follow [that link](https://www.npmjs.com/package/@nolikein/typed-alpinejs#user-content-alpinejs-with-livewire-3), install the package then read the instructions.
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
#### The hard way to learn
|
|
41
|
+
|
|
42
|
+
Here we create a Dropdown component inside a Livewire component that have a "state" public bool property and an "open" public method that return the "state" property as a value.
|
|
43
|
+
```ts
|
|
44
|
+
import { AlpineWired } from "@nolikein/types-livewire3";
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Create the types/interfaces of your component.
|
|
48
|
+
*/
|
|
49
|
+
// Your AlpineJs component properties and methods
|
|
50
|
+
interface AlpineData {
|
|
51
|
+
open: boolean
|
|
52
|
+
toggle(): void
|
|
53
|
+
callBackendToOpen(): void
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Your Livewire public properties
|
|
57
|
+
interface LivewireData {
|
|
58
|
+
// The livewire component has an "state" property that is a boolean
|
|
59
|
+
state: boolean
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Your Livewire public methods
|
|
63
|
+
interface LivewireMethods {
|
|
64
|
+
// The livewire component has an "open" method that you can call then return a boolean
|
|
65
|
+
open(): boolean
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Create your component
|
|
70
|
+
*/
|
|
71
|
+
export default (
|
|
72
|
+
// Component parameters...
|
|
73
|
+
) => ({
|
|
74
|
+
// Component properties ...
|
|
75
|
+
open: false,
|
|
76
|
+
|
|
77
|
+
// Component methods ...
|
|
78
|
+
toggle(): void {
|
|
79
|
+
this.open = !this.open;
|
|
80
|
+
},
|
|
81
|
+
|
|
82
|
+
callBackendToOpen(): void {
|
|
83
|
+
this.$wire.call('open', (data: ({ state: boolean })) => {
|
|
84
|
+
console.log("The livewire component has updated its open property with the value " + data.state);
|
|
85
|
+
});
|
|
86
|
+
},
|
|
87
|
+
}) as AlpineWired.AlpineComponent<AlpineData, LivewireData, LivewireMethods>;
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### Create an AlpineJS only component
|
|
93
|
+
|
|
94
|
+
#### Use my dedicated package
|
|
95
|
+
Why complicating things when i created the easiest way to do it ? Follow [that link](https://www.npmjs.com/package/@nolikein/typed-alpinejs#user-content-alpinejs-component-only), install the package then read the instructions.
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
#### The hard way to learn
|
|
99
|
+
|
|
100
|
+
```ts
|
|
101
|
+
import { Alpine } from "@nolikein/types-livewire3";
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Create the type/interface of your component.
|
|
105
|
+
*/
|
|
106
|
+
// Type version
|
|
107
|
+
type Component = Alpine.AlpineComponent<{
|
|
108
|
+
open: boolean
|
|
109
|
+
toggle(): void
|
|
110
|
+
// Here is a method that pass "this" as a parameter named "self"
|
|
111
|
+
// It is rarely used. The only case i found is when you use the method as a callback
|
|
112
|
+
// See the component code to know more about it
|
|
113
|
+
displayOpenState(self: Alpine.AlpineComponentSelf<Component>): void
|
|
114
|
+
}>
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Create your component
|
|
118
|
+
*/
|
|
119
|
+
export default (
|
|
120
|
+
// Component parameters...
|
|
121
|
+
) => ({
|
|
122
|
+
// Component properties ...
|
|
123
|
+
open: false,
|
|
124
|
+
|
|
125
|
+
// Component methods ...
|
|
126
|
+
toggle(): void {
|
|
127
|
+
this.open = !this.open;
|
|
128
|
+
},
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* The hook that is run when the component is created.
|
|
132
|
+
*
|
|
133
|
+
* You don't need to include it inside your component interface
|
|
134
|
+
* since it is a part of the Alpine.AlpineComponent interface.
|
|
135
|
+
*/
|
|
136
|
+
init(): void {
|
|
137
|
+
// You can execute a component method as a callback and use "this"
|
|
138
|
+
// inside of it with that pattern
|
|
139
|
+
setTimeout(
|
|
140
|
+
() => this.displayOpenState(this),
|
|
141
|
+
1000,
|
|
142
|
+
);
|
|
143
|
+
},
|
|
144
|
+
// A method that use "this" but named as "self".
|
|
145
|
+
displayOpenState(self: Alpine.AlpineComponentSelf<Component>): void {
|
|
146
|
+
console.log("Here is your open state: " + self.open);
|
|
147
|
+
},
|
|
148
|
+
}) as Component;
|
|
149
|
+
|
|
150
|
+
```
|
|
8
151
|
|
|
9
152
|
## Licence
|
|
10
153
|
|