nope/resources/ui/forms/dynamicForm.tsx

50 lines
1.4 KiB
TypeScript
Raw Normal View History

2020-10-26 07:39:34 +00:00
import React from 'react';
2020-09-02 05:49:53 +00:00
import Form from "@rjsf/bootstrap-4";
2020-11-09 06:42:24 +00:00
import { IJsonSchema } from '../../../lib/types/IJSONSchema'
2020-10-26 07:39:34 +00:00
import { Button } from 'react-bootstrap';
export interface DynamicFormProps<T> {
schema: IJsonSchema,
data?: any
uiSchema?: any,
onCancel: (err: any) => Promise<void>;
onSubmit: (data: T) => Promise<void>;
}
export interface DynamicFormState<T> {
}
2020-10-30 18:30:59 +00:00
/**
* Uses:
* https://react-jsonschema-form.readthedocs.io/en/latest/
*/
2020-10-26 07:39:34 +00:00
class DynamicForm<T = any> extends React.Component<DynamicFormProps<T>, DynamicFormState<T>> {
constructor(props) {
super(props);
}
2020-09-02 05:49:53 +00:00
2020-10-26 07:39:34 +00:00
_submitButtonPressed(data: any){
if (typeof this.props.onSubmit == "function"){
this.props.onSubmit(data as T);
2020-09-02 05:49:53 +00:00
}
2020-10-26 07:39:34 +00:00
}
_cancelButtonPressed(){
if (typeof this.props.onCancel == "function"){
this.props.onCancel(new Error("Input Canceled"));
}
}
public render() {
return (
2020-10-30 18:30:59 +00:00
<Form schema={this.props.schema as any} uiSchema={this.props.uiSchema} formData={this.props.data} liveValidate onSubmit={e => this._submitButtonPressed(e.formData)}>
2020-10-26 07:39:34 +00:00
<Button type="submit" variant="success">Submit</Button>{' '}
2020-10-26 08:54:30 +00:00
<Button variant="danger" onClick={_ =>this._cancelButtonPressed()}>Cancel</Button>{' '}
2020-10-26 07:39:34 +00:00
</Form>
);
}
}
export default DynamicForm;