This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 219
Convert TextInput and ValidatedTextInput to TypeScript #4226
Merged
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b469836
Change index file from base/context to .ts
opr 7a9e066
Convert ValidatedTextInput to TypeScript
opr d93aecd
Convert TextInput to TypeScript
opr 710acbc
Ensure Label accepts correct HTML Attributes Props
opr 178af16
Remove PropTypes from TextInput and ValidatedTextInput
opr 0ac4e76
Use correct error id to show validation message
opr c0844bf
Use HTMLElement instead of a specific element type for LabelProps
opr 29340fc
Update assets/js/base/components/text-input/validated-text-input.tsx
opr 653913e
Update assets/js/base/components/text-input/validated-text-input.tsx
opr 24c1476
Use correct formatting in ValidatedTextInput
opr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
File renamed without changes.
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
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 |
---|---|---|
|
@@ -3,7 +3,6 @@ | |
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { useCallback, useRef, useEffect, useState } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import classnames from 'classnames'; | ||
import { | ||
ValidationInputError, | ||
|
@@ -17,6 +16,29 @@ import { withInstanceId } from '@woocommerce/base-hocs/with-instance-id'; | |
import TextInput from './text-input'; | ||
import './style.scss'; | ||
|
||
interface ValidatedTextInputPropsWithId { | ||
instanceId?: string; | ||
id: string; | ||
} | ||
|
||
interface ValidatedTextInputPropsWithInstanceId { | ||
instanceId: string; | ||
id?: string; | ||
} | ||
|
||
type ValidatedTextInputProps = ( | ||
| ValidatedTextInputPropsWithId | ||
| ValidatedTextInputPropsWithInstanceId | ||
) & { | ||
className?: string; | ||
ariaDescribedBy?: string; | ||
errorId?: string; | ||
validateOnMount?: boolean; | ||
focusOnMount?: boolean; | ||
showError?: boolean; | ||
onChange: ( newValue: string ) => void; | ||
}; | ||
|
||
const ValidatedTextInput = ( { | ||
className, | ||
instanceId, | ||
|
@@ -28,9 +50,9 @@ const ValidatedTextInput = ( { | |
onChange, | ||
showError = true, | ||
...rest | ||
} ) => { | ||
}: ValidatedTextInputProps ) => { | ||
const [ isPristine, setIsPristine ] = useState( true ); | ||
const inputRef = useRef(); | ||
const inputRef = useRef< HTMLInputElement >( null ); | ||
const { | ||
getValidationError, | ||
hideValidationError, | ||
|
@@ -39,8 +61,8 @@ const ValidatedTextInput = ( { | |
getValidationErrorId, | ||
} = useValidationContext(); | ||
|
||
const textInputId = id || 'textinput-' + instanceId; | ||
errorId = errorId || textInputId; | ||
const textInputId = typeof id !== 'undefined' ? id : 'textinput-' + instanceId; | ||
const errorIdString = errorId !== undefined ? errorId : textInputId; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change was made because TS still couldn't properly infer that after this line, |
||
|
||
const validateInput = useCallback( | ||
( errorsHidden = true ) => { | ||
|
@@ -52,10 +74,10 @@ const ValidatedTextInput = ( { | |
inputObject.value = inputObject.value.trim(); | ||
const inputIsValid = inputObject.checkValidity(); | ||
if ( inputIsValid ) { | ||
clearValidationError( errorId ); | ||
clearValidationError( errorIdString ); | ||
} else { | ||
setValidationErrors( { | ||
[ errorId ]: { | ||
[ errorIdString ]: { | ||
message: | ||
inputObject.validationMessage || | ||
__( | ||
|
@@ -67,13 +89,13 @@ const ValidatedTextInput = ( { | |
} ); | ||
} | ||
}, | ||
[ clearValidationError, errorId, setValidationErrors ] | ||
[ clearValidationError, errorIdString, setValidationErrors ] | ||
); | ||
|
||
useEffect( () => { | ||
if ( isPristine ) { | ||
if ( focusOnMount ) { | ||
inputRef.current.focus(); | ||
inputRef.current?.focus(); | ||
} | ||
setIsPristine( false ); | ||
} | ||
|
@@ -91,15 +113,19 @@ const ValidatedTextInput = ( { | |
// Remove validation errors when unmounted. | ||
useEffect( () => { | ||
return () => { | ||
clearValidationError( errorId ); | ||
clearValidationError( errorIdString ); | ||
}; | ||
}, [ clearValidationError, errorId ] ); | ||
}, [ clearValidationError, errorIdString ] ); | ||
|
||
const errorMessage = getValidationError( errorId ) || {}; | ||
// TODO - When useValidationContext is converted to TypeScript, remove this cast and use the correct type. | ||
opr marked this conversation as resolved.
Show resolved
Hide resolved
opr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const errorMessage = ( getValidationError( errorIdString ) || {} ) as { | ||
message?: string; | ||
hidden?: boolean; | ||
}; | ||
const hasError = errorMessage.message && ! errorMessage.hidden; | ||
const describedBy = | ||
showError && hasError && getValidationErrorId( errorId ) | ||
? getValidationErrorId( errorId ) | ||
showError && hasError && getValidationErrorId( errorIdString ) | ||
? getValidationErrorId( errorIdString ) | ||
: ariaDescribedBy; | ||
|
||
return ( | ||
|
@@ -112,11 +138,13 @@ const ValidatedTextInput = ( { | |
validateInput( false ); | ||
} } | ||
feedback={ | ||
showError && <ValidationInputError propertyName={ errorId } /> | ||
showError && ( | ||
<ValidationInputError propertyName={ errorIdString } /> | ||
) | ||
} | ||
ref={ inputRef } | ||
onChange={ ( val ) => { | ||
hideValidationError( errorId ); | ||
hideValidationError( errorIdString ); | ||
onChange( val ); | ||
} } | ||
ariaDescribedBy={ describedBy } | ||
|
@@ -125,15 +153,4 @@ const ValidatedTextInput = ( { | |
); | ||
}; | ||
|
||
ValidatedTextInput.propTypes = { | ||
onChange: PropTypes.func.isRequired, | ||
id: PropTypes.string, | ||
value: PropTypes.string, | ||
ariaDescribedBy: PropTypes.string, | ||
errorId: PropTypes.string, | ||
validateOnMount: PropTypes.bool, | ||
focusOnMount: PropTypes.bool, | ||
showError: PropTypes.bool, | ||
}; | ||
|
||
export default withInstanceId( ValidatedTextInput ); |
File renamed without changes.
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 |
---|---|---|
|
@@ -3,13 +3,13 @@ | |
*/ | ||
import { Fragment } from '@wordpress/element'; | ||
import classNames from 'classnames'; | ||
import type { ReactElement, HTMLAttributes } from 'react'; | ||
import type { ReactElement, HTMLProps } from 'react'; | ||
|
||
interface LabelProps { | ||
interface LabelProps extends HTMLProps< HTMLElement > { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This lets us pass extra props to |
||
label?: string; | ||
screenReaderLabel?: string; | ||
wrapperElement?: string; | ||
wrapperProps?: HTMLAttributes< HTMLElement >; | ||
wrapperProps?: HTMLProps< HTMLElement >; | ||
} | ||
|
||
/** | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Essentially this means: if
id
is passed,instanceId
is optional, and vice/versa.