@hzab/form-render-mobile 0.0.16 → 0.0.17

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hzab/form-render-mobile",
3
- "version": "0.0.16",
3
+ "version": "0.0.17",
4
4
  "description": "formily-form-render-mobile",
5
5
  "main": "lib",
6
6
  "scripts": {
@@ -52,7 +52,16 @@
52
52
  "lib": "lib"
53
53
  },
54
54
  "lint-staged": {
55
- "**.{js,jsx,ts,tsx,css,scss,less,json,html}": [
55
+ "src/**.{js,jsx,ts,tsx,css,scss,less,json,html}": [
56
+ "prettier --write"
57
+ ],
58
+ "src/**/**.{js,jsx,ts,tsx,css,scss,less,json,html}": [
59
+ "prettier --write"
60
+ ],
61
+ "example/**.{js,jsx,ts,tsx,css,scss,less,json,html}": [
62
+ "prettier --write"
63
+ ],
64
+ "example/**/**.{js,jsx,ts,tsx,css,scss,less,json,html}": [
56
65
  "prettier --write"
57
66
  ]
58
67
  }
@@ -0,0 +1,11 @@
1
+ import { createContext, useContext } from "react";
2
+
3
+ /**
4
+ * 组件外部传入的 props context
5
+ */
6
+ export const GlobalPropsContext = createContext(null);
7
+
8
+ /**
9
+ * 组件外部传入的 props
10
+ */
11
+ export const useGlobalPropsContext = () => useContext(GlobalPropsContext);
@@ -1,6 +1,6 @@
1
1
  import _ from "lodash";
2
2
 
3
- export function schemaHandler(_schema) {
3
+ export function schemaHandler(_schema, opt = {}) {
4
4
  let formSchema = _.cloneDeep(_schema);
5
5
  let schema = formSchema.schema || formSchema;
6
6
  const formConf = formSchema.form || {};
@@ -9,7 +9,9 @@ export function schemaHandler(_schema) {
9
9
  formConf.layout = "horizontal";
10
10
  }
11
11
 
12
- handleLayout(schema.properties, formConf.layout);
12
+ if (opt.layout) {
13
+ formConf.layout = opt.layout;
14
+ }
13
15
 
14
16
  formSchema = {
15
17
  ..._schema,
@@ -19,26 +21,3 @@ export function schemaHandler(_schema) {
19
21
 
20
22
  return formSchema;
21
23
  }
22
-
23
- export function handleLayout(properties, layout) {
24
- const keys = properties && Object.keys(properties);
25
- if (keys && keys.length > 0) {
26
- // 手动设置 FormLayout
27
- keys.forEach((key) => {
28
- const field = properties[key];
29
- if (!field["x-decorator-props"]) {
30
- field["x-decorator-props"] = {};
31
- }
32
- if (!field["x-decorator-props"].layout) {
33
- field["x-decorator-props"].layout = layout;
34
- }
35
- if (field?.properties) {
36
- handleLayout(field?.properties, layout);
37
- }
38
- if (field?.items?.properties) {
39
- handleLayout(field?.items?.properties, layout);
40
- }
41
- });
42
- }
43
- return properties;
44
- }
@@ -10,8 +10,8 @@ export function getSignature(opt = {}) {
10
10
  ) {
11
11
  return Promise.resolve(window.__ossSignatureRes);
12
12
  }
13
- const { axios: _ax, params = {}, axiosConf } = opt;
14
- return (_ax || axios)
13
+ const { axios: _ax = axios, params = {}, axiosConf } = opt;
14
+ return _ax
15
15
  .get(serverUrl, {
16
16
  ...axiosConf,
17
17
  params: {
@@ -32,11 +32,17 @@ export function getSignature(opt = {}) {
32
32
  class OssUpload {
33
33
  constructor(props = {}) {
34
34
  this.axios = props.axios || axios;
35
+ this.axiosConf = props.axiosConf || {};
35
36
  this.serverUrl = props.serverUrl || "/api/v1/user/oss/getWebOssConfig";
36
37
  }
37
38
 
38
39
  getSignature(serverUrl = this.serverUrl, opt) {
39
- return getSignature({ ...opt, serverUrl });
40
+ return getSignature({
41
+ ...opt,
42
+ serverUrl,
43
+ axios: opt?.axios || this.axios,
44
+ axiosConf: { ...this.axiosConf, ...opt?.axiosConf },
45
+ });
40
46
  }
41
47
 
42
48
  upload(file, opt = {}) {
@@ -69,8 +75,10 @@ class OssUpload {
69
75
  }
70
76
  }
71
77
 
72
- return this.axios
73
- .post(ossParams.host, formData)
78
+ const _axios = opt?.axios || this.axios;
79
+
80
+ return _axios
81
+ .post(ossParams.host, formData, { ...this.axiosConf, ...opt?.axiosConf })
74
82
  .then((res) => {
75
83
  resolve(res);
76
84
  return res;
@@ -94,6 +102,8 @@ export async function handleOssUpload(files, opt) {
94
102
  const _files = files;
95
103
  const { ossUrl, signatureParams, ossParams, axiosConf } = opt || {};
96
104
  const ossUpload = new OssUpload({
105
+ axios: opt.axios,
106
+ axiosConf: axiosConf,
97
107
  serverUrl: ossUrl || "/api/v1/user/oss/getWebOssConfig",
98
108
  });
99
109
 
@@ -1,6 +1,9 @@
1
1
  import UploaderCom from "./uploader";
2
+ import { useGlobalPropsContext } from "../../common/global-props-context";
2
3
 
3
4
  export const Uploader = (props) => {
5
+ // 组件外部传入的 props
6
+ const globalProps = useGlobalPropsContext() || {};
4
7
  const { field = {}, onChange, value } = props;
5
8
  const { name, mode, componentProps = {} } = field;
6
9
  const { multiple } = componentProps;
@@ -24,6 +27,8 @@ export const Uploader = (props) => {
24
27
  mode: mode,
25
28
  ...props,
26
29
  value: typeof value === "string" ? [value] : value,
30
+ axios: globalProps?.axios,
31
+ axiosConf: globalProps?.axiosConf,
27
32
  ...componentProps,
28
33
  };
29
34
 
@@ -2,8 +2,6 @@ import { useState, useRef, useEffect, useMemo } from "react";
2
2
  import { Button, Dialog, ActionSheet, Toast } from "antd-mobile";
3
3
  import { nanoid } from "nanoid";
4
4
 
5
- import Video from "./components/video";
6
- import Image from "./components/Image";
7
5
  import ItemList from "./components/ItemList";
8
6
 
9
7
  import { getImage, getVideo, getAudio } from "./common/cordova-camera";
@@ -75,7 +73,12 @@ function Uploader(props) {
75
73
  // 处理 oss 逻辑
76
74
  if (isOssUpload) {
77
75
  setLoading(true);
78
- files = await handleOssUpload(files, { ...ossOpt, ossUrl });
76
+ files = await handleOssUpload(files, {
77
+ axios: props.axios,
78
+ axiosConf: props?.axiosConf,
79
+ ...ossOpt,
80
+ ossUrl,
81
+ });
79
82
  setLoading(false);
80
83
  }
81
84
 
package/src/index.tsx CHANGED
@@ -23,6 +23,7 @@ import { FormProvider, createSchemaField, Schema } from "@formily/react";
23
23
 
24
24
  // 自定义组件
25
25
  import * as customComponents from "./components/index";
26
+ import { GlobalPropsContext } from "./common/global-props-context";
26
27
  import { schemaHandler } from "./common/schemaHandler";
27
28
 
28
29
  import { formPropsI } from "./type.d";
@@ -79,28 +80,36 @@ const FormRender = forwardRef((props: formPropsI, parentRef) => {
79
80
  }, []);
80
81
 
81
82
  const schema = useMemo(() => {
82
- return schemaHandler(props.schema).schema;
83
+ return schemaHandler(props.schema);
83
84
  }, [props.schema]);
84
85
 
86
+ const formLayoutProps = {
87
+ ...schema?.form,
88
+ };
89
+
85
90
  return (
86
- <div className={`form-render ${props.className} ${isDetail ? "form-render-detail" : ""}`}>
87
- <FormProvider form={formRender}>
88
- {/* @ts-ignore */}
89
- <SchemaField schema={schema} />
90
- {props.hasSubmit !== false && isDetail !== true && readOnly !== true && !props.disabled ? (
91
- <FormButtonGroup>
92
- <Submit
93
- onSubmit={(values) => {
94
- props.onSubmit && props.onSubmit(values);
95
- }}
96
- >
97
- {props.submitText || "提交"}
98
- </Submit>
99
- </FormButtonGroup>
100
- ) : null}
101
- <div className="form-render-footer">{props.footerRender && props.footerRender()}</div>
102
- </FormProvider>
103
- </div>
91
+ <GlobalPropsContext.Provider value={props}>
92
+ <div className={`form-render ${props.className} ${isDetail ? "form-render-detail" : ""}`}>
93
+ <FormProvider form={formRender}>
94
+ <FormLayout {...formLayoutProps}>
95
+ {/* @ts-ignore */}
96
+ <SchemaField schema={schema?.schema} />
97
+ {props.hasSubmit !== false && isDetail !== true && readOnly !== true && !props.disabled ? (
98
+ <FormButtonGroup>
99
+ <Submit
100
+ onSubmit={(values) => {
101
+ props.onSubmit && props.onSubmit(values);
102
+ }}
103
+ >
104
+ {props.submitText || "提交"}
105
+ </Submit>
106
+ </FormButtonGroup>
107
+ ) : null}
108
+ </FormLayout>
109
+ <div className="form-render-footer">{props.footerRender && props.footerRender()}</div>
110
+ </FormProvider>
111
+ </div>
112
+ </GlobalPropsContext.Provider>
104
113
  );
105
114
  });
106
115