Skip to content

Commit

Permalink
Fix #4724: Autocomplete prevent error on trim() (#4741)
Browse files Browse the repository at this point in the history
  • Loading branch information
melloware authored Aug 4, 2023
1 parent 9f53c4c commit e695965
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
11 changes: 5 additions & 6 deletions components/lib/autocomplete/AutoComplete.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import * as React from 'react';
import PrimeReact, { localeOption } from '../api/Api';
import { PrimeReactContext } from '../api/Api';
import PrimeReact, { PrimeReactContext, localeOption } from '../api/Api';
import { Button } from '../button/Button';
import { useHandleStyle } from '../componentbase/ComponentBase';
import { useMountEffect, useOverlayListener, useUnmountEffect, useUpdateEffect } from '../hooks/Hooks';
import { ChevronDownIcon } from '../icons/chevrondown';
import { SpinnerIcon } from '../icons/spinner';
import { TimesCircleIcon } from '../icons/timescircle';
import { InputText } from '../inputtext/InputText';
import { OverlayService } from '../overlayservice/OverlayService';
import { Tooltip } from '../tooltip/Tooltip';
import { DomHandler, IconUtils, ObjectUtils, UniqueComponentId, ZIndexUtils, classNames, mergeProps } from '../utils/Utils';
import { DomHandler, IconUtils, ObjectUtils, UniqueComponentId, ZIndexUtils, mergeProps } from '../utils/Utils';
import { AutoCompleteBase } from './AutoCompleteBase';
import { AutoCompletePanel } from './AutoCompletePanel';
import { useHandleStyle } from '../componentbase/ComponentBase';

export const AutoComplete = React.memo(
React.forwardRef((inProps, ref) => {
Expand Down Expand Up @@ -398,11 +397,11 @@ export const AutoComplete = React.memo(
return;
}

const inputValue = event.target.value.trim();
const inputValue = ObjectUtils.trim(event.target.value);
const item = (props.suggestions || []).find((it) => {
const value = props.field ? ObjectUtils.resolveFieldData(it, props.field) : it;

return value && inputValue === value.trim();
return value && inputValue === ObjectUtils.trim(value);
});

if (item) {
Expand Down
5 changes: 5 additions & 0 deletions components/lib/utils/ObjectUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,11 @@ export default class ObjectUtils {
return this.isNotEmpty(str) && this.isString(str) ? str[0].toUpperCase() + str.slice(1) : str;
}

static trim(value) {
// trim only if the value is actually a string
return this.isNotEmpty(value) && this.isString(value) ? value.trim() : value;
}

static isEmpty(value) {
return value === null || value === undefined || value === '' || (Array.isArray(value) && value.length === 0) || (!(value instanceof Date) && typeof value === 'object' && Object.keys(value).length === 0);
}
Expand Down
1 change: 1 addition & 0 deletions components/lib/utils/utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ export declare class ObjectUtils {
static removeAccents(str: any): string;
static toFlatCase(str: string): string;
static toCapitalCase(str: string): string;
static trim(value: any): any;
static isEmpty(value: any): boolean;
static isNotEmpty(value: any): boolean;
static isFunction(value: any): boolean;
Expand Down

0 comments on commit e695965

Please sign in to comment.