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

add a new currency input #13

Draft
wants to merge 5 commits into
base: next
Choose a base branch
from
Draft

Conversation

rkcrisafi
Copy link

@rkcrisafi rkcrisafi commented Jun 8, 2023

@vercel
Copy link

vercel bot commented Jun 8, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
shoelace ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 13, 2023 7:39pm

docs/components/input.md Outdated Show resolved Hide resolved
Copy link

@CrookedGrin CrookedGrin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for contributing! I think we need to re-think the comma masking approach since it's currently modifying the actual value of the input, and therefore what it sends in the form.

I think we can make a few other improvements related to how the styles and attributes are being applied.

@@ -249,6 +252,11 @@ export default class SlInput extends ShoelaceElement implements ShoelaceFormCont
this.emit('sl-change');
}

private handleCurrencyInput() {
const numberWithCommas = this.input.value.replace(/\D/g, "").replace(/\B(?=(\d{3})+(?!\d))/g, ",");
this.input.value = numberWithCommas;
Copy link

@CrookedGrin CrookedGrin Jun 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably shouldn't be modifying the value of the input directly, because this will send up a string including the commas when the form is submitted, which we would then have to parse back out on the server. It would also interfere with validation. If you want to do input masking, it should be on the display only, not the actual value.

If we do absolutely need to modify the value, we need to figure out how to undo this mutation when the value is read.

src/components/input/input.ts Outdated Show resolved Hide resolved
docs/components/input.md Outdated Show resolved Hide resolved
this.input.value = numberWithCommas;
} else {
this.value = this.input.value;
this.formControlController.updateValidity();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line still needs to be called regardless of the currency attribute

this.formControlController.updateValidity();
if (this.currency === "usd") {
const numberWithCommas = this.input.value.replace(/\D/g, "").replace(/\B(?=(\d{3})+(?!\d))/g, ",");
this.input.value = numberWithCommas;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving this logic into this method is better than having it inline, but we're still modifying the actual value of the input, which could break forms when we try to use it.

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.

3 participants