UNPKG

@gravel-form/blueprintjs

Version:

A flexible middlewares driven json schema form with Blueprintjs

1 lines 2.59 kB
{"version":3,"sources":["middlewares/SubmitButtonMw.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAG/B,OAAO,EAAE,4BAA4B,EAAyB,MAAM,UAAU,CAAC;AAE/E,eAAO,MAAM,cAAc,EAAE,KAAK,CAAC,aAAa,CAAC,4BAA4B,CAwB5E,CAAC;AAEF,eAAO,MAAM,4BAA4B,EAAE,KAAK,CAAC,aAAa,CAAC,4BAA4B,CAyC1F,CAAC;AAEF,eAAe,cAAc,CAAC","file":"SubmitButtonMw.d.ts","sourcesContent":["import * as React from 'react';\nimport get from 'lodash/get';\nimport { FormGroup, Button, Callout, Intent } from '@blueprintjs/core';\nimport { BlueprintFormMiddlewareProps, ErrorObject, validate } from '../share';\n\nexport const SubmitButtonMw: React.ComponentType<BlueprintFormMiddlewareProps> = (props) => {\n const { parent, next, data } = props;\n if (parent) return next(props);\n const { extraProps } = props;\n\n const onSubmit = props.onSubmit || props.formProps.onSubmit;\n return (\n <>\n {props.next(props)}\n <FormGroup>\n <Button\n intent={Intent.PRIMARY}\n onClick={(e: React.MouseEvent<HTMLElement>) => {\n e.preventDefault();\n onSubmit && onSubmit(data);\n }}\n type=\"primary\"\n {...get(extraProps, 'props')}\n >\n Submit\n </Button>\n </FormGroup>\n </>\n );\n};\n\nexport const SubmitButtonWithValidationMw: React.ComponentType<BlueprintFormMiddlewareProps> = (props) => {\n const {\n data,\n parent,\n next,\n formProps: { schema },\n } = props;\n const [errors, setErrors] = React.useState<ErrorObject[] | null | undefined>();\n const [ajvException, setAjvException] = React.useState<Error | null>(null);\n\n if (parent) return next(props);\n const { extraProps } = props;\n\n const handleClick = (e: React.MouseEvent<HTMLElement>) => {\n e.preventDefault();\n try {\n const onSubmit = props.onSubmit || props.formProps.onSubmit;\n const errors = validate(schema, data);\n if (!errors && onSubmit) onSubmit(data);\n setErrors(errors);\n setAjvException(null);\n } catch (err) {\n setAjvException(err);\n }\n };\n\n return (\n <>\n {props.next(errors === props.errors ? props : { ...props, errors })}\n <FormGroup>\n <Button intent={Intent.PRIMARY} onClick={handleClick} type=\"primary\" {...get(extraProps, 'props')}>\n Submit\n </Button>\n </FormGroup>\n {ajvException ? (\n <Callout intent={Intent.DANGER} title=\"Ajv exception\">\n {ajvException.message}\n </Callout>\n ) : null}\n </>\n );\n};\n\nexport default SubmitButtonMw;\n"]}