Skip to content

Commit

Permalink
fix: validator triggering too many times #32, field does not become d…
Browse files Browse the repository at this point in the history
…irty after touching it #33
  • Loading branch information
victorgarciaesgi committed Dec 12, 2024
1 parent 1caf6a5 commit 662192b
Show file tree
Hide file tree
Showing 15 changed files with 658 additions and 211 deletions.
1 change: 0 additions & 1 deletion docs/.vitepress/theme/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import TwoslashFloatingVue from '@shikijs/vitepress-twoslash/client';
import type { EnhanceAppContext } from 'vitepress';
import 'virtual:group-icons.css';
import { createPinia } from 'pinia';
import './tailwind.postcss';

export default {
extends: DefaultTheme,
Expand Down
11 changes: 0 additions & 11 deletions docs/src/demo/FieldError.vue

This file was deleted.

144 changes: 0 additions & 144 deletions docs/src/demo/SimpleDemo.vue

This file was deleted.

7 changes: 0 additions & 7 deletions docs/src/examples/simple.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
title: Simple demo
---

<script setup>
import SimpleDemo from '../demo/SimpleDemo.vue';
</script>

# Simple demo

You can play with the code of this example in the stackblitz sandbox.
Expand All @@ -20,8 +16,5 @@ Don't forgot to install the `Vue` extension in the online IDE.
</a>


## Rendered demo

<SimpleDemo/>


Original file line number Diff line number Diff line change
Expand Up @@ -443,13 +443,13 @@ export function createReactiveCollectionStatus({
});

if (filterNullishValues) {
dirtyFields = dirtyFields.filter((value) => {
if (isObject(value)) {
return !isEmpty(value);
} else {
return !!value;
}
});
if (
dirtyFields.every((value) => {
return isEmpty(value);
})
) {
dirtyFields = [];
}
}
return dirtyFields;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,10 +453,14 @@ export function createReactiveFieldStatus({
$reset();
}

function $extractDirtyFields(filterNullishValues: boolean = true): any | null {
function $extractDirtyFields(filterNullishValues: boolean = true): unknown | null | { _null: true } {
if ($dirty.value) {
return state.value;
}
if (filterNullishValues) {
// Differenciate untouched empty values from dirty empty ones
return { _null: true };
}
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,20 +371,24 @@ export function createReactiveNestedStatus({
createReactiveFieldsStatus();
}

function filterNullishFields(fields: [string, unknown][]) {
return fields.filter(([key, value]) => {
if (isObject(value)) {
return !(value && typeof value === 'object' && '_null' in value) && !isEmpty(value);
} else if (Array.isArray(value)) {
return value.length;
} else {
return true;
}
});
}

function $extractDirtyFields(filterNullishValues: boolean = true): Record<string, any> {
let dirtyFields = Object.entries($fields.value).map(([key, field]) => {
let dirtyFields: [string, unknown][] = Object.entries($fields.value).map(([key, field]) => {
return [key, field.$extractDirtyFields(filterNullishValues)];
});
if (filterNullishValues) {
dirtyFields = dirtyFields.filter(([key, value]) => {
if (isObject(value)) {
return !isEmpty(value);
} else if (Array.isArray(value)) {
return value.length;
} else {
return !!value;
}
});
dirtyFields = filterNullishFields(dirtyFields);
}
return Object.fromEntries(dirtyFields);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,7 @@ export function createReactiveRuleStatus({
} satisfies ScopeState;
})!;

$unwatchState = watch(scopeState.$params, $validate, {
deep: true,
});
$unwatchState = watch(scopeState.$params, $validate);
}

$watch();
Expand Down
4 changes: 2 additions & 2 deletions playground/vue3/src/App.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<template>
<!-- <TestForm /> -->
<Test14 />
<Test15 />
</template>

<script setup lang="ts">
import Test14 from './components/Test14.vue';
import Test15 from './components/Test15.vue';
// const form = ref({ name: [{ foo: '' }] });
Expand Down
Loading

0 comments on commit 662192b

Please sign in to comment.