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 all commits
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
19 changes: 19 additions & 0 deletions docs/components/input.md
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,25 @@ const App = () => (
);
```

### Currency

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>
```

```pug slim
sl-input currency="usd"
```

```jsx react
import { SlInput } from '@teamshares/shoelace/dist/react';

const App = () => <SlInput currency="usd" />;
```


### Customizing Label Position

Use [CSS parts](#css-parts) to customize the way form controls are drawn. This example uses CSS grid to position the label to the left of the control, but the possible orientations are nearly endless. The same technique works for inputs, textareas, radio groups, and similar form controls.
Expand Down
2 changes: 2 additions & 0 deletions docs/teamshares/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Changelog

## 1.2.1
- Added `currency` attribute with a value of `usd` to the input
## 1.2.0

- Updated to upstream [2.4.0](/resources/changelog)
Expand Down
18 changes: 16 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@teamshares/shoelace",
"description": "The Teamshares flavor of a forward-thinking library of web components.",
"version": "1.2.0",
"version": "1.2.1",
"upstreamVersion": "2.4.0",
"homepage": "https://github.com/teamshares/shoelace",
"author": "Cory LaViska",
Expand Down Expand Up @@ -74,6 +74,7 @@
"@shoelace-style/localize": "^3.0.4",
"composed-offset-position": "^0.0.4",
"data-grid-component": "github:teamshares/data-grid",
"imask": "^6.6.3",
"lit": "^2.6.1",
"qr-creator": "^1.0.0"
},
Expand Down
18 changes: 12 additions & 6 deletions src/components/input/input.styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,13 @@ export default css`
width: calc(1em + var(--sl-input-spacing-small) * 2);
}

.input--small .input__prefix::slotted(*) {
.input--small .input__prefix::slotted(*),
.input--small.input--currency .input__prefix {
margin-inline-start: var(--sl-input-spacing-small);
}

.input--small .input__suffix::slotted(*) {
.input--small .input__suffix::slotted(*),
.input--small.input--currency .input__suffix {
margin-inline-end: var(--sl-input-spacing-small);
}

Expand All @@ -196,11 +198,13 @@ export default css`
width: calc(1em + var(--sl-input-spacing-medium) * 2);
}

.input--medium .input__prefix::slotted(*) {
.input--medium .input__prefix::slotted(*),
.input--medium.input--currency .input__prefix {
margin-inline-start: var(--sl-input-spacing-medium);
}

.input--medium .input__suffix::slotted(*) {
.input--medium .input__suffix::slotted(*),
.input--medium.input--currency .input__suffix {
margin-inline-end: var(--sl-input-spacing-medium);
}

Expand All @@ -220,11 +224,13 @@ export default css`
width: calc(1em + var(--sl-input-spacing-large) * 2);
}

.input--large .input__prefix::slotted(*) {
.input--large .input__prefix::slotted(*),
.input--large.input--currency .input__prefix {
margin-inline-start: var(--sl-input-spacing-large);
}

.input--large .input__suffix::slotted(*) {
.input--large .input__suffix::slotted(*),
.input--large.input--currency .input__ {
margin-inline-end: var(--sl-input-spacing-large);
}

Expand Down
25 changes: 22 additions & 3 deletions src/components/input/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import ShoelaceElement from '../../internal/shoelace-element';
import styles from './input.styles';
import type { CSSResultGroup } from 'lit';
import type { ShoelaceFormControl } from '../../internal/shoelace-element';
import IMask from 'imask';

/**
* @summary Inputs collect data from the user.
Expand Down Expand Up @@ -173,6 +174,9 @@ export default class SlInput extends ShoelaceElement implements ShoelaceFormCont
/** Used to customize the label or icon of the Enter key on virtual keyboards. */
@property() enterkeyhint: 'enter' | 'done' | 'go' | 'next' | 'previous' | 'search' | 'send';

/** Adds currency symbol and abbreviation */
@property() currency = '';

/** Enables spell checking on the input. */
@property({
type: Boolean,
Expand Down Expand Up @@ -265,6 +269,15 @@ export default class SlInput extends ShoelaceElement implements ShoelaceFormCont
}

private handleInput() {
if (this.currency) {
var element = this.input;
var maskOptions = {
mask: Number,
thousandsSeparator: ','
}
IMask(element, maskOptions);
}

this.value = this.input.value;
this.formControlController.updateValidity();
this.emit('sl-input');
Expand Down Expand Up @@ -449,10 +462,14 @@ export default class SlInput extends ShoelaceElement implements ShoelaceFormCont
'input--disabled': this.disabled,
'input--focused': this.hasFocus,
'input--empty': !this.value,
'input--no-spin-buttons': this.noSpinButtons
'input--no-spin-buttons': this.noSpinButtons,
'input--currency': !!this.currency
})}
>
<slot name="prefix" part="prefix" class="input__prefix"></slot>
<slot name="prefix" part="prefix" class="input__prefix">
${ this.currency === "usd" ? "$" : ""}
</slot>

<input
part="input"
id="input"
Expand Down Expand Up @@ -532,7 +549,9 @@ export default class SlInput extends ShoelaceElement implements ShoelaceFormCont
: ''
}

<slot name="suffix" part="suffix" class="input__suffix"></slot>
<slot name="suffix" part="suffix" class="input__suffix">
${ this.currency === "usd" ? "USD" : "" }
</slot>
</div>
</div>

Expand Down