group.tsx 6.5 KB

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