Adding x-bind Dynamically through Custom Directive #4492
-
I want to write a custom directive called "register," which I will use to register my input fields in my form. I want to add "x-bind" to that input field from this directive. I want react-hook-form like system to register my input fields, which will add all required attributes and events like OnClick or OnChange in that field. Usage will be like
I earlier wrote this
This adds "x-bind" when I see HTML like this.
But it does not work. I remove "x-register" and add "x-bind" with all data then it works which means there is no issue in my
I want to add a specific "x-bind" for each type of input field such as radio, text, checkbox, or number as all will have different behaviors. If there is any better solution, please do suggest. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You need to make sure the order is correct and your custom directive is processed before bind.
Im your code, you don't really need the x-bind attribute (unless it's for you to see it in the html) and you can call Alpine.bind directly in your custom directive. You also don't need effect. Look at the combobox component in the ui packages for an example. |
Beta Was this translation helpful? Give feedback.
You need to make sure the order is correct and your custom directive is processed before bind.
Alpine.directive(...your code...).before('bind')
Im your code, you don't really need the x-bind attribute (unless it's for you to see it in the html) and you can call Alpine.bind directly in your custom directive.
You also don't need effect.
Look at the combobox component in the ui packages for an example.