1
0

arrayField.tsx 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 } from 'lodash';
  5. import { FormUpdaterContext, ArrayFieldContext } from './context';
  6. import warning from '@douyinfe/semi-foundation/utils/warning';
  7. import { 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. if (arrayField && arrayField.updateKey) {
  32. return arrayField.updateKey;
  33. }
  34. return undefined;
  35. };
  36. const initValueAdapter = (initValue: any) => {
  37. const iv: any[] = [];
  38. if (Array.isArray(initValue)) {
  39. return initValue;
  40. } else {
  41. warning(
  42. !isUndefined(initValue),
  43. '[Semi Form ArrayField] initValue of ArrayField must be an array. Please check the type of your props'
  44. );
  45. return iv;
  46. }
  47. };
  48. /**
  49. *
  50. * @param {any[]} value
  51. * @param {string[]} oldKeys
  52. * @returns string[]
  53. */
  54. const generateKeys = (value: any[], oldKeys?: string[]) => {
  55. const val = initValueAdapter(value);
  56. const newKeys = getUuidByArray(val);
  57. // return newKeys;
  58. const keys = newKeys.map((key, i) => (oldKeys && oldKeys[i] ? oldKeys[i] : key));
  59. return keys;
  60. };
  61. class ArrayFieldComponent extends Component<ArrayFieldProps, ArrayFieldState> {
  62. static contextType = FormUpdaterContext;
  63. cacheFieldValues: any[];
  64. shouldUseInitValue: boolean;
  65. cacheUpdateKey: string;
  66. context: FormUpdaterContextType;
  67. constructor(props: ArrayFieldProps, context: FormUpdaterContextType) {
  68. super(props, context);
  69. const initValueInProps = this.props.initValue;
  70. const { field } = this.props;
  71. const initValueInForm = context.getValue(field);
  72. const initValue = initValueInProps || initValueInForm;
  73. this.state = {
  74. keys: generateKeys(initValue),
  75. };
  76. this.add = this.add.bind(this);
  77. this.addWithInitValue = this.addWithInitValue.bind(this);
  78. this.remove = this.remove.bind(this);
  79. this.cacheFieldValues = null;
  80. this.cacheUpdateKey = null;
  81. /*
  82. If updateKey exists, it means that the arrayField (usually a nested ArrayField not at the first level) is only re-mounted due to setValues,
  83. and the fields it contains do not need to consume initValue
  84. */
  85. // whether the fields inside arrayField should use props.initValue in current render process
  86. this.shouldUseInitValue = !context.getArrayField(field);
  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. context.registerArrayField(field, initValueCopyForReset);
  91. // register ArrayField will update state.updateKey to render, So there is no need to execute forceUpdate here
  92. context.updateStateValue(field, initValueCopyForFormState, { notNotify: true, notUpdate: true });
  93. }
  94. componentWillUnmount() {
  95. const updater = this.context;
  96. const { field } = this.props;
  97. updater.unRegisterArrayField(field);
  98. }
  99. componentDidUpdate() {
  100. const updater = this.context;
  101. const { field } = this.props;
  102. const { keys } = this.state;
  103. const fieldValues = updater.getValue(field);
  104. const updateKey = getUpdateKey(updater.getArrayField(field));
  105. // when update form outside, like use formApi.setValue('field', [{newItem1, newItem2}]), formApi.setValues
  106. // re generate keys to update arrayField;
  107. if (updateKey !== this.cacheUpdateKey) {
  108. const newKeys = generateKeys(fieldValues, keys);
  109. // eslint-disable-next-line
  110. this.setState({ keys: newKeys });
  111. this.cacheUpdateKey = updateKey;
  112. if (this.cacheUpdateKey !== null) {
  113. this.shouldUseInitValue = false;
  114. }
  115. }
  116. }
  117. add() {
  118. const { keys } = this.state;
  119. keys.push(getUuidv4());
  120. this.shouldUseInitValue = true;
  121. this.setState({ keys });
  122. }
  123. addWithInitValue(lineObject: Record<string, any>) {
  124. const updater = this.context;
  125. const { field } = this.props;
  126. const newArrayFieldVal = updater.getValue(field) ? updater.getValue(field).slice() : [];
  127. newArrayFieldVal.push(lineObject);
  128. updater.updateStateValue(field, newArrayFieldVal, {});
  129. updater.updateArrayField(field, { updateKey: new Date().valueOf() });
  130. }
  131. remove(i: number) {
  132. const updater = this.context;
  133. const { keys } = this.state;
  134. const { field } = this.props;
  135. const newKeys = filterArrayByIndex(keys, i);
  136. // 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
  137. let newArrayFieldError = updater.getError(field);
  138. const opts = { notNotify: true, notUpdate: true };
  139. if (Array.isArray(newArrayFieldError)) {
  140. newArrayFieldError = newArrayFieldError.slice();
  141. newArrayFieldError.splice(i, 1);
  142. updater.updateStateError(field, newArrayFieldError, opts);
  143. }
  144. // if (Array.isArray(newArrayFieldTouched)) {
  145. // newArrayFieldTouched = newArrayFieldTouched.slice();
  146. // newArrayFieldTouched.splice(i, 1);
  147. // updater.updateStateTouched(field, newArrayFieldTouched, opts);
  148. // }
  149. let newArrayFieldValue = updater.getValue(field);
  150. if (Array.isArray(newArrayFieldValue)) {
  151. newArrayFieldValue = newArrayFieldValue.slice();
  152. newArrayFieldValue.splice(i, 1);
  153. updater.updateStateValue(field, newArrayFieldValue);
  154. }
  155. this.setState({ keys: newKeys });
  156. }
  157. render() {
  158. const { children, field } = this.props;
  159. const { keys } = this.state;
  160. const arrayFields = keys.map((key, i) => ({
  161. // key: i,
  162. key,
  163. field: `${field}[${i}]`,
  164. remove: () => this.remove(i),
  165. }));
  166. const { add } = this;
  167. const { addWithInitValue } = this;
  168. const contextVal = {
  169. shouldUseInitValue: this.shouldUseInitValue,
  170. };
  171. return (
  172. <ArrayFieldContext.Provider value={contextVal}>
  173. {children({ arrayFields, add, addWithInitValue })}
  174. </ArrayFieldContext.Provider>
  175. );
  176. }
  177. }
  178. export default ArrayFieldComponent;