Skip to content

Commit

Permalink
Update store packages to match dedicated repo (#427)
Browse files Browse the repository at this point in the history
* 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 😅😅
  • Loading branch information
crutchcorn authored Sep 2, 2023
1 parent 09edc1f commit 4cb1225
Show file tree
Hide file tree
Showing 9 changed files with 2,125 additions and 440 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
]
},
"dependencies": {
"@tanstack/store": "0.0.1-beta.90",
"@tanstack/store": "0.1.3",
"fs-extra": "^11.1.1",
"rollup-plugin-dts": "^5.3.0"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/form-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@
"build:types": "tsc --emitDeclarationOnly"
},
"dependencies": {
"@tanstack/store": "0.0.1-beta.89"
"@tanstack/store": "0.1.3"
}
}
14 changes: 10 additions & 4 deletions packages/form-core/src/FieldApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export class FieldApi<TData, TFormData> {
{
value: this.getValue(),
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
meta: this.getMeta() ?? {
meta: this._getMeta() ?? {
isValidating: false,
isTouched: false,
...opts.defaultMeta,
Expand Down Expand Up @@ -211,8 +211,7 @@ export class FieldApi<TData, TFormData> {
}

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

getMeta = (): FieldMeta => this.form.getFieldMeta(this.name)
_getMeta = () => this.form.getFieldMeta(this.name)
getMeta = () =>
this._getMeta() ??
({
isValidating: false,
isTouched: false,
...this.options.defaultMeta,
} as FieldMeta)

setMeta = (updater: Updater<FieldMeta>) =>
this.form.setFieldMeta(this.name, updater)
Expand Down
7 changes: 1 addition & 6 deletions packages/form-core/src/FormApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,6 @@ export class FormApi<TFormData> {
const shouldUpdateState =
options.defaultState !== this.options.defaultState

// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (!shouldUpdateValues || !shouldUpdateValues) {
return
}

this.store.setState(() =>
getDefaultFormState(
Object.assign(
Expand Down Expand Up @@ -295,7 +290,7 @@ export class FormApi<TFormData> {

getFieldMeta = <TField extends DeepKeys<TFormData>>(
field: TField,
): FieldMeta => {
): FieldMeta | undefined => {
return this.state.fieldMeta[field]
}

Expand Down
23 changes: 23 additions & 0 deletions packages/form-core/src/tests/FieldApi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,27 @@ describe('field api', () => {
field.setValue('other', { touch: true })
expect(field.getMeta().error).toBe('Please enter a different value')
})

it('should not throw errors when no meta info is stored on a field and a form re-renders', async () => {
const form = new FormApi({
defaultValues: {
name: 'test',
},
})

const field = new FieldApi({
form,
name: 'name',
})

field.mount()

expect(() =>
form.update({
defaultValues: {
name: 'other',
},
}),
).not.toThrow()
})
})
4 changes: 2 additions & 2 deletions packages/form-core/src/tests/FormApi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('form api', () => {
const form = new FormApi()

expect(form.state).toEqual({
values: undefined,
values: {},
fieldMeta: {},
canSubmit: true,
isFieldsValid: true,
Expand Down Expand Up @@ -59,7 +59,7 @@ describe('form api', () => {
})

expect(form.state).toEqual({
values: undefined,
values: {},
fieldMeta: {},
canSubmit: true,
isFieldsValid: true,
Expand Down
3 changes: 2 additions & 1 deletion packages/react-form/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
},
"dependencies": {
"@tanstack/form-core": "workspace:*",
"@tanstack/react-store": "0.0.1-beta.85"
"@tanstack/react-store": "0.1.3",
"@tanstack/store": "0.1.3"
},
"peerDependencies": {
"react": "^17.0.0 || ^18.0.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/react-form/src/tests/useField.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ describe('useField', () => {

const { getByTestId, getByText, queryByText } = render(<Comp />)
const input = getByTestId('fieldinput')
await user.type(input, 'other')
expect(queryByText(error)).not.toBeInTheDocument()
await user.type(input, 'other')
await waitFor(() => getByText(error))
expect(getByText(error)).toBeInTheDocument()
})
Expand All @@ -178,7 +178,7 @@ describe('useField', () => {
<form.Field
name="firstName"
defaultMeta={{ isTouched: true }}
onChangeAsyncDebounceMs={10}
onChangeAsyncDebounceMs={100}
onChangeAsync={async () => {
mockFn()
await sleep(10)
Expand Down
Loading

0 comments on commit 4cb1225

Please sign in to comment.