arrayField.tsx 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 initValueAdapter = (initValue: any) => {
  28. const iv: any[] = [];
  29. if (Array.isArray(initValue)) {
  30. return initValue;
  31. } else {
  32. warning(
  33. !isUndefined(initValue),
  34. '[Semi Form ArrayField] initValue of ArrayField must be an array. Please check the type of your props'
  35. );
  36. return iv;
  37. }
  38. };
  39. /**
  40. *
  41. * @param {any[]} value
  42. * @param {string[]} oldKeys
  43. * @param {any[]} cacheValue
  44. * @returns string[]
  45. *
  46. */
  47. const generateKeys = (value: any[] = [], oldKeys?: string[], cacheValues: any[] = []) => {
  48. const val = initValueAdapter(value);
  49. const newKeys = getUuidByArray(val);
  50. // const keys = newKeys.map((key, i) => (oldKeys && oldKeys[i] ? oldKeys[i] : key));
  51. const keys = [];
  52. value.forEach((newRow, i) => {
  53. const cacheRow = get(cacheValues, i);
  54. if (!isEqual(newRow, cacheRow)) {
  55. keys[i] = newKeys[i];
  56. } else {
  57. keys[i] = oldKeys && oldKeys[i] ? oldKeys[i] : newKeys[i];
  58. }
  59. });
  60. return keys;
  61. };
  62. class ArrayFieldComponent extends Component<ArrayFieldProps, ArrayFieldState> {
  63. static contextType = FormUpdaterContext;
  64. hasMounted: boolean;
  65. context: FormUpdaterContextType;
  66. constructor(props: ArrayFieldProps, context: FormUpdaterContextType) {
  67. super(props, context);
  68. const { field } = this.props;
  69. const initValueInArrayFieldProps = this.props.initValue;
  70. const formProps = context.getFormProps(['initValues']);
  71. const initValueInFormProps = get(formProps, field);
  72. const initValueInFormState = context.getValue(field);
  73. // const initValue = initValueInArrayFieldProps || initValueInFormState;
  74. const initValue = initValueInArrayFieldProps || initValueInFormProps || initValueInFormState;
  75. this.hasMounted = Boolean(context.getArrayField(field));
  76. this.state = {
  77. keys: generateKeys(initValue),
  78. };
  79. this.add = this.add.bind(this);
  80. this.addWithInitValue = this.addWithInitValue.bind(this);
  81. this.remove = this.remove.bind(this);
  82. // /*
  83. // If updateKey exists, it means that the arrayField (usually a nested ArrayField not at the first level) is only re-mounted due to setValues,
  84. // and the fields it contains do not need to consume initValue
  85. // */
  86. // // whether the fields inside arrayField should use props.initValue in current render process
  87. // Separate the arrays that reset and the usual add and remove modify, otherwise they will affect each other
  88. const initValueCopyForFormState = cloneDeep(initValue);
  89. const initValueCopyForReset = cloneDeep(initValue);
  90. if (!this.hasMounted) {
  91. context.registerArrayField(field, { initValue: initValueCopyForReset, forceUpdate: this.forceKeysUpdate });
  92. context.updateStateValue(field, initValueCopyForFormState, { notNotify: true, notUpdate: true });
  93. } else {
  94. context.registerArrayField(field, { forceUpdate: this.forceKeysUpdate });
  95. }
  96. }
  97. componentDidMount() {
  98. // const { field } = this.props;
  99. // const initValueInArrayFieldProps = this.props.initValue;
  100. // const updater = this.context;
  101. // const formProps = updater.getFormProps(['initValues']);
  102. // const initValueInFormProps = get(formProps, field);
  103. // const initValueInFormState = updater.getValue(field);
  104. // const initValue = initValueInArrayFieldProps || initValueInFormProps || initValueInFormState;
  105. // const initValueCopyForFormState = cloneDeep(initValue);
  106. // const initValueCopyForReset = cloneDeep(initValue);
  107. // // 如果首次挂载,应该使用初始值,如果不是首次挂载,例如嵌套场景下,level 1 keys变更导致的 level 2子级重新挂载,那应该直接使用formState 中的值, 且无需注册 initValue
  108. // if (!this.hasMounted) {
  109. // updater.registerArrayField(field, { initValue: initValueCopyForReset, forceUpdate: this.forceKeysUpdate });
  110. // updater.updateStateValue(field, initValueCopyForFormState, { notNotify: true, notUpdate: true });
  111. // } else {
  112. // updater.registerArrayField(field, { forceUpdate: this.forceKeysUpdate });
  113. // }
  114. }
  115. componentWillUnmount() {
  116. const updater = this.context;
  117. const { field } = this.props;
  118. const hasParentArrayField = updater.getParentArrayField(field);
  119. const size = updater.getParentArrayField(field).size;
  120. if (Boolean(size)) {
  121. // if is parant arraField, need to unregister nested arrayField here
  122. // 嵌套的ArrayField在父级仍存在时,不需要走卸载, 统一在父级ArrayField做卸载
  123. // console.log('嵌套的ArrayField在父级仍存在时,不需要自己走卸载, 统一在父级ArrayField做卸载', field);
  124. } else {
  125. updater.unRegisterArrayField(field);
  126. }
  127. }
  128. forceKeysUpdate = ({ newValue, oldValue }): void => {
  129. const updater = this.context;
  130. const { field } = this.props;
  131. const { keys } = this.state;
  132. const fieldValues = newValue ? newValue : updater.getValue(field);
  133. const newKeys = generateKeys(fieldValues, keys, oldValue);
  134. // eslint-disable-next-line
  135. this.setState({ keys: newKeys })
  136. }
  137. add() {
  138. const { keys } = this.state;
  139. keys.push(getUuidv4());
  140. // this.shouldUseInitValue = true;
  141. // TODO allowEmpty 为 false 的情况下
  142. this.setState({ keys });
  143. }
  144. addWithInitValue(lineObject: Record<string, any>) {
  145. const updater = this.context;
  146. const { field } = this.props;
  147. const oldArrayFieldVal = updater.getValue(field) ? updater.getValue(field) : [];
  148. const newArrayFieldVal = oldArrayFieldVal.slice();
  149. newArrayFieldVal.push(lineObject);
  150. updater.updateStateValue(field, newArrayFieldVal, {});
  151. updater.updateArrayField(field, { newValue: newArrayFieldVal, oldValue: oldArrayFieldVal });
  152. }
  153. remove(i: number) {
  154. const updater = this.context;
  155. const { keys } = this.state;
  156. const { field } = this.props;
  157. const newKeys = filterArrayByIndex(keys, i);
  158. // 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
  159. let newArrayFieldError = updater.getError(field);
  160. const opts = { notNotify: true, notUpdate: true };
  161. if (Array.isArray(newArrayFieldError)) {
  162. newArrayFieldError = newArrayFieldError.slice();
  163. newArrayFieldError.splice(i, 1);
  164. updater.updateStateError(field, newArrayFieldError, opts);
  165. }
  166. // if (Array.isArray(newArrayFieldTouched)) {
  167. // newArrayFieldTouched = newArrayFieldTouched.slice();
  168. // newArrayFieldTouched.splice(i, 1);
  169. // updater.updateStateTouched(field, newArrayFieldTouched, opts);
  170. // }
  171. let newArrayFieldValue = updater.getValue(field);
  172. if (Array.isArray(newArrayFieldValue)) {
  173. newArrayFieldValue = newArrayFieldValue.slice();
  174. newArrayFieldValue.splice(i, 1);
  175. updater.updateStateValue(field, newArrayFieldValue);
  176. }
  177. this.setState({ keys: newKeys });
  178. }
  179. render() {
  180. const { children, field } = this.props;
  181. const { keys } = this.state;
  182. const arrayFields = keys.map((key, i) => ({
  183. key,
  184. field: `${field}[${i}]`,
  185. remove: () => this.remove(i),
  186. }));
  187. const { add } = this;
  188. const { addWithInitValue } = this;
  189. const contextVal = {
  190. isInArrayField: true,
  191. };
  192. return (
  193. <ArrayFieldContext.Provider value={contextVal}>
  194. {children({ arrayFields, add, addWithInitValue })}
  195. </ArrayFieldContext.Provider>
  196. );
  197. }
  198. }
  199. export default ArrayFieldComponent;