-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
[core] add AsyncControllableTextArea based on AsyncControllableInput & add async control option for TextArea component #6312
Merged
Merged
Changes from 5 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
93f9f0c
async control TextArea component
nurseiit ee920ff
add correct types & copy tests from default input
nurseiit dd62954
fix lint, object order
nurseiit 7407ca9
move types to their own file
nurseiit c28308c
refactor tests
nurseiit 6548a62
Merge remote-tracking branch 'origin/develop' into na/async-control-t…
nurseiit b5c2643
tests pass, but WIP
nurseiit c562eaa
remove example
nurseiit 276814e
add more comments
nurseiit 29c07fe
forwardRef for TextArea
nurseiit cd41247
useRef empty type with undefined
nurseiit bdc07b6
flatten & simplify state derival + comments
nurseiit c35297a
dedup tests & add note
nurseiit 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 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
38 changes: 38 additions & 0 deletions
38
packages/core/src/components/forms/asyncControllableInputProps.ts
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,38 @@ | ||
/* ! | ||
* (c) Copyright 2023 Palantir Technologies Inc. All rights reserved. | ||
*/ | ||
|
||
export type InputTagName = "input" | "textarea"; | ||
|
||
export type AsyncControllableElement<T extends InputTagName> = T extends "input" | ||
? HTMLInputElement | ||
: T extends "textarea" | ||
? HTMLTextAreaElement | ||
: never; | ||
|
||
type AsyncControllableElementAttributes<T extends InputTagName> = T extends "input" | ||
? React.InputHTMLAttributes<HTMLInputElement> | ||
: T extends "textarea" | ||
? React.TextareaHTMLAttributes<HTMLTextAreaElement> | ||
: never; | ||
|
||
export type AsyncControllableInputValue<T extends InputTagName> = AsyncControllableElementAttributes<T>["value"]; | ||
|
||
export type AsyncControllableInputProps<T extends InputTagName = "input"> = Omit< | ||
AsyncControllableElementAttributes<T>, | ||
"onChange" | "onCompositionStart" | "onCompositionEnd" | ||
> & { | ||
/** | ||
* HTML tag name to use for rendered input element. | ||
* | ||
* @type {"input" | "textarea"} | ||
* @default "input" | ||
*/ | ||
tagName?: T; | ||
inputRef?: React.Ref<AsyncControllableElement<T>>; | ||
|
||
// NOTE: these are copied from the React.HTMLAttributes interface definition. | ||
onChange?: React.ChangeEventHandler<AsyncControllableElement<T>> | undefined; | ||
onCompositionStart?: React.CompositionEventHandler<AsyncControllableElement<T>> | undefined; | ||
onCompositionEnd?: React.CompositionEventHandler<AsyncControllableElement<T>> | undefined; | ||
}; |
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 |
---|---|---|
|
@@ -19,8 +19,18 @@ import * as React from "react"; | |
|
||
import { AbstractPureComponent, Classes, refHandler, setRef } from "../../common"; | ||
import { DISPLAYNAME_PREFIX, IntentProps, Props } from "../../common/props"; | ||
import { AsyncControllableInput } from "./asyncControllableInput"; | ||
|
||
export interface TextAreaProps extends IntentProps, Props, React.TextareaHTMLAttributes<HTMLTextAreaElement> { | ||
/** | ||
* Set this to `true` if you will be controlling the `value` of this input with asynchronous updates. | ||
* These may occur if you do not immediately call setState in a parent component with the value from | ||
* the `onChange` handler, or if working with certain libraries like __redux-form__. | ||
* | ||
* @default false | ||
*/ | ||
asyncControl?: boolean; | ||
|
||
/** | ||
* Whether the component should automatically resize vertically as a user types in the text input. | ||
* This will disable manual resizing in the vertical dimension. | ||
|
@@ -142,9 +152,19 @@ export class TextArea extends AbstractPureComponent<TextAreaProps, TextAreaState | |
} | ||
|
||
public render() { | ||
// eslint-disable-next-line deprecation/deprecation | ||
const { autoResize, className, fill, growVertically, inputRef, intent, large, small, ...htmlProps } = | ||
this.props; | ||
const { | ||
asyncControl, | ||
autoResize, | ||
className, | ||
fill, | ||
// eslint-disable-next-line deprecation/deprecation | ||
growVertically, | ||
inputRef, | ||
intent, | ||
large, | ||
small, | ||
...htmlProps | ||
} = this.props; | ||
|
||
const rootClasses = classNames( | ||
Classes.INPUT, | ||
|
@@ -170,13 +190,22 @@ export class TextArea extends AbstractPureComponent<TextAreaProps, TextAreaState | |
}; | ||
} | ||
|
||
return ( | ||
return asyncControl ? ( | ||
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. Does this seem to work when testing live? The code changes look correct. Just wondering if |
||
<AsyncControllableInput | ||
{...htmlProps} | ||
tagName="textarea" | ||
className={rootClasses} | ||
onChange={this.handleChange} | ||
style={style} | ||
inputRef={this.handleRef} | ||
/> | ||
) : ( | ||
<textarea | ||
{...htmlProps} | ||
className={rootClasses} | ||
onChange={this.handleChange} | ||
ref={this.handleRef} | ||
style={style} | ||
ref={this.handleRef} | ||
/> | ||
); | ||
} | ||
|
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.
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.
not sure if this is the best way to do it, but I would argue this is fine since this component is not exported directly.
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.
I did not want to further complicate the types by excluding and extending the native interfaces above.