-
Notifications
You must be signed in to change notification settings - Fork 350
refactor(text-area): migrate TextArea from Flow to TypeScript #4761
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
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 hidden or 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,99 @@ | ||
| import * as React from 'react'; | ||
| import classNames from 'classnames'; | ||
| import uniqueId from 'lodash/uniqueId'; | ||
|
|
||
| import Label from '../label'; | ||
| import Tooltip, { TooltipPosition, TooltipTheme, type TooltipProps } from '../tooltip'; | ||
|
|
||
| import './TextArea.scss'; | ||
|
|
||
| export interface TextAreaProps extends React.TextareaHTMLAttributes<HTMLTextAreaElement> { | ||
| /** Add a class to the component */ | ||
| className?: string; | ||
| /** Description shown below the label */ | ||
| description?: React.ReactNode; | ||
| /** Error message shown in the error tooltip */ | ||
| error?: React.ReactNode; | ||
| /** Renders error tooltip at the specified position (positions are those from Tooltip) */ | ||
| errorTooltipPosition?: NonNullable<TooltipProps['position']>; | ||
| /** Hides the label */ | ||
| hideLabel?: boolean; | ||
| /** Hides (optional) text from the label */ | ||
| hideOptionalLabel?: boolean; | ||
| /** Makes the text area value required */ | ||
| isRequired?: boolean; | ||
| /** Is text area resizable */ | ||
| isResizable?: boolean; | ||
| /** Label displayed for the text area */ | ||
| label: React.ReactNode; | ||
| /** Ref to the underlying textarea element. @TODO: eventually rename to innerRef for consistancy across all form elements */ | ||
| textareaRef?: React.Ref<HTMLTextAreaElement>; | ||
| /** A CSS class for the tooltip's tether element component */ | ||
| tooltipTetherClassName?: string; | ||
| } | ||
|
|
||
| const TextArea = ({ | ||
| className = '', | ||
| description, | ||
| error, | ||
| errorTooltipPosition, | ||
| hideLabel, | ||
| hideOptionalLabel, | ||
| isRequired, | ||
| isResizable, | ||
| label, | ||
| textareaRef, | ||
| tooltipTetherClassName, | ||
| ...rest | ||
| }: TextAreaProps) => { | ||
| const hasError = !!error; | ||
| const classes = classNames(className, 'text-area-container', { | ||
| 'show-error': hasError, | ||
| }); | ||
|
|
||
| const errorMessageID = React.useRef(uniqueId('errorMessage')).current; | ||
| const descriptionID = React.useRef(uniqueId('description')).current; | ||
|
|
||
| const ariaAttrs = { | ||
| 'aria-invalid': hasError, | ||
| 'aria-required': isRequired, | ||
| 'aria-errormessage': errorMessageID, | ||
| 'aria-describedby': description ? descriptionID : undefined, | ||
| }; | ||
|
|
||
| return ( | ||
| <div className={classes}> | ||
| <Label hideLabel={hideLabel} showOptionalText={!hideOptionalLabel && !isRequired} text={label}> | ||
| <> | ||
| {!!description && ( | ||
| <div id={descriptionID} className="text-area-description"> | ||
| {description} | ||
| </div> | ||
| )} | ||
| <Tooltip | ||
| isShown={hasError} | ||
| position={errorTooltipPosition || TooltipPosition.BOTTOM_LEFT} | ||
| tetherElementClassName={tooltipTetherClassName} | ||
| text={error || ''} | ||
| theme={TooltipTheme.ERROR} | ||
| > | ||
| <textarea | ||
| ref={textareaRef} | ||
| required={isRequired} | ||
| style={{ resize: isResizable ? '' : 'none' } as React.CSSProperties} | ||
| {...ariaAttrs} | ||
| {...rest} | ||
| /> | ||
| </Tooltip> | ||
| <span id={errorMessageID} className="accessibility-hidden" role="alert"> | ||
| {error} | ||
| </span> | ||
| </> | ||
| </Label> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| TextArea.displayName = 'TextArea'; | ||
|
|
||
| export default TextArea; | ||
File renamed without changes.
This file contains hidden or 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,29 @@ | ||
| import * as React from 'react'; | ||
| import getProp from 'lodash/get'; | ||
| import type { FieldProps } from 'formik'; | ||
|
|
||
| import TextAreaPrimitive from './TextArea'; | ||
| import type { TextAreaProps } from './TextArea'; | ||
|
|
||
| export interface TextAreaFieldProps extends Omit<TextAreaProps, 'form'>, FieldProps { | ||
| /** Ref forwarded to the underlying textarea element as textareaRef */ | ||
| innerRef?: (instance: HTMLTextAreaElement | null) => void; | ||
| } | ||
|
|
||
| const TextAreaField = ({ field, form, innerRef, isRequired, ...rest }: TextAreaFieldProps) => { | ||
| const { name } = field; | ||
| const { errors, touched } = form; | ||
| const isTouched = getProp(touched, name); | ||
| const error = isTouched ? getProp(errors, name) : null; | ||
| return ( | ||
| <TextAreaPrimitive | ||
| {...field} | ||
| {...rest} | ||
| textareaRef={innerRef} | ||
| error={error as React.ReactNode} | ||
| hideOptionalLabel={isRequired} | ||
| /> | ||
|
bonchevskyi marked this conversation as resolved.
|
||
| ); | ||
| }; | ||
|
|
||
| export default TextAreaField; | ||
2 changes: 1 addition & 1 deletion
2
...ents/text-area/__tests__/TextArea.test.js → ...nts/text-area/__tests__/TextArea.test.tsx
This file contains hidden or 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
7 changes: 4 additions & 3 deletions
7
...text-area/__tests__/TextAreaField.test.js → ...ext-area/__tests__/TextAreaField.test.tsx
This file contains hidden or 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.
File renamed without changes.
File renamed without changes.
This file contains hidden or 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,4 @@ | ||
| export { default } from './TextArea'; | ||
| export { default as TextAreaField } from './TextAreaField'; | ||
| export type { TextAreaProps } from './TextArea'; | ||
| export type { TextAreaFieldProps } from './TextAreaField'; | ||
|
bonchevskyi marked this conversation as resolved.
|
||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.