-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new data and validation (#423)
- Loading branch information
Showing
29 changed files
with
322 additions
and
73 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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,21 @@ | ||
import { type Ref, ref } from 'vue' | ||
|
||
export interface D<T extends Record<string, any>> { | ||
data: Ref<T> | ||
init(): void | ||
} | ||
|
||
export function useD<T extends Record<string, any>>(data: T): D<T> { | ||
const initialData = JSON.parse(JSON.stringify(data)) | ||
|
||
const refData = ref(data) as Ref<T> | ||
|
||
function init(): void { | ||
refData.value = initialData | ||
} | ||
|
||
return { | ||
data: refData, | ||
init | ||
} | ||
} |
This file contains 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 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 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 Validation, type ValidationArgs, useVuelidate } from '@vuelidate/core' | ||
import { type MaybeRefOrGetter, type Ref, computed, toValue } from 'vue' | ||
import { type Snackbar, useSnackbars } from '../stores/Snackbars' | ||
import { useTrans } from './Lang' | ||
|
||
export interface V< | ||
Data extends { [key in keyof Rules]: any }, | ||
Rules extends ValidationArgs = ValidationArgs | ||
> { | ||
validation: Ref<Validation<Rules, Data>> | ||
validate(): Promise<boolean> | ||
validateAndNotify(message?: Snackbar): Promise<boolean> | ||
reset(): void | ||
} | ||
|
||
export interface Validatable { | ||
readonly $dirty: boolean | ||
readonly $invalid: boolean | ||
readonly $errors: ValidatableError[] | ||
readonly $touch: () => void | ||
} | ||
|
||
export interface ValidatableError { | ||
readonly $message: string | Ref<string> | ||
} | ||
|
||
export function useV< | ||
Data extends { [key in keyof Rules]: any }, | ||
Rules extends ValidationArgs = ValidationArgs | ||
>( | ||
data: MaybeRefOrGetter<Data>, | ||
rules: MaybeRefOrGetter<Rules> | ||
): V<Data, Rules> { | ||
const { t } = useTrans({ | ||
en: { notify: 'Form contains errors. Please correct them and try again.' }, | ||
ja: { notify: 'フォームにエラーがあります。内容を確認し、再度お試しください。' } | ||
}) | ||
|
||
const snackbars = useSnackbars() | ||
|
||
const d = computed(() => toValue(data)) | ||
const r = computed(() => toValue(rules)) | ||
|
||
const validation = useVuelidate(r, d) | ||
|
||
function reset(): void { | ||
validation.value.$reset() | ||
} | ||
|
||
function validate(): Promise<boolean> { | ||
return validation.value.$validate() | ||
} | ||
|
||
async function validateAndNotify(message?: Snackbar): Promise<boolean> { | ||
const valid = await validate() | ||
|
||
if (!valid) { | ||
snackbars.push(message ?? { | ||
mode: 'danger', | ||
text: t.notify | ||
}) | ||
} | ||
|
||
return valid | ||
} | ||
|
||
return { | ||
validation, | ||
validate, | ||
validateAndNotify, | ||
reset | ||
} | ||
} |
Oops, something went wrong.