Skip to content

Commit

Permalink
EuiValidatableControl code review fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Theo committed Apr 30, 2019
1 parent 3cf372c commit 17e219b
Showing 1 changed file with 13 additions and 25 deletions.
38 changes: 13 additions & 25 deletions src/components/form/validatable_control/validatable_control.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,17 @@
import {
Children,
cloneElement,
Component,
ReactElement,
ReactNode,
} from 'react';
import { Children, cloneElement, Component, ReactElement } from 'react';
import { CommonProps } from '../../common';

export type HTMLConstraintValidityElement =
| HTMLButtonElement
| HTMLFieldSetElement
| HTMLInputElement
| HTMLObjectElement
| HTMLOutputElement
| HTMLSelectElement
| HTMLTextAreaElement
| Element & { setCustomValidity?: any };
export interface HTMLConstraintValidityElement extends Element {
setCustomValidity: (error: string) => void;
}

export type EuiNodeWithRef = ReactNode & {
ref?: (node: ReactNode) => void;
};
export interface ReactElementWithRef extends ReactElement {
ref?: (element: HTMLConstraintValidityElement) => void;
}

export interface EuiValidatableControlProps {
isInvalid?: boolean;
children?: EuiNodeWithRef;
children: ReactElementWithRef;
}

export class EuiValidatableControl extends Component<
Expand Down Expand Up @@ -54,19 +42,19 @@ export class EuiValidatableControl extends Component<
this.updateValidity();
}

setRef = (node: HTMLConstraintValidityElement) => {
this.control = node;
setRef = (element: HTMLConstraintValidityElement) => {
this.control = element;

// Call the original ref, if any
const { ref } = this.props.children as EuiNodeWithRef;
const { ref } = this.props.children;
if (typeof ref === 'function') {
ref(node);
ref(element);
}
};

render() {
const child = Children.only(this.props.children);
return cloneElement(child as ReactElement<any>, {
return cloneElement(child, {
ref: this.setRef,
});
}
Expand Down

0 comments on commit 17e219b

Please sign in to comment.