@hw-component/form 1.6.3 → 1.6.5

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.
@@ -0,0 +1,139 @@
1
+ 'use strict';
2
+
3
+ require('core-js/modules/es.object.keys.js');
4
+ require('core-js/modules/es.symbol.js');
5
+ require('core-js/modules/es.array.filter.js');
6
+ require('core-js/modules/es.object.get-own-property-descriptor.js');
7
+ require('core-js/modules/es.object.get-own-property-descriptors.js');
8
+ var _objectWithoutProperties = require('@babel/runtime-corejs3/helpers/objectWithoutProperties');
9
+ var _defineProperty = require('@babel/runtime-corejs3/helpers/defineProperty');
10
+ require('core-js/modules/es.array.find-index.js');
11
+ require('core-js/modules/es.object.to-string.js');
12
+ require('core-js/modules/web.dom-collections.for-each.js');
13
+ require('core-js/modules/es.array.map.js');
14
+
15
+ var _excluded = ["index"],
16
+ _excluded2 = ["index"];
17
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
18
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
19
+ var findNewValInOldVal = function findNewValInOldVal(val, oldVal) {
20
+ if (!oldVal) {
21
+ return -1;
22
+ }
23
+ return oldVal.findIndex(function (item) {
24
+ return item.value === val;
25
+ });
26
+ };
27
+ var sourceDataProvider = function sourceDataProvider(_ref, oldVal) {
28
+ var value = _ref.value;
29
+ var oldData = [];
30
+ var newData = [];
31
+ value.forEach(function (item, i) {
32
+ var index = findNewValInOldVal(item, oldVal);
33
+ if (index === -1) {
34
+ newData.push({
35
+ value: item,
36
+ index: i
37
+ });
38
+ return;
39
+ }
40
+ var newVal = oldVal === null || oldVal === void 0 ? void 0 : oldVal[index];
41
+ oldData.push(_objectSpread(_objectSpread({}, newVal), {}, {
42
+ index: i
43
+ }));
44
+ });
45
+ return {
46
+ oldData: oldData,
47
+ newData: newData
48
+ };
49
+ };
50
+ var single = function single(_ref2) {
51
+ var options = _ref2.options,
52
+ value = _ref2.value;
53
+ var newOptions = options || [];
54
+ var index = newOptions.findIndex(function (item) {
55
+ return item.value === value;
56
+ });
57
+ if (index !== -1) {
58
+ var label = newOptions[index].label;
59
+ return {
60
+ value: value,
61
+ label: label
62
+ };
63
+ }
64
+ return {
65
+ value: value,
66
+ label: value,
67
+ noMatch: true
68
+ };
69
+ };
70
+ var resultProvider = function resultProvider(newData, oldData, value) {
71
+ var newResult = new Array(value.length);
72
+ oldData.forEach(function (item) {
73
+ var index = item.index,
74
+ val = _objectWithoutProperties(item, _excluded);
75
+ newResult[index] = _objectSpread({}, val);
76
+ });
77
+ newData.forEach(function (item) {
78
+ var index = item.index,
79
+ val = _objectWithoutProperties(item, _excluded2);
80
+ newResult[index] = _objectSpread({}, val);
81
+ });
82
+ return newResult;
83
+ };
84
+ var multiple = function multiple(_ref3, oldVal) {
85
+ var options = _ref3.options,
86
+ value = _ref3.value;
87
+ var _sourceDataProvider = sourceDataProvider({
88
+ value: value
89
+ }, oldVal),
90
+ newData = _sourceDataProvider.newData,
91
+ oldData = _sourceDataProvider.oldData;
92
+ var newMatchVal = newData.map(function (item) {
93
+ var itemVal = item.value,
94
+ index = item.index;
95
+ var newItem = single({
96
+ options: options,
97
+ value: itemVal
98
+ });
99
+ return _objectSpread(_objectSpread({}, newItem), {}, {
100
+ index: index
101
+ });
102
+ });
103
+ return resultProvider(newMatchVal, oldData, value);
104
+ };
105
+ var tag = function tag(_ref4, oldVal) {
106
+ var options = _ref4.options,
107
+ value = _ref4.value;
108
+ var _sourceDataProvider2 = sourceDataProvider({
109
+ options: options,
110
+ value: value
111
+ }, oldVal),
112
+ newData = _sourceDataProvider2.newData,
113
+ oldData = _sourceDataProvider2.oldData;
114
+ return resultProvider(newData, oldData, value);
115
+ };
116
+ var matchNotFind = function matchNotFind(_ref5, oldVale) {
117
+ var options = _ref5.options,
118
+ value = _ref5.value,
119
+ mode = _ref5.mode;
120
+ if (!mode) {
121
+ return single({
122
+ options: options,
123
+ value: value
124
+ });
125
+ }
126
+ if (mode === "multiple") {
127
+ return multiple({
128
+ options: options,
129
+ value: value
130
+ }, oldVale) || [];
131
+ }
132
+ return tag({
133
+ options: options,
134
+ value: value
135
+ }, oldVale);
136
+ };
137
+
138
+ exports.matchNotFind = matchNotFind;
139
+ // powered by h
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hw-component/form",
3
- "version": "1.6.3",
3
+ "version": "1.6.5",
4
4
  "description": "基于antd二次开发",
5
5
  "repository": {
6
6
  "type": "git",
@@ -1,116 +1,7 @@
1
- import type {
2
- OptionType,
3
- PartialHSelectProps,
4
- } from "@/components/Select/modal";
1
+ import type { PartialHSelectProps } from "@/components/Select/modal";
5
2
  import { useEffect, useState } from "react";
6
- import type { FilterDataModal } from "@/components/Select/modal";
7
3
  import { itemOpProvider } from "../utils";
8
-
9
- const single = ({ options, value, noMatchItemRender }: PartialHSelectProps) => {
10
- const newOptions = options || [];
11
- const index = newOptions.findIndex((item) => {
12
- return item.value === value;
13
- });
14
- if (index !== -1) {
15
- const { label } = newOptions[index];
16
- return {
17
- value,
18
- label,
19
- };
20
- }
21
- return {
22
- value,
23
- label: noMatchItemRender?.({ value }) || value,
24
- };
25
- }; //单选模式
26
- const findNewValInOldVal = (val, oldVal?: any[]) => {
27
- if (!oldVal) {
28
- return -1;
29
- }
30
- return oldVal.findIndex((item) => {
31
- return item.value === val;
32
- });
33
- }; //查询新增的 value
34
- const sourceDataProvider = (
35
- { options, value }: PartialHSelectProps,
36
- oldVal?: any[]
37
- ) => {
38
- const oldData: FilterDataModal[] = [];
39
- const newData: FilterDataModal[] = [];
40
- value.forEach((item, i) => {
41
- const index = findNewValInOldVal(item, oldVal);
42
- if (index === -1) {
43
- newData.push({
44
- value: item,
45
- index: i,
46
- });
47
- return;
48
- }
49
- const newVal = oldVal?.[index];
50
- oldData.push({
51
- ...newVal,
52
- index: i,
53
- });
54
- });
55
- return {
56
- oldData,
57
- newData,
58
- };
59
- }; //创建新旧数据
60
- const resultProvider = (
61
- newData: FilterDataModal[],
62
- oldData: FilterDataModal[],
63
- value: any[]
64
- ) => {
65
- const newResult = new Array(value.length);
66
- oldData.forEach((item) => {
67
- const { index, ...val } = item;
68
- newResult[index] = {
69
- ...val,
70
- };
71
- });
72
- newData.forEach((item) => {
73
- const { index, ...val } = item;
74
- newResult[index] = {
75
- ...val,
76
- };
77
- });
78
- return newResult;
79
- };
80
- const multiple = (
81
- { options, value, noMatchItemRender }: PartialHSelectProps,
82
- oldVal?: any[]
83
- ) => {
84
- const { newData, oldData } = sourceDataProvider({ options, value }, oldVal);
85
- const newMatchVal = newData.map((item) => {
86
- const { value: itemVal, index } = item;
87
- const newItem = single({ options, value: itemVal, noMatchItemRender });
88
- return {
89
- ...newItem,
90
- index,
91
- };
92
- });
93
- return resultProvider(newMatchVal, oldData, value);
94
- }; //多选
95
- const tag = ({ options, value }: PartialHSelectProps, oldVal?: any[]) => {
96
- const { newData, oldData } = sourceDataProvider({ options, value }, oldVal);
97
- return resultProvider(newData, oldData, value);
98
- }; //tags模式
99
- const matchNotFind = (
100
- { options, value, mode, noMatchItemRender ,labelInValue}: PartialHSelectProps,
101
- oldVale?: OptionType[]
102
- ) => {
103
- if (labelInValue){
104
- return value;
105
- }
106
- if (!mode) {
107
- return single({ options, value, noMatchItemRender });
108
- }
109
- if (mode === "multiple") {
110
- return multiple({ options, value, noMatchItemRender }, oldVale) || [];
111
- }
112
- return tag({ options, value }, oldVale);
113
- }; //不匹配
4
+ import { matchNotFind } from "./util";
114
5
  export const useValueChange = (params: PartialHSelectProps) => {
115
6
  const {
116
7
  labelInValue,
@@ -158,9 +49,36 @@ export const useValueChange = (params: PartialHSelectProps) => {
158
49
  return matchNotFind(params, oldVale);
159
50
  });
160
51
  }
161
- }, [value, options, mode, noMatchItemRender,labelInValue]);
52
+ }, [value, options, mode]);
53
+ const itemMaker = (item) => {
54
+ const { value: itemVal, label, noMatch } = item;
55
+ if (noMatch) {
56
+ return {
57
+ value: itemVal,
58
+ label: noMatchItemRender?.({ value }) || value,
59
+ };
60
+ }
61
+ return {
62
+ value: itemVal,
63
+ label,
64
+ };
65
+ };
66
+ const valuesMaker = () => {
67
+ if (labelInValue) {
68
+ return val;
69
+ }
70
+ if (!val) {
71
+ return val;
72
+ }
73
+ if (Array.isArray(val)) {
74
+ return val.map((item) => {
75
+ return itemMaker(item);
76
+ });
77
+ }
78
+ return itemMaker(val);
79
+ };
162
80
  return {
163
- val,
81
+ val: valuesMaker(),
164
82
  change,
165
83
  };
166
84
  };
@@ -1,80 +1,77 @@
1
- import {useEffect, useMemo, useState} from "react";
2
- import type {
3
- OptionType,
4
- PartialHSelectProps,
5
- } from "../modal";
1
+ import { useEffect, useMemo, useState } from "react";
2
+ import type { OptionType, PartialHSelectProps } from "../modal";
6
3
  import { useRequest } from "ahooks";
7
4
  import { useFormContext } from "../../Form/Context";
8
- import {OptionsPageResultModal} from "../modal";
5
+ import type { OptionsPageResultModal } from "../modal";
9
6
 
10
7
  interface ParamsModal {
11
8
  options?: OptionType[];
12
9
  params?: any;
13
10
  }
14
- const useSaveParams=()=>{
15
- const saveParams=useMemo(()=>{
11
+ const useSaveParams = () => {
12
+ const saveParams = useMemo(() => {
16
13
  return {
17
- listParams:{
18
- isEnd:false,
19
- page:1,
20
- size:100,
21
- reqParams:{}
22
- }
23
- }
24
- },[]);
25
- const resetSaveParams=()=>{
26
- saveParams.listParams={
27
- isEnd:false,
28
- page:1,
29
- size:100,
30
- reqParams:{}
31
- }
32
- }
33
- const nextPage=(data:OptionsPageResultModal)=>{
34
- const {size,total,page}=data;
35
- saveParams.listParams.size=size;
36
- saveParams.listParams.page=page+1;
37
- saveParams.listParams.isEnd=size*page>=total;
38
- }
14
+ listParams: {
15
+ isEnd: false,
16
+ page: 1,
17
+ size: 100,
18
+ reqParams: {},
19
+ },
20
+ };
21
+ }, []);
22
+ const resetSaveParams = () => {
23
+ saveParams.listParams = {
24
+ isEnd: false,
25
+ page: 1,
26
+ size: 100,
27
+ reqParams: {},
28
+ };
29
+ };
30
+ const nextPage = (data: OptionsPageResultModal) => {
31
+ const { size, total, page } = data;
32
+ saveParams.listParams.size = size;
33
+ saveParams.listParams.page = page + 1;
34
+ saveParams.listParams.isEnd = size * page >= total;
35
+ };
39
36
  return {
40
37
  saveParams,
41
38
  resetSaveParams,
42
- nextPage
43
- }
44
- }
39
+ nextPage,
40
+ };
41
+ };
45
42
  const useOptionsRequest = ({
46
43
  request,
47
44
  dispatch = {},
48
45
  manual,
49
46
  options,
50
- isList
47
+ isList,
51
48
  }: PartialHSelectProps) => {
52
- const {saveParams,resetSaveParams,nextPage}=useSaveParams();
49
+ const { saveParams, resetSaveParams, nextPage } = useSaveParams();
53
50
  const { form } = useFormContext();
54
51
  const { manual: dispatchManual } = dispatch;
55
52
  const [data, setData] = useState<OptionType[] | undefined>();
56
- const paramsProvider=(params:Record<string, any>)=>{
57
- if (isList){
58
- const page=saveParams.listParams.page;
59
- const size=saveParams.listParams.size;
60
- return {
53
+ const paramsProvider = (params: Record<string, any>) => {
54
+ if (isList) {
55
+ const page = saveParams.listParams.page;
56
+ const size = saveParams.listParams.size;
57
+ return {
61
58
  ...params,
62
59
  page,
63
- size
64
- }
60
+ size,
61
+ };
65
62
  }
66
63
  return params;
67
- }
68
- const { run, loading, error } = useRequest(
69
- async (requestParams = {},type="init") => {
70
- const { params = saveParams.listParams.reqParams, values} = requestParams;
64
+ };
65
+ const { run:listRun, loading, error } = useRequest(
66
+ async (requestParams = {}, type = "init") => {
67
+ const { params = saveParams.listParams.reqParams, values } = requestParams;
71
68
  const formData = values || form?.getFieldsValue();
72
69
  if (type === "init") {
73
70
  resetSaveParams();
74
71
  setData(undefined);
75
72
  }
76
- saveParams.listParams.reqParams=params;
77
- if (isList&&saveParams.listParams.isEnd){
73
+ saveParams.listParams.reqParams = params;
74
+ if (isList && saveParams.listParams.isEnd) {
78
75
  return data;
79
76
  }
80
77
  if (request) {
@@ -86,20 +83,23 @@ const useOptionsRequest = ({
86
83
  manual: dispatchManual === false ? true : manual,
87
84
  debounceInterval: 300,
88
85
  onSuccess: (resultData) => {
89
- if (Array.isArray(resultData)||!resultData){
90
- return setData(resultData);
86
+ if (Array.isArray(resultData) || !resultData) {
87
+ return setData(resultData);
91
88
  }
92
- const oldData=data||[];
93
- const {data:resultOption}=resultData;
94
- const op = resultOption||[];
89
+ const oldData = data || [];
90
+ const { data: resultOption } = resultData;
91
+ const op = resultOption || [];
95
92
  nextPage(resultData);
96
- setData([
97
- ...oldData,
98
- ...op
99
- ]);
93
+ setData([...oldData, ...op]);
100
94
  },
101
95
  }
102
96
  );
97
+ const run=(requestParams = {}, type = "init")=>{
98
+ if (loading){
99
+ return;
100
+ }
101
+ listRun(requestParams,type);
102
+ }
103
103
  return {
104
104
  loading,
105
105
  run,
@@ -124,7 +124,7 @@ export const useSelectReq = ({
124
124
  dispatch,
125
125
  manual,
126
126
  options,
127
- isList
127
+ isList,
128
128
  });
129
129
  const reload = (reloadParams?: ParamsModal) => {
130
130
  const { options: changeOpts, params } = reloadParams || {};
@@ -145,13 +145,16 @@ export const useSelectReq = ({
145
145
  setData(options);
146
146
  }
147
147
  }, [options]);
148
- const onPopupScroll=(e)=>{
149
- const {target} = e;
150
- const changeTarget=(target as any);
151
- if (changeTarget.scrollTop + changeTarget.offsetHeight === changeTarget.scrollHeight) {
152
- run({},"page");
148
+ const onPopupScroll = (e) => {
149
+ const { target } = e;
150
+ const changeTarget = target as any;
151
+ if (
152
+ changeTarget.scrollTop + changeTarget.offsetHeight ===
153
+ changeTarget.scrollHeight
154
+ ) {
155
+ run({}, "page");
153
156
  }
154
- }
157
+ };
155
158
  const mathShowSearch = showSearch || serviceSearch;
156
159
  return {
157
160
  run,
@@ -161,7 +164,7 @@ export const useSelectReq = ({
161
164
  onSearch: mathShowSearch ? onSearch : propsOnSearch,
162
165
  mathShowSearch: showSearch || serviceSearch,
163
166
  reload,
164
- onPopupScroll:isList?onPopupScroll:undefined
167
+ onPopupScroll: isList ? onPopupScroll : undefined,
165
168
  };
166
169
  };
167
170
 
@@ -170,7 +173,7 @@ export const useFilterOption = ({
170
173
  serviceSearch,
171
174
  }: PartialHSelectProps) => {
172
175
  if (serviceSearch) {
173
- return false
176
+ return false;
174
177
  }
175
178
  return filterOption;
176
179
  };
@@ -0,0 +1,106 @@
1
+ import type {
2
+ FilterDataModal,
3
+ OptionType,
4
+ PartialHSelectProps,
5
+ } from "@/components/Select/modal";
6
+
7
+ const findNewValInOldVal = (val, oldVal?: any[]) => {
8
+ if (!oldVal) {
9
+ return -1;
10
+ }
11
+ return oldVal.findIndex((item) => {
12
+ return item.value === val;
13
+ });
14
+ }; //查询新增的 value
15
+
16
+ const sourceDataProvider = ({ value }: PartialHSelectProps, oldVal?: any[]) => {
17
+ const oldData: FilterDataModal[] = [];
18
+ const newData: FilterDataModal[] = [];
19
+ value.forEach((item, i) => {
20
+ const index = findNewValInOldVal(item, oldVal);
21
+ if (index === -1) {
22
+ newData.push({
23
+ value: item,
24
+ index: i,
25
+ });
26
+ return;
27
+ }
28
+ const newVal = oldVal?.[index];
29
+ oldData.push({
30
+ ...newVal,
31
+ index: i,
32
+ });
33
+ });
34
+ return {
35
+ oldData,
36
+ newData,
37
+ };
38
+ }; //创建新旧数据
39
+
40
+ const single = ({ options, value }: PartialHSelectProps) => {
41
+ const newOptions = options || [];
42
+ const index = newOptions.findIndex((item) => {
43
+ return item.value === value;
44
+ });
45
+ if (index !== -1) {
46
+ const { label } = newOptions[index];
47
+ return {
48
+ value,
49
+ label,
50
+ };
51
+ }
52
+ return {
53
+ value,
54
+ label: value,
55
+ noMatch: true,
56
+ };
57
+ }; //单选模式
58
+ const resultProvider = (
59
+ newData: FilterDataModal[],
60
+ oldData: FilterDataModal[],
61
+ value: any[]
62
+ ) => {
63
+ const newResult = new Array(value.length);
64
+ oldData.forEach((item) => {
65
+ const { index, ...val } = item;
66
+ newResult[index] = {
67
+ ...val,
68
+ };
69
+ });
70
+ newData.forEach((item) => {
71
+ const { index, ...val } = item;
72
+ newResult[index] = {
73
+ ...val,
74
+ };
75
+ });
76
+ return newResult;
77
+ };
78
+ const multiple = ({ options, value }: PartialHSelectProps, oldVal?: any[]) => {
79
+ const { newData, oldData } = sourceDataProvider({ value }, oldVal);
80
+ const newMatchVal = newData.map((item) => {
81
+ const { value: itemVal, index } = item;
82
+ const newItem = single({ options, value: itemVal });
83
+ return {
84
+ ...newItem,
85
+ index,
86
+ };
87
+ });
88
+ return resultProvider(newMatchVal, oldData, value);
89
+ }; //多选
90
+
91
+ const tag = ({ options, value }: PartialHSelectProps, oldVal?: any[]) => {
92
+ const { newData, oldData } = sourceDataProvider({ options, value }, oldVal);
93
+ return resultProvider(newData, oldData, value);
94
+ }; //tags模式
95
+ export const matchNotFind = (
96
+ { options, value, mode }: PartialHSelectProps,
97
+ oldVale?: OptionType[]
98
+ ) => {
99
+ if (!mode) {
100
+ return single({ options, value });
101
+ }
102
+ if (mode === "multiple") {
103
+ return multiple({ options, value }, oldVale) || [];
104
+ }
105
+ return tag({ options, value }, oldVale);
106
+ }; //不匹配
@@ -1,6 +1,6 @@
1
1
  import { Select } from "antd";
2
2
  import React from "react";
3
- import {useFilterOption, useSelectReq} from "./hooks/norHooks";
3
+ import { useFilterOption, useSelectReq } from "./hooks/norHooks";
4
4
  import { useValueChange } from "./hooks/changeHooks";
5
5
  import type { HSelectProps } from "./modal";
6
6
  import { defaultModeConfig, defaultSelectConfig } from "./defaultConfig";
@@ -34,7 +34,7 @@ const Index: React.FC<HSelectProps> = ({
34
34
  addFormat,
35
35
  dispatch,
36
36
  isList,
37
- onPopupScroll:propsOnPopupScroll,
37
+ onPopupScroll: propsOnPopupScroll,
38
38
  ...props
39
39
  }) => {
40
40
  const { icon, render } = modeConfig?.[mode || ""] || {};
@@ -48,7 +48,7 @@ const Index: React.FC<HSelectProps> = ({
48
48
  onSearch,
49
49
  mathShowSearch,
50
50
  reload,
51
- onPopupScroll
51
+ onPopupScroll,
52
52
  } = useSelectReq({
53
53
  options,
54
54
  manual,
@@ -58,7 +58,7 @@ const Index: React.FC<HSelectProps> = ({
58
58
  showSearch,
59
59
  onSearch: propsOnSearch,
60
60
  dispatch,
61
- isList
61
+ isList,
62
62
  }); //options
63
63
 
64
64
  const data = useChangeOptions({ options: resultData, fieldNames });
@@ -107,7 +107,7 @@ const Index: React.FC<HSelectProps> = ({
107
107
  filterOption={selfFilterOption}
108
108
  showSearch={mathShowSearch}
109
109
  labelInValue={true}
110
- onPopupScroll={propsOnPopupScroll||onPopupScroll}
110
+ onPopupScroll={propsOnPopupScroll || onPopupScroll}
111
111
  {...props}
112
112
  >
113
113
  {data?.map((item) => {
@@ -20,11 +20,9 @@ export interface OptionsPageResultModal {
20
20
  page: number;
21
21
  size: number;
22
22
  data: OptionsListType;
23
- total:number;
23
+ total: number;
24
24
  }
25
- export type OptionsDataType =
26
- | OptionsListType
27
- | OptionsPageResultModal
25
+ export type OptionsDataType = OptionsListType | OptionsPageResultModal;
28
26
  export interface HSelectProps
29
27
  extends Omit<SelectProps, "options" | "placeholder"> {
30
28
  style?: React.CSSProperties;
@@ -40,7 +38,7 @@ export interface HSelectProps
40
38
  addFormat?: (format: Record<string, addFormatItemModal>) => void;
41
39
  placeholder?: string;
42
40
  dispatch?: DispatchModal;
43
- isList?:boolean
41
+ isList?: boolean;
44
42
  }
45
43
  export interface FilterDataModal {
46
44
  value: any;