-
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
[TagInput] onAdd can return false to prevent clearing input #1309
Changes from 3 commits
35c045b
89a5100
df0624f
9c6acf4
92b17df
77e68be
6b94980
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 |
---|---|---|
|
@@ -19,8 +19,11 @@ export interface ITagInputProps extends IProps { | |
* Callback invoked when a new tag is added by the user pressing `enter` on the input. | ||
* Receives the current value of the input field. New tags are expected to be appended to | ||
* the list. | ||
* | ||
* If the provided function returns `false`, the input will not be cleared. This is useful, | ||
* for example, if the provided `value` is somehow invalid and should not be added as a tag. | ||
*/ | ||
onAdd?: (value: string) => void; | ||
onAdd?: (value: string) => boolean; | ||
|
||
/** | ||
* Callback invoked when the user clicks the X button on a tag. | ||
|
@@ -167,8 +170,10 @@ export class TagInput extends AbstractComponent<ITagInputProps, ITagInputState> | |
const { selectionEnd, value } = event.currentTarget; | ||
if (event.which === Keys.ENTER && value.length > 0) { | ||
// enter key on non-empty string invokes onAdd | ||
this.setState({ inputValue: "" }); | ||
Utils.safeInvoke(this.props.onAdd, value); | ||
const shouldClearInput = Utils.safeInvoke(this.props.onAdd, value); | ||
if (shouldClearInput) { | ||
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. Let's add a switch in the examples for "Unique values" 👍 Can also come as a follow up PR, happy to take care of this 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. I'm iffy on adding a "unique values" switch, because it's doesn't correspond to out-of-the-box behavior. Would love to figure out a good way to communicate how to do that in a future PR though. Maybe we could just include a code snippet similar to http://blueprintjs.com/docs/#core/components/forms/numeric-input.uncontrolled-mode 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. That's fair, I mean I guess the documentation speaks for itself now, we really were lacking this |
||
this.setState({ inputValue: "" }); | ||
} | ||
} else if (selectionEnd === 0 && this.props.values.length > 0) { | ||
// cursor at beginning of input allows interaction with tags. | ||
// use selectionEnd to verify cursor position and no text selection. | ||
|
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.
What about
boolean | void
, so that you can return bothtrue
or nothing if you want the tag to be added?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.
Ah yep, missed that. Fixed. 👍