-
Notifications
You must be signed in to change notification settings - Fork 841
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EuiValidatableControl
convert to TS (#1879)
* `EuiValidatableControl` convert to TS * `EuiValidatableControl` code review fix * `EuiValidatableControl` updated changelog
- Loading branch information
1 parent
b4febb3
commit 2505c6d
Showing
7 changed files
with
65 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { EuiValidatableControl } from './validatable_control'; |
51 changes: 0 additions & 51 deletions
51
src/components/form/validatable_control/validatable_control.js
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/components/form/validatable_control/validatable_control.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
} | ||
} |