UNPKG

@gravel-form/antd

Version:

A flexible middlewares driven json schema form with Ant Design

1 lines 2.23 kB
{"version":3,"sources":["middlewares/SubmitButtonMw.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAG/B,OAAO,EAAE,eAAe,EAAyB,MAAM,UAAU,CAAC;AAElE,eAAO,MAAM,cAAc,EAAE,KAAK,CAAC,aAAa,CAAC,eAAe,CAgB/D,CAAC;AAEF,eAAO,MAAM,4BAA4B,EAAE,KAAK,CAAC,aAAa,CAAC,eAAe,CAoC7E,CAAC;AAEF,eAAe,cAAc,CAAC","file":"SubmitButtonMw.d.ts","sourcesContent":["import * as React from 'react';\nimport get from 'lodash/get';\nimport { Form, Button, Alert } from 'antd';\nimport { MiddlewareProps, ErrorObject, validate } from '../share';\n\nexport const SubmitButtonMw: React.ComponentType<MiddlewareProps> = (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 <Form.Item>\n <Button onClick={() => onSubmit && onSubmit(data)} type=\"primary\" {...get(extraProps, 'props')}>\n Submit\n </Button>\n </Form.Item>\n </>\n );\n};\n\nexport const SubmitButtonWithValidationMw: React.ComponentType<MiddlewareProps> = (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 = () => {\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 <Form.Item>\n <Button onClick={handleClick} type=\"primary\" {...get(extraProps, 'props')}>\n Submit\n </Button>\n </Form.Item>\n {ajvException ? <Alert message=\"Ajv exception\" description={ajvException.message} type=\"error\" showIcon /> : null}\n </>\n );\n};\n\nexport default SubmitButtonMw;\n"]}