Skip to content

Commit

Permalink
JNG-5786 tags on value change (#418)
Browse files Browse the repository at this point in the history
  • Loading branch information
noherczeg authored Jun 16, 2024
1 parent 3994812 commit 29993da
Showing 1 changed file with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{> fragment.header.hbs }}

import { type MouseEvent, type SyntheticEvent, useCallback, useMemo, useState } from 'react';
import { type MouseEvent, type SyntheticEvent, useCallback, useMemo, useState, useRef, useEffect } from 'react';
import clsx from 'clsx';
import InputAdornment from '@mui/material/InputAdornment';
import ButtonGroup from '@mui/material/ButtonGroup';
Expand Down Expand Up @@ -74,6 +74,7 @@ export function Tags<P, T> (props: TagsProps<P, T>) {
} = props;
const [options, setOptions] = useState<any[]>([]);
const [loading, setLoading] = useState<boolean>(false);
const prevValues = useRef<T[]>(ownerData[name] as T[]);

const handleSearch = async (searchText: string) => {
try {
Expand Down Expand Up @@ -122,6 +123,8 @@ export function Tags<P, T> (props: TagsProps<P, T>) {
);

const onChange = useCallback((event: SyntheticEvent, value: (string | any)[]) => {
// prevent useEffect below from triggering recursion
prevValues.current = value;
onValueChange(value as any);
}, [ownerData, onValueChange]);

Expand All @@ -135,6 +138,14 @@ export function Tags<P, T> (props: TagsProps<P, T>) {
}
}, [ownerData, onItemClick]);

useEffect(() => {
// prevent recursion
if (prevValues.current !== ownerData[name]) {
prevValues.current = ownerData[name] as T[];
onValueChange(ownerData[name] as any);
}
}, [ownerData[name]]);

return (
<Autocomplete
multiple
Expand Down

0 comments on commit 29993da

Please sign in to comment.