nope/resources/ui/forms/dynamicForm.tsx

48 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-10-26 07:39:34 +00:00
import { deepClone } from '../../../lib/helpers/objectMethods';
import { IJsonSchema } from '../../types/IJSONSchema'
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> {
}
class DynamicForm<T = any> extends React.Component<DynamicFormProps<T>, DynamicFormState<T>> {
constructor(props) {
super(props);
console.log(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 (
<Form schema={this.props.schema as any} formData={this.props.data} liveValidate onSubmit={e => this._submitButtonPressed(e.formData)}>
<Button type="submit" variant="success">Submit</Button>{' '}
<Button variant="warning" onClick={_ =>this._cancelButtonPressed()}>Cancel</Button>{' '}
</Form>
);
}
}
export default DynamicForm;