Skip to content

Commit

Permalink
Merge pull request #1850 from VisActor/feat/inputEditorConfig
Browse files Browse the repository at this point in the history
Feat/input editor config
  • Loading branch information
fangsmile committed Jun 11, 2024
2 parents 631dbeb + d73bcf8 commit 2f26ad6
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"changes": [
{
"comment": "feat: add textArea editor\n\n",
"type": "none",
"packageName": "@visactor/vtable"
}
],
"packageName": "@visactor/vtable",
"email": "892739385@qq.com"
}
13 changes: 12 additions & 1 deletion docs/assets/demo/en/edit/edit-cell.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ option: ListTable-columns-text#editor

# edit cell

This example shows the editability of the table. Double-click a cell to enter the edit state. If you want to change the timing of entering the edit state, you can set:
This example shows the editability of the table. Click a cell to enter the edit state. If you want to change the timing of entering the edit state, you can set:

```
/** Edit triggering time: double click event | single click event | api to manually start editing. Default is double click 'doubleclick' */
editCellTrigger?: 'doubleclick' | 'click' | 'api';
```

The current example has four editors: input, date, list, and textArea. Different effects can be achieved by setting different editors.

For detailed introduction, please click on the tutorial to learn!

## Key Configurations
Expand All @@ -34,9 +36,11 @@ let tableInstance;
const input_editor = new VTable_editors.InputEditor();
const date_input_editor = new VTable_editors.DateInputEditor();
const list_editor = new VTable_editors.ListEditor({ values: ['girl', 'boy'] });
const textArea_editor = new VTable_editors.TextAreaEditor({ readonly: false });
VTable.register.editor('input-editor', input_editor);
VTable.register.editor('date-input-editor', date_input_editor);
VTable.register.editor('list-editor', list_editor);
VTable.register.editor('textArea-editor', textArea_editor);

function generateRandomString(length) {
let result = '';
Expand Down Expand Up @@ -135,6 +139,7 @@ const generatePersons = count => {
hobbies: generateRandomHobbies(),
birthday: generateRandomBirthday(),
tel: generateRandomPhoneNumber(),
address: `No.${i + 100} ${generateRandomString(10)} ${generateRandomString(5)}\n${generateRandomString(5)}`,
sex: i % 2 === 0 ? 'boy' : 'girl',
work: i % 2 === 0 ? 'back-end engineer' : 'front-end engineer',
city: 'beijing'
Expand Down Expand Up @@ -192,6 +197,12 @@ const columns = [
width: 100,
editor: 'list-editor'
},
{
field: 'address',
title: 'address\n(textArea editor)',
width: 350,
editor: 'textArea-editor'
},
{
field: 'tel',
title: 'telephone',
Expand Down
22 changes: 19 additions & 3 deletions docs/assets/demo/zh/edit/edit-cell.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ option: ListTable-columns-text#editor

# 编辑单元格

该示例展示了表格的可编辑能力。双击单元格,即可进入编辑状态。如果想要修改进入编辑的时机,可以设置:
该示例展示了表格的可编辑能力。单击单元格,即可进入编辑状态。如果想要修改进入编辑的时机,可以设置:

```
/** 编辑触发时机:双击事件 | 单击事件 | api手动开启编辑。默认为双击'doubleclick' */
editCellTrigger?: 'doubleclick' | 'click' | 'api';
```

当前示例中有 input、date、list、textArea 四种编辑器,可以通过设置不同的编辑器来实现不同的效果。

具体介绍可以点击教程进入学习!

## 关键配置
Expand All @@ -34,9 +36,10 @@ let tableInstance;
const input_editor = new VTable_editors.InputEditor();
const date_input_editor = new VTable_editors.DateInputEditor();
const list_editor = new VTable_editors.ListEditor({ values: ['girl', 'boy'] });
const textArea_editor = new VTable_editors.TextAreaEditor({ readonly: false });
VTable.register.editor('input-editor', input_editor);
VTable.register.editor('date-input-editor', date_input_editor);
VTable.register.editor('list-editor', list_editor);
VTable.register.editor('textArea-editor', textArea_editor);

function generateRandomString(length) {
let result = '';
Expand Down Expand Up @@ -135,6 +138,7 @@ const generatePersons = count => {
hobbies: generateRandomHobbies(),
birthday: generateRandomBirthday(),
tel: generateRandomPhoneNumber(),
address: `No.${i + 100} ${generateRandomString(10)} ${generateRandomString(5)}\n${generateRandomString(5)}`,
sex: i % 2 === 0 ? 'boy' : 'girl',
work: i % 2 === 0 ? 'back-end engineer' : 'front-end engineer',
city: 'beijing'
Expand Down Expand Up @@ -192,6 +196,12 @@ const columns = [
width: 100,
editor: 'list-editor'
},
{
field: 'address',
title: 'address\n(textArea editor)',
width: 300,
editor: 'textArea-editor'
},
{
field: 'tel',
title: 'telephone',
Expand All @@ -212,10 +222,16 @@ const option = {
container: document.getElementById(CONTAINER_ID),
records,
columns,
enableLineBreak: true,
autoWrapText: true,
limitMaxAutoWidth: 600,
heightMode: 'autoHeight',
editCellTrigger: 'click'
editCellTrigger: 'click',
keyboardOptions: {
copySelected: true,
pasteValueToCell: true,
selectAllOnCtrlA: true
}
};
tableInstance = new VTable.ListTable(option);
tableInstance.on('change_cell_value', arg => {
Expand Down
9 changes: 6 additions & 3 deletions docs/assets/guide/en/edit/edit_cell.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ npm install @visactor/vtable-editors
Introduce the required type of editor module into your code:

```javascript
import { DateInputEditor, InputEditor, ListEditor } from '@visactor/vtable-editors';
import { DateInputEditor, InputEditor, TextareaEditor, ListEditor } from '@visactor/vtable-editors';
```

### use CDN
Expand All @@ -33,17 +33,18 @@ import { DateInputEditor, InputEditor, ListEditor } from '@visactor/vtable-edito

## 2. Create editor:

The VTable-ediotrs library currently provides three editor types, including text input boxes, date pickers, drop-down lists, etc. You can choose the appropriate editor according to your needs.
The VTable-ediotrs library currently provides four editor types, including text input boxes, textarea input boxes, date pickers, drop-down lists, etc. You can choose the appropriate editor according to your needs.

Here is sample code to create an editor:

```javascript
const inputEditor = new InputEditor();
const textAreaEditor = new TextAreaEditor();
const dateInputEditor = new DateInputEditor();
const listEditor = new ListEditor({ values: ['Female', 'Male'] });
```

In the above example, we created a text input box editor (`InputEditor`), a date picker editor (`DateInputEditor`) and a drop-down list editor (`ListEditor`). You can choose the appropriate editor type according to your actual needs.
In the above example, we created a text input box editor (`InputEditor`), a multi-line text area editor (`TextAreaEditor`), a date picker editor (`DateInputEditor`) and a drop-down list editor (`ListEditor`). You can choose the appropriate editor type according to your actual needs.

## 3. Register and use the editor:

Expand All @@ -53,6 +54,7 @@ Before using the editor, you need to register the editor instance into VTable:
//Register editor to VTable
VTable.register.editor('name-editor', inputEditor);
VTable.register.editor('name-editor2', inputEditor2);
VTable.register.editor('textArea-editor', textAreaEditor);
VTable.register.editor('number-editor', numberEditor);
VTable.register.editor('date-editor', dateInputEditor);
VTable.register.editor('list-editor', listEditor);
Expand All @@ -70,6 +72,7 @@ columns: [
} },
{ title: 'age', field: 'age', editor: 'number-editor' },
{ title: 'gender', field: 'gender', editor: 'list-editor' },
{ title: 'address', field: 'address', editor: 'textArea-editor' },
{ title: 'birthday', field: 'birthDate', editor: 'date-editor' },
]
```
Expand Down
9 changes: 6 additions & 3 deletions docs/assets/guide/zh/edit/edit_cell.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ yarn add @visactor/vtable-editors
在代码中引入所需类型的编辑器模块:

```javascript
import { DateInputEditor, InputEditor, ListEditor } from '@visactor/vtable-editors';
import { DateInputEditor, InputEditor, ListEditor, TextAreaEditor } from '@visactor/vtable-editors';
```

### 使用 CDN
Expand All @@ -38,17 +38,18 @@ import { DateInputEditor, InputEditor, ListEditor } from '@visactor/vtable-edito

## 2. 创建编辑器:

VTable-ediotrs 库中目前提供了三种编辑器类型,包括文本输入框、日期选择器、下拉列表等。你可以根据需要选择合适的编辑器。(下拉列表编辑器效果还在优化中,目前比较丑哈)
VTable-ediotrs 库中目前提供了四种编辑器类型,包括文本输入框、多行文本输入框、日期选择器、下拉列表等。你可以根据需要选择合适的编辑器。(下拉列表编辑器效果还在优化中,目前比较丑哈)

以下是创建编辑器的示例代码:

```javascript
const inputEditor = new InputEditor();
const textAreaEditor = new TextAreaEditor();
const dateInputEditor = new DateInputEditor();
const listEditor = new ListEditor({ values: ['', ''] });
```

在上面的示例中,我们创建了一个文本输入框编辑器(`InputEditor`)、一个日期选择器编辑器(`DateInputEditor`)和一个下拉列表编辑器(`ListEditor`)。你可以根据实际需求选择适合的编辑器类型。
在上面的示例中,我们创建了一个文本输入框编辑器(`InputEditor`)、一个多行文本框编辑器(`TextAreaEditor`)、 一个日期选择器编辑器(`DateInputEditor`)和一个下拉列表编辑器(`ListEditor`)。你可以根据实际需求选择适合的编辑器类型。

## 3. 注册并使用编辑器:

Expand All @@ -58,6 +59,7 @@ const listEditor = new ListEditor({ values: ['女', '男'] });
// 注册编辑器到VTable
VTable.register.editor('name-editor', inputEditor);
VTable.register.editor('name-editor2', inputEditor2);
VTable.register.editor('textArea-editor', textAreaEditor);
VTable.register.editor('number-editor', numberEditor);
VTable.register.editor('date-editor', dateInputEditor);
VTable.register.editor('list-editor', listEditor);
Expand All @@ -75,6 +77,7 @@ columns: [
} },
{ title: 'age', field: 'age', editor: 'number-editor' },
{ title: 'gender', field: 'gender', editor: 'list-editor' },
{ title: 'address', field: 'address', editor: 'textArea-editor' },
{ title: 'birthday', field: 'birthDate', editor: 'date-editor' },
]
```
Expand Down
3 changes: 2 additions & 1 deletion packages/vtable-editors/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { InputEditor } from './input-editor';
import { DateInputEditor } from './date-input-editor';
import { ListEditor } from './list-editor';
export { InputEditor, DateInputEditor, ListEditor };
import { TextAreaEditor } from './textArea-editor';
export { InputEditor, DateInputEditor, ListEditor, TextAreaEditor };
export * from './types';
10 changes: 7 additions & 3 deletions packages/vtable-editors/src/input-editor.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type { EditContext, IEditor, Placement, RectProps } from './types';
import type { EditContext, IEditor, RectProps } from './types';

export interface InputEditorConfig {
max?: number;
min?: number;
readonly?: boolean;
}

export class InputEditor implements IEditor {
Expand All @@ -19,6 +18,11 @@ export class InputEditor implements IEditor {
createElement() {
const input = document.createElement('input');
input.setAttribute('type', 'text');

if (this.editorConfig?.readonly) {
input.setAttribute('readonly', `${this.editorConfig.readonly}`);
}

input.style.position = 'absolute';
input.style.padding = '4px';
input.style.width = '100%';
Expand Down
89 changes: 89 additions & 0 deletions packages/vtable-editors/src/textArea-editor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import type { EditContext, IEditor, RectProps } from './types';

export interface TextAreaEditorConfig {
readonly?: boolean;
}

export class TextAreaEditor implements IEditor {
editorType: string = 'TextArea';
editorConfig: TextAreaEditorConfig;
container: HTMLElement;
successCallback?: () => void;
element?: HTMLTextAreaElement;

constructor(editorConfig?: TextAreaEditorConfig) {
this.editorConfig = editorConfig || {};
}

createElement() {
const input = document.createElement('textArea') as HTMLTextAreaElement;
if (this.editorConfig?.readonly) {
input.setAttribute('readonly', `${this.editorConfig.readonly}`);
}
input.style.resize = 'none';
input.style.position = 'absolute';
input.style.padding = '4px';
input.style.width = '100%';
input.style.height = '100%';
input.style.boxSizing = 'border-box';
this.element = input;

this.container.appendChild(input);

// 监听键盘事件
input.addEventListener('keydown', (e: KeyboardEvent) => {
const _isSelectAll = e.key === 'a' && (e.ctrlKey || e.metaKey);
const _isTextAreaNewLine = e.key === 'Enter' && e.shiftKey;
if (_isSelectAll || _isTextAreaNewLine) {
// 阻止冒泡 防止处理成表格全选事件
e.stopPropagation();
}
});
}

setValue(value: string) {
this.element.value = typeof value !== 'undefined' ? value : '';
}

getValue() {
return this.element.value;
}

onStart({ value, referencePosition, container, endEdit }: EditContext<string>) {
this.container = container;
this.successCallback = endEdit;
if (!this.element) {
this.createElement();

if (value !== undefined && value !== null) {
this.setValue(value);
}
if (referencePosition?.rect) {
this.adjustPosition(referencePosition.rect);
}
}
this.element.focus();
// do nothing
}

adjustPosition(rect: RectProps) {
this.element.style.top = rect.top + 'px';
this.element.style.left = rect.left + 'px';
this.element.style.width = rect.width + 'px';
this.element.style.height = rect.height + 'px';
}

endEditing() {
// do nothing
}

onEnd() {
// do nothing
this.container.removeChild(this.element);
this.element = undefined;
}

isEditorElement(target: HTMLElement) {
return target === this.element;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import * as VTable from '../../src';
import { AggregationType } from '../../src/ts-types';
import { InputEditor } from '@visactor/vtable-editors';
const CONTAINER_ID = 'vTable';
const input_editor = new InputEditor({});
const input_editor = new InputEditor({
// readonly: true,
editorType: 'textArea'
});
VTable.register.editor('input', input_editor);
const generatePersons = count => {
return Array.from(new Array(count)).map((_, i) => ({
Expand Down Expand Up @@ -171,6 +174,7 @@ export function createTable() {
}),
editor: 'input',
headerEditor: 'input',
enableLineBreak: true,
aggregation(args) {
if (args.col === 1) {
return [
Expand Down

0 comments on commit 2f26ad6

Please sign in to comment.