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
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
address code review
  • Loading branch information
rkcrisafi committed Jun 9, 2023
commit 62d3d1e53633dd24d801a3cf4789baead6c14488
2 changes: 1 addition & 1 deletion docs/components/input.md
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ const App = () => (

### Currency

Has the `prefix` of `$` and `suffix` of `USD`. It only allows numeric input. It converts numbers into comma separated currency format.
Use the `currency` attribute to set the currency symbol and abbreviation. It only allows integers which will be converted into a comma-separated currency format. Only `usd` is implemented.

```html preview
<sl-input currency="usd"></sl-input>
Expand Down
12 changes: 6 additions & 6 deletions src/components/input/input.styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,27 +296,27 @@ export default css`
* Currency
*/

.input__prefix--currency.input__prefix--small {
.input--currency.prefix--small {
margin-left: var(--sl-spacing-small);
}

.input__prefix--currency.input__prefix--medium {
.input--currency.prefix--medium {
margin-left: var(--sl-spacing-medium);
}

.input__prefix--currency.input__prefix--large {
.input--currency.prefix--large {
margin-left: var(--sl-spacing-large);
}

.input__suffix--currency.input__suffix--small {
.input--currency.suffix--small {
margin-right: var(--sl-spacing-small);
}

.input__suffix--currency.input__suffix--medium {
.input--currency.suffix--medium {
margin-right: var(--sl-spacing-medium);
}

.input__suffix--currency.input__suffix--large {
.input--currency.suffix--large {
margin-right: var(--sl-spacing-large);
}
`;
73 changes: 43 additions & 30 deletions src/components/input/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,11 +252,6 @@ 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;
}

private handleClearClick(event: MouseEvent) {
this.value = '';
this.emit('sl-clear');
Expand All @@ -273,8 +268,14 @@ export default class SlInput extends ShoelaceElement implements ShoelaceFormCont
}

private handleInput() {
this.value = this.input.value;
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.

} 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.emit('sl-input');
}

Expand Down Expand Up @@ -460,17 +461,23 @@ export default class SlInput extends ShoelaceElement implements ShoelaceFormCont
'input--no-spin-buttons': this.noSpinButtons
})}
>
<slot
name="prefix"
part="prefix"
class="${classMap({
'input__prefix': true,
'input__prefix--currency': !!this.currency,
'input__prefix--small': this.size === 'small',
'input__prefix--medium': this.size === 'medium',
'input__prefix--large': this.size === 'large'
})}">
${ this.currency === "usd" ? "$" : "" }
<slot name="prefix" part="prefix" class="input__prefix">
${
this.currency === "usd"
? html`
<div
class=${classMap({
'input--currency': !!this.currency,
'prefix--small': this.size === 'small',
'prefix--medium': this.size === 'medium',
'prefix--large': this.size === 'large'
})}
>
$
</div>
`
: ''
}
</slot>

<input
Expand Down Expand Up @@ -500,7 +507,7 @@ export default class SlInput extends ShoelaceElement implements ShoelaceFormCont
inputmode=${ifDefined(this.inputmode)}
aria-describedby="help-text"
@change=${this.handleChange}
@input=${this.currency === 'usd' ? this.handleCurrencyInput : this.handleInput}
@input=${this.handleInput}
@invalid=${this.handleInvalid}
@keydown=${this.handleKeyDown}
@focus=${this.handleFocus}
Expand Down Expand Up @@ -552,17 +559,23 @@ export default class SlInput extends ShoelaceElement implements ShoelaceFormCont
: ''
}

<slot
name="suffix"
part="suffix"
class="${classMap({
'input__suffix': true,
'input__suffix--currency': !!this.currency,
'input__suffix--small': this.size === 'small',
'input__suffix--medium': this.size === 'medium',
'input__suffix--large': this.size === 'large'
})}">
${ this.currency === "usd" ? "USD" : "" }
<slot name="suffix" part="suffix" class="input__suffix">
${
this.currency === "usd"
? html`
<div
class=${classMap({
'input--currency': !!this.currency,
'suffix--small': this.size === 'small',
'suffix--medium': this.size === 'medium',
'suffix--large': this.size === 'large'
})}
>
USD
</div>
`
: ''
}
</slot>
</div>
</div>
Expand Down