Skip to content

Commit

Permalink
EuiValidatableControl convert to TS (#1879)
Browse files Browse the repository at this point in the history
* `EuiValidatableControl` convert to TS

* `EuiValidatableControl` code review fix

* `EuiValidatableControl` updated changelog
  • Loading branch information
theodesp authored and thompsongl committed May 1, 2019
1 parent b4febb3 commit 2505c6d
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 56 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## [`master`](https://github.com/elastic/eui/tree/master)

- Converted `EuiValidatableControl` to TS ([#1879](https://github.com/elastic/eui/pull/1879))

**Bug fixes**

- Fixed overflow scrolling of `EuiModal` and `EuiConfirmModal` for Chrome and Safari ([#1902](https://github.com/elastic/eui/pull/1902))
Expand Down
3 changes: 0 additions & 3 deletions src/components/form/validatable_control/index.js

This file was deleted.

1 change: 1 addition & 0 deletions src/components/form/validatable_control/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { EuiValidatableControl } from './validatable_control';
51 changes: 0 additions & 51 deletions src/components/form/validatable_control/validatable_control.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ describe('EuiValidatableControl', () => {
</EuiValidatableControl>
);

expect(component)
.toMatchSnapshot();
expect(component).toMatchSnapshot();
});
});
61 changes: 61 additions & 0 deletions src/components/form/validatable_control/validatable_control.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { Children, cloneElement, Component, ReactElement } from 'react';
import { CommonProps } from '../../common';

export interface HTMLConstraintValidityElement extends Element {
setCustomValidity: (error: string) => void;
}

export interface ReactElementWithRef extends ReactElement {
ref?: (element: HTMLConstraintValidityElement) => void;
}

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

export class EuiValidatableControl extends Component<
CommonProps & EuiValidatableControlProps
> {
private control?: HTMLConstraintValidityElement;

updateValidity() {
if (
this.control == null ||
typeof this.control.setCustomValidity !== 'function'
) {
return; // jsdom doesn't polyfill this for the server-side
}

if (this.props.isInvalid) {
this.control.setCustomValidity('Invalid');
} else {
this.control.setCustomValidity('');
}
}

componentDidMount() {
this.updateValidity();
}

componentDidUpdate() {
this.updateValidity();
}

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

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

render() {
const child = Children.only(this.props.children);
return cloneElement(child, {
ref: this.setRef,
});
}
}

0 comments on commit 2505c6d

Please sign in to comment.