Skip to content

Commit

Permalink
update validator on morpher
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Jones authored and Ryan Jones committed Sep 3, 2019
1 parent 458f4fb commit dead30c
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions ensure-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ import * as fromValidate from './Data/validators.js';
import get from 'lodash/get';
import produce from 'immer';
import set from 'lodash/set';
import React, { useContext, useEffect, useMemo, useReducer } from 'react';
import React, { useContext, useEffect, useMemo, useReducer, useRef } from 'react';
import parseDate from 'date-fns/parse';
import parseISO from 'date-fns/parseISO';
import formatDate from 'date-fns/format';
import isValidDate from 'date-fns/isValid';
import { useInputValidator } from 'utils/useInputValidator.js';
let identity = { in: i => i, out: i => i };
Expand Down Expand Up @@ -66,6 +65,7 @@ export let useCaptureItem = ({
path = null, format = identity, validate = null
} = {}) => {
let captureItem = useContext(CaptureItemContext);
let touched = useRef(false);
if (process.env.NODE_ENV === 'development') {
if (validate && !(validate in fromValidate)) {
Expand All @@ -83,9 +83,14 @@ export let useCaptureItem = ({
if (!item) return {};
let value = format.in(get(item, path));
let isValid = useInputValidator(value, validate);
let isValid =
touched.current && validate ? fromValidate[validate](value) : true;
let onChange = value => {
touched.current = true;
dispatch(setField(path, format.out(value)));
}
return {
onChange: value => dispatch(setField(path, format.out(value))),
onChange,
onSubmit,
value,
isValid,
Expand Down

0 comments on commit dead30c

Please sign in to comment.