@topthink/components 1.0.56 → 1.0.58
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/es/editor-widget-ebec8493.js +356 -0
- package/es/editor-widget-ebec8493.js.map +1 -0
- package/es/form-74ef0417.js +2 -0
- package/es/form-74ef0417.js.map +1 -0
- package/es/form-8925a750.js +231 -0
- package/es/form-8925a750.js.map +1 -0
- package/es/form-9c0e638a.js +230 -1
- package/es/form-9c0e638a.js.map +1 -1
- package/es/index-2b97b70d.js +2 -0
- package/es/index-2b97b70d.js.map +1 -0
- package/es/index-303e1845.js +560 -0
- package/es/index-303e1845.js.map +1 -0
- package/es/index-4f51cece.js +1304 -19
- package/es/index-4f51cece.js.map +1 -1
- package/es/index-563b11c6.js +67 -0
- package/es/index-563b11c6.js.map +1 -0
- package/es/index-6ae8237d.js +67 -0
- package/es/index-6ae8237d.js.map +1 -0
- package/es/index-7a7c06a9.js +40 -0
- package/es/index-7a7c06a9.js.map +1 -0
- package/es/index-9869b1c6.js +1350 -0
- package/es/index-9869b1c6.js.map +1 -0
- package/es/index-a214613e.js +40 -0
- package/es/index-a214613e.js.map +1 -0
- package/es/index-d964165a.js +529 -9
- package/es/index-d964165a.js.map +1 -1
- package/es/index.js +1 -1
- package/package.json +7 -5
- package/types/components/lazy/form/widgets/editor-widget.d.ts +5 -0
- package/types/index.d.ts +1 -1
- package/types/utils/is-record.d.ts +1 -0
- package/types/errors/unauthorized.d.ts +0 -4
- /package/types/components/lazy/{form.d.ts → form/index.d.ts} +0 -0
package/es/form-9c0e638a.js
CHANGED
|
@@ -1,2 +1,231 @@
|
|
|
1
|
-
import{jsx
|
|
1
|
+
import { jsx } from 'react/jsx-runtime';
|
|
2
|
+
import JsonForm, { getRegistry } from '@topthink/json-form';
|
|
3
|
+
import { forwardRef, useState, useCallback } from 'react';
|
|
4
|
+
import axios from 'axios';
|
|
5
|
+
import { u as useSafeState, r as request, B as Button } from './index-4f51cece.js';
|
|
6
|
+
import { mapValues } from 'lodash';
|
|
7
|
+
import 'sweetalert2/dist/sweetalert2.js';
|
|
8
|
+
import 'sweetalert2-react-content';
|
|
9
|
+
import 'react-bootstrap';
|
|
10
|
+
import 'query-string';
|
|
11
|
+
import 'retry-axios';
|
|
12
|
+
import '@babel/runtime/helpers/defineProperty';
|
|
13
|
+
import 'rc-notification';
|
|
14
|
+
import 'styled-components';
|
|
15
|
+
import 'classnames';
|
|
16
|
+
import 'react-async-hook';
|
|
17
|
+
|
|
18
|
+
const localize = require('ajv-i18n/localize/zh');
|
|
19
|
+
const toExtraErrors = error => {
|
|
20
|
+
const errors = error.errors;
|
|
21
|
+
if (typeof errors === 'string') {
|
|
22
|
+
return {
|
|
23
|
+
__errors: [errors]
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
return mapValues(errors, e => {
|
|
27
|
+
return {
|
|
28
|
+
__errors: [e]
|
|
29
|
+
};
|
|
30
|
+
});
|
|
31
|
+
};
|
|
32
|
+
const widgets = {
|
|
33
|
+
upload: function (_ref) {
|
|
34
|
+
let {
|
|
35
|
+
options,
|
|
36
|
+
...props
|
|
37
|
+
} = _ref;
|
|
38
|
+
const {
|
|
39
|
+
widgets
|
|
40
|
+
} = getRegistry();
|
|
41
|
+
if (options.endpoint) {
|
|
42
|
+
options.onUpload = async file => {
|
|
43
|
+
const data = new FormData();
|
|
44
|
+
data.set('file', file);
|
|
45
|
+
const {
|
|
46
|
+
url,
|
|
47
|
+
value
|
|
48
|
+
} = await request({
|
|
49
|
+
url: options.endpoint,
|
|
50
|
+
method: 'post',
|
|
51
|
+
data
|
|
52
|
+
});
|
|
53
|
+
return {
|
|
54
|
+
url,
|
|
55
|
+
value
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
return jsx(widgets.upload, {
|
|
60
|
+
...props,
|
|
61
|
+
options: options
|
|
62
|
+
});
|
|
63
|
+
},
|
|
64
|
+
editor: function (_ref2) {
|
|
65
|
+
let {
|
|
66
|
+
options,
|
|
67
|
+
...props
|
|
68
|
+
} = _ref2;
|
|
69
|
+
const {
|
|
70
|
+
widgets
|
|
71
|
+
} = getRegistry();
|
|
72
|
+
if (options.endpoint) {
|
|
73
|
+
options.onUpload = async file => {
|
|
74
|
+
const data = new FormData();
|
|
75
|
+
data.set('file', file);
|
|
76
|
+
const {
|
|
77
|
+
url
|
|
78
|
+
} = await request({
|
|
79
|
+
url: options.endpoint,
|
|
80
|
+
method: 'post',
|
|
81
|
+
data
|
|
82
|
+
});
|
|
83
|
+
return url;
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
return jsx(widgets.editor, {
|
|
87
|
+
...props,
|
|
88
|
+
options: options
|
|
89
|
+
});
|
|
90
|
+
},
|
|
91
|
+
typeahead: function (_ref3) {
|
|
92
|
+
let {
|
|
93
|
+
options,
|
|
94
|
+
...props
|
|
95
|
+
} = _ref3;
|
|
96
|
+
const {
|
|
97
|
+
widgets
|
|
98
|
+
} = getRegistry();
|
|
99
|
+
if (options.endpoint) {
|
|
100
|
+
options.onSearch = async _ref4 => {
|
|
101
|
+
let {
|
|
102
|
+
value,
|
|
103
|
+
query
|
|
104
|
+
} = _ref4;
|
|
105
|
+
return await request({
|
|
106
|
+
url: options.endpoint,
|
|
107
|
+
params: {
|
|
108
|
+
value,
|
|
109
|
+
query
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
return jsx(widgets.typeahead, {
|
|
115
|
+
...props,
|
|
116
|
+
options: options
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
const Form = forwardRef((_ref5, ref) => {
|
|
121
|
+
let {
|
|
122
|
+
action,
|
|
123
|
+
method = 'post',
|
|
124
|
+
onSuccess,
|
|
125
|
+
formData,
|
|
126
|
+
onSubmitting,
|
|
127
|
+
onSubmit,
|
|
128
|
+
onChange,
|
|
129
|
+
submitText = '提交',
|
|
130
|
+
transformData,
|
|
131
|
+
children,
|
|
132
|
+
...props
|
|
133
|
+
} = _ref5;
|
|
134
|
+
const [extraErrors, setExtraErrors] = useState();
|
|
135
|
+
const [loading, setLoading] = useSafeState(false);
|
|
136
|
+
const [data, setData] = useState(formData);
|
|
137
|
+
const handleSubmit = useCallback(async (e, nativeEvent) => {
|
|
138
|
+
if (!loading) {
|
|
139
|
+
try {
|
|
140
|
+
setLoading(true);
|
|
141
|
+
if (onSubmitting) {
|
|
142
|
+
onSubmitting(true);
|
|
143
|
+
}
|
|
144
|
+
if (action) {
|
|
145
|
+
let {
|
|
146
|
+
formData
|
|
147
|
+
} = e;
|
|
148
|
+
if (transformData) {
|
|
149
|
+
formData = transformData(formData);
|
|
150
|
+
}
|
|
151
|
+
//todo 包含File对象需转FormData
|
|
152
|
+
try {
|
|
153
|
+
const result = await request({
|
|
154
|
+
url: action,
|
|
155
|
+
method,
|
|
156
|
+
data: formData
|
|
157
|
+
});
|
|
158
|
+
setExtraErrors(undefined);
|
|
159
|
+
if (onSuccess) {
|
|
160
|
+
await onSuccess(result);
|
|
161
|
+
}
|
|
162
|
+
return result;
|
|
163
|
+
} catch (e) {
|
|
164
|
+
if (axios.isAxiosError(e)) {
|
|
165
|
+
setExtraErrors(toExtraErrors(e));
|
|
166
|
+
} else {
|
|
167
|
+
throw e;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
} else if (onSubmit) {
|
|
171
|
+
return await onSubmit(e, nativeEvent);
|
|
172
|
+
}
|
|
173
|
+
} finally {
|
|
174
|
+
setLoading(false);
|
|
175
|
+
if (onSubmitting) {
|
|
176
|
+
onSubmitting(false);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}, [action, method, onSubmit]);
|
|
181
|
+
const handleChange = useCallback(e => {
|
|
182
|
+
const {
|
|
183
|
+
formData
|
|
184
|
+
} = e;
|
|
185
|
+
setData(formData);
|
|
186
|
+
if (onChange) {
|
|
187
|
+
onChange(e);
|
|
188
|
+
}
|
|
189
|
+
}, [setData, onChange]);
|
|
190
|
+
const transformErrors = useCallback(errors => {
|
|
191
|
+
errors = errors.map(error => ({
|
|
192
|
+
keyword: error.name,
|
|
193
|
+
dataPath: error.property,
|
|
194
|
+
...error
|
|
195
|
+
}));
|
|
196
|
+
localize(errors);
|
|
197
|
+
return errors;
|
|
198
|
+
}, []);
|
|
199
|
+
const submit = jsx(Button, {
|
|
200
|
+
className: 'px-4',
|
|
201
|
+
loading: loading,
|
|
202
|
+
type: 'submit',
|
|
203
|
+
variant: 'primary',
|
|
204
|
+
children: submitText
|
|
205
|
+
});
|
|
206
|
+
if (typeof children === 'function') {
|
|
207
|
+
children = children({
|
|
208
|
+
submit,
|
|
209
|
+
loading
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
return jsx(JsonForm, {
|
|
213
|
+
ref: ref,
|
|
214
|
+
extraErrors: extraErrors,
|
|
215
|
+
onSubmit: handleSubmit,
|
|
216
|
+
transformErrors: transformErrors,
|
|
217
|
+
noHtml5Validate: true,
|
|
218
|
+
noValidate: true,
|
|
219
|
+
...props,
|
|
220
|
+
formData: data,
|
|
221
|
+
onChange: handleChange,
|
|
222
|
+
widgets: widgets,
|
|
223
|
+
children: children || jsx("div", {
|
|
224
|
+
className: 'col-12',
|
|
225
|
+
children: submit
|
|
226
|
+
})
|
|
227
|
+
});
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
export { Form as default };
|
|
2
231
|
//# sourceMappingURL=form-9c0e638a.js.map
|
package/es/form-9c0e638a.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"form-9c0e638a.js","sources":["../src/components/lazy/form.tsx"],"sourcesContent":["import JsonForm, { getRegistry, JsonFormProps, JsonFormType } from '@topthink/json-form';\r\nimport * as React from 'react';\r\nimport {\r\n forwardRef,\r\n ForwardRefExoticComponent,\r\n PropsWithoutRef,\r\n ReactNode,\r\n RefAttributes,\r\n useCallback,\r\n useState\r\n} from 'react';\r\nimport axios, { AxiosError, Method } from 'axios';\r\nimport request from '../../request';\r\nimport { mapValues } from 'lodash';\r\nimport type { AjvError, ISubmitEvent, WidgetProps } from '@rjsf/core';\r\nimport Button from '../button';\r\nimport useSafeState from '../../hooks/use-safe-state';\r\n\r\nconst localize = require('ajv-i18n/localize/zh');\r\n\r\nexport interface FormProps<T = any> extends JsonFormProps<T> {\r\n onSuccess?: (data: any) => void;\r\n method?: Method;\r\n children?: ReactNode | ((props: { submit: ReactNode, loading: boolean }) => ReactNode);\r\n submitText?: string;\r\n onSubmitting?: (submitting: boolean) => void;\r\n transformData?: (data: T) => T;\r\n}\r\n\r\ntype Error = {\r\n __errors: string[]\r\n}\r\n\r\ntype Errors = Error | {\r\n [key: string]: Error\r\n}\r\n\r\nconst toExtraErrors = (error: AxiosError): Errors => {\r\n const errors = error.errors;\r\n if (typeof errors === 'string') {\r\n return {\r\n __errors: [errors]\r\n };\r\n }\r\n return mapValues(errors, (e) => {\r\n return {\r\n __errors: [e]\r\n };\r\n });\r\n};\r\n\r\nconst widgets = {\r\n upload: function({ options, ...props }: WidgetProps) {\r\n const { widgets } = getRegistry();\r\n\r\n if (options.endpoint) {\r\n options.onUpload = async (file: File) => {\r\n\r\n const data = new FormData();\r\n\r\n data.set('file', file);\r\n\r\n const { url, value } = await request({\r\n url: options.endpoint as string,\r\n method: 'post',\r\n data\r\n });\r\n\r\n return { url, value };\r\n };\r\n }\r\n\r\n return <widgets.upload {...props} options={options} />;\r\n },\r\n editor: function({ options, ...props }: WidgetProps) {\r\n const { widgets } = getRegistry();\r\n\r\n if (options.endpoint) {\r\n options.onUpload = async (file: File) => {\r\n\r\n const data = new FormData();\r\n\r\n data.set('file', file);\r\n\r\n const { url } = await request({\r\n url: options.endpoint as string,\r\n method: 'post',\r\n data\r\n });\r\n\r\n return url;\r\n };\r\n }\r\n\r\n return <widgets.editor {...props} options={options} />;\r\n },\r\n typeahead: function({ options, ...props }: WidgetProps) {\r\n const { widgets } = getRegistry();\r\n\r\n if (options.endpoint) {\r\n options.onSearch = async ({ value, query }) => {\r\n return await request({\r\n url: options.endpoint as string,\r\n params: { value, query }\r\n });\r\n };\r\n }\r\n\r\n return <widgets.typeahead {...props} options={options} />;\r\n }\r\n};\r\n\r\nexport interface FormType extends JsonFormType {\r\n\r\n}\r\n\r\nexport type CustomFormType<T = any> = ForwardRefExoticComponent<PropsWithoutRef<FormProps<T>> & RefAttributes<FormType>>\r\n\r\nconst Form: CustomFormType = forwardRef(({\r\n action,\r\n method = 'post',\r\n onSuccess,\r\n formData,\r\n onSubmitting,\r\n onSubmit,\r\n onChange,\r\n submitText = '提交',\r\n transformData,\r\n children,\r\n ...props\r\n}, ref) => {\r\n\r\n const [extraErrors, setExtraErrors] = useState<Errors>();\r\n const [loading, setLoading] = useSafeState(false);\r\n const [data, setData] = useState(formData);\r\n\r\n const handleSubmit = useCallback(async (e: ISubmitEvent<any>, nativeEvent: React.FormEvent<HTMLFormElement>) => {\r\n if (!loading) {\r\n try {\r\n setLoading(true);\r\n if (onSubmitting) {\r\n onSubmitting(true);\r\n }\r\n if (action) {\r\n let { formData } = e;\r\n\r\n if (transformData) {\r\n formData = transformData(formData);\r\n }\r\n\r\n //todo 包含File对象需转FormData\r\n try {\r\n const result = await request({\r\n url: action,\r\n method,\r\n data: formData\r\n });\r\n setExtraErrors(undefined);\r\n if (onSuccess) {\r\n await onSuccess(result);\r\n }\r\n return result;\r\n } catch (e) {\r\n if (axios.isAxiosError(e)) {\r\n setExtraErrors(toExtraErrors(e));\r\n } else {\r\n throw e;\r\n }\r\n }\r\n } else if (onSubmit) {\r\n return await onSubmit(e, nativeEvent);\r\n }\r\n } finally {\r\n setLoading(false);\r\n if (onSubmitting) {\r\n onSubmitting(false);\r\n }\r\n }\r\n }\r\n }, [action, method, onSubmit]);\r\n\r\n const handleChange = useCallback((e: ISubmitEvent<any>) => {\r\n const { formData } = e;\r\n setData(formData);\r\n if (onChange) {\r\n onChange(e);\r\n }\r\n }, [setData, onChange]);\r\n\r\n const transformErrors = useCallback((errors: AjvError[]) => {\r\n errors = errors.map(error => ({\r\n keyword: error.name,\r\n dataPath: error.property,\r\n ...error\r\n }));\r\n\r\n localize(errors);\r\n\r\n return errors;\r\n }, []);\r\n\r\n const submit = <Button className={'px-4'} loading={loading} type='submit' variant='primary'>{submitText}</Button>;\r\n\r\n if (typeof children === 'function') {\r\n children = children({ submit, loading });\r\n }\r\n\r\n return <JsonForm\r\n ref={ref}\r\n extraErrors={extraErrors}\r\n onSubmit={handleSubmit}\r\n transformErrors={transformErrors}\r\n noHtml5Validate\r\n noValidate\r\n {...props}\r\n formData={data}\r\n onChange={handleChange}\r\n widgets={widgets}\r\n >\r\n {children || <div className='col-12'>\r\n {submit}\r\n </div>}\r\n </JsonForm>;\r\n});\r\n\r\nexport default Form;\r\n"],"names":["localize","require","widgets","upload","_ref","options","props","getRegistry","endpoint","onUpload","async","data","FormData","set","file","url","value","request","method","_jsx","editor","_ref2","typeahead","_ref3","onSearch","query","_ref4","params","Form","forwardRef","_ref5","ref","action","onSuccess","formData","onSubmitting","onSubmit","onChange","submitText","transformData","children","extraErrors","setExtraErrors","useState","loading","setLoading","useSafeState","setData","handleSubmit","useCallback","e","nativeEvent","result","undefined","axios","isAxiosError","error","errors","__errors","mapValues","toExtraErrors","handleChange","transformErrors","map","keyword","name","dataPath","property","submit","Button","className","type","variant","JsonForm","noHtml5Validate","noValidate"],"mappings":"oiBAkBA,MAAMA,EAAWC,QAAQ,wBAiCnBC,EAAU,CACZC,OAAQ,SAA2CC,GAAA,IAAlCC,QAAEA,KAAYC,GAAoBF,EAC/C,MAAMF,QAAEA,GAAYK,IAmBpB,OAjBIF,EAAQG,WACRH,EAAQI,SAAWC,UAEf,MAAMC,EAAO,IAAIC,SAEjBD,EAAKE,IAAI,OAAQC,GAEjB,MAAMC,IAAEA,EAAGC,MAAEA,SAAgBC,EAAQ,CACjCF,IAAKV,EAAQG,SACbU,OAAQ,OACRP,SAGJ,MAAO,CAAEI,M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ACLd,SACAP,KAAMuB,IAMV,OAJAQ,OAAeW,GACXpB,SACMA,EAAUmB,GAEbA,CAOV,CANC,MAAOF,GACL,IAAII,EAAMC,aAAaL,GAGnB,MAAMA,EAFNR,EA/HLc,KACnB,MAAMC,EAASD,EAAMC,OACrB,MAAsB,iBAAXA,EACA,CACHC,SAAU,CAACD,IAGZE,EAAUF,GAASP,IACf,CACHQ,SAAU,CAACR,MAEjB,EAoHqCU,CAAcV,GAIpC,CACJ,MAAM,GAAId,EACP,aAAaA,EAASc,EAAGC,EAOhC,CALS,QACNN,GAAW,GACPV,GACAA,GAAa,EAEpB,CACJ,GACF,CAACH,EAAQd,EAAQkB,IAEdyB,EAAeZ,GAAaC,IAC9B,MAAMhB,SAAEA,GAAagB,EACrBH,EAAQb,GACJG,GACAA,EAASa,EACZ,GACF,CAACH,EAASV,IAEPyB,EAAkBb,GAAaQ,IACjCA,EAASA,EAAOM,KAAIP,IAAU,CAC1BQ,QAASR,EAAMS,KACfC,SAAUV,EAAMW,YACbX,MAGPxD,EAASyD,GAEFA,IACR,IAEGW,EAASjD,EAACkD,GAAOC,UAAW,OAAQ1B,QAASA,EAAS2B,KAAK,SAASC,QAAQ,UAAShC,SAAEF,IAM7F,MAJwB,mBAAbE,IACPA,EAAWA,EAAS,CAAE4B,SAAQxB,aAG3BzB,EAACsD,EACJ,CAAA1C,IAAKA,EACLU,YAAaA,EACbL,SAAUY,EACVc,gBAAiBA,EACjBY,iBAAe,EACfC,YAAU,KACNrE,EACJ4B,SAAUvB,EACV0B,SAAUwB,EACV3D,QAASA,WAERsC,GAAYrB,SAAKmD,UAAU,SACvB9B,SAAA4B,KAEE"}
|
|
1
|
+
{"version":3,"file":"form-9c0e638a.js","sources":["../src/components/lazy/form.tsx"],"sourcesContent":["import JsonForm, { getRegistry, JsonFormProps, JsonFormType } from '@topthink/json-form';\r\nimport * as React from 'react';\r\nimport {\r\n forwardRef,\r\n ForwardRefExoticComponent,\r\n PropsWithoutRef,\r\n ReactNode,\r\n RefAttributes,\r\n useCallback,\r\n useState\r\n} from 'react';\r\nimport axios, { AxiosError, Method } from 'axios';\r\nimport request from '../../request';\r\nimport { mapValues } from 'lodash';\r\nimport type { AjvError, ISubmitEvent, WidgetProps } from '@rjsf/core';\r\nimport Button from '../button';\r\nimport useSafeState from '../../hooks/use-safe-state';\r\n\r\nconst localize = require('ajv-i18n/localize/zh');\r\n\r\nexport interface FormProps<T = any> extends JsonFormProps<T> {\r\n onSuccess?: (data: any) => void;\r\n method?: Method;\r\n children?: ReactNode | ((props: { submit: ReactNode, loading: boolean }) => ReactNode);\r\n submitText?: string;\r\n onSubmitting?: (submitting: boolean) => void;\r\n transformData?: (data: T) => T;\r\n}\r\n\r\ntype Error = {\r\n __errors: string[]\r\n}\r\n\r\ntype Errors = Error | {\r\n [key: string]: Error\r\n}\r\n\r\nconst toExtraErrors = (error: AxiosError): Errors => {\r\n const errors = error.errors;\r\n if (typeof errors === 'string') {\r\n return {\r\n __errors: [errors]\r\n };\r\n }\r\n return mapValues(errors, (e) => {\r\n return {\r\n __errors: [e]\r\n };\r\n });\r\n};\r\n\r\nconst widgets = {\r\n upload: function({ options, ...props }: WidgetProps) {\r\n const { widgets } = getRegistry();\r\n\r\n if (options.endpoint) {\r\n options.onUpload = async (file: File) => {\r\n\r\n const data = new FormData();\r\n\r\n data.set('file', file);\r\n\r\n const { url, value } = await request({\r\n url: options.endpoint as string,\r\n method: 'post',\r\n data\r\n });\r\n\r\n return { url, value };\r\n };\r\n }\r\n\r\n return <widgets.upload {...props} options={options} />;\r\n },\r\n editor: function({ options, ...props }: WidgetProps) {\r\n const { widgets } = getRegistry();\r\n\r\n if (options.endpoint) {\r\n options.onUpload = async (file: File) => {\r\n\r\n const data = new FormData();\r\n\r\n data.set('file', file);\r\n\r\n const { url } = await request({\r\n url: options.endpoint as string,\r\n method: 'post',\r\n data\r\n });\r\n\r\n return url;\r\n };\r\n }\r\n\r\n return <widgets.editor {...props} options={options} />;\r\n },\r\n typeahead: function({ options, ...props }: WidgetProps) {\r\n const { widgets } = getRegistry();\r\n\r\n if (options.endpoint) {\r\n options.onSearch = async ({ value, query }) => {\r\n return await request({\r\n url: options.endpoint as string,\r\n params: { value, query }\r\n });\r\n };\r\n }\r\n\r\n return <widgets.typeahead {...props} options={options} />;\r\n }\r\n};\r\n\r\nexport interface FormType extends JsonFormType {\r\n\r\n}\r\n\r\nexport type CustomFormType<T = any> = ForwardRefExoticComponent<PropsWithoutRef<FormProps<T>> & RefAttributes<FormType>>\r\n\r\nconst Form: CustomFormType = forwardRef(({\r\n action,\r\n method = 'post',\r\n onSuccess,\r\n formData,\r\n onSubmitting,\r\n onSubmit,\r\n onChange,\r\n submitText = '提交',\r\n transformData,\r\n children,\r\n ...props\r\n}, ref) => {\r\n\r\n const [extraErrors, setExtraErrors] = useState<Errors>();\r\n const [loading, setLoading] = useSafeState(false);\r\n const [data, setData] = useState(formData);\r\n\r\n const handleSubmit = useCallback(async (e: ISubmitEvent<any>, nativeEvent: React.FormEvent<HTMLFormElement>) => {\r\n if (!loading) {\r\n try {\r\n setLoading(true);\r\n if (onSubmitting) {\r\n onSubmitting(true);\r\n }\r\n if (action) {\r\n let { formData } = e;\r\n\r\n if (transformData) {\r\n formData = transformData(formData);\r\n }\r\n\r\n //todo 包含File对象需转FormData\r\n try {\r\n const result = await request({\r\n url: action,\r\n method,\r\n data: formData\r\n });\r\n setExtraErrors(undefined);\r\n if (onSuccess) {\r\n await onSuccess(result);\r\n }\r\n return result;\r\n } catch (e) {\r\n if (axios.isAxiosError(e)) {\r\n setExtraErrors(toExtraErrors(e));\r\n } else {\r\n throw e;\r\n }\r\n }\r\n } else if (onSubmit) {\r\n return await onSubmit(e, nativeEvent);\r\n }\r\n } finally {\r\n setLoading(false);\r\n if (onSubmitting) {\r\n onSubmitting(false);\r\n }\r\n }\r\n }\r\n }, [action, method, onSubmit]);\r\n\r\n const handleChange = useCallback((e: ISubmitEvent<any>) => {\r\n const { formData } = e;\r\n setData(formData);\r\n if (onChange) {\r\n onChange(e);\r\n }\r\n }, [setData, onChange]);\r\n\r\n const transformErrors = useCallback((errors: AjvError[]) => {\r\n errors = errors.map(error => ({\r\n keyword: error.name,\r\n dataPath: error.property,\r\n ...error\r\n }));\r\n\r\n localize(errors);\r\n\r\n return errors;\r\n }, []);\r\n\r\n const submit = <Button className={'px-4'} loading={loading} type='submit' variant='primary'>{submitText}</Button>;\r\n\r\n if (typeof children === 'function') {\r\n children = children({ submit, loading });\r\n }\r\n\r\n return <JsonForm\r\n ref={ref}\r\n extraErrors={extraErrors}\r\n onSubmit={handleSubmit}\r\n transformErrors={transformErrors}\r\n noHtml5Validate\r\n noValidate\r\n {...props}\r\n formData={data}\r\n onChange={handleChange}\r\n widgets={widgets}\r\n >\r\n {children || <div className='col-12'>\r\n {submit}\r\n </div>}\r\n </JsonForm>;\r\n});\r\n\r\nexport default Form;\r\n"],"names":["localize","require","toExtraErrors","error","errors","__errors","mapValues","e","widgets","upload","options","props","getRegistry","endpoint","onUpload","file","data","FormData","set","url","value","request","method","_jsx","editor","typeahead","onSearch","query","params","Form","forwardRef","ref","action","onSuccess","formData","onSubmitting","onSubmit","onChange","submitText","transformData","children","extraErrors","setExtraErrors","useState","loading","setLoading","useSafeState","setData","handleSubmit","useCallback","nativeEvent","result","undefined","axios","isAxiosError","handleChange","transformErrors","map","keyword","name","dataPath","property","submit","Button","className","type","variant","JsonForm","noHtml5Validate","noValidate"],"mappings":";;;;;;;;;;;;;;;;;AAkBA,MAAMA,QAAQ,GAAGC,OAAO,CAAC,sBAAsB,CAAC,CAAA;AAmBhD,MAAMC,aAAa,GAAIC,KAAiB,IAAY;AAChD,EAAA,MAAMC,MAAM,GAAGD,KAAK,CAACC,MAAM,CAAA;AAC3B,EAAA,IAAI,OAAOA,MAAM,KAAK,QAAQ,EAAE;IAC5B,OAAO;MACHC,QAAQ,EAAE,CAACD,MAAM,CAAA;KACpB,CAAA;AACJ,GAAA;AACD,EAAA,OAAOE,SAAS,CAAC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C,CAAC,CAAA;IAEHH,QAAQ,CAACI,MAAM,CAAC,CAAA;AAEhB,IAAA,OAAOA,MAAM,CAAA;GAChB,EAAE,EAAE,CAAC,CAAA;AAEN,EAAA,MAAM0D,MAAM,GAAGvC,GAAC,CAAAwC,MAAM;AAACC,IAAAA,SAAS,EAAE,MAAM;AAAEpB,IAAAA,OAAO,EAAEA,OAAO;AAAEqB,IAAAA,IAAI,EAAC,QAAQ;AAACC,IAAAA,OAAO,EAAC,SAAS;AAAA1B,IAAAA,QAAA,EAAEF,UAAAA;AAAU,GAAA,CAAU,CAAA;AAEjH,EAAA,IAAI,OAAOE,QAAQ,KAAK,UAAU,EAAE;IAChCA,QAAQ,GAAGA,QAAQ,CAAC;MAAEsB,MAAM;AAAElB,MAAAA,OAAAA;AAAS,KAAA,CAAC,CAAA;AAC3C,GAAA;EAED,OAAOrB,GAAA,CAAC4C,QAAQ,EACZ;AAAApC,IAAAA,GAAG,EAAEA,GAAG;AACRU,IAAAA,WAAW,EAAEA,WAAW;AACxBL,IAAAA,QAAQ,EAAEY,YAAY;AACtBQ,IAAAA,eAAe,EAAEA,eAAe;AAChCY,IAAAA,eAAe,EAAA,IAAA;AACfC,IAAAA,UAAU,EAAA,IAAA;AAAA,IAAA,GACN1D,KAAK;AACTuB,IAAAA,QAAQ,EAAElB,IAAI;AACdqB,IAAAA,QAAQ,EAAEkB,YAAY;AACtB/C,IAAAA,OAAO,EAAEA,OAAO;cAEfgC,QAAQ,IAAIjB;AAAKyC,MAAAA,SAAS,EAAC,QAAQ;AAC/BxB,MAAAA,QAAA,EAAAsB,MAAAA;KACC,CAAA;AAAA,GAAA,CACC,CAAA;AACf,CAAC;;;;"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{jsx as t}from"react/jsx-runtime";import o,{getRegistry as r}from"@topthink/json-form";import*as e from"react";import{forwardRef as a,useState as n,useCallback as i}from"react";import s from"axios";import{u as l,r as m,B as p}from"./index-563b11c6.js";import{mapValues as c}from"lodash";import"sweetalert2/dist/sweetalert2.js";import"sweetalert2-react-content";import"react-bootstrap";import"query-string";import"retry-axios";import"rc-notification";import"styled-components";import"classnames";import"react-async-hook";const d=require("ajv-i18n/localize/zh"),u=e.lazy((()=>import("./editor-widget-ebec8493.js"))),f=o=>t(e.Suspense,{fallback:null,children:t(u,{...o})}),y={upload:function(o){let{options:e,...a}=o;const{widgets:n}=r();return e.endpoint&&(e.onUpload=async t=>{const o=new FormData;o.set("file",t);const{url:r,value:a}=await m({url:e.endpoint,method:"post",data:o});return{url:r,value:a}}),t(n.upload,{...a,options:e})},editor:function(o){let{options:r,...e}=o;return r.endpoint&&(r.onUpload=async t=>{const o=new FormData;o.set("file",t);const{url:e}=await m({url:r.endpoint,method:"post",data:o});return e}),t(f,{...e,options:r})},typeahead:function(o){let{options:e,...a}=o;const{widgets:n}=r();return e.endpoint&&(e.onSearch=async t=>{let{value:o,query:r}=t;return await m({url:e.endpoint,params:{value:o,query:r}})}),t(n.typeahead,{...a,options:e})}},h=a(((r,e)=>{let{action:a,method:u="post",onSuccess:f,formData:h,onSubmitting:w,onSubmit:g,onChange:b,submitText:x="提交",transformData:v,children:D,...j}=r;const[S,k]=n(),[q,_]=l(!1),[z,E]=n(h),C=i((async(t,o)=>{if(!q)try{if(_(!0),w&&w(!0),a){let{formData:o}=t;v&&(o=v(o));try{const t=await m({url:a,method:u,data:o});return k(void 0),f&&await f(t),t}catch(t){if(!s.isAxiosError(t))throw t;k((t=>{const o=t.errors;return"string"==typeof o?{__errors:[o]}:c(o,(t=>({__errors:[t]})))})(t))}}else if(g)return await g(t,o)}finally{_(!1),w&&w(!1)}}),[a,u,g]),F=i((t=>{const{formData:o}=t;E(o),b&&b(t)}),[E,b]),N=i((t=>(t=t.map((t=>({keyword:t.name,dataPath:t.property,...t}))),d(t),t)),[]),U=t(p,{className:"px-4",loading:q,type:"submit",variant:"primary",children:x});return"function"==typeof D&&(D=D({submit:U,loading:q})),t(o,{ref:e,extraErrors:S,onSubmit:C,transformErrors:N,noHtml5Validate:!0,noValidate:!0,...j,formData:z,onChange:F,widgets:y,children:D||t("div",{className:"col-12",children:U})})}));export{h as default};
|
|
2
|
+
//# sourceMappingURL=index-2b97b70d.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index-2b97b70d.js","sources":["../src/components/lazy/form/index.tsx"],"sourcesContent":["import JsonForm, { getRegistry, JsonFormProps, JsonFormType } from '@topthink/json-form';\r\nimport * as React from 'react';\r\nimport {\r\n forwardRef,\r\n ForwardRefExoticComponent,\r\n PropsWithoutRef,\r\n ReactNode,\r\n RefAttributes,\r\n useCallback,\r\n useState\r\n} from 'react';\r\nimport axios, { AxiosError, Method } from 'axios';\r\nimport request from '../../../request';\r\nimport { mapValues } from 'lodash';\r\nimport type { AjvError, ISubmitEvent, WidgetProps } from '@rjsf/core';\r\nimport Button from '../../button';\r\nimport useSafeState from '../../../hooks/use-safe-state';\r\n\r\nconst localize = require('ajv-i18n/localize/zh');\r\n\r\nconst LazyEditorWidget = React.lazy(() => import('./widgets/editor-widget'));\r\nconst EditorWidget = (props: WidgetProps) => {\r\n return <React.Suspense fallback={null}>\r\n <LazyEditorWidget {...props} />\r\n </React.Suspense>;\r\n};\r\n\r\nexport interface FormProps<T = any> extends JsonFormProps<T> {\r\n onSuccess?: (data: any) => void;\r\n method?: Method;\r\n children?: ReactNode | ((props: { submit: ReactNode, loading: boolean }) => ReactNode);\r\n submitText?: string;\r\n onSubmitting?: (submitting: boolean) => void;\r\n transformData?: (data: T) => T;\r\n}\r\n\r\ntype Error = {\r\n __errors: string[]\r\n}\r\n\r\ntype Errors = Error | {\r\n [key: string]: Error\r\n}\r\n\r\nconst toExtraErrors = (error: AxiosError): Errors => {\r\n const errors = error.errors;\r\n if (typeof errors === 'string') {\r\n return {\r\n __errors: [errors]\r\n };\r\n }\r\n return mapValues(errors, (e) => {\r\n return {\r\n __errors: [e]\r\n };\r\n });\r\n};\r\n\r\nconst widgets = {\r\n upload: function({ options, ...props }: WidgetProps) {\r\n const { widgets } = getRegistry();\r\n\r\n if (options.endpoint) {\r\n options.onUpload = async (file: File) => {\r\n\r\n const data = new FormData();\r\n\r\n data.set('file', file);\r\n\r\n const { url, value } = await request({\r\n url: options.endpoint as string,\r\n method: 'post',\r\n data\r\n });\r\n\r\n return { url, value };\r\n };\r\n }\r\n\r\n return <widgets.upload {...props} options={options} />;\r\n },\r\n editor: function({ options, ...props }: WidgetProps) {\r\n if (options.endpoint) {\r\n options.onUpload = async (file: File) => {\r\n\r\n const data = new FormData();\r\n\r\n data.set('file', file);\r\n\r\n const { url } = await request({\r\n url: options.endpoint as string,\r\n method: 'post',\r\n data\r\n });\r\n\r\n return url;\r\n };\r\n }\r\n\r\n return <EditorWidget {...props} options={options} />;\r\n },\r\n typeahead: function({ options, ...props }: WidgetProps) {\r\n const { widgets } = getRegistry();\r\n\r\n if (options.endpoint) {\r\n options.onSearch = async ({ value, query }) => {\r\n return await request({\r\n url: options.endpoint as string,\r\n params: { value, query }\r\n });\r\n };\r\n }\r\n\r\n return <widgets.typeahead {...props} options={options} />;\r\n }\r\n};\r\n\r\nexport interface FormType extends JsonFormType {\r\n\r\n}\r\n\r\nexport type CustomFormType<T = any> = ForwardRefExoticComponent<PropsWithoutRef<FormProps<T>> & RefAttributes<FormType>>\r\n\r\nconst Form: CustomFormType = forwardRef(({\r\n action,\r\n method = 'post',\r\n onSuccess,\r\n formData,\r\n onSubmitting,\r\n onSubmit,\r\n onChange,\r\n submitText = '提交',\r\n transformData,\r\n children,\r\n ...props\r\n}, ref) => {\r\n\r\n const [extraErrors, setExtraErrors] = useState<Errors>();\r\n const [loading, setLoading] = useSafeState(false);\r\n const [data, setData] = useState(formData);\r\n\r\n const handleSubmit = useCallback(async (e: ISubmitEvent<any>, nativeEvent: React.FormEvent<HTMLFormElement>) => {\r\n if (!loading) {\r\n try {\r\n setLoading(true);\r\n if (onSubmitting) {\r\n onSubmitting(true);\r\n }\r\n if (action) {\r\n let { formData } = e;\r\n\r\n if (transformData) {\r\n formData = transformData(formData);\r\n }\r\n\r\n //todo 包含File对象需转FormData\r\n try {\r\n const result = await request({\r\n url: action,\r\n method,\r\n data: formData\r\n });\r\n setExtraErrors(undefined);\r\n if (onSuccess) {\r\n await onSuccess(result);\r\n }\r\n return result;\r\n } catch (e) {\r\n if (axios.isAxiosError(e)) {\r\n setExtraErrors(toExtraErrors(e));\r\n } else {\r\n throw e;\r\n }\r\n }\r\n } else if (onSubmit) {\r\n return await onSubmit(e, nativeEvent);\r\n }\r\n } finally {\r\n setLoading(false);\r\n if (onSubmitting) {\r\n onSubmitting(false);\r\n }\r\n }\r\n }\r\n }, [action, method, onSubmit]);\r\n\r\n const handleChange = useCallback((e: ISubmitEvent<any>) => {\r\n const { formData } = e;\r\n setData(formData);\r\n if (onChange) {\r\n onChange(e);\r\n }\r\n }, [setData, onChange]);\r\n\r\n const transformErrors = useCallback((errors: AjvError[]) => {\r\n errors = errors.map(error => ({\r\n keyword: error.name,\r\n dataPath: error.property,\r\n ...error\r\n }));\r\n\r\n localize(errors);\r\n\r\n return errors;\r\n }, []);\r\n\r\n const submit = <Button className={'px-4'} loading={loading} type='submit' variant='primary'>{submitText}</Button>;\r\n\r\n if (typeof children === 'function') {\r\n children = children({ submit, loading });\r\n }\r\n\r\n return <JsonForm\r\n ref={ref}\r\n extraErrors={extraErrors}\r\n onSubmit={handleSubmit}\r\n transformErrors={transformErrors}\r\n noHtml5Validate\r\n noValidate\r\n {...props}\r\n formData={data}\r\n onChange={handleChange}\r\n widgets={widgets}\r\n >\r\n {children || <div className='col-12'>\r\n {submit}\r\n </div>}\r\n </JsonForm>;\r\n});\r\n\r\nexport default Form;\r\n"],"names":["localize","require","LazyEditorWidget","React","lazy","import","EditorWidget","props","_jsx","Suspense","fallback","children","widgets","upload","_ref","options","getRegistry","endpoint","onUpload","async","data","FormData","set","file","url","value","request","method","editor","_ref2","typeahead","_ref3","onSearch","query","_ref4","params","Form","forwardRef","_ref5","ref","action","onSuccess","formData","onSubmitting","onSubmit","onChange","submitText","transformData","extraErrors","setExtraErrors","useState","loading","setLoading","useSafeState","setData","handleSubmit","useCallback","e","nativeEvent","result","undefined","axios","isAxiosError","error","errors","__errors","mapValues","toExtraErrors","handleChange","transformErrors","map","keyword","name","dataPath","property","submit","Button","className","type","variant","JsonForm","noHtml5Validate","noValidate"],"mappings":"8gBAkBA,MAAMA,EAAWC,QAAQ,wBAEnBC,EAAmBC,EAAMC,MAAK,IAAMC,OAAO,iCAC3CC,EAAgBC,GACXC,EAACL,EAAMM,UAASC,SAAU,KAAIC,SACjCH,EAACN,EAAgB,IAAKK,MAmCxBK,EAAU,CACZC,OAAQ,SAA2CC,GAAA,IAAlCC,QAAEA,KAAYR,GAAoBO,EAC/C,MAAMF,QAAEA,GAAYI,IAmBpB,OAjBID,EAAQE,WACRF,EAAQG,SAAWC,UAEf,MAAMC,EAAO,IAAIC,SAEjBD,EAAKE,IAAI,OAAQC,GAEjB,MAAMC,IAAEA,EAAGC,MAAEA,SAAgBC,EAAQ,CACjCF,IAAKT,EAAQE,SACbU,OAAQ,OACRP,SAGJ,MAAO,CAAEI,MAAKC,QAAO,GAItBjB,EAACI,EAAQC,OAAM,IAAKN,EAAOQ,QAASA,GAC9C,EACDa,OAAQ,SAA2CC,GAAA,IAAlCd,QAAEA,KAAYR,GAAoBsB,EAkB/C,OAjBId,EAAQE,WACRF,EAAQG,SAAWC,UAEf,MAAMC,EAAO,IAAIC,SAEjBD,EAAKE,IAAI,OAAQC,GAEjB,MAAMC,IAAEA,SAAcE,EAAQ,CAC1BF,IAAKT,EAAQE,SACbU,OAAQ,OACRP,SAGJ,OAAOI,CAAG,GAIXhB,EAACF,EAAiB,IAAAC,EAAOQ,QAASA,GAC5C,EACDe,UAAW,SAA2CC,GAAA,IAAlChB,QAAEA,KAAYR,GAAoBwB,EAClD,MAAMnB,QAAEA,GAAYI,IAWpB,OATID,EAAQE,WACRF,EAAQiB,SAAWb,UAA2B,IAApBM,MA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