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

feat: instant applying of built-in editor #1611

Merged
merged 3 commits into from Mar 18, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/toast-ui.grid/cypress/integration/datePicker.spec.ts
Expand Up @@ -51,6 +51,10 @@ const columns = [
},
},
},
{
name: 'instantApply',
editor: { type: 'datePicker', options: { instantApply: true } },
},
];
const data = [
{
Expand All @@ -60,6 +64,7 @@ const data = [
timePickerWithTab: '2019-11-11 11:11 AM',
monthPicker: '2019-11',
yearPicker: '2019',
instantApply: '2019-11-11',
},
];

Expand Down Expand Up @@ -166,3 +171,10 @@ it('focus the editing cell when datepicker layer is closed', () => {

cy.get('@editingInput').should('be.focused');
});

it('should apply selected value instantly when instantApply option is true', () => {
cy.gridInstance().invoke('startEditing', 0, 'instantApply');
cy.get('.tui-calendar-date').contains('14').click();

cy.getCell(0, 'instantApply').should('have.text', '2019-11-14');
});
71 changes: 71 additions & 0 deletions packages/toast-ui.grid/cypress/integration/editor.spec.ts
Expand Up @@ -530,6 +530,77 @@ describe('select, checkbox, radio editor', () => {
});
});
});

describe('Inatant apply', () => {
function createGridWithTypeAndInstantApply(type: string) {
adhrinae marked this conversation as resolved.
Show resolved Hide resolved
const columns = [
{
name: 'name',
formatter: 'listItemText',
editor: {
type,
options: {
listItems: [
{ text: 'A', value: '1' },
{ text: 'B', value: '2' },
{ text: 'C', value: '3' },
],
instantApply: true,
},
},
},
{
name: 'isHuman',
defaultValue: '2',
formatter: 'listItemText',
editor: {
type,
options: {
listItems: [
{ text: 'true', value: '1' },
{ text: 'false', value: '2' },
],
instantApply: true,
},
},
},
];

cy.createGrid({ data: dataForGridWithType, columns });
}

it('should apply selected value instantly when instantApply option is true(select)', () => {
createGridWithTypeAndInstantApply('select');
cy.gridInstance().invoke('setFrozenColumnCount', 1);
cy.gridInstance().invoke('startEditing', 1, 'isHuman');

cy.get('.tui-select-box-item').eq(0).click();

cy.getByCls('editor-select-box-layer').should('be.not.visible');
cy.getCell(1, 'isHuman').should('have.text', 'true');
});

it('should apply selected value instantly when instantApply option is true(radio)', () => {
createGridWithTypeAndInstantApply('radio');
cy.gridInstance().invoke('setFrozenColumnCount', 1);
cy.gridInstance().invoke('startEditing', 1, 'isHuman');

cy.getByCls(`editor-label-icon-radio`).eq(0).click();

cy.getByCls('editor-checkbox-list-layer').should('be.not.visible');
cy.getCell(1, 'isHuman').should('have.text', 'true');
});

it('should not apply selected value instantly when instantApply option is true(checkbox)', () => {
createGridWithTypeAndInstantApply('checkbox');
cy.gridInstance().invoke('setFrozenColumnCount', 1);
cy.gridInstance().invoke('startEditing', 1, 'isHuman');

cy.getByCls(`editor-label-icon-checkbox`).eq(0).click();

cy.getByCls('editor-checkbox-list-layer').should('be.visible');
});
});
});

// @TODO: should rewrite test case after modifying casting editing value
Expand Down
14 changes: 12 additions & 2 deletions packages/toast-ui.grid/src/editor/checkbox.ts
Expand Up @@ -37,14 +37,17 @@ export class CheckboxEditor implements CellEditor {

private initLayerPos: LayerPos | null = null;

private instantApplyCallback?: Function;

public constructor(props: CellEditorProps) {
const { columnInfo, width, formattedValue, portalEditingKeydown } = props;
const { columnInfo, width, formattedValue, portalEditingKeydown, instantApplyCallback } = props;
const { type: inputType, instantApply } = columnInfo.editor?.options ?? {};
const el = document.createElement('div');
const value = String(isNil(props.value) ? '' : props.value);
el.className = cls('layer-editing-inner');
el.innerText = formattedValue;

this.inputType = columnInfo.editor!.options!.type;
this.inputType = inputType;

const listItems = getListItems(props);
const layer = this.createLayer(listItems, width);
Expand All @@ -54,6 +57,10 @@ export class CheckboxEditor implements CellEditor {
this.layer = layer;

this.setValue(value);

if (instantApply && inputType === 'radio') {
this.instantApplyCallback = instantApplyCallback;
}
}

private createLayer(listItems: ListItem[], width: number) {
Expand Down Expand Up @@ -119,6 +126,9 @@ export class CheckboxEditor implements CellEditor {
private onChange = (ev: Event) => {
const value = (ev.target as HTMLInputElement).value;
this.setLabelClass(value);

// eslint-disable-next-line no-unused-expressions
Copy link

Choose a reason for hiding this comment

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

주석처리 없인 룰 만족이 안되나요?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

넵, 기존에 다른 부분에서는 어떻게 사용했나 봤더니 주석처리 해서 사용하고 있었습니다...

this.instantApplyCallback?.();
};

private onKeydown = (ev: KeyboardEvent) => {
Expand Down
8 changes: 7 additions & 1 deletion packages/toast-ui.grid/src/editor/datePicker.ts
Expand Up @@ -57,6 +57,7 @@ export class DatePickerEditor implements CellEditor {
const {
grid: { usageStatistics },
columnInfo,
instantApplyCallback,
} = props;
const value = String(isNil(props.value) ? '' : props.value);
const el = document.createElement('div');
Expand Down Expand Up @@ -103,7 +104,12 @@ export class DatePickerEditor implements CellEditor {
};

this.datePickerEl = new TuiDatePicker(layer, deepMergedCopy(defaultOptions, options));
this.datePickerEl.on('close', () => this.focus());
this.datePickerEl.on('close', () => {
this.focus();
if (options.instantApply) {
instantApplyCallback();
}
});
}

public moveDropdownLayer(gridRect: GridRectForDropDownLayerPos) {
Expand Down
8 changes: 7 additions & 1 deletion packages/toast-ui.grid/src/editor/select.ts
Expand Up @@ -30,7 +30,9 @@ export class SelectEditor implements CellEditor {
private initLayerPos: LayerPos | null = null;

public constructor(props: CellEditorProps) {
const { width, formattedValue, portalEditingKeydown } = props;
const { width, formattedValue, portalEditingKeydown, columnInfo, instantApplyCallback } = props;

const { instantApply } = columnInfo.editor?.options ?? {};
const el = document.createElement('div');
const value = String(isNil(props.value) ? '' : props.value);
el.className = cls('layer-editing-inner');
Expand All @@ -43,6 +45,10 @@ export class SelectEditor implements CellEditor {
this.el = el;
this.layer = layer;
this.layer.addEventListener('keydown', this.onKeydown);

if (instantApply) {
this.selectBoxEl.on('close', instantApplyCallback);
}
}

private onKeydown = (ev: KeyboardEvent) => {
Expand Down
5 changes: 3 additions & 2 deletions packages/toast-ui.grid/src/view/editingLayer.tsx
Expand Up @@ -94,7 +94,7 @@ export class EditingLayerComp extends Component<Props> {
});
}

private saveAndFinishEditing(triggeredByKey = false) {
private saveAndFinishEditing = (triggeredByKey = false) => {
const { dispatch, active } = this.props;

if (this.editor && active) {
Expand All @@ -103,7 +103,7 @@ export class EditingLayerComp extends Component<Props> {
dispatch('setValue', rowKey, columnName, value);
dispatch('finishEditing', rowKey, columnName, value, { save: true, triggeredByKey });
}
}
};

private setInitScrollPos() {
const { bodyScrollTop, bodyScrollLeft } = this.props;
Expand Down Expand Up @@ -132,6 +132,7 @@ export class EditingLayerComp extends Component<Props> {
formattedValue,
width: right - left,
portalEditingKeydown: this.handleKeyDown,
instantApplyCallback: this.saveAndFinishEditing,
};
const cellEditor = new EditorClass(editorProps);
const editorEl = cellEditor.getElement();
Expand Down
1 change: 1 addition & 0 deletions packages/toast-ui.grid/types/editor/index.d.ts
Expand Up @@ -17,6 +17,7 @@ export interface CellEditorProps {
formattedValue: string;
width: number;
portalEditingKeydown: PortalEditingKeydown;
instantApplyCallback: Function;
}

export interface CellEditor {
Expand Down