0.11.0 • Published 3 months ago

adonisjs-livewire v0.11.0

Weekly downloads
-
License
MIT
Repository
-
Last release
3 months ago

Demo

https://pingcrm.kabbouchi.com/ - https://github.com/KABBOUCHI/adonisjs-livewire-pingcrm

Getting Started

This package is available in the npm registry.

npm install adonisjs-livewire

Next, configure the package by running the following command.

node ace configure adonisjs-livewire

Configuration

Enable ALS in config/app.ts https://docs.adonisjs.com/guides/async-local-storage#usage

// config/app.ts
export const http: ServerConfig = {
  useAsyncLocalStorage: true,
}

now you can use this.ctx in your Livewire components.

Create a Livewire component

node ace make:livewire Counter

# or

node ace make:livewire Counter --inline

Basic Usage

// views/welcome.edge
<!DOCTYPE html>
<html lang="en">
<head>
  @livewireStyles
</head>
<body>
  @livewire('counter') or  @livewire('Counter') or <livewire:counter />
  @livewire('search-users') or  @livewire('SearchUsers') or <livewire:search-users />

  @livewireScripts
</body>
</html>

Component as Page

Create layout file

node ace livewire:layout
node ace livewire:layout <name>

Add routes

// start/routes.ts

router.livewire('/', 'counter') // app/livewire/counter.ts
router.livewire('/', 'counter', { initialCounter: 10 })
router.livewire('/search-users', 'search-users') // app/livewire/search-users.ts
router.livewire('/search-users') // app/livewire/search-users.ts
router.livewire('/search-users', 'search-users.index') // app/livewire/search-users/index.ts

Registering Custom Components

You may manually register components using the Livewire::component method. This can be useful if you want to provide Livewire components from a composer package. Typically this should be done in the ready method of a service provider.

import type { ApplicationService } from '@adonisjs/core/types'
import { Component } from 'adonisjs-livewire'

export default class AppProvider {
  constructor(protected app: ApplicationService) {}

  public async ready() {
    const Livewire = await this.app.container.make('livewire')

    Livewire.component(
      'custom-component',
      class extends Component {
        public title = ''

        public mount({ title }) {
          this.title = title
        }

        async render() {
          return '<div>{{ title }}</div>'
        }
      }
    )
  }
}

Now, applications with your package installed can consume your component in their views like so:

@livewire('custom-component', {
  title: 'My Component'
})

// or

<livewire:custom-component title="My Component" />

Creating mixins

// app/livewire_mixins/my_mixin.ts
import { Component } from 'adonisjs-livewire'
export interface MyMixin extends Component {}
export class MyMixin {
  public foo = 'bar'

  public baz() {
    return 'baz'
  }
}

// or (not recommended)

export class MyMixin extends Component {
  public foo = 'bar'

  public baz() {
    return 'baz'
  }
}
// app/livewire/counter.ts
import { Component, Mixin } from 'adonisjs-livewire'
import MyMixin from '#app/livewire_mixins/my_mixin'

export default class Counter extends Mixin(Component, MyMixin) {
  public count = 0

  public increment() {
    this.count++
  }

  public decrement() {
    this.count--
  }

  public render() {
    return `
      <div>
        <button wire:click="increment">+</button>
        <h1>{{ count }}</h1>
        <button wire:click="decrement">-</button>
        <h2>{{ foo }}</h2>
        <h3>{{ baz() }}</h3>
      </div>
    `
  }
}

SFC (Experimental)

Define component logic using <script server> tag inside livewire edge component. e.g: resources/views/livewire/counter.edge

<script server>
  import { Component } from 'adonisjs-livewire'

  export default class extends Component {
    count = 0

    increment() {
      this.count++
    }

    decrement() {
      this.count--
    }
  }
</script>

<div>
  <button wire:click="decrement">-</button>

  <h1>{{ count }}</h1>

  <button wire:click="increment">+</button>
</div>

Helpers

Edge tag compiler (Experimental)

This package includes Edge tags support via edge-tags.

<x-button class="bg-red" a="b" :foo="bar" baz="{{ 1 + 2 }}">
  Hello
</x-button>

will be compiled to

@component('button or components/button or components/button/index', { class: 'bg-red', a: 'b', foo: bar, baz: `${1 + 2}` })
  Hello
@end

Edge component class (Experimental)

// app/compoments/button.ts
import { ViewComponent } from 'adonisjs-livewire'

export default class Button extends ViewComponent {
  type = 'button'
  text = ''

  constructor({ type, text }) {
    this.type = type || this.type
    this.text = text || this.text
  }

  isLoading() {
    return true // some logic
  }

  public render() {
    // or return this.view.render("components/button")
    return `
      <button type="{{ type }}" {{ isLoading() ? 'data-loading': '' }} {{ $props.only(['class']).toAttrs() }}>
        {{{ text || await $slots.main() }}}
      </button>
    `
  }
}
0.10.0

3 months ago

0.11.0

3 months ago

0.11.0-beta.1

3 months ago

0.8.2

3 months ago

0.9.0

3 months ago

0.8.1

4 months ago

0.8.0

4 months ago

0.8.1-beta.1

4 months ago

0.8.1-beta.0

4 months ago

0.8.1-beta.2

4 months ago

0.7.0

8 months ago

0.5.16

10 months ago

0.5.14

10 months ago

0.5.15

10 months ago

0.5.13

10 months ago

0.6.1

9 months ago

0.6.0

10 months ago

0.5.10

10 months ago

0.5.11

10 months ago

0.5.12

10 months ago

0.5.8

10 months ago

0.5.9

10 months ago

0.5.7

10 months ago

0.4.0-beta.7

10 months ago

0.5.0-beta.1

10 months ago

0.4.0

10 months ago

0.5.4

10 months ago

0.5.3

10 months ago

0.5.6

10 months ago

0.5.5

10 months ago

0.5.0

10 months ago

0.5.2

10 months ago

0.5.1

10 months ago

0.4.0-beta.4

10 months ago

0.4.0-beta.5

10 months ago

0.4.0-beta.6

10 months ago

0.3.5

10 months ago

0.3.4

10 months ago

0.4.0-beta.1

10 months ago

0.4.0-beta.2

10 months ago

0.4.0-beta.3

10 months ago

0.3.0

11 months ago

0.3.2

10 months ago

0.3.1

11 months ago

0.3.3

10 months ago

0.2.13

11 months ago

0.2.12

11 months ago

0.2.11

11 months ago

0.2.10

11 months ago

0.2.9

11 months ago

0.1.22

11 months ago

0.2.1

11 months ago

0.2.0

11 months ago

0.2.7

11 months ago

0.2.6

11 months ago

0.2.8

11 months ago

0.2.3

11 months ago

0.2.2

11 months ago

0.2.5

11 months ago

0.2.4

11 months ago

0.1.20

1 year ago

0.1.21

1 year ago

0.1.19

1 year ago

0.1.11

1 year ago

0.1.12

1 year ago

0.1.14

1 year ago

0.1.15

1 year ago

0.1.16

1 year ago

0.1.17

1 year ago

0.1.18

1 year ago

0.1.10

1 year ago

0.1.9

1 year ago

0.1.8

1 year ago

0.1.7

1 year ago

0.1.4

1 year ago

0.1.6

1 year ago

0.1.5

1 year ago

0.1.0

1 year ago

0.1.2

1 year ago

0.1.1

1 year ago

0.1.3

1 year ago

0.0.40

1 year ago

0.0.41

1 year ago

0.0.42

1 year ago

0.0.39

1 year ago

0.0.38

1 year ago

0.0.37

1 year ago

0.0.35

1 year ago

0.0.36

1 year ago

0.0.33

1 year ago

0.0.34

1 year ago

0.0.32

1 year ago

0.0.20

1 year ago

0.0.21

1 year ago

0.0.22

1 year ago

0.0.23

1 year ago

0.0.24

1 year ago

0.0.25

1 year ago

0.0.15

1 year ago

0.0.16

1 year ago

0.0.17

1 year ago

0.0.18

1 year ago

0.0.19

1 year ago

0.0.30

1 year ago

0.0.31

1 year ago

0.0.26

1 year ago

0.0.27

1 year ago

0.0.28

1 year ago

0.0.29

1 year ago

0.0.14

1 year ago

0.0.13

1 year ago

0.0.12

1 year ago

0.0.11

1 year ago

0.0.10

1 year ago

0.0.9

1 year ago

0.0.8

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago