-
Notifications
You must be signed in to change notification settings - Fork 351
refactor(form-elements-text-area): migrate TextArea from Flow to Type… #4790
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
mergify
merged 1 commit into
box:master
from
bonchevskyi:refactor/flow-to-ts-form-elements-text-area
Sep 16, 2026
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
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
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
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
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
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
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,199 @@ | ||
| import * as React from 'react'; | ||
|
|
||
| import TextAreaCore from '../../text-area'; | ||
|
|
||
| import * as messages from '../input-messages'; | ||
| import FormInput from '../form/FormInput'; | ||
|
|
||
| /** Return type for the `validation` callback (`code` required for HTML constraint validation). */ | ||
| export interface TextAreaValidationError { | ||
| code: string; | ||
| message?: React.ReactNode; | ||
| } | ||
|
|
||
| /** Internal error shape in state (optional `code` for externally injected errors). */ | ||
| interface TextAreaError { | ||
| code?: string; | ||
| message?: React.ReactNode; | ||
| } | ||
|
|
||
| export interface TextAreaProps { | ||
| /** Whether the text area is focused on mount */ | ||
| autoFocus?: boolean; | ||
| /** Add a class to the component */ | ||
| className?: string; | ||
| /** Whether the text area is disabled */ | ||
| isDisabled?: boolean; | ||
| /** Whether the text area is read-only */ | ||
| isReadOnly?: boolean; | ||
| /** Whether the text area value is required */ | ||
| isRequired?: boolean; | ||
| /** Is text area resizable */ | ||
| isResizable?: boolean; | ||
| /** Label displayed for the text area */ | ||
| label: React.ReactNode; | ||
| /** Maximum number of characters allowed */ | ||
| maxLength?: number; | ||
| /** Name of the text area */ | ||
| name: string; | ||
| /** Placeholder for the text area */ | ||
| placeholder?: string; | ||
| /** Validation function that returns `TextAreaValidationError` or a falsy value when valid */ | ||
| validation?: (value: string) => TextAreaValidationError | null | undefined; | ||
| /** Default value of the text area */ | ||
| value: string; | ||
| } | ||
|
|
||
| interface TextAreaState { | ||
| error: TextAreaError | null | undefined; | ||
| value: string; | ||
| } | ||
|
|
||
| class TextArea extends React.Component<TextAreaProps, TextAreaState> { | ||
| static defaultProps = { | ||
| autoFocus: false, | ||
| value: '', | ||
| isReadOnly: false, | ||
| }; | ||
|
|
||
| constructor(props: TextAreaProps) { | ||
| super(props); | ||
| this.state = { | ||
| error: null, | ||
| value: props.value, | ||
| }; | ||
| } | ||
|
|
||
| componentDidUpdate({ value: prevValue }: TextAreaProps): void { | ||
| // If a new value is passed by prop, set it | ||
| if (prevValue !== this.props.value) { | ||
| this.setState({ | ||
| value: this.props.value, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| onChange = ({ currentTarget }: React.ChangeEvent<HTMLTextAreaElement>): void => { | ||
| const { value } = currentTarget; | ||
| if (this.state.error) { | ||
| this.setState( | ||
| { | ||
| value, | ||
| }, | ||
| this.checkValidity, | ||
| ); | ||
| } else { | ||
| this.setState({ | ||
| value, | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| onValidityStateUpdateHandler = (error: ValidityState | TextAreaError): void => { | ||
| if ((error as ValidityState).valid !== undefined) { | ||
| this.setErrorFromValidityState(error as ValidityState); | ||
| } else { | ||
| this.setState({ | ||
| error: error as TextAreaError, | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| setErrorFromValidityState(validityState: ValidityState): void { | ||
| const { badInput, customError, tooLong, valid, valueMissing } = validityState; | ||
|
|
||
| const { isRequired, maxLength, validation } = this.props; | ||
|
|
||
| const { value } = this.state; | ||
|
|
||
| let error; | ||
|
|
||
| if (valid) { | ||
| error = null; | ||
| } else if (badInput) { | ||
| error = messages.badInput(); | ||
| } else if (tooLong && typeof maxLength !== 'undefined') { | ||
| error = messages.tooLong(maxLength); | ||
| } else if (valueMissing) { | ||
| error = messages.valueMissing(); | ||
| } else if (customError && (isRequired || value.trim().length) && validation) { | ||
| error = validation(value); | ||
| } | ||
|
|
||
| this.setState({ | ||
| error, | ||
| }); | ||
| } | ||
|
|
||
| textarea: HTMLTextAreaElement | null | undefined; | ||
|
|
||
| // Updates component value and validity state | ||
| checkValidity = (): void => { | ||
| const { isRequired, validation } = this.props; | ||
| const { textarea } = this; | ||
|
|
||
| if (!textarea) { | ||
| return; | ||
| } | ||
|
|
||
| if (validation && (isRequired || textarea.value.trim().length)) { | ||
| const error = validation(textarea.value); | ||
| this.setState({ | ||
| error, | ||
| value: textarea.value, | ||
| }); | ||
|
|
||
| if (error) { | ||
| textarea.setCustomValidity(error.code); | ||
| } else { | ||
| textarea.setCustomValidity(''); | ||
| } | ||
| } else { | ||
| this.setErrorFromValidityState(textarea.validity); | ||
| } | ||
|
bonchevskyi marked this conversation as resolved.
|
||
| }; | ||
|
|
||
| render(): React.ReactNode { | ||
| const { | ||
| autoFocus, | ||
| className = '', | ||
| isDisabled, | ||
| isReadOnly, | ||
| isRequired, | ||
| isResizable, | ||
| label, | ||
| maxLength, | ||
| name, | ||
| placeholder, | ||
| } = this.props; | ||
|
|
||
| const { error, value } = this.state; | ||
|
|
||
| return ( | ||
| <div className={className}> | ||
| <FormInput name={name} onValidityStateUpdate={this.onValidityStateUpdateHandler}> | ||
| <TextAreaCore | ||
| autoFocus={autoFocus} | ||
| disabled={isDisabled} | ||
| error={error ? error.message : null} | ||
| label={label} | ||
| isRequired={isRequired} | ||
| isResizable={isResizable} | ||
| maxLength={maxLength} | ||
| name={name} | ||
| onBlur={this.checkValidity} | ||
| onChange={this.onChange} | ||
| placeholder={placeholder} | ||
| readOnly={isReadOnly} | ||
| textareaRef={textarea => { | ||
| this.textarea = textarea; | ||
| }} | ||
| value={value} | ||
| /> | ||
|
bonchevskyi marked this conversation as resolved.
|
||
| </FormInput> | ||
| </div> | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| export default TextArea; | ||
Oops, something went wrong.
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.