Skip to content

Commit

Permalink
Merge pull request #5652 from matuzalemsteles/LPS-191768
Browse files Browse the repository at this point in the history
fix(@clayui/autocomplete): fix error when not validating value with items
  • Loading branch information
matuzalemsteles authored Jul 26, 2023
2 parents ab29f8c + 024cc7b commit 1e34361
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 9 deletions.
6 changes: 3 additions & 3 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ module.exports = {
statements: 100,
},
'./packages/clay-autocomplete/src/': {
branches: 70,
branches: 69,
functions: 84,
lines: 88,
statements: 88,
lines: 85,
statements: 86,
},
'./packages/clay-badge/src/': {
branches: 0,
Expand Down
53 changes: 47 additions & 6 deletions packages/clay-autocomplete/src/Autocomplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
useControlledState,
useDebounce,
useId,
useIsFirstRender,
useNavigation,
useOverlayPosition,
} from '@clayui/shared';
Expand Down Expand Up @@ -64,6 +65,11 @@ export type IProps<T> = {
*/
alignmentByViewport?: boolean;

/**
* Flag to allow an input value not corresponding to an item to be set.
*/
allowsCustomValue?: boolean;

/**
* The initial value of the active state (uncontrolled).
*/
Expand Down Expand Up @@ -170,8 +176,9 @@ function AutocompleteInner<T extends Record<string, any> | string | number>(
{
UNSAFE_loadingShrink,
active: externalActive,
as: As = Input,
alignmentByViewport: _,
allowsCustomValue,
as: As = Input,
children,
containerElementRef,
defaultActive,
Expand Down Expand Up @@ -240,11 +247,7 @@ function AutocompleteInner<T extends Record<string, any> | string | number>(

const announcerAPI = useRef<AnnouncerAPI>(null);

useEffect(() => {
if (active === false) {
setValue(currentItemSelected.current);
}
}, [active]);
const isFirst = useIsFirstRender();

const filterFn = useCallback(
(itemValue: string) =>
Expand All @@ -254,6 +257,44 @@ function AutocompleteInner<T extends Record<string, any> | string | number>(
[value]
);

useEffect(() => {
// Validates that the initial value exists in the items.
if (
!allowsCustomValue &&
!currentItemSelected.current &&
value &&
items
) {
const hasItem = items.find((item) => {
if (typeof item === 'string') {
return item === value;
} else if (typeof item === 'object') {
// filter key is not defined and we cannot infer the data type.
if (!filterKey) {
return false;
}

return item[filterKey] === value;
}
});

if (hasItem) {
currentItemSelected.current = value;
}
}
}, []);

useEffect(() => {
// Does not update state on first render.
if (isFirst || allowsCustomValue) {
return;
}

if (active === false && currentItemSelected.current !== value) {
setValue(currentItemSelected.current);
}
}, [active]);

const filteredItems = useMemo(() => {
if (debouncedLoadingChange) {
return [];
Expand Down
1 change: 1 addition & 0 deletions packages/clay-shared/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export {useNavigation, isTypeahead, getFocusableList} from './useNavigation';
export {useOverlayPosition} from './useOverlayPositon';
export {useHover} from './useHover';
export {useIsMobileDevice} from './useIsMobileDevice';
export {useIsFirstRender} from './useIsFirstRender';
export {isMac, isIPhone, isIPad, isIOS, isAppleDevice} from './platform';
export type {AlignPoints} from './useOverlayPositon';
export type {IBaseProps as IPortalBaseProps} from './Portal';
Expand Down
18 changes: 18 additions & 0 deletions packages/clay-shared/src/useIsFirstRender.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* SPDX-FileCopyrightText: © 2023 Liferay, Inc. <https://liferay.com>
* SPDX-License-Identifier: BSD-3-Clause
*/

import {useRef} from 'react';

export function useIsFirstRender() {
const isFirst = useRef(true);

if (isFirst.current) {
isFirst.current = false;

return true;
}

return isFirst.current;
}

0 comments on commit 1e34361

Please sign in to comment.