Skip to content

Commit 2efa0f2

Browse files
authored
fix(text-field): Default to min on empty value (#523)
* fix(trim-input): Check nullish first * fix(text-field): Default to min on empty value * chore: Update tests * chore: Update Freestyle * chore: Bump package version
1 parent 31aa70e commit 2efa0f2

File tree

5 files changed

+41
-7
lines changed

5 files changed

+41
-7
lines changed

addon/components/nrg-text-field.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import NrgValidationComponent from './nrg-validation-component';
22
import { action } from '@ember/object';
3+
import { isEmpty } from '@ember/utils';
34

45
const defaultType = 'text';
56
export default class NrgTextFieldComponent extends NrgValidationComponent {
@@ -13,6 +14,11 @@ export default class NrgTextFieldComponent extends NrgValidationComponent {
1314
}
1415

1516
if (this.type === 'number') {
17+
if (isEmpty(input)) {
18+
const min = this.args.min ?? 0;
19+
super.onChange(min);
20+
return false;
21+
}
1622
if (this.args.allowDecimals) {
1723
return !isNaN(parseFloat(input));
1824
}

addon/components/nrg-text-field/trim-input.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@ export default class TrimInputComponent extends Component {
1313

1414
get trimmedOuterValue() {
1515
const value = this.args.value;
16-
if (value && typeof value == 'string') {
16+
17+
if (value === undefined || value === null) {
18+
return '';
19+
}
20+
if (typeof value == 'string') {
1721
return value.trim() ?? '';
18-
} else if (value && typeof value == 'number') {
22+
} else if (typeof value == 'number') {
1923
return value.toString() ?? '';
20-
} else {
21-
return '';
2224
}
25+
return '';
2326
}
2427

2528
get trimmedInnerValue() {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ember-nrg-ui",
3-
"version": "4.1.9",
3+
"version": "4.1.10",
44
"description": "Opinionated UI addon based on how KUB scaffolds web applications",
55
"keywords": [
66
"ember-addon",

tests/dummy/app/components/freestyle/forms/text-field.hbs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
@readonly={{this.readonly}}
1111
@valuePath="inputValue"
1212
@model={{this}}
13+
@type={{this.type}}
1314
as |view|
1415
>
1516
{{view.input}}
@@ -31,6 +32,13 @@
3132
@value={{this.readonly}}
3233
@onInput={{fn (mut this.readonly)}}
3334
/>
35+
<Args.String
36+
@name="type"
37+
@description="Type"
38+
@value={{this.type}}
39+
@onInput={{fn (mut this.type)}}
40+
@options={{array "text" "number"}}
41+
/>
3442
</:api>
3543
</Freestyle::Usage>
3644

tests/integration/components/nrg-text-field-test.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ module('Integration | Component | nrg-text-field', function (hooks) {
2121

2222
await fillIn('input', 'bob');
2323

24-
assert.dom('input').hasValue('');
25-
assert.strictEqual(this.value, undefined);
24+
assert.dom('input').hasValue('0');
25+
assert.strictEqual(this.value, 0);
2626
});
2727

2828
test('number fields allow numeric characters', async function (assert) {
@@ -92,4 +92,21 @@ module('Integration | Component | nrg-text-field', function (hooks) {
9292
assert.dom('input').hasValue('-123');
9393
assert.strictEqual(this.value, '-123');
9494
});
95+
96+
test('number fields default to min', async function (assert) {
97+
this.min = -5;
98+
this.value = 7;
99+
100+
await render(
101+
hbs`<NrgTextField @model={{this}} @valuePath="value" @type="number" @min={{this.min}} />`
102+
);
103+
104+
assert.dom('input').hasValue('7');
105+
assert.strictEqual(this.value, 7);
106+
107+
await fillIn('input', '');
108+
109+
assert.dom('input').hasValue('-5');
110+
assert.strictEqual(this.value, -5);
111+
});
95112
});

0 commit comments

Comments
 (0)