import React from 'react' ; import { Schema , ValidationError } from 'yup' ; import * as dot from './dot-prop' ; export const isRadio = ( ref ) => { return ref . current ?. querySelector ( 'input[type="radio"]' ) ; } ; export function extractRadioButtons ( ref ) { return Array . from ( ref . current ?. querySelectorAll ( 'input[type="radio"]' ) || [ ] ) ; } export const isCheckbox = ( type ) => type === 'checkbox' ; export function debounce < TThis , TFn extends Function > ( this , fn , wait , immediate ? ) { let timeout ; return < TArgs > ( ... args ) => { const context = this ; const later = ( ) => { timeout = null ; if ( ! immediate ) fn . apply ( context , args ) ; } ; const callNow = immediate && ! timeout ; clearTimeout ( timeout ) ; timeout = setTimeout ( later , wait ) ; if ( callNow ) { fn . apply ( context , args ) ; } } ; } export function makeDotNotation ( str ) { return str . split ( '[' ) . join ( '.' ) . split ( ']' ) . join ( '' ) ; } export function getNextState ( next , state ) { const nextState = typeof next === 'function' ? next ( state ) : next ; return nextState ; } export function validate < TValues extends { } > ( values , validationSchema ) { return validationSchema ?. validate ( values , { abortEarly : false } ) . then ( ( ) => { return { } ; } ) . catch ( ( e ) => { throw e . inner . reduce ( ( acc , key ) => { const path = makeDotNotation ( key . path ) ; return dot . set ( acc , path , key . message ) ; } , { } ) ; } ) ; }