Skip to content
This repository was archived by the owner on Jul 9, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion Composer/packages/adaptive-form/src/utils/arrayUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

import { generateUniqueId } from '@bfc/shared';
import { ChangeHandler } from '@bfc/extension-client';
import { useState } from 'react';
import { useEffect, useState } from 'react';
import isEqual from 'lodash/isEqual';

type ArrayChangeHandler<ItemType> = (items: ArrayItem<ItemType>[]) => void;

Expand Down Expand Up @@ -78,6 +79,19 @@ export function useArrayItems<ItemType = unknown>(
): ArrayItemState<ItemType> {
const [cache, setCache] = useState(generateArrayItems(items));

useEffect(() => {
const newCache = generateArrayItems(items);

if (
!isEqual(
cache.map(({ value }) => value),
newCache.map(({ value }) => value)
)
) {
setCache(newCache);
}
}, [items]);

const handleChange = (newItems: ArrayItem<ItemType>[]) => {
setCache(newItems);
onChange(newItems.map(({ value }) => value));
Expand Down
16 changes: 15 additions & 1 deletion Composer/packages/adaptive-form/src/utils/objectUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

import { generateUniqueId } from '@bfc/shared';
import { ChangeHandler } from '@bfc/extension-client';
import { useState } from 'react';
import { useEffect, useState } from 'react';
import isEqual from 'lodash/isEqual';

type ItemType<ValueType = unknown> = { [key: string]: ValueType };
type ObjectChangeHandler<ValueType = unknown> = (items: ObjectItem<ValueType>[]) => void;
Expand Down Expand Up @@ -64,6 +65,19 @@ export function useObjectItems<ValueType = unknown>(
): ObjectItemState<ValueType> {
const [cache, setCache] = useState(generateObjectEntries(items));

useEffect(() => {
const newCache = generateObjectEntries(items);

if (
!isEqual(
newCache.map(({ id, ...rest }) => rest),
cache.map(({ id, ...rest }) => rest)
)
) {
setCache(generateObjectEntries(items));
}
}, [items]);

const handleChange = (items) => {
setCache(items);
onChange(items.reduce((acc, { propertyName, propertyValue }) => ({ ...acc, [propertyName]: propertyValue }), {}));
Expand Down