useError.ts 1018 B

1234567891011121314151617181920212223242526272829303132
  1. import { AnyStore } from '~/types/interfaces'
  2. import {computed, ComputedRef} from "@nuxtjs/composition-api";
  3. import * as _ from 'lodash'
  4. /**
  5. * @category composables/form
  6. * Composable pour gérer l'apparition de message si le formulaire est dirty
  7. * @param field
  8. * @param emit
  9. * @param store
  10. */
  11. export function useError(field: string, emit: any, store: AnyStore){
  12. const violation:ComputedRef<string> = computed(()=>{
  13. return _.get(store.state.form.violations, field, '')
  14. })
  15. /**
  16. * 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
  17. * @param emit
  18. * @param fieldValue
  19. * @param changeField
  20. */
  21. function onChange (emit:any, fieldValue:any, changeField:string) {
  22. store.commit('form/setViolations', _.omit(store.state.form.violations, changeField))
  23. emit('update', fieldValue, changeField)
  24. }
  25. return {
  26. onChange: (fieldValue:any) => onChange(emit, fieldValue, field),
  27. violation
  28. }
  29. }