Skip to content

Commit 0ad1d99

Browse files
committed
fix(file-upload): add syncRead as bindable parameter to fix stale onFileChange callback data
Signed-off-by: Dustin Pham <dustinpham95@gmail.com>
1 parent 059abcc commit 0ad1d99

3 files changed

Lines changed: 12 additions & 0 deletions

File tree

packages/common/core/src/file-upload/file-upload.machine.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ export const fileUploadMachine: MachineConfig<FileUploadSchema> = createMachine(
156156
rejectedFiles: ctx.get("rejectedFiles"),
157157
})
158158
},
159+
syncRead: true,
159160
value: prop("acceptedFiles"),
160161
})),
161162
rejectedFiles: bindable<FileRejection[]>(() => ({
@@ -171,6 +172,7 @@ export const fileUploadMachine: MachineConfig<FileUploadSchema> = createMachine(
171172
rejectedFiles: value,
172173
})
173174
},
175+
syncRead: true,
174176
})),
175177
transforming: bindable<boolean>(() => ({
176178
defaultValue: false,

packages/common/utils/src/machine/machine.types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ export interface BindableParams<T, ChangeEvent> {
101101
isEqual?: (a: T, b: T | undefined) => boolean
102102
onChange?: (value: T, details: ChangeEvent, prevValue: T | undefined) => void
103103
sync?: boolean
104+
/**
105+
* When true, immediately updates the ref value before triggering a re-render.
106+
* This ensures `ctx.get()` returns the updated value synchronously, which is
107+
* useful when multiple bindables need to read each other's values in onChange
108+
* callbacks.
109+
*/
110+
syncRead?: boolean
104111
value?: T | undefined
105112
}
106113

packages/frameworks/react-core/src/machine/use-bindable.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ export function useBindable<T, ChangeEvent extends EventDetails | void | null>(
3939
const next = isFunction(value) ? value(prev as T) : value
4040

4141
if (!controlled) {
42+
if (props().syncRead) {
43+
valueRef.current = next
44+
}
4245
setValue(next)
4346
}
4447
if (!eq(next, prev)) {

0 commit comments

Comments
 (0)