fix(form-core): 🐛 File input field - Picking a different file does not trigger rerender (deep equality issue with File/Blob objects) #1939
ClosedjoaoGabriel55(GitHub profile, opens in a new tab)wants to merge 11 commits into
main fromrefs/pull/1939/headopened - @@ -4097,2 +4097,21 @@40974097 })409840984099+it('it should set a file value with uuid when setting a field value with File', () => {4100+ const form = new FormApi({4101+ defaultValues: {4102+ avatar: undefined,4103+ } as { avatar: unknown },4104+ })4105+4106+ form.mount()4107+4108+ const firstFile = new File(['first'], 'first.png', { type: 'image/png' })4109+ form.setFieldValue('avatar', firstFile)4110+4111+ const firstValue = form.state.values.avatar as { file: File; uuid: string }4112+4113+ expect(firstValue).toBeDefined()4114+ expect(firstValue.file instanceof File).toBe(true)4115+ expect(firstValue.file.name).toBe('first.png')4116+ expect(typeof firstValue.uuid).toBe('string')4117+ expect(firstValue.uuid.length > 0).toBe(true)@@ -4098,0 +4106,21 @@4106+ form.mount()4107+4108+ const firstFile = new File(['first'], 'first.png', { type: 'image/png' })4109+ form.setFieldValue('avatar', firstFile)4110+4111+ const firstValue = form.state.values.avatar as { file: File; uuid: string }4112+4113+ expect(firstValue).toBeDefined()4114+ expect(firstValue.file instanceof File).toBe(true)4115+ expect(firstValue.file.name).toBe('first.png')4116+ expect(typeof firstValue.uuid).toBe('string')4117+ expect(firstValue.uuid.length > 0).toBe(true)4118+4119+ const secondFile = new File(['second'], 'second.png', { type: 'image/png' })4120+ form.setFieldValue('avatar', secondFile)4121+4122+ const secondValue = form.state.values.avatar as { file: File; uuid: string }4123+4124+ expect(secondValue.file.name).toBe('second.png')4125+ expect(typeof secondValue.uuid).toBe('string')4126+ expect(secondValue.uuid.length > 0).toBe(true)@@ -4096,3 +4096,4 @@40964096 expect(form.formId.length).toBeGreaterThan(1)40974097 })409840984099+it('it should set a file value with uuid when setting a field value with File', () => {
LeCarbonator(GitHub profile, opens in a new tab) requested changes
@@ -48,3 +48,4 @@4848 * @private4949 */5050 export function setBy(obj: any, _path: any, updater: Updater<any>) {51+ const isFile = updater instanceof FileLeCarbonator(GitHub profile, opens in a new tab) requested changes
@@ -52,4 +66,5 @@52665367 function doSet(parent?: any): any {5468 if (!path.length) {55- return functionalUpdate(updater, parent)69+ return functionalUpdate(70+ isFile(updater) ? { file: updater, uuid: uuid() } : updater,@@ -422,3 +439,4 @@422439 }423440424441 export function evaluate<T>(objA: T, objB: T) {442+ if (objA instanceof File && objB instanceof File) {@@ -4096,3 +4096,16 @@40964096 expect(form.formId.length).toBeGreaterThan(1)40974097 })409840984099+it('should set a file value with uuid when setting a field value with File', () => {4100+ const form = new FormApi({4101+ defaultValues: {4102+ avatar: undefined,4103+ } as { avatar: unknown },4104+ })4105+4106+ form.mount()4107+4108+ const firstFile = new File(['first'], 'first.png', { type: 'image/png' })4109+ form.setFieldValue('avatar', firstFile)4110+4111+ const firstValue = form.state.values.avatar as { file: File; uuid: string }- @@ -0,0 +1,2 @@1+---2+'@tanstack/react-form': patch
crutchcorn(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
⚠️ No Changeset found#
Latest commit: df74f020af00ec035b0337b5590cd68f89f32606
Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.
This PR includes no changesets
When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types
Click here to learn what changesets are, and how to add one.
Click here if you're a maintainer who wants to add a changeset to this PR