Giter Club home page Giter Club logo

Comments (6)

excid3 avatar excid3 commented on June 19, 2024 1

Thanks @adrienpoly. Using super isn't much of a concern, I know that'd work.

What I don't know is how to append items to the static targets array. It'd be nice not having to redefine that and simply add new targets in. I also wondered if redefining a static variable would throw an error or not.

from tailwindcss-stimulus-components.

excid3 avatar excid3 commented on June 19, 2024 1

Updated the readme with an example of this.

from tailwindcss-stimulus-components.

adrienpoly avatar adrienpoly commented on June 19, 2024

This is fairly easy to achieve with standard ES6 class inheritance.

I am using this very often and described it in the Flatpickr Stimulus Wrapper I did.
Here is the relevant documentation :
https://github.com/adrienpoly/stimulus-flatpickr#extends-the-controller

For the Modal controller it would look something like that:

//controllers/modal_controller.js
import { Modal } from "tailwindcss-stimulus-components"

export default class extends Modal {
  connect() {
    //your custom connect code
    super.connect(); // always call super to run parent connect code
  };

  open(e) {
    //your custom open code
    super.open(e); // always call super to run parent open code
  };
}

from tailwindcss-stimulus-components.

excid3 avatar excid3 commented on June 19, 2024

From a quick test, it looks like you can rewrite it as static, but not modify the array. We may be able to just remove the static from our components to make these extendable.

class SuperDropdown extends Dropdown {
  static targets = [ 'menu', 'extra' ]
}
console.log(SuperDropdown.targets)
// ['menu', 'extra']
class SuperDropdown extends Dropdown {
  connect() {
    this.targets.push('extra')
    super.connect()
  }
}
console.log(SuperDropdown.targets)
// ['menu']

from tailwindcss-stimulus-components.

adrienpoly avatar adrienpoly commented on June 19, 2024

Ok I didn't though initially about the targets that is indeed a good point. From my testing I think this works out of the box.

you can have a look at this example :
https://glitch.com/edit/#!/pepper-dingo?path=src/controllers/super-input_controller.js:10:1

The extended controller

//controllers/super-input_controller.js
import { Controller } from "stimulus";

class Input extends Controller {
  static targets = ["parent"]

  connect() {
    console.log("the value of parentTarget : ", this.parentTarget.value)
  }
}

export default class extends Input {
  static targets = ["children"]

  connect() {
    super.connect();
    console.log("the value of childrenTarget : ", this.childrenTarget.value)
  }
}

The Html

<div data-controller="super-input">
  <input type="text"
        value="children target"
        data-target="super-input.children">
  
  <input type="text"
        value="parent target"
        data-target="super-input.parent">
</div>

As a result in my console.log I have both targets values available 🎉

the value of parentTarget :  parent target
the value of childrenTarget :  children target

from tailwindcss-stimulus-components.

excid3 avatar excid3 commented on June 19, 2024

Sweet, I didn't even try that, assuming it would replace the value. That looks like it does the trick.

from tailwindcss-stimulus-components.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.