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

[table] add editableTextProps to allow adjusting cells during editing #3350

Merged
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
20 changes: 18 additions & 2 deletions packages/table/src/cell/editableCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
Hotkey,
Hotkeys,
HotkeysTarget,
IEditableTextProps,
Utils as CoreUtils,
} from "@blueprintjs/core";

Expand Down Expand Up @@ -65,6 +66,11 @@ export interface IEditableCellProps extends ICellProps {
* were originally provided via props.
*/
onConfirm?: (value: string, rowIndex?: number, columnIndex?: number) => void;

/**
* Props that should be passed to the EditableText when it is used to edit
*/
editableTextProps?: IEditableTextProps;
}

export interface IEditableCellState {
Expand Down Expand Up @@ -121,17 +127,27 @@ export class EditableCell extends React.Component<IEditableCellProps, IEditableC
}

public render() {
const { onCancel, onChange, onConfirm, truncated, wrapText, ...spreadableProps } = this.props;
const {
onCancel,
onChange,
onConfirm,
truncated,
wrapText,
editableTextProps,
...spreadableProps
} = this.props;

const { isEditing, dirtyValue, savedValue } = this.state;
const interactive = spreadableProps.interactive || isEditing;

let cellContents: JSX.Element = null;
if (isEditing) {
const className = editableTextProps ? editableTextProps.className : null;
cellContents = (
<EditableText
{...editableTextProps}
isEditing={true}
className={classNames(Classes.TABLE_EDITABLE_TEXT, Classes.TABLE_EDITABLE_NAME)}
className={classNames(Classes.TABLE_EDITABLE_TEXT, Classes.TABLE_EDITABLE_NAME, className)}
intent={spreadableProps.intent}
minWidth={null}
onCancel={this.handleCancel}
Expand Down
45 changes: 38 additions & 7 deletions packages/table/test/editableCellTests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ import { mount } from "enzyme";
import * as React from "react";
import * as sinon from "sinon";

import * as Classes from "../src/common/classes";
import { Classes } from "@blueprintjs/core";
import * as TableClasses from "../src/common/classes";
import { EditableCell } from "../src/index";
import { CellType, expectCellLoading } from "./cellTestUtils";

describe("<EditableCell>", () => {
it("renders", () => {
const elem = mount(<EditableCell value="test-value-5000" />);
expect(elem.find(`.${Classes.TABLE_TRUNCATED_TEXT}`).text()).to.equal("test-value-5000");
expect(elem.find(`.${TableClasses.TABLE_TRUNCATED_TEXT}`).text()).to.equal("test-value-5000");
});

it("renders loading state", () => {
Expand All @@ -39,10 +40,10 @@ describe("<EditableCell>", () => {
const VALUE_2 = "bar";

const elem = mount(<EditableCell value={VALUE_1} />);
expect(elem.find(`.${Classes.TABLE_TRUNCATED_TEXT}`).text()).to.equal(VALUE_1);
expect(elem.find(`.${TableClasses.TABLE_TRUNCATED_TEXT}`).text()).to.equal(VALUE_1);

elem.setProps({ value: VALUE_2 });
expect(elem.find(`.${Classes.TABLE_TRUNCATED_TEXT}`).text()).to.equal(VALUE_2);
expect(elem.find(`.${TableClasses.TABLE_TRUNCATED_TEXT}`).text()).to.equal(VALUE_2);
});

it("edits", () => {
Expand Down Expand Up @@ -83,7 +84,7 @@ describe("<EditableCell>", () => {

// start editing
elem.setState({ isEditing: true, dirtyValue: "test-value-5000" });
const input = elem.find(`.${Classes.TABLE_EDITABLE_TEXT} input`);
const input = elem.find(`.${TableClasses.TABLE_EDITABLE_TEXT} input`);
expect(input.length).to.equal(1);

// make changes
Expand Down Expand Up @@ -139,11 +140,41 @@ describe("<EditableCell>", () => {

it("defaults to no wrapText", () => {
const elem = mount(<EditableCell />);
expect(elem.find(`.${Classes.TABLE_NO_WRAP_TEXT}`).exists()).to.be.true;
expect(elem.find(`.${TableClasses.TABLE_NO_WRAP_TEXT}`).exists()).to.be.true;
});

it("wraps text when wrapText is true", () => {
const elem = mount(<EditableCell wrapText={true} />);
expect(elem.find(`.${Classes.TABLE_NO_WRAP_TEXT}`).exists()).to.be.false;
expect(elem.find(`.${TableClasses.TABLE_NO_WRAP_TEXT}`).exists()).to.be.false;
});

it("Passes editableTextProps to inner EditableText", () => {
const onCancel = sinon.spy();
const onChange = sinon.spy();
const onConfirm = sinon.spy();

const elem = mount(
<EditableCell
value="test-value-5000"
onCancel={onCancel}
onChange={onChange}
onConfirm={onConfirm}
editableTextProps={{
className: "input-only-class",
maxLength: 345,
value: "ignore this",
}}
/>,
);

// start editing
elem.setState({ isEditing: true, dirtyValue: "test-value-5000" });
const input = elem.find("input");
// input props that EditableCell does not care about should pass through unchanged
expect(input.prop("maxLength")).to.equal(345);
expect(elem.find(`.${Classes.EDITABLE_TEXT}`).prop("className")).to.contain("input-only-class");

// But special values should be overridden by EditableCell
expect(input.prop("value")).to.equal("test-value-5000");
});
});