-
-
Notifications
You must be signed in to change notification settings - Fork 27
Add getDirtyInput, getDirtyPaths and pickDirty method #98
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d914e02
Add getDirtyInput, getDirtyPaths and pickDirty method
fabian-hiller 8e52487
Fix fomatting issues of dirty fields guide and pickDirty method
fabian-hiller df2e4bd
Refactor type definitions to use FormSchema in getDirtyInput, getDirt…
fabian-hiller d6ff21e
Add tests for pickDirty to handle null values and absent keys
fabian-hiller c6793a2
Further improve implementation of new dirty methods
fabian-hiller 1fa0f88
Simplify DirtyPath tests
fabian-hiller ab2e37c
Update changelog to include pull request references for dirty methods
fabian-hiller b7b5dc6
Add documentation for dirty state methods in form methods guides
fabian-hiller 199cf9b
Address reveiw feedback of PR #98
fabian-hiller ed354ad
Add test for emitting leaf path when both object and descendant are d…
fabian-hiller File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
packages/core/src/field/getDirtyFieldInput/getDirtyFieldInput.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| import * as v from 'valibot'; | ||
| import { describe, expect, test } from 'vitest'; | ||
| import { createTestStore } from '../../vitest/index.ts'; | ||
| import { getDirtyFieldInput } from './getDirtyFieldInput.ts'; | ||
|
|
||
| describe('getDirtyFieldInput', () => { | ||
| test('should return undefined when no field is dirty', () => { | ||
| const store = createTestStore( | ||
| v.object({ name: v.string(), age: v.number() }), | ||
| { initialInput: { name: 'John', age: 25 } } | ||
| ); | ||
| expect(getDirtyFieldInput(store)).toBeUndefined(); | ||
| }); | ||
|
|
||
| test('should omit clean siblings of a dirty value', () => { | ||
| const store = createTestStore( | ||
| v.object({ name: v.string(), email: v.string() }), | ||
| { initialInput: { name: 'John', email: 'a@example.com' } } | ||
| ); | ||
| store.children.email.input.value = 'b@example.com'; | ||
| store.children.email.isDirty.value = true; | ||
| expect(getDirtyFieldInput(store)).toStrictEqual({ | ||
| email: 'b@example.com', | ||
| }); | ||
| }); | ||
|
|
||
| test('should return the full current array when any item is dirty', () => { | ||
| const store = createTestStore(v.object({ items: v.array(v.string()) }), { | ||
| initialInput: { items: ['a', 'b', 'c'] }, | ||
| }); | ||
| const itemsStore = store.children.items; | ||
| expect(itemsStore.kind).toBe('array'); | ||
| if (itemsStore.kind === 'array') { | ||
| itemsStore.children[1].input.value = 'B'; | ||
| itemsStore.children[1].isDirty.value = true; | ||
| } | ||
| expect(getDirtyFieldInput(store)).toStrictEqual({ | ||
| items: ['a', 'B', 'c'], | ||
| }); | ||
| }); | ||
|
|
||
| test('should include dirty leaves under a clean object parent', () => { | ||
| const store = createTestStore( | ||
| v.object({ user: v.object({ email: v.string(), name: v.string() }) }), | ||
| { initialInput: { user: { email: 'a@example.com', name: 'John' } } } | ||
| ); | ||
| const userStore = store.children.user; | ||
| expect(userStore.kind).toBe('object'); | ||
| if (userStore.kind === 'object') { | ||
| userStore.children.email.input.value = 'b@example.com'; | ||
| userStore.children.email.isDirty.value = true; | ||
| } | ||
| expect(getDirtyFieldInput(store)).toStrictEqual({ | ||
| user: { email: 'b@example.com' }, | ||
| }); | ||
| }); | ||
|
|
||
| test('should return undefined when called directly on a clean value field', () => { | ||
| const store = createTestStore(v.object({ name: v.string() }), { | ||
| initialInput: { name: 'John' }, | ||
| }); | ||
| expect(getDirtyFieldInput(store.children.name)).toBeUndefined(); | ||
| }); | ||
|
|
||
| test('should return the value when called directly on a dirty value field', () => { | ||
| const store = createTestStore(v.object({ name: v.string() }), { | ||
| initialInput: { name: 'John' }, | ||
| }); | ||
| store.children.name.input.value = 'Jane'; | ||
| store.children.name.isDirty.value = true; | ||
| expect(getDirtyFieldInput(store.children.name)).toBe('Jane'); | ||
| }); | ||
| }); |
73 changes: 73 additions & 0 deletions
73
packages/core/src/field/getDirtyFieldInput/getDirtyFieldInput.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| import type { InternalFieldStore } from '../../types/index.ts'; | ||
| import { getFieldBool } from '../getFieldBool/getFieldBool.ts'; | ||
|
|
||
| /** | ||
| * Returns only the dirty input of the field store. Arrays are treated as | ||
| * atomic and returned in full if any item is dirty, while object keys without | ||
| * a dirty descendant are omitted. Returns `undefined` if no descendant is | ||
| * dirty. | ||
| * | ||
| * @param internalFieldStore The field store to get dirty input from. | ||
| * @param dirtyOnly Whether to only include dirty fields. Defaults to `true`. | ||
| * | ||
| * @returns The dirty input, or `undefined` if no descendant is dirty. | ||
| */ | ||
| // @__NO_SIDE_EFFECTS__ | ||
| export function getDirtyFieldInput( | ||
| internalFieldStore: InternalFieldStore, | ||
| dirtyOnly: boolean = true | ||
| ): unknown { | ||
| // If field has no dirty descendant, return undefined | ||
| if (dirtyOnly && !getFieldBool(internalFieldStore, 'isDirty')) { | ||
| return undefined; | ||
| } | ||
|
|
||
| // If field store is array, collect input from children | ||
| if (internalFieldStore.kind === 'array') { | ||
| // If array input is not nullish, build full array from children | ||
| if (internalFieldStore.input.value) { | ||
| // Create output array | ||
| const value = []; | ||
|
|
||
| // Collect input from each array item | ||
| for ( | ||
| let index = 0; | ||
| index < internalFieldStore.items.value.length; | ||
| index++ | ||
| ) { | ||
| value[index] = getDirtyFieldInput( | ||
| internalFieldStore.children[index], | ||
| false | ||
| ); | ||
| } | ||
| return value; | ||
| } | ||
|
|
||
| // Otherwise, return nullish input as-is | ||
| return internalFieldStore.input.value; | ||
| } | ||
|
|
||
| // If field store is object, recurse only into dirty children | ||
| if (internalFieldStore.kind === 'object') { | ||
| // If object input is not nullish, build object from children | ||
| if (internalFieldStore.input.value) { | ||
| // Create output object | ||
| const value: Record<string, unknown> = {}; | ||
|
|
||
| // Collect input from each dirty object property | ||
| for (const key in internalFieldStore.children) { | ||
| const child = internalFieldStore.children[key]; | ||
| if (!dirtyOnly || getFieldBool(child, 'isDirty')) { | ||
| value[key] = getDirtyFieldInput(child, dirtyOnly); | ||
| } | ||
| } | ||
| return value; | ||
| } | ||
|
|
||
| // Otherwise, return nullish input as-is | ||
| return internalFieldStore.input.value; | ||
| } | ||
|
|
||
| // Return primitive value input | ||
| return internalFieldStore.input.value; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * from './getDirtyFieldInput.ts'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| export type { | ||
| DirtyPath, | ||
| Path, | ||
| PathValue, | ||
| PathKey, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.