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
[NumericInput] Add onButtonClick prop #1178
Conversation
Add onButtonClick prop, add testPreview: documentation |
@@ -110,6 +110,9 @@ export interface INumericInputProps extends IIntentProps, IProps { | |||
/** The value to display in the input field. */ | |||
value?: number | string; | |||
|
|||
/** The callback invoked when the value changes as a result of a button click. */ | |||
onButtonClick?(valueAsNumber: number, valueAsString: string): void; | |||
|
|||
/** The callback invoked when the value changes. */ |
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.
add more details here to distinguish from oBC
: "when the value changes due to typing, arrow keys, or button clicks."
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.
Done
@@ -477,22 +483,26 @@ export class NumericInput extends AbstractComponent<HTMLInputProps & INumericInp | |||
} | |||
|
|||
private invokeOnValueChangeCallback(value: string) { | |||
const valueAsString = value; | |||
const valueAsNumber = +value; // coerces non-numeric strings to NaN |
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.
can we keep this comment around? perhaps in invokeOnValueChangeCallback
, to remind us why we're not using parseInt
.
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.
Done
} | ||
|
||
private invokeOnButtonClickCallback(value: string) { | ||
Utils.safeInvoke(this.props.onButtonClick, +value, value); |
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.
ok let's get clever and do both in one method:
private invokeValueCallback(value: string, callback = this.props.onButtonClick) {
Utils.safeInvoke(callback, +value, value);
}
types of oBC and oVC are the same so by assigning a default value you don't have to redeclare the function signature here!
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.
Niiiiice.
Respond to CR feedbackPreview: documentation | table |
|
||
private invokeOnButtonClickCallback(value: string) { | ||
Utils.safeInvoke(this.props.onButtonClick, +value, value); | ||
private invokeValueCallback(value: string, callback: (valueAsNumber: number, valueAsString: string) => void) { |
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.
wait i don't understand why you can't set this.props.onValueChange
as the default value for callback
parameter?
maybe the invokeValueCallback
in incrementValue
is causing those double-events?
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, finally got it. Say you define only one of the callbacks:
<NumericInput onButtonClick={...} />
or
<NumericInput onValueChange={...} />
If you define a default callback, Utils.safeInvoke
will never receive an undefined function for whichever callback is undefined; it'll simply re-invoke the default function whether in handle[*]ButtonClick
or incrementValue
. Given that, I think we should go with the explicit type definition implemented above. ^
Fixes #607 (see #607 (comment))
Checklist
Changes proposed in this pull request:
onButtonClick
prop that fires when either button is "clicked" (via mouse, focus +Space
, focus +Enter
, etc.)Reviewers should focus on: