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

Improve NumericInput and AbstractButton internals #4201

Merged
merged 6 commits into from
Jul 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions packages/core/src/components/button/abstractButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,20 @@ export abstract class AbstractButton<H extends React.HTMLAttributes<HTMLElement>

private currentKeyDown: number = null;

// A disabled element cannot be active, but there are cases where
// this situation can happen (NumericInput buttons), hence the need
// to check if disabled prop has been passed and change isActive
// accordingly
public static getDerivedStateFromProps(props: IButtonProps, state: IButtonState) {
if (state.isActive && props.disabled) {
return {
isActive: false,
};
}

return null;
}

public abstract render(): JSX.Element;

protected getCommonButtonProps() {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/components/forms/_numeric-input.scss
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
}
}

// fix button border radii when the buttons are on the left
// fix button border radius when the buttons are on the left
.#{$ns}-button-group.#{$ns}-vertical {
&:first-child > .#{$ns}-button {
&:first-child {
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/components/forms/numericInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import {
import * as Errors from "../../common/errors";

import { ButtonGroup } from "../button/buttonGroup";
import { Button } from "../button/buttons";
import { AnchorButton } from "../button/buttons";
import { ControlGroup } from "./controlGroup";
import { InputGroup } from "./inputGroup";
import {
Expand Down Expand Up @@ -333,13 +333,13 @@ export class NumericInput extends AbstractPureComponent2<HTMLInputProps & INumer
const disabled = this.props.disabled || this.props.readOnly;
return (
<ButtonGroup className={Classes.FIXED} key="button-group" vertical={true}>
<Button
<AnchorButton
disabled={disabled || (value !== "" && +value >= max)}
icon="chevron-up"
intent={intent}
{...this.incrementButtonHandlers}
/>
<Button
<AnchorButton
disabled={disabled || (value !== "" && +value <= min)}
icon="chevron-down"
intent={intent}
Expand Down
72 changes: 36 additions & 36 deletions packages/core/test/controls/numericInputTests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { spy } from "sinon";
import { dispatchMouseEvent, expectPropValidationError } from "@blueprintjs/test-commons";

import {
Button,
AnchorButton,
ButtonGroup,
ControlGroup,
HTMLInputProps,
Expand Down Expand Up @@ -87,7 +87,7 @@ describe("<NumericInput>", () => {
it("increments the value from 0 if the field is empty", () => {
const component = mount(<NumericInput />);

const incrementButton = component.find(Button).first();
const incrementButton = component.find(AnchorButton).first();
incrementButton.simulate("mousedown");

const value = component.state().value;
Expand Down Expand Up @@ -197,7 +197,7 @@ describe("<NumericInput>", () => {
const onValueChangeSpy = spy();
const component = mount(<NumericInput onValueChange={onValueChangeSpy} />);

const incrementButton = component.find(Button).first();
const incrementButton = component.find(AnchorButton).first();
incrementButton.simulate("mousedown");
dispatchMouseEvent(document, "mouseup");

Expand All @@ -209,8 +209,8 @@ describe("<NumericInput>", () => {
const onButtonClickSpy = spy();
const component = mount(<NumericInput onButtonClick={onButtonClickSpy} />);

const incrementButton = component.find(Button).first();
const decrementButton = component.find(Button).last();
const incrementButton = component.find(AnchorButton).first();
const decrementButton = component.find(AnchorButton).last();

// incrementing from 0
incrementButton.simulate("mousedown");
Expand Down Expand Up @@ -475,12 +475,12 @@ describe("<NumericInput>", () => {
// Enable these tests once we have a solution for testing Button onKeyUp callbacks (see PR #561)
describe("Keyboard interactions on buttons (with Space key)", () => {
const simulateIncrement = (component: ReactWrapper<any>, mockEvent: IMockEvent = {}) => {
const incrementButton = component.find(Button).first();
const incrementButton = component.find(AnchorButton).first();
incrementButton.simulate("keydown", addKeyCode(mockEvent, Keys.SPACE));
};

const simulateDecrement = (component: ReactWrapper<any>, mockEvent: IMockEvent = {}) => {
const decrementButton = component.find(Button).last();
const decrementButton = component.find(AnchorButton).last();
decrementButton.simulate("keydown", addKeyCode(mockEvent, Keys.SPACE));
};

Expand All @@ -489,12 +489,12 @@ describe("<NumericInput>", () => {

describe("Keyboard interactions on buttons (with Enter key)", () => {
const simulateIncrement = (component: ReactWrapper<any>, mockEvent?: IMockEvent) => {
const incrementButton = component.find(Button).first();
const incrementButton = component.find(AnchorButton).first();
incrementButton.simulate("keydown", addKeyCode(mockEvent, Keys.ENTER));
};

const simulateDecrement = (component: ReactWrapper<any>, mockEvent?: IMockEvent) => {
const decrementButton = component.find(Button).last();
const decrementButton = component.find(AnchorButton).last();
decrementButton.simulate("keydown", addKeyCode(mockEvent, Keys.ENTER));
};

Expand All @@ -503,12 +503,12 @@ describe("<NumericInput>", () => {

describe("Mouse interactions", () => {
const simulateIncrement = (component: ReactWrapper<any>, mockEvent?: IMockEvent) => {
const incrementButton = component.find(Button).first();
const incrementButton = component.find(AnchorButton).first();
incrementButton.simulate("mousedown", mockEvent);
};

const simulateDecrement = (component: ReactWrapper<any>, mockEvent?: IMockEvent) => {
const decrementButton = component.find(Button).last();
const decrementButton = component.find(AnchorButton).last();
decrementButton.simulate("mousedown", mockEvent);
};

Expand All @@ -520,7 +520,7 @@ describe("<NumericInput>", () => {
it("enforces no minimum bound", () => {
const component = mount(<NumericInput />);

const decrementButton = component.find(Button).last();
const decrementButton = component.find(AnchorButton).last();
decrementButton.simulate("mousedown", { shiftKey: true });
decrementButton.simulate("mousedown", { shiftKey: true });

Expand All @@ -531,7 +531,7 @@ describe("<NumericInput>", () => {
it("enforces no maximum bound", () => {
const component = mount(<NumericInput />);

const incrementButton = component.find(Button).first();
const incrementButton = component.find(AnchorButton).first();
incrementButton.simulate("mousedown", { shiftKey: true });
incrementButton.simulate("mousedown", { shiftKey: true });

Expand Down Expand Up @@ -574,7 +574,7 @@ describe("<NumericInput>", () => {
const component = mount(<NumericInput min={MIN_VALUE} />);

// try to decrement by 1
const decrementButton = component.find(Button).last();
const decrementButton = component.find(AnchorButton).last();
decrementButton.simulate("mousedown");

const newValue = component.state().value;
Expand All @@ -586,7 +586,7 @@ describe("<NumericInput>", () => {
const component = mount(<NumericInput min={MIN_VALUE} />);

// try to decrement by 1
const decrementButton = component.find(Button).last();
const decrementButton = component.find(AnchorButton).last();
decrementButton.simulate("mousedown");

const newValue = component.state().value;
Expand All @@ -598,7 +598,7 @@ describe("<NumericInput>", () => {
const component = mount(<NumericInput min={MIN_VALUE} />);

// try to decrement by 0.1
const decrementButton = component.find(Button).last();
const decrementButton = component.find(AnchorButton).last();
decrementButton.simulate("mousedown", { altKey: true });

const newValue = component.state().value;
Expand All @@ -610,7 +610,7 @@ describe("<NumericInput>", () => {
const component = mount(<NumericInput min={MIN_VALUE} />);

// try to decrement by 10
const decrementButton = component.find(Button).last();
const decrementButton = component.find(AnchorButton).last();
decrementButton.simulate("mousedown", { shiftKey: true });

const newValue = component.state().value;
Expand Down Expand Up @@ -648,7 +648,7 @@ describe("<NumericInput>", () => {
const component = mount(<NumericInput max={MAX_VALUE} />);

// try to increment by 1
const incrementButton = component.find(Button).first();
const incrementButton = component.find(AnchorButton).first();
incrementButton.simulate("mousedown");

const newValue = component.state().value;
Expand All @@ -660,7 +660,7 @@ describe("<NumericInput>", () => {
const component = mount(<NumericInput max={MAX_VALUE} />);

// try to increment in by 1
const incrementButton = component.find(Button).first();
const incrementButton = component.find(AnchorButton).first();
incrementButton.simulate("mousedown");

const newValue = component.state().value;
Expand All @@ -672,7 +672,7 @@ describe("<NumericInput>", () => {
const component = mount(<NumericInput max={MAX_VALUE} />);

// try to increment by 0.1
const incrementButton = component.find(Button).first();
const incrementButton = component.find(AnchorButton).first();
incrementButton.simulate("mousedown", { altKey: true });

const newValue = component.state().value;
Expand All @@ -684,7 +684,7 @@ describe("<NumericInput>", () => {
const component = mount(<NumericInput max={MAX_VALUE} />);

// try to increment by 10
const incrementButton = component.find(Button).first();
const incrementButton = component.find(AnchorButton).first();
incrementButton.simulate("mousedown", { shiftKey: true });

const newValue = component.state().value;
Expand Down Expand Up @@ -722,7 +722,7 @@ describe("<NumericInput>", () => {
const component = mount(<NumericInput min={2} max={2} onValueChange={onValueChangeSpy} />);
// repeated interactions, no change in state
component
.find(Button)
.find(AnchorButton)
.first()
.simulate("mousedown")
.simulate("mousedown")
Expand Down Expand Up @@ -835,7 +835,7 @@ describe("<NumericInput>", () => {
const value = component.state().value;
expect(value).to.equal("<invalid>");

const incrementButton = component.find(Button).first();
const incrementButton = component.find(AnchorButton).first();
incrementButton.simulate("mousedown");

const newValue = component.state().value;
Expand All @@ -848,7 +848,7 @@ describe("<NumericInput>", () => {
const value = component.state().value;
expect(value).to.equal("<invalid>");

const decrementButton = component.find(Button).last();
const decrementButton = component.find(AnchorButton).last();
decrementButton.simulate("mousedown");

const newValue = component.state().value;
Expand All @@ -869,8 +869,8 @@ describe("<NumericInput>", () => {
it("disables the increment button when the value is greater than or equal to max", () => {
const component = mount(<NumericInput value={100} max={100} />);

const decrementButton = component.find(Button).last();
const incrementButton = component.find(Button).first();
const decrementButton = component.find(AnchorButton).last();
const incrementButton = component.find(AnchorButton).first();

expect(decrementButton.props().disabled).to.be.false;
expect(incrementButton.props().disabled).to.be.true;
Expand All @@ -879,8 +879,8 @@ describe("<NumericInput>", () => {
it("disables the decrement button when the value is less than or equal to min", () => {
const component = mount(<NumericInput value={-10} min={-10} />);

const decrementButton = component.find(Button).last();
const incrementButton = component.find(Button).first();
const decrementButton = component.find(AnchorButton).last();
const incrementButton = component.find(AnchorButton).first();

expect(decrementButton.props().disabled).to.be.true;
expect(incrementButton.props().disabled).to.be.false;
Expand All @@ -890,8 +890,8 @@ describe("<NumericInput>", () => {
const component = mount(<NumericInput disabled={true} />);

const inputGroup = component.find(InputGroup);
const decrementButton = component.find(Button).last();
const incrementButton = component.find(Button).first();
const decrementButton = component.find(AnchorButton).last();
const incrementButton = component.find(AnchorButton).first();

expect(inputGroup.props().disabled).to.be.true;
expect(decrementButton.props().disabled).to.be.true;
Expand All @@ -902,8 +902,8 @@ describe("<NumericInput>", () => {
const component = mount(<NumericInput readOnly={true} />);

const inputGroup = component.find(InputGroup);
const decrementButton = component.find(Button).last();
const incrementButton = component.find(Button).first();
const decrementButton = component.find(AnchorButton).last();
const incrementButton = component.find(AnchorButton).first();

expect(inputGroup.props().readOnly).to.be.true;
expect(decrementButton.props().disabled).to.be.true;
Expand All @@ -926,8 +926,8 @@ describe("<NumericInput>", () => {
});

it("shows right element if provided", () => {
const component = mount(<NumericInput rightElement={<Button />} />);
expect(component.find(InputGroup).find(Button)).to.exist;
const component = mount(<NumericInput rightElement={<AnchorButton />} />);
expect(component.find(InputGroup).find(AnchorButton)).to.exist;
});

it("passed decimal value should be rounded by stepSize", () => {
Expand All @@ -942,7 +942,7 @@ describe("<NumericInput>", () => {

it("changes max precision of displayed value to that of the smallest step size defined", () => {
const component = mount(<NumericInput majorStepSize={1} stepSize={0.1} minorStepSize={0.001} />);
const incrementButton = component.find(Button).first();
const incrementButton = component.find(AnchorButton).first();

incrementButton.simulate("mousedown");
expect(component.find("input").prop("value")).to.equal("0.1");
Expand All @@ -961,7 +961,7 @@ describe("<NumericInput>", () => {

it("changes max precision appropriately when the [*]stepSize props change", () => {
const component = mount(<NumericInput majorStepSize={1} stepSize={0.1} minorStepSize={0.001} />);
const incrementButton = component.find(Button).first();
const incrementButton = component.find(AnchorButton).first();

// excess digits should truncate to max precision
component.setState({ value: "0.0001" });
Expand Down