|
|
@@ -1,41 +1,32 @@
|
|
|
-import { AnyJson, AnyStore } from '~/types/interfaces'
|
|
|
-import {computed, ComputedRef, useContext} from "@nuxtjs/composition-api";
|
|
|
+import { AnyStore } from '~/types/interfaces'
|
|
|
+import {computed, ComputedRef} from "@nuxtjs/composition-api";
|
|
|
|
|
|
/**
|
|
|
* @category composables/form
|
|
|
- * @class UseError
|
|
|
- * Use Classe pour gérer l'apparition de message si le formulaire est dirty
|
|
|
+ * Composable pour gérer l'apparition de message si le formulaire est dirty
|
|
|
+ * @param field
|
|
|
+ * @param emit
|
|
|
+ * @param store
|
|
|
*/
|
|
|
-export class UseError {
|
|
|
- private store: AnyStore
|
|
|
-
|
|
|
- constructor () {
|
|
|
- const {store} = useContext()
|
|
|
- this.store = store
|
|
|
- }
|
|
|
+export function useError(field: string, emit: any, store: AnyStore){
|
|
|
+ const error:ComputedRef<Boolean> = computed(()=>{
|
|
|
+ return store.state.form.violations.indexOf(field) >= 0
|
|
|
+ })
|
|
|
|
|
|
/**
|
|
|
- * Composition function
|
|
|
+ * Lorsque la valeur d'un champ change, on supprime le fait qu'il puisse être "faux" dans le store, et on émet sa nouvelle valeur au parent
|
|
|
+ * @param emit
|
|
|
+ * @param fieldValue
|
|
|
+ * @param changeField
|
|
|
*/
|
|
|
- public invoke (field: string, emit: any): AnyJson {
|
|
|
- const error:ComputedRef<Boolean> = computed(()=>{
|
|
|
- return this.store.state.form.violations.indexOf(field) >= 0
|
|
|
- })
|
|
|
-
|
|
|
- return {
|
|
|
- onChange: (fieldValue:any) => this.onChange(emit, fieldValue, field),
|
|
|
- error
|
|
|
- }
|
|
|
+ function onChange (emit:any, fieldValue:any, changeField:string) {
|
|
|
+ const errors = store.state.form.violations.filter((field:string) => field !== changeField)
|
|
|
+ store.commit('form/setViolations', errors)
|
|
|
+ emit('update', fieldValue, changeField)
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- *
|
|
|
- */
|
|
|
- private onChange (emit:any, fieldValue:any, changeField:string) {
|
|
|
- const errors = this.store.state.form.violations.filter((field:string) => field !== changeField)
|
|
|
- this.store.commit('form/setViolations', errors)
|
|
|
- emit('update', fieldValue, changeField)
|
|
|
+ return {
|
|
|
+ onChange: (fieldValue:any) => onChange(emit, fieldValue, field),
|
|
|
+ error
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
-export const $useError = (field: string, emit: any) => new UseError().invoke(field, emit)
|