-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(form): add widget override for number separator
- Loading branch information
Showing
11 changed files
with
206 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
packages/form/addon/components/cf-field/input/number-separator.hbs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<input | ||
type="text" | ||
class="uk-input | ||
{{if @field.isInvalid 'uk-form-danger'}} | ||
{{if this.disabled 'uk-disabled'}}" | ||
name={{@field.pk}} | ||
id={{@field.pk}} | ||
value={{this.displayValue}} | ||
placeholder={{@field.question.raw.placeholder}} | ||
readonly={{this.disabled}} | ||
{{on "input" this.input}} | ||
/> |
46 changes: 46 additions & 0 deletions
46
packages/form/addon/components/cf-field/input/number-separator.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { action } from "@ember/object"; | ||
import { inject as service } from "@ember/service"; | ||
import Component from "@glimmer/component"; | ||
import { cached } from "tracked-toolbox"; | ||
|
||
export default class CfFieldInputNumberSeparatorComponent extends Component { | ||
@service intl; | ||
|
||
get disabled() { | ||
return this.args.disabled || this.args.field?.question.isCalculated; | ||
} | ||
|
||
get displayValue() { | ||
if (!this.args.field.value) { | ||
return ""; | ||
} | ||
|
||
return this.intl.formatNumber(this.args.field.value, { | ||
maximumFractionDigits: 20, | ||
}); | ||
} | ||
|
||
@cached | ||
get thousandSeparator() { | ||
return this.intl.formatNumber(11111).replace(/\p{Number}/gu, ""); | ||
} | ||
|
||
@cached | ||
get decimalSeparator() { | ||
return this.intl.formatNumber(1.1).replace(/\p{Number}/gu, ""); | ||
} | ||
|
||
@action | ||
input({ target: { value } }) { | ||
// We need to remove the thousand separator and replace the decimal | ||
// separator with a dot in order to parse it into a number. Which character | ||
// those are is determined per locale in the getters above. | ||
const serialized = Number( | ||
value | ||
.replace(new RegExp(`\\${this.thousandSeparator}`, "g"), "") | ||
.replace(new RegExp(`\\${this.decimalSeparator}`), "."), | ||
); | ||
|
||
this.args.onSave(isNaN(serialized) ? null : serialized); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
packages/form/app/components/cf-field/input/number-separator.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from "@projectcaluma/ember-form/components/cf-field/input/number-separator"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
packages/form/tests/integration/components/cf-field/input/number-separator-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { fillIn, render } from "@ember/test-helpers"; | ||
import { tracked } from "@glimmer/tracking"; | ||
import { hbs } from "ember-cli-htmlbars"; | ||
import { setLocale, setupIntl } from "ember-intl/test-support"; | ||
import { module, test } from "qunit"; | ||
|
||
import { setupRenderingTest } from "dummy/tests/helpers"; | ||
|
||
module( | ||
"Integration | Component | cf-field/input/number-separator", | ||
function (hooks) { | ||
setupRenderingTest(hooks); | ||
setupIntl(hooks); | ||
|
||
hooks.beforeEach(function () { | ||
setLocale(["de-ch", "de"]); | ||
|
||
this.field = new (class { | ||
questionType = "IntegerQuestion"; | ||
question = { isCalculated: false }; | ||
@tracked value = null; | ||
})(); | ||
|
||
this.save = (value) => { | ||
this.field.value = value; | ||
}; | ||
}); | ||
|
||
test("it converts integers to formatted strings and saves them properly", async function (assert) { | ||
await render( | ||
hbs`<CfField::Input::NumberSeparator @field={{this.field}} @onSave={{this.save}} />`, | ||
); | ||
|
||
await fillIn("input", "1234"); | ||
|
||
assert.strictEqual(this.field.value, 1234); | ||
assert.dom("input").hasValue("1’234"); | ||
}); | ||
|
||
test("it converts floats to formatted strings and saves them properly", async function (assert) { | ||
this.field.questionType = "FloatQuestion"; | ||
|
||
await render( | ||
hbs`<CfField::Input::NumberSeparator @field={{this.field}} @onSave={{this.save}} />`, | ||
); | ||
|
||
await fillIn("input", "1234.123"); | ||
|
||
assert.strictEqual(this.field.value, 1234.123); | ||
assert.dom("input").hasValue("1’234.123"); | ||
}); | ||
|
||
test("it displays calculated floats properly", async function (assert) { | ||
this.field.questionType = "CalculatedFloatQuestion"; | ||
this.field.question.isCalculated = true; | ||
this.field.value = 1234.123; | ||
|
||
await render( | ||
hbs`<CfField::Input::NumberSeparator @field={{this.field}} />`, | ||
); | ||
|
||
assert.dom("input").hasAttribute("readonly"); | ||
assert.dom("input").hasClass("uk-disabled"); | ||
assert.dom("input").hasValue("1’234.123"); | ||
}); | ||
|
||
test("it works with other locales", async function (assert) { | ||
setLocale(["en-us", "en"]); | ||
|
||
await render( | ||
hbs`<CfField::Input::NumberSeparator @field={{this.field}} @onSave={{this.save}} />`, | ||
); | ||
|
||
await fillIn("input", "1234.123"); | ||
|
||
assert.strictEqual(this.field.value, 1234.123); | ||
assert.dom("input").hasValue("1,234.123"); | ||
}); | ||
}, | ||
); |