|
| 1 | +import React from 'react' |
| 2 | +import WithParams from './WithParams' |
| 3 | +import Form, {AutoFormFormProps} from './Form' |
| 4 | +import WithMutation from './WithMutation' |
| 5 | +import getFragment from './getFragment' |
| 6 | +import {getValidationErrors, clean, Blackbox} from '@orion-js/schema' |
| 7 | +import debounce from 'lodash/debounce' |
| 8 | +import {ApolloClient} from '@apollo/client' |
| 9 | +import Fields from './Fields' |
| 10 | + |
| 11 | +export interface AutoFormChildrenProps { |
| 12 | + params: Blackbox |
| 13 | + omit: string[] | string |
| 14 | + only: string[] | string |
| 15 | +} |
| 16 | + |
| 17 | +export interface AutoFormProps { |
| 18 | + mutation: string |
| 19 | + doc?: Blackbox |
| 20 | + onChange?: (newDoc: any) => any |
| 21 | + children?: React.ReactNode | ((props: AutoFormChildrenProps) => React.ReactNode) |
| 22 | + fragment?: any |
| 23 | + getErrorFieldLabel?: AutoFormFormProps['getErrorFieldLabel'] |
| 24 | + onSuccess?: AutoFormFormProps['onSuccess'] |
| 25 | + onValidationError?: AutoFormFormProps['onValidationError'] |
| 26 | + onError?: AutoFormFormProps['onError'] |
| 27 | + clean?: AutoFormFormProps['clean'] |
| 28 | + validate?: AutoFormFormProps['validate'] |
| 29 | + schema?: AutoFormFormProps['schema'] |
| 30 | + omit?: string[] | string |
| 31 | + only?: string[] | string |
| 32 | + getErrorText?: AutoFormFormProps['getErrorText'] |
| 33 | + getDefaultLabel?: AutoFormFormProps['getDefaultLabel'] |
| 34 | + refetchQueries?: string[] |
| 35 | + buttonRef?: AutoFormFormProps['buttonRef'] |
| 36 | + autoSave?: boolean |
| 37 | + autoSaveDebounceTime?: number |
| 38 | + useFormTag?: AutoFormFormProps['useFormTag'] |
| 39 | + className?: AutoFormFormProps['className'] |
| 40 | + client?: any |
| 41 | +} |
| 42 | + |
| 43 | +export interface CreateAutoFormOptions { |
| 44 | + getField: (fieldType: string) => any |
| 45 | + onError: (error: any) => any |
| 46 | + getErrorText: AutoFormFormProps['getErrorText'] |
| 47 | + loading: JSX.Element |
| 48 | + getDefaultLabel: () => string |
| 49 | + getClient?: () => ApolloClient<any> |
| 50 | +} |
| 51 | + |
| 52 | +export const options: CreateAutoFormOptions = { |
| 53 | + getField: () => { |
| 54 | + throw new Error('You must pass a getField function') |
| 55 | + }, |
| 56 | + onError: error => alert(error.message), |
| 57 | + getErrorText: (code, field) => code, |
| 58 | + loading: null, |
| 59 | + getDefaultLabel: () => 'This field' |
| 60 | +} |
| 61 | + |
| 62 | +export class AutoForm extends React.Component<AutoFormProps> { |
| 63 | + static defaultProps: Partial<AutoFormProps> = { |
| 64 | + children: props => <Fields getField={options.getField} {...props} />, |
| 65 | + clean: clean, |
| 66 | + validate: getValidationErrors, |
| 67 | + omit: [], |
| 68 | + only: [], |
| 69 | + onSuccess: () => {}, |
| 70 | + onValidationError: () => {}, |
| 71 | + onError: error => options.onError(error), |
| 72 | + getErrorText: (code, field) => options.getErrorText(code, field), |
| 73 | + getDefaultLabel: () => options.getDefaultLabel(), |
| 74 | + autoSaveDebounceTime: 500 |
| 75 | + } |
| 76 | + |
| 77 | + form: Form = null |
| 78 | + debouncedSubmit: Function = null |
| 79 | + |
| 80 | + constructor(props) { |
| 81 | + super(props) |
| 82 | + this.debouncedSubmit = debounce(this.submit, props.autoSaveDebounceTime) |
| 83 | + } |
| 84 | + |
| 85 | + submit = () => { |
| 86 | + return this.form.submit() |
| 87 | + } |
| 88 | + |
| 89 | + onChange = newDoc => { |
| 90 | + if (this.props.onChange) { |
| 91 | + this.props.onChange(newDoc) |
| 92 | + } |
| 93 | + if (this.props.autoSave) { |
| 94 | + this.debouncedSubmit() |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + setDoc = newDoc => { |
| 99 | + this.onChange(newDoc) |
| 100 | + } |
| 101 | + |
| 102 | + renderChildren({params}) { |
| 103 | + if (typeof this.props.children === 'function') { |
| 104 | + return this.props.children({ |
| 105 | + params, |
| 106 | + omit: this.props.omit, |
| 107 | + only: this.props.only |
| 108 | + }) |
| 109 | + } else { |
| 110 | + return this.props.children |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + getFragment({name, result, basicResultQuery, params}) { |
| 115 | + if (this.props.fragment) { |
| 116 | + return this.props.fragment |
| 117 | + } else { |
| 118 | + return getFragment({name, result, basicResultQuery, params}) |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + render() { |
| 123 | + const passedClient = options.getClient ? options.getClient() : undefined |
| 124 | + const client = this.props.client ? this.props.client : passedClient |
| 125 | + |
| 126 | + if (!client) { |
| 127 | + throw new Error('You must pass a client or pass a getClient function') |
| 128 | + } |
| 129 | + |
| 130 | + return ( |
| 131 | + <WithParams name={this.props.mutation} loading={options.loading} client={client}> |
| 132 | + {({name, result, basicResultQuery, params}) => ( |
| 133 | + <WithMutation |
| 134 | + client={client} |
| 135 | + refetchQueries={this.props.refetchQueries} |
| 136 | + params={params} |
| 137 | + fragment={this.getFragment({name, result, basicResultQuery, params})} |
| 138 | + mutation={this.props.mutation}> |
| 139 | + {(mutate: AutoFormFormProps['mutate']) => ( |
| 140 | + <Form |
| 141 | + setRef={form => (this.form = form)} |
| 142 | + buttonRef={this.props.buttonRef} |
| 143 | + doc={this.props.doc} |
| 144 | + mutate={mutate} |
| 145 | + useFormTag={this.props.useFormTag} |
| 146 | + className={this.props.className} |
| 147 | + onChange={this.onChange} |
| 148 | + params={params} |
| 149 | + getDefaultLabel={this.props.getDefaultLabel} |
| 150 | + schema={this.props.schema || params} |
| 151 | + onSuccess={this.props.onSuccess} |
| 152 | + onError={this.props.onError} |
| 153 | + getErrorFieldLabel={this.props.getErrorFieldLabel} |
| 154 | + onValidationError={this.props.onValidationError} |
| 155 | + clean={this.props.clean} |
| 156 | + getErrorText={this.props.getErrorText} |
| 157 | + validate={this.props.validate}> |
| 158 | + {this.renderChildren({params: this.props.schema || params})} |
| 159 | + </Form> |
| 160 | + )} |
| 161 | + </WithMutation> |
| 162 | + )} |
| 163 | + </WithParams> |
| 164 | + ) |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +export function setupAutoForm(passedOptions: CreateAutoFormOptions) { |
| 169 | + for (const key in passedOptions) { |
| 170 | + options[key] = passedOptions[key] |
| 171 | + } |
| 172 | +} |
0 commit comments