group.tsx 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. interface GroupErrorProps {
  15. showValidateIcon?: boolean;
  16. isInInputGroup?: boolean;
  17. error?: ReactFieldError;
  18. fieldSet?: string[];
  19. }
  20. export interface InputGroupProps extends BacisInputGroupProps {
  21. label?: LabelProps;
  22. labelPosition?: 'left' | 'top';
  23. }
  24. const prefix = cssClasses.PREFIX;
  25. // Group component to remove Labels and ErrorMessages from its child fields
  26. // Unified insertion of Labels and ErrorMessages from the group level
  27. // Get Errors of all field in this group
  28. const GroupError = (props: GroupErrorProps) => {
  29. const { fieldSet } = props;
  30. const formState: FormState = useFormState();
  31. const error = fieldSet.map((field: string) => ObjectUtil.get(formState.errors, field));
  32. if (isValid(error)) {
  33. return null;
  34. }
  35. return (
  36. <ErrorMessage error={error} showValidateIcon={props.showValidateIcon} isInInputGroup={props.isInInputGroup} />
  37. );
  38. };
  39. class FormInputGroup extends Component<InputGroupProps> {
  40. static contextType = FormUpdaterContext;
  41. renderLabel(label: LabelProps, formProps: BaseFormProps) {
  42. if (label) {
  43. if (isString(label)) {
  44. return (<Label width={formProps.labelWidth} text={label} />);
  45. } else {
  46. return (<Label width={formProps.labelWidth} {...label} />);
  47. }
  48. }
  49. return null;
  50. }
  51. render() {
  52. const { children, label, ...rest } = this.props;
  53. const updater = this.context;
  54. const formProps = updater.getFormProps(['labelPosition', 'labelWidth', 'labelAlign', 'showValidateIcon']);
  55. const labelPosition = this.props.labelPosition || formProps.labelPosition;
  56. const groupFieldSet: Array<string> = [];
  57. const inner = React.Children.map(children, (child: any) => {
  58. if (child && child.props && child.props.field) {
  59. groupFieldSet.push(child.props.field);
  60. return React.cloneElement(child, {
  61. isInInputGroup: true,
  62. // noErrorMessage: true,
  63. // noLabel: true
  64. });
  65. }
  66. return null;
  67. });
  68. const groupCls = classNames({
  69. [`${prefix}-field-group`]: true
  70. });
  71. return (
  72. <div x-label-pos={labelPosition} className={groupCls}>
  73. {this.renderLabel(label, formProps)}
  74. <div>
  75. <InputGroup {...rest}>
  76. {inner}
  77. </InputGroup>
  78. <GroupError fieldSet={groupFieldSet} showValidateIcon={formProps.showValidateIcon} isInInputGroup />
  79. </div>
  80. </div>
  81. );
  82. }
  83. }
  84. export default FormInputGroup;