Skip to content

Commit

Permalink
GC-2176/Add ability to remove dollar sign from CurrencyInput component (
Browse files Browse the repository at this point in the history
#433)

* Add prop and test to remove dollar sign from CurrencyInput

* Update version and changelog

* Oooops left on one too many zeros in versions
  • Loading branch information
bethqiang authored Jan 18, 2024
1 parent b81df3b commit fd68329
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## v2.0.0
- Removing beta tag since this is no longer in beta
- Adds a `showDollarSign` prop to the `CurrencyInput` component so we can hide it at will

## v2.0.0-beta.64
- Fixes import of `UserCircle` icon

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lob/ui-components",
"version": "2.0.0-beta.64",
"version": "2.0.0",
"engines": {
"node": ">=14.18.2",
"npm": ">=8.3.0"
Expand Down
17 changes: 14 additions & 3 deletions src/components/CurrencyInput/CurrencyInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ export default {
selectOnFocus: {
type: Boolean,
default: true
},
showDollarSign: {
type: Boolean,
default: true
}
},
emits: ['update:modelValue', 'input', 'change', 'focus', 'blur'],
Expand Down Expand Up @@ -132,7 +136,7 @@ export default {
return;
}
const formattedValue = this.formatter.format(value);
let formattedValue = this.formatter.format(value);
const { type, prevValue, editIndex } = stringDiff(this.formattedValue, formattedValue);
Expand All @@ -148,16 +152,23 @@ export default {
const separatorOffset = countOccurrences(formattedValue, ',') - countOccurrences(this.formattedValue, ',');
let newCaretPosition = inputEl.selectionStart + separatorOffset;
// If the user is inputting or deleting at the 0th index (the `$` sign), increment the caret position by an additional 1
if (newCaretPosition === 0 || newCaretPosition === 1) {
// If the user is inputting or deleting at the 0th index (the `$` sign), increment the caret position by an additional 1 unless showDollarSign is false
if (this.showDollarSign && (newCaretPosition === 0 || newCaretPosition === 1)) {
newCaretPosition = 2;
} else if (!this.showDollarSign && newCaretPosition === 0) {
newCaretPosition = 1;
}
// If the user replaces a '0' value, keep the caret position on the integer side of the decimal
if (type === 'edit' && editIndex <= this.formattedValue.indexOf('.') && prevValue === '0') {
newCaretPosition--;
}
// If showDollarSign is false, remove it from formatted value
if (!this.showDollarSign) {
formattedValue = formattedValue.slice(1);
}
// Update the value and formatted value
this.$emit('update:modelValue', this.formatter.parse(formattedValue));
this.formattedValue = formattedValue;
Expand Down
15 changes: 15 additions & 0 deletions src/components/CurrencyInput/__tests__/CurrencyInput.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,19 @@ describe('Currency input', () => {
expect(helperText).toBeInTheDocument();
});

describe('if show dollar sign is false', () => {

it('does not show the dollar sign', async () => {
const props = { ...initialProps, showDollarSign: false };
const { getByLabelText } = render(CurrencyInput, {
props
});
const currencyInput = getByLabelText(props.inputProps.label);

await fireEvent.update(currencyInput, '6.00');
expect(currencyInput.value).toEqual('6.00');
});

});

});

0 comments on commit fd68329

Please sign in to comment.