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

remove anonymous function, fix docs in <EditableName component and add test #3649

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
51 changes: 23 additions & 28 deletions packages/table-dev-app/src/features.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,11 @@ class EditableTable extends React.Component<{}, IEditableTableState> {
<EditableCell
value={value == null ? "" : value}
intent={this.state.sparseCellIntent[dataKey]}
onCancel={this.cellValidator(rowIndex, columnIndex)}
onChange={this.cellValidator(rowIndex, columnIndex)}
onConfirm={this.cellSetter(rowIndex, columnIndex)}
rowIndex={rowIndex}
columnIndex={columnIndex}
onCancel={this.cellValidator}
onChange={this.cellValidator}
onConfirm={this.cellSetter}
/>
);
};
Expand All @@ -216,10 +218,11 @@ class EditableTable extends React.Component<{}, IEditableTableState> {
return (
<EditableName
name={name}
index={columnIndex}
intent={this.state.intents[columnIndex]}
onChange={this.nameValidator(columnIndex)}
onCancel={this.nameValidator(columnIndex)}
onConfirm={this.nameSetter(columnIndex)}
onChange={this.nameValidator}
onCancel={this.nameValidator}
onConfirm={this.nameSetter}
/>
);
};
Expand All @@ -236,36 +239,28 @@ class EditableTable extends React.Component<{}, IEditableTableState> {
return /^[a-zA-Z]*$/.test(value);
}

private nameValidator = (index: number) => {
return (name: string) => {
const intent = this.isValidValue(name) ? null : Intent.DANGER;
this.setArrayState("intents", index, intent);
this.setArrayState("names", index, name);
};
private nameValidator = (name: string, index: number) => {
const intent = this.isValidValue(name) ? null : Intent.DANGER;
this.setArrayState("intents", index, intent);
this.setArrayState("names", index, name);
};

private nameSetter = (index: number) => {
return (name: string) => {
this.setArrayState("names", index, name);
};
private nameSetter = (name: string, index: number) => {
this.setArrayState("names", index, name);
};

private cellValidator = (rowIndex: number, columnIndex: number) => {
private cellValidator = (value: string, rowIndex: number, columnIndex: number) => {
const dataKey = EditableTable.dataKey(rowIndex, columnIndex);
return (value: string) => {
const intent = this.isValidValue(value) ? null : Intent.DANGER;
this.setSparseState("sparseCellIntent", dataKey, intent);
this.setSparseState("sparseCellData", dataKey, value);
};
const intent = this.isValidValue(value) ? null : Intent.DANGER;
this.setSparseState("sparseCellIntent", dataKey, intent);
this.setSparseState("sparseCellData", dataKey, value);
};

private cellSetter = (rowIndex: number, columnIndex: number) => {
private cellSetter = (value: string, rowIndex: number, columnIndex: number) => {
const dataKey = EditableTable.dataKey(rowIndex, columnIndex);
return (value: string) => {
const intent = this.isValidValue(value) ? null : Intent.DANGER;
this.setSparseState("sparseCellData", dataKey, value);
this.setSparseState("sparseCellIntent", dataKey, intent);
};
const intent = this.isValidValue(value) ? null : Intent.DANGER;
this.setSparseState("sparseCellData", dataKey, value);
this.setSparseState("sparseCellIntent", dataKey, intent);
};

private setArrayState<T>(key: string, index: number, value: T) {
Expand Down
6 changes: 3 additions & 3 deletions packages/table/src/headers/editableName.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,19 @@ export interface IEditableNameProps extends IIntentProps, IProps {
* important to listen to if you are doing anything with `onChange` events,
* since you'll likely want to revert whatever changes you made.
*/
onCancel?: (value: string) => void;
onCancel?: (value: string, columnIndex?: number) => void;

/**
* A listener that is triggered as soon as the editable name is modified.
* This can be due, for example, to keyboard input or the clipboard.
*/
onChange?: (value: string) => void;
onChange?: (value: string, columnIndex?: number) => void;

/**
* A listener that is triggered once the editing is confirmed. This is
* usually due to the `return` (or `enter`) key press.
*/
onConfirm?: (value: string) => void;
onConfirm?: (value: string, columnIndex?: number) => void;

/**
* The index of the name in the header. If provided, this will be passed as an argument to any
Expand Down
31 changes: 31 additions & 0 deletions packages/table/test/editableNameTests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,35 @@ describe("<EditableName>", () => {
expect(onCancel.called).to.be.false;
expect(onConfirm.called).to.be.true;
});

it("passes index prop to callbacks if index was provided", () => {
const onCancelSpy = sinon.spy();
const onChangeSpy = sinon.spy();
const onConfirmSpy = sinon.spy();

const CHANGED_VALUE = "my-changed-value";
const INDEX = 17;

const elem = mount(
<EditableName
name="test-name-5000"
index={INDEX}
onCancel={onCancelSpy}
onChange={onChangeSpy}
onConfirm={onConfirmSpy}
/>,
);

elem.find(EditableText).simulate("focus");

elem.find(EditableText)
.find("input")
.simulate("change", { target: { value: CHANGED_VALUE } });
expect(onChangeSpy.firstCall.args).to.deep.equal([CHANGED_VALUE, INDEX]);

elem.find(EditableText)
.find("input")
.simulate("blur");
expect(onChangeSpy.firstCall.args).to.deep.equal([CHANGED_VALUE, INDEX]);
});
});