arrayField.tsx 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /* eslint-disable react/destructuring-assignment */
  2. import React, { Component } from 'react';
  3. import { getUuidv4 } from '@douyinfe/semi-foundation/utils/uuid';
  4. import { cloneDeep, isUndefined, isEqual, get } from 'lodash';
  5. import { FormUpdaterContext, ArrayFieldContext } from './context';
  6. import warning from '@douyinfe/semi-foundation/utils/warning';
  7. import type { ArrayFieldStaff, FormUpdaterContextType } from '@douyinfe/semi-foundation/form/interface';
  8. export interface ArrayFieldProps {
  9. initValue?: any[];
  10. field?: string;
  11. children?: (props: ArrayFieldChildrenProps) => React.ReactNode
  12. }
  13. export interface ArrayFieldChildrenProps {
  14. arrayFields: {
  15. key: string;
  16. field: string;
  17. remove: () => void
  18. }[];
  19. add: () => void;
  20. addWithInitValue: (lineObject: Record<string, any>) => void
  21. }
  22. export interface ArrayFieldState {
  23. keys: string[]
  24. }
  25. const filterArrayByIndex = (array: any[], index: number) => array.filter((item, i) => i !== index);
  26. const getUuidByArray = (array: any[]) => array.map(() => getUuidv4());
  27. // const getUpdateKey = (arrayField: ArrayFieldStaff): string | undefined => {
  28. // if (!arrayField) {
  29. // return undefined;
  30. // }
  31. // // TODO
  32. // if (arrayField && arrayField.updateKey) {
  33. // return arrayField.updateKey;
  34. // }
  35. // return undefined;
  36. // };
  37. const initValueAdapter = (initValue: any) => {
  38. const iv: any[] = [];
  39. if (Array.isArray(initValue)) {
  40. return initValue;
  41. } else {
  42. warning(
  43. !isUndefined(initValue),
  44. '[Semi Form ArrayField] initValue of ArrayField must be an array. Please check the type of your props'
  45. );
  46. return iv;
  47. }
  48. };
  49. /**
  50. *
  51. * @param {any[]} value
  52. * @param {string[]} oldKeys
  53. * @returns string[]
  54. */
  55. const generateKeys = (value : any[] = [], oldKeys?: string[], cacheValue: any[] = []) => {
  56. const val = initValueAdapter(value);
  57. const newKeys = getUuidByArray(val);
  58. const keys = [];
  59. value.forEach((newRow, i) => {
  60. const cacheRow = get(cacheValue, i);
  61. if (!isEqual(newRow, cacheRow)) {
  62. keys[i] = newKeys[i];
  63. } else {
  64. keys[i] = oldKeys && oldKeys[i] ? oldKeys[i] : newKeys[i];
  65. }
  66. });
  67. return keys;
  68. };
  69. class ArrayFieldComponent extends Component<ArrayFieldProps, ArrayFieldState> {
  70. static contextType = FormUpdaterContext;
  71. cacheFieldValues: any[] | null;
  72. shouldUseInitValue: boolean;
  73. // cacheUpdateKey: string;
  74. context: FormUpdaterContextType;
  75. constructor(props: ArrayFieldProps, context: FormUpdaterContextType) {
  76. super(props, context);
  77. const initValueInProps = this.props.initValue;
  78. const { field } = this.props;
  79. const initValueInForm = context.getValue(field);
  80. const initValue = initValueInProps || initValueInForm;
  81. this.state = {
  82. keys: generateKeys(initValue),
  83. };
  84. this.add = this.add.bind(this);
  85. this.addWithInitValue = this.addWithInitValue.bind(this);
  86. this.remove = this.remove.bind(this);
  87. this.cacheFieldValues = null;
  88. // this.cacheUpdateKey = null;
  89. /*
  90. If updateKey exists, it means that the arrayField (usually a nested ArrayField not at the first level) is only re-mounted due to setValues,
  91. and the fields it contains do not need to consume initValue
  92. */
  93. // whether the fields inside arrayField should use props.initValue in current render process
  94. this.shouldUseInitValue = !context.getArrayField(field);
  95. // Separate the arrays that reset and the usual add and remove modify, otherwise they will affect each other
  96. const initValueCopyForFormState = cloneDeep(initValue);
  97. const initValueCopyForReset = cloneDeep(initValue);
  98. context.registerArrayField(field, initValueCopyForReset);
  99. context.updateStateValue(field, initValueCopyForFormState, { notNotify: true, notUpdate: true });
  100. }
  101. componentDidMount(): void {
  102. const { field } = this.props;
  103. const updater = this.context;
  104. updater.updateArrayField(field, { forceUpdate: this.forceUpdate });
  105. }
  106. componentWillUnmount() {
  107. const updater = this.context;
  108. const { field } = this.props;
  109. updater.unRegisterArrayField(field);
  110. }
  111. // componentDidUpdate() {
  112. // const updater = this.context;
  113. // // const { field } = this.props;
  114. // // const { keys } = this.state;
  115. // // const fieldValues = updater.getValue(field);
  116. // // const updateKey = getUpdateKey(updater.getArrayField(field));
  117. // // // when update form outside, like use formApi.setValue('field', [{newItem1, newItem2}]), formApi.setValues
  118. // // // re generate keys to update arrayField;
  119. // // if (updateKey !== this.cacheUpdateKey) {
  120. // // const newKeys = generateKeys(fieldValues, keys);
  121. // // // eslint-disable-next-line
  122. // // this.setState({ keys: newKeys });
  123. // // this.cacheUpdateKey = updateKey;
  124. // // if (this.cacheUpdateKey !== null) {
  125. // // this.shouldUseInitValue = false;
  126. // // }
  127. // // } else {
  128. // // console.log('not update');
  129. // // }
  130. // }
  131. forceUpdate = (value?: any): void => {
  132. // const updater = this.context;
  133. // const { field } = this.props;
  134. // const { keys } = this.state;
  135. // const fieldValues = value ? value : updater.getValue(field);
  136. // const updateKey = getUpdateKey(updater.getArrayField(field));
  137. // if (updateKey !== this.cacheUpdateKey) {
  138. // const newKeys = generateKeys(fieldValues, keys);
  139. // // eslint-disable-next-line
  140. // this.setState({ keys: newKeys });
  141. // this.cacheUpdateKey = updateKey;
  142. // if (this.cacheUpdateKey !== null) {
  143. // this.shouldUseInitValue = false;
  144. // }
  145. // } else {
  146. // // console.log('not update');
  147. // }
  148. const updater = this.context;
  149. const { field } = this.props;
  150. const { keys } = this.state;
  151. const fieldValues = value ? value : updater.getValue(field);
  152. // TODO fieldValues 如果长度相同,keys目前仍会相同,需要为新的
  153. const newKeys = generateKeys(fieldValues, keys, this.cacheFieldValues);
  154. // eslint-disable-next-line
  155. this.setState({ keys: newKeys });
  156. this.cacheFieldValues = value;
  157. this.shouldUseInitValue = false;
  158. }
  159. add() {
  160. const { keys } = this.state;
  161. keys.push(getUuidv4());
  162. this.shouldUseInitValue = true;
  163. // TODO allowEmpty 为 false 的情况下
  164. this.setState({ keys });
  165. }
  166. addWithInitValue(lineObject: Record<string, any>) {
  167. const updater = this.context;
  168. const { field } = this.props;
  169. const newArrayFieldVal = updater.getValue(field) ? updater.getValue(field).slice() : [];
  170. newArrayFieldVal.push(lineObject);
  171. updater.updateStateValue(field, newArrayFieldVal, {});
  172. updater.updateArrayField(field, { updateKey: new Date().valueOf() });
  173. }
  174. remove(i: number) {
  175. const updater = this.context;
  176. const { keys } = this.state;
  177. const { field } = this.props;
  178. const newKeys = filterArrayByIndex(keys, i);
  179. // Make sure that all the keys in the line are removed, because some keys are not taken over by the field, only set in the initValue
  180. let newArrayFieldError = updater.getError(field);
  181. const opts = { notNotify: true, notUpdate: true };
  182. if (Array.isArray(newArrayFieldError)) {
  183. newArrayFieldError = newArrayFieldError.slice();
  184. newArrayFieldError.splice(i, 1);
  185. updater.updateStateError(field, newArrayFieldError, opts);
  186. }
  187. // if (Array.isArray(newArrayFieldTouched)) {
  188. // newArrayFieldTouched = newArrayFieldTouched.slice();
  189. // newArrayFieldTouched.splice(i, 1);
  190. // updater.updateStateTouched(field, newArrayFieldTouched, opts);
  191. // }
  192. let newArrayFieldValue = updater.getValue(field);
  193. if (Array.isArray(newArrayFieldValue)) {
  194. newArrayFieldValue = newArrayFieldValue.slice();
  195. newArrayFieldValue.splice(i, 1);
  196. updater.updateStateValue(field, newArrayFieldValue);
  197. }
  198. this.setState({ keys: newKeys });
  199. }
  200. render() {
  201. const { children, field } = this.props;
  202. const { keys } = this.state;
  203. const arrayFields = keys.map((key, i) => ({
  204. key,
  205. field: `${field}[${i}]`,
  206. remove: () => this.remove(i),
  207. }));
  208. const { add } = this;
  209. const { addWithInitValue } = this;
  210. const contextVal = {
  211. shouldUseInitValue: this.shouldUseInitValue,
  212. };
  213. return (
  214. <ArrayFieldContext.Provider value={contextVal}>
  215. {children({ arrayFields, add, addWithInitValue })}
  216. </ArrayFieldContext.Provider>
  217. );
  218. }
  219. }
  220. export default ArrayFieldComponent;