Skip to content

Commit

Permalink
fix(input-field): render current value correctly
Browse files Browse the repository at this point in the history
Avoids rendering the current value as empty when there's bad input.
  • Loading branch information
vicgeralds committed May 14, 2024
1 parent 6e23563 commit a93f3f3
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions src/components/input-field/input-field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ export class InputField {
'mdc-text-field--disabled': this.disabled || this.readonly,
'lime-text-field--readonly': this.readonly,
'mdc-text-field--required': this.required,
'lime-text-field--empty': !this.value,
'lime-text-field--empty': this.isEmpty(),
'lime-has-prefix': this.hasPrefix(),
'lime-has-suffix': this.hasSuffix(),
};
Expand All @@ -422,6 +422,28 @@ export class InputField {
return classList;
};

private isEmpty = () => {
if (this.type === 'number') {
const element = this.getInputElement();
if (element && element.validity.badInput) {
return false;
}
}

return !this.getCurrentValue();
};

private getCurrentValue = () => {
if (this.changeWaiting) {
const element = this.getInputElement();
if (element) {
return element.value;
}
}

return this.value;
};

private renderInput = (
properties: JSXBase.InputHTMLAttributes<HTMLInputElement>,
) => {
Expand Down Expand Up @@ -510,7 +532,7 @@ export class InputField {
};

private renderHelperLine = () => {
const text: string = this.value || '';
const text: string = this.getCurrentValue() || '';
const length = text.length;

if (!this.hasHelperLine()) {
Expand All @@ -529,7 +551,7 @@ export class InputField {
};

private renderEmptyValueForReadonly = () => {
if (this.readonly && !this.value) {
if (this.readonly && this.isEmpty()) {
return (
<span class="lime-empty-value-for-readonly lime-looks-like-input-value">
Expand Down Expand Up @@ -619,7 +641,7 @@ export class InputField {
const labelClassList = {
'mdc-floating-label': true,
'mdc-floating-label--float-above':
!!this.value || this.isFocused || this.readonly,
!this.isEmpty() || this.isFocused || this.readonly,
};

if (!this.label) {
Expand Down Expand Up @@ -697,7 +719,7 @@ export class InputField {
<a
{...linkProps}
class="material-icons mdc-text-field__icon lime-trailing-icon-for-link"
tabindex={this.disabled || !this.value ? '-1' : '0'}
tabindex={this.disabled || this.isEmpty() ? '-1' : '0'}
role="button"
>
<limel-icon name={icon} />
Expand Down Expand Up @@ -766,6 +788,9 @@ export class InputField {
renderValue = new Intl.NumberFormat(this.locale).format(
Number(this.value),
);
if (renderValue === 'NaN') {
return;
}
}

return (
Expand Down Expand Up @@ -880,7 +905,7 @@ export class InputField {

private renderListResult = () => {
const filteredCompletions: ListItem[] = this.filterCompletions(
this.value,
this.getCurrentValue(),
);
if (!filteredCompletions || filteredCompletions.length === 0) {
return null;
Expand Down

0 comments on commit a93f3f3

Please sign in to comment.