useFieldViolation.ts 993 B

123456789101112131415161718192021222324252627282930313233
  1. import { AnyStore } from '~/types/interfaces'
  2. import {ComputedRef} from "@vue/reactivity";
  3. import {useFormStore} from "~/store/form";
  4. /**
  5. * Composable pour gérer l'apparition de message d'erreurs de validation d'un champ de formulaire
  6. *
  7. * @param field
  8. * @param emit
  9. */
  10. export function useFieldViolation(field: string, emit: any){
  11. const violation: ComputedRef<string> = computed(()=>{
  12. return useGet(useFormStore().violations, field, '')
  13. })
  14. /**
  15. * 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
  16. * @param emit
  17. * @param fieldValue
  18. * @param changeField
  19. */
  20. function onChange (emit:any, fieldValue:any, changeField:string) {
  21. //@ts-ignore
  22. useFormStore().setViolations(useOmit(useFormStore().violations, changeField))
  23. emit('update', fieldValue, changeField)
  24. }
  25. return {
  26. onChange: (fieldValue:any) => onChange(emit, fieldValue, field),
  27. violation
  28. }
  29. }