Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix mask plugin with data directive (CSP) #4090

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

kg-currenxie
Copy link

@kg-currenxie kg-currenxie commented Mar 12, 2024

Before:

Data directive:

Alpine.data('form', function() {
  return {
    amountMask: function (inputValue) {
      return this.$money(inputValue) // ❌ How to access the $money formatter?
    }
  }
})

HTML:

<form x-data="form">
  <input x-mask:dynamic="amountMask($input, $money)" /> // ❌ 
</form>

image

After

Data directive:

Alpine.data('form', function() {
  return {
    amountMask: function (inputValue, moneyMaskFormat) { // ✅ We get access to the formatter
      return moneyMaskFormat(inputValue)
    }
  }
})

HTML:

<form x-data="form">
  <input x-mask:dynamic="amountMask" /> // ✅ Valid CSP expression
</form>

Let me know if I'm missing something, or if it would break something else?
It does work in our app's UI tho.

@kg-currenxie
Copy link
Author

I can't figure out what causes the breaking tests really :) Help appreciated 🙏

This is the last bit of a huge CSP re-write, to deploy our new app to production :)

@ekwoka
Copy link
Contributor

ekwoka commented Mar 12, 2024

I think the improved API would probably be actually binding the function to an object with the scope/magics...

also not sure why it causes an issue with the failed tests...but definitely need to figure that one out...

@kg-currenxie
Copy link
Author

kg-currenxie commented Mar 12, 2024

binding the function to an object with the scope/magics...

not following a 100% :)

do you mean passing this as the 2nd argument instead? 🤔

{
  '$input': input,
  '$money': formatMoney.bind({ el }),
}

or

value(input).bind({
                                '$input': input,
                                '$money': formatMoney.bind({ el }),
                            })

🤔

@ekwoka
Copy link
Contributor

ekwoka commented Mar 12, 2024

more like

value.bind({ $input: input, $money: formatMoney.bind({el}) })(input)

But also whatever would make this refer to the data context as well...

@kg-currenxie
Copy link
Author

But also whatever would make this refer to the data context as well...

Yea, i just came across that issue. this would be undefined. Haven't solved that yet 😓

@ekwoka
Copy link
Contributor

ekwoka commented Mar 12, 2024

Well, I just showed....bind the value...

@kg-currenxie
Copy link
Author

Sorry, i spoke too soon :P

this now have access to $input and $money 👌 but not the data directive :/ which i need in my case.

basically im doing something like

      this.$money(
        this.$input,
        '.',
        ',',
        this.nonFractionCurrencies.includes(this.currency) ? 0 : 2,
      )

where nonFractionCurrencies and currency is stored in the data context

thanks for you continuing support! 🙏

@ekwoka
Copy link
Contributor

ekwoka commented Mar 12, 2024

You can possibly look at how magics are injected into the state in the first place, or use mergeProxies directly to layer them.

@kg-currenxie kg-currenxie marked this pull request as draft March 13, 2024 09:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants