-
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,190 @@ | ||
| import * as React from 'react'; | ||
|
|
||
| import TextAreaCore from '../../text-area'; | ||
|
|
||
| import * as messages from '../input-messages'; | ||
| import FormInput from '../form/FormInput'; | ||
|
|
||
| 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 an error object (`code`, `message`) or a falsy value when valid */ | ||
| validation?: (value: string) => TextAreaError | 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) { | ||
| // 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>) => { | ||
| const { value } = currentTarget; | ||
| if (this.state.error) { | ||
| this.setState( | ||
| { | ||
| value, | ||
| }, | ||
| this.checkValidity, | ||
| ); | ||
| } else { | ||
| this.setState({ | ||
| value, | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| onValidityStateUpdateHandler = (error: ValidityState | TextAreaError) => { | ||
| if ((error as ValidityState).valid !== undefined) { | ||
| this.setErrorFromValidityState(error as ValidityState); | ||
| } else { | ||
| this.setState({ | ||
| error: error as TextAreaError, | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| setErrorFromValidityState(validityState: ValidityState) { | ||
| 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 = () => { | ||
| 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); | ||
| } | ||
|
Comment on lines
+132
to
+146
Contributor
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. 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win Clear custom validity before native validation. If optional custom validation first fails and the user then clears the value, the fallback path keeps the previous custom validity message. The UI error clears, but the textarea remains natively invalid. The same path also skips the native required error when a required empty value has a validator that returns
📍 Affects 2 files
🤖 Prompt for AI Agents |
||
| }; | ||
|
|
||
| render() { | ||
| const { | ||
| autoFocus, | ||
| className = '', | ||
| isDisabled, | ||
| isReadOnly, | ||
| isRequired, | ||
| isResizable, | ||
| label, | ||
| 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} | ||
| name={name} | ||
| onBlur={this.checkValidity} | ||
| onChange={this.onChange} | ||
| placeholder={placeholder} | ||
| readOnly={isReadOnly} | ||
| textareaRef={textarea => { | ||
| this.textarea = textarea; | ||
| }} | ||
| value={value} | ||
| /> | ||
|
Comment on lines
+150
to
+183
Contributor
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. 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win Forward Both wrappers declare
📍 Affects 2 files
🤖 Prompt for AI Agents |
||
| </FormInput> | ||
| </div> | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| export default TextArea; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export { default } from './TextArea'; | ||
| export type { TextAreaProps } from './TextArea'; |
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.
🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: box/box-ui-elements
Length of output: 8232
🏁 Script executed:
Repository: box/box-ui-elements
Length of output: 25298
🏁 Script executed:
Repository: box/box-ui-elements
Length of output: 50377
🏁 Script executed:
Repository: box/box-ui-elements
Length of output: 955
Align
TextAreaProps.validationwith the runtime result shape.TextArea.tsxreadserror.codeanderror.messagesynchronously. The documented string and Promise results inTextArea.js.floware not supported at runtime. Define the same object result type in both declarations, or normalize and await those documented result forms before reading their fields.📍 Affects 2 files
src/components/form-elements/text-area/TextArea.tsx#L34-L35(this comment)src/components/form-elements/text-area/TextArea.js.flow#L25-L26🤖 Prompt for AI Agents