Skip to content

Commit 4cb1225

Browse files
authored
Update store packages to match dedicated repo (#427)
* chore: update store dependency * fix: form meta should never be undefined now * chore: fix tests that only break on fast computers Guess I don't need to upgrade my computer anytime soon 😅😅
1 parent 09edc1f commit 4cb1225

File tree

9 files changed

+2125
-440
lines changed

9 files changed

+2125
-440
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@
107107
]
108108
},
109109
"dependencies": {
110-
"@tanstack/store": "0.0.1-beta.90",
110+
"@tanstack/store": "0.1.3",
111111
"fs-extra": "^11.1.1",
112112
"rollup-plugin-dts": "^5.3.0"
113113
},

packages/form-core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,6 @@
4040
"build:types": "tsc --emitDeclarationOnly"
4141
},
4242
"dependencies": {
43-
"@tanstack/store": "0.0.1-beta.89"
43+
"@tanstack/store": "0.1.3"
4444
}
4545
}

packages/form-core/src/FieldApi.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ export class FieldApi<TData, TFormData> {
129129
{
130130
value: this.getValue(),
131131
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
132-
meta: this.getMeta() ?? {
132+
meta: this._getMeta() ?? {
133133
isValidating: false,
134134
isTouched: false,
135135
...opts.defaultMeta,
@@ -211,8 +211,7 @@ export class FieldApi<TData, TFormData> {
211211
}
212212

213213
// Default Meta
214-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
215-
if (this.getMeta() === undefined) {
214+
if (this._getMeta() === undefined) {
216215
this.setMeta(this.state.meta)
217216
}
218217
}
@@ -229,7 +228,14 @@ export class FieldApi<TData, TFormData> {
229228
this.validate('change', this.state.value)
230229
}
231230

232-
getMeta = (): FieldMeta => this.form.getFieldMeta(this.name)
231+
_getMeta = () => this.form.getFieldMeta(this.name)
232+
getMeta = () =>
233+
this._getMeta() ??
234+
({
235+
isValidating: false,
236+
isTouched: false,
237+
...this.options.defaultMeta,
238+
} as FieldMeta)
233239

234240
setMeta = (updater: Updater<FieldMeta>) =>
235241
this.form.setFieldMeta(this.name, updater)

packages/form-core/src/FormApi.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,6 @@ export class FormApi<TFormData> {
171171
const shouldUpdateState =
172172
options.defaultState !== this.options.defaultState
173173

174-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
175-
if (!shouldUpdateValues || !shouldUpdateValues) {
176-
return
177-
}
178-
179174
this.store.setState(() =>
180175
getDefaultFormState(
181176
Object.assign(
@@ -295,7 +290,7 @@ export class FormApi<TFormData> {
295290

296291
getFieldMeta = <TField extends DeepKeys<TFormData>>(
297292
field: TField,
298-
): FieldMeta => {
293+
): FieldMeta | undefined => {
299294
return this.state.fieldMeta[field]
300295
}
301296

packages/form-core/src/tests/FieldApi.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,27 @@ describe('field api', () => {
176176
field.setValue('other', { touch: true })
177177
expect(field.getMeta().error).toBe('Please enter a different value')
178178
})
179+
180+
it('should not throw errors when no meta info is stored on a field and a form re-renders', async () => {
181+
const form = new FormApi({
182+
defaultValues: {
183+
name: 'test',
184+
},
185+
})
186+
187+
const field = new FieldApi({
188+
form,
189+
name: 'name',
190+
})
191+
192+
field.mount()
193+
194+
expect(() =>
195+
form.update({
196+
defaultValues: {
197+
name: 'other',
198+
},
199+
}),
200+
).not.toThrow()
201+
})
179202
})

packages/form-core/src/tests/FormApi.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ describe('form api', () => {
77
const form = new FormApi()
88

99
expect(form.state).toEqual({
10-
values: undefined,
10+
values: {},
1111
fieldMeta: {},
1212
canSubmit: true,
1313
isFieldsValid: true,
@@ -59,7 +59,7 @@ describe('form api', () => {
5959
})
6060

6161
expect(form.state).toEqual({
62-
values: undefined,
62+
values: {},
6363
fieldMeta: {},
6464
canSubmit: true,
6565
isFieldsValid: true,

packages/react-form/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@
5555
},
5656
"dependencies": {
5757
"@tanstack/form-core": "workspace:*",
58-
"@tanstack/react-store": "0.0.1-beta.85"
58+
"@tanstack/react-store": "0.1.3",
59+
"@tanstack/store": "0.1.3"
5960
},
6061
"peerDependencies": {
6162
"react": "^17.0.0 || ^18.0.0",

packages/react-form/src/tests/useField.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,8 @@ describe('useField', () => {
155155

156156
const { getByTestId, getByText, queryByText } = render(<Comp />)
157157
const input = getByTestId('fieldinput')
158-
await user.type(input, 'other')
159158
expect(queryByText(error)).not.toBeInTheDocument()
159+
await user.type(input, 'other')
160160
await waitFor(() => getByText(error))
161161
expect(getByText(error)).toBeInTheDocument()
162162
})
@@ -178,7 +178,7 @@ describe('useField', () => {
178178
<form.Field
179179
name="firstName"
180180
defaultMeta={{ isTouched: true }}
181-
onChangeAsyncDebounceMs={10}
181+
onChangeAsyncDebounceMs={100}
182182
onChangeAsync={async () => {
183183
mockFn()
184184
await sleep(10)

0 commit comments

Comments
 (0)