Skip to content
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

Merged
merged 2 commits into from Jun 5, 2017

Conversation

cmslewis
Copy link
Contributor

@cmslewis cmslewis commented Jun 1, 2017

Fixes #607 (see #607 (comment))

Checklist

  • Include tests

Changes proposed in this pull request:

  • Add onButtonClick prop that fires when either button is "clicked" (via mouse, focus + Space, focus + Enter, etc.)

Reviewers should focus on:

  • Is this a good name/API for the callback?

@blueprint-bot
Copy link

Add onButtonClick prop, add test

Preview: documentation
Coverage: core | datetime

@@ -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. */
Copy link
Contributor

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."

Copy link
Contributor Author

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
Copy link
Contributor

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.

Copy link
Contributor Author

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);
Copy link
Contributor

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!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Niiiiice.

@blueprint-bot
Copy link

Respond to CR feedback

Preview: documentation | table
Coverage: core | datetime


private invokeOnButtonClickCallback(value: string) {
Utils.safeInvoke(this.props.onButtonClick, +value, value);
private invokeValueCallback(value: string, callback: (valueAsNumber: number, valueAsString: string) => void) {
Copy link
Contributor

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?

Copy link
Contributor Author

@cmslewis cmslewis Jun 5, 2017

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. ^

@cmslewis cmslewis merged commit 674f8f1 into master Jun 5, 2017
@cmslewis cmslewis deleted the cl/numeric-input-onbuttonchange branch June 5, 2017 19:35
@giladgray giladgray mentioned this pull request Jun 8, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants