Element.tsx 904 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import React, { PureComponent, ReactNode, ReactElement } from 'react';
  2. import { Label } from './Label';
  3. import { uniqueId } from 'lodash';
  4. interface Props {
  5. label?: ReactNode;
  6. labelClassName?: string;
  7. id?: string;
  8. children: ReactElement<any>;
  9. }
  10. export class Element extends PureComponent<Props> {
  11. elementId: string = this.props.id || uniqueId('form-element-');
  12. get elementLabel() {
  13. const { label, labelClassName } = this.props;
  14. if (label) {
  15. return (
  16. <Label htmlFor={this.elementId} className={labelClassName}>
  17. {label}
  18. </Label>
  19. );
  20. }
  21. return null;
  22. }
  23. get children() {
  24. const { children } = this.props;
  25. return React.cloneElement(children, { id: this.elementId });
  26. }
  27. render() {
  28. return (
  29. <div className="our-custom-wrapper-class">
  30. {this.elementLabel}
  31. {this.children}
  32. </div>
  33. );
  34. }
  35. }