UNPKG

@gravel-form/blueprintjs

Version:

A flexible middlewares driven json schema form with Blueprintjs

1 lines 1.17 kB
{"version":3,"sources":["middlewares/LiveValidateMw.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B,OAAO,EAAE,4BAA4B,EAAY,MAAM,UAAU,CAAC;AAElE,eAAO,MAAM,cAAc,EAAE,KAAK,CAAC,EAAE,CAAC,4BAA4B,CAoBjE,CAAC;AAEF,eAAe,cAAc,CAAC","file":"LiveValidateMw.d.ts","sourcesContent":["import React from 'react';\nimport { Callout, Intent } from '@blueprintjs/core';\nimport { BlueprintFormMiddlewareProps, validate } from '../share';\n\nexport const LiveValidateMw: React.FC<BlueprintFormMiddlewareProps> = (props) => {\n const { schema, data, next, parent } = props;\n const [errors, ajvException] = React.useMemo(() => {\n try {\n return [parent || typeof schema === 'boolean' ? null : validate(schema, data), null];\n } catch (err) {\n return [null, err];\n }\n }, [schema, data]);\n const newProps = React.useMemo(() => ({ ...props, errors }), [errors, props]);\n return ajvException ? (\n <>\n <Callout intent={Intent.DANGER} title=\"Ajv exception\">\n {ajvException.message}\n </Callout>\n {next(props)}\n </>\n ) : (\n next(errors ? newProps : props)\n );\n};\n\nexport default LiveValidateMw;\n"]}