group.tsx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* eslint-disable react/destructuring-assignment */
  2. import React, { Component } from 'react';
  3. import classNames from 'classnames';
  4. import { isString } from 'lodash';
  5. import { isValid } from '@douyinfe/semi-foundation/form/utils';
  6. import { cssClasses } from '@douyinfe/semi-foundation/form/constants';
  7. import * as ObjectUtil from '@douyinfe/semi-foundation/utils/object';
  8. import ErrorMessage, { ReactFieldError } from './errorMessage';
  9. import Label, { LabelProps } from './label';
  10. import { FormUpdaterContext } from './context';
  11. import { useFormState } from './hooks/index';
  12. import InputGroup, { InputGroupProps as BacisInputGroupProps } from '../input/inputGroup';
  13. import { BaseFormProps, FormState } from './interface';
  14. import { FormUpdaterContextType } from '@douyinfe/semi-foundation/form/interface';
  15. import { Col, Row } from '../grid/index';
  16. interface GroupErrorProps {
  17. showValidateIcon?: boolean;
  18. isInInputGroup?: boolean;
  19. error?: ReactFieldError;
  20. fieldSet?: string[]
  21. }
  22. export interface InputGroupProps extends BacisInputGroupProps {
  23. label?: LabelProps;
  24. labelPosition?: 'left' | 'top';
  25. extraText?: React.ReactNode;
  26. extraTextPosition?: 'bottom' | 'middle'
  27. }
  28. const prefix = cssClasses.PREFIX;
  29. // Group component to remove Labels and ErrorMessages from its child fields
  30. // Unified insertion of Labels and ErrorMessages from the group level
  31. // Get Errors of all field in this group
  32. const GroupError = (props: GroupErrorProps) => {
  33. const { fieldSet } = props;
  34. const formState: FormState = useFormState();
  35. const error = fieldSet.map((field: string) => ObjectUtil.get(formState.errors, field));
  36. if (isValid(error)) {
  37. return null;
  38. }
  39. return (
  40. <ErrorMessage error={error} showValidateIcon={props.showValidateIcon} isInInputGroup={props.isInInputGroup} />
  41. );
  42. };
  43. class FormInputGroup extends Component<InputGroupProps> {
  44. static contextType = FormUpdaterContext;
  45. context: FormUpdaterContextType;
  46. renderLabel(label: LabelProps, formProps: BaseFormProps) {
  47. if (label) {
  48. if (isString(label)) {
  49. return (<Label width={formProps.labelWidth} text={label} />);
  50. } else {
  51. return (<Label width={formProps.labelWidth} {...label} />);
  52. }
  53. }
  54. return null;
  55. }
  56. render() {
  57. const { children, label, extraText, extraTextPosition, ...rest } = this.props;
  58. const updater = this.context;
  59. const formProps = updater.getFormProps(['labelPosition', 'labelWidth', 'labelAlign', 'showValidateIcon', 'wrapperCol', 'labelCol', 'disabled']);
  60. const labelPosition = this.props.labelPosition || formProps.labelPosition;
  61. const groupFieldSet: Array<string> = [];
  62. const inner = React.Children.map(children, (child: any) => {
  63. if (child && child.props && child.props.field) {
  64. groupFieldSet.push(child.props.field);
  65. return React.cloneElement(child, {
  66. isInInputGroup: true,
  67. // noErrorMessage: true,
  68. // noLabel: true
  69. });
  70. }
  71. return null;
  72. });
  73. const groupCls = classNames({
  74. [`${prefix}-field-group`]: true
  75. });
  76. const labelCol = formProps.labelCol;
  77. const wrapperCol = formProps.wrapperCol;
  78. const labelAlign = formProps.labelAlign;
  79. const appendCol = labelCol && wrapperCol;
  80. const labelColCls = labelCol ? `${prefix}-col-${labelAlign}` : '';
  81. const labelContent = this.renderLabel(label, formProps);
  82. const inputGroupContent = (
  83. <InputGroup disabled={formProps.disabled} {...rest}>
  84. {inner}
  85. </InputGroup>
  86. );
  87. const groupErrorContent = (<GroupError fieldSet={groupFieldSet} showValidateIcon={formProps.showValidateIcon} isInInputGroup />);
  88. const extraCls = classNames(`${prefix}-field-extra`, {
  89. [`${prefix}-field-extra-string`]: typeof extraText === 'string',
  90. [`${prefix}-field-extra-middle`]: extraTextPosition === 'middle',
  91. [`${prefix}-field-extra-bottom`]: extraTextPosition === 'bottom',
  92. });
  93. const extraContent = extraText ? <div className={extraCls} x-semi-prop="extraText">{extraText}</div> : null;
  94. let content: any;
  95. switch (true) {
  96. case !appendCol:
  97. content = (
  98. <>
  99. {labelContent}
  100. <div>
  101. {extraTextPosition === 'middle' ? extraContent : null}
  102. {inputGroupContent}
  103. {extraTextPosition === 'bottom' ? extraContent : null}
  104. {groupErrorContent}
  105. </div>
  106. </>
  107. );
  108. break;
  109. case appendCol && labelPosition === 'top':
  110. // When labelPosition is top, you need to add an overflow hidden div to the label, otherwise it will be arranged horizontally
  111. content = (
  112. <>
  113. <div style={{ overflow: 'hidden' }}>
  114. <Col {...labelCol} className={labelColCls}>
  115. {labelContent}
  116. </Col>
  117. </div>
  118. <Col {...wrapperCol}>
  119. {extraTextPosition === 'middle' ? extraContent : null}
  120. {inputGroupContent}
  121. {extraTextPosition === 'bottom' ? extraContent : null}
  122. {groupErrorContent}
  123. </Col>
  124. </>
  125. );
  126. break;
  127. case appendCol && labelPosition !== 'top':
  128. content = (
  129. <>
  130. <Col {...labelCol} className={labelColCls}>
  131. {labelContent}
  132. </Col>
  133. <Col {...wrapperCol}>
  134. {extraTextPosition === 'middle' ? extraContent : null}
  135. {inputGroupContent}
  136. {extraTextPosition === 'bottom' ? extraContent : null}
  137. {groupErrorContent}
  138. </Col>
  139. </>
  140. );
  141. break;
  142. default:
  143. break;
  144. }
  145. return (
  146. <div x-label-pos={labelPosition} className={groupCls}>
  147. {content}
  148. </div>
  149. );
  150. }
  151. }
  152. export default FormInputGroup;