|
1 |
| -import { Entity, IEngine, InputAction, PointerEventsSystem, PointerEventType } from '@dcl/ecs' |
| 1 | +import { |
| 2 | + Entity, |
| 3 | + IEngine, |
| 4 | + InputAction, |
| 5 | + PointerEventsSystem, |
| 6 | + PointerEventType, |
| 7 | + PBUiInputResult, |
| 8 | + PBUiDropdownResult |
| 9 | +} from '@dcl/ecs' |
2 | 10 | import * as components from '@dcl/ecs/dist/components'
|
3 | 11 | import Reconciler, { HostConfig } from 'react-reconciler'
|
4 | 12 | import { Callback, isListener, Listeners } from '../components'
|
@@ -183,6 +191,7 @@ export function createReconciler(
|
183 | 191 |
|
184 | 192 | function removeChildEntity(instance: Instance) {
|
185 | 193 | changeEvents.delete(instance.entity)
|
| 194 | + clickEvents.delete(instance.entity) |
186 | 195 | engine.removeEntity(instance.entity)
|
187 | 196 | for (const child of instance._child) {
|
188 | 197 | removeChildEntity(child)
|
@@ -233,13 +242,24 @@ export function createReconciler(
|
233 | 242 | }
|
234 | 243 |
|
235 | 244 | function updateOnChange(entity: Entity, componentId: number, state?: OnChangeState) {
|
| 245 | + const hasEvent = changeEvents.has(entity) |
236 | 246 | const event = changeEvents.get(entity) || changeEvents.set(entity, new Map()).get(entity)!
|
237 |
| - const oldState = event.get(componentId) |
238 | 247 | const onChangeCallback = state?.onChangeCallback
|
239 | 248 | const onSubmitCallback = state?.onSubmitCallback
|
240 |
| - const value = state?.value ?? oldState?.value |
241 |
| - const isSubmit = state?.isSubmit ?? oldState?.isSubmit |
242 |
| - event.set(componentId, { onChangeCallback, onSubmitCallback, value, isSubmit }) |
| 249 | + event.set(componentId, { onChangeCallback, onSubmitCallback }) |
| 250 | + // Create onChange callback if its the first callback event for this entity |
| 251 | + if (!hasEvent) { |
| 252 | + const resultComponentId = |
| 253 | + componentId === UiDropdown.componentId ? UiDropdownResult.componentId : UiInputResult.componentId |
| 254 | + engine.getComponent<PBUiInputResult | PBUiDropdownResult>(resultComponentId).onChange(entity, (value) => { |
| 255 | + if ((value as PBUiInputResult)?.isSubmit) { |
| 256 | + const onSubmit = changeEvents.get(entity)?.get(componentId)?.onSubmitCallback |
| 257 | + onSubmit && onSubmit(value?.value) |
| 258 | + } |
| 259 | + const onChange = changeEvents.get(entity)?.get(componentId)?.onChangeCallback |
| 260 | + onChange && onChange(value?.value) |
| 261 | + }) |
| 262 | + } |
243 | 263 | }
|
244 | 264 |
|
245 | 265 | const hostConfig: HostConfig<
|
@@ -352,34 +372,8 @@ export function createReconciler(
|
352 | 372 | null
|
353 | 373 | )
|
354 | 374 |
|
355 |
| - // Maybe this could be something similar to Input system, but since we |
356 |
| - // are going to use this only here, i prefer to scope it here. |
357 |
| - function handleOnChange(componentId: number, resultComponent: typeof UiDropdownResult | typeof UiInputResult) { |
358 |
| - for (const [entity, Result] of engine.getEntitiesWith(resultComponent)) { |
359 |
| - const entityState = changeEvents.get(entity)?.get(componentId) |
360 |
| - const isSubmit = !!(Result as any).isSubmit |
361 |
| - if (entityState?.onChangeCallback && Result.value !== entityState.value) { |
362 |
| - entityState.onChangeCallback(Result.value) |
363 |
| - } |
364 |
| - if (entityState?.onSubmitCallback && isSubmit && !entityState.isSubmit) { |
365 |
| - entityState.onSubmitCallback(Result.value) |
366 |
| - } |
367 |
| - |
368 |
| - updateOnChange(entity, componentId, { |
369 |
| - onChangeCallback: entityState?.onChangeCallback, |
370 |
| - onSubmitCallback: entityState?.onSubmitCallback, |
371 |
| - value: Result.value, |
372 |
| - isSubmit |
373 |
| - }) |
374 |
| - } |
375 |
| - } |
376 |
| - |
377 | 375 | return {
|
378 | 376 | update: function (component: ReactEcs.JSX.ReactNode) {
|
379 |
| - if (changeEvents.size) { |
380 |
| - handleOnChange(UiInput.componentId, UiInputResult) |
381 |
| - handleOnChange(UiDropdown.componentId, UiDropdownResult) |
382 |
| - } |
383 | 377 | return reconciler.updateContainer(component as any, root, null)
|
384 | 378 | },
|
385 | 379 | getEntities: () => Array.from(entities)
|
|
0 commit comments