diff --git a/packages/@dcl/react-ecs/src/reconciler/index.ts b/packages/@dcl/react-ecs/src/reconciler/index.ts index e4df60274..8ff31d5cf 100644 --- a/packages/@dcl/react-ecs/src/reconciler/index.ts +++ b/packages/@dcl/react-ecs/src/reconciler/index.ts @@ -126,8 +126,13 @@ export function createReconciler( const onChangeExists = 'onChange' in props const onSubmitExists = 'onSubmit' in props - const onChange = onChangeExists ? (props['onChange'] as OnChangeState['onChangeCallback']) : undefined - const onSubmit = onSubmitExists ? (props['onSubmit'] as OnChangeState['onSubmitCallback']) : undefined + const entityState = changeEvents.get(instance.entity)?.get(componentId) + const onChange = onChangeExists + ? (props['onChange'] as OnChangeState['onChangeCallback']) + : entityState?.onChangeCallback + const onSubmit = onSubmitExists + ? (props['onSubmit'] as OnChangeState['onSubmitCallback']) + : entityState?.onSubmitCallback if (onChangeExists || onSubmitExists) { updateOnChange(instance.entity, componentId, { @@ -329,26 +334,20 @@ export function createReconciler( for (const [entity, Result] of engine.getEntitiesWith(resultComponent)) { const entityState = changeEvents.get(entity)?.get(componentId) const isSubmit = !!(Result as any).isSubmit - let update: boolean = false if (entityState?.onChangeCallback && Result.value !== entityState.value) { - update = true entityState.onChangeCallback(Result.value) } - if (entityState?.onSubmitCallback && isSubmit && !entityState.isSubmit) { - update = true entityState.onSubmitCallback(Result.value) } - if (update) { - updateOnChange(entity, componentId, { - onChangeCallback: entityState?.onChangeCallback, - onSubmitCallback: entityState?.onSubmitCallback, - value: Result.value, - isSubmit - }) - } + updateOnChange(entity, componentId, { + onChangeCallback: entityState?.onChangeCallback, + onSubmitCallback: entityState?.onSubmitCallback, + value: Result.value, + isSubmit + }) } } diff --git a/test/react-ecs/input.spec.tsx b/test/react-ecs/input.spec.tsx index 078aca1a1..68ad80e91 100644 --- a/test/react-ecs/input.spec.tsx +++ b/test/react-ecs/input.spec.tsx @@ -10,7 +10,7 @@ describe('Ui Listeners React Ecs', () => { const UiInputResult = components.UiInputResult(engine) const uiEntity = ((engine.addEntity() as number) + 1) as Entity const onChange: jest.Mock | undefined = jest.fn() - const onSubmit: jest.Mock | undefined = jest.fn() + const onSubmit: jest.Mock | undefined = jest.fn() const undefinedChange: jest.Mock | undefined = undefined let conditional = true let removeComponent = false @@ -64,19 +64,14 @@ describe('Ui Listeners React Ecs', () => { await engine.update(1) expect(onChange).toBeCalledWith('Casla - ') onChange.mockClear() + expect(onSubmit).toBeCalledTimes(0) + UiInputResult.getMutable(uiEntity).isSubmit = true + await engine.update(1) + expect(onSubmit).toBeCalledTimes(1) removeComponent = true await engine.update(1) UiInputResult.create(uiEntity, { value: 'BOEDO' }) await engine.update(1) expect(onChange).toBeCalledTimes(0) - - onChange.mockClear() - removeComponent = false - await engine.update(1) - expect(onSubmit).toBeCalledTimes(0) - UiInputResult.getMutable(uiEntity).value = '666' - UiInputResult.getMutable(uiEntity).isSubmit = true - await engine.update(1) - expect(onSubmit).toBeCalledTimes(1) }) })