Skip to content

Commit

Permalink
feat: add number control #925
Browse files Browse the repository at this point in the history
  • Loading branch information
Hufe921 committed Dec 10, 2024
1 parent 012dc7d commit aff4979
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 5 deletions.
3 changes: 2 additions & 1 deletion docs/en/guide/schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ interface IElement {
SELECT = 'select',
CHECKBOX = 'checkbox',
RADIO = 'radio',
DATE = 'date'
DATE = 'date',
NUMBER = 'number'
};
value: IElement[] | null;
placeholder?: string;
Expand Down
3 changes: 2 additions & 1 deletion docs/guide/schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ interface IElement {
SELECT = 'select',
CHECKBOX = 'checkbox',
RADIO = 'radio'
DATE = 'date'
DATE = 'date',
NUMBER = 'number'
};
value: IElement[] | null;
placeholder?: string;
Expand Down
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@
<div class="options">
<ul>
<li data-control='text'>文本</li>
<li data-control="number">数值</li>
<li data-control="select">列举</li>
<li data-control="date">日期</li>
<li data-control="checkbox">复选框</li>
Expand Down
26 changes: 24 additions & 2 deletions src/editor/core/draw/control/Control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import { ControlBorder } from './richtext/Border'
import { SelectControl } from './select/SelectControl'
import { TextControl } from './text/TextControl'
import { DateControl } from './date/DateControl'
import { NumberControl } from './number/NumberControl'
import { MoveDirection } from '../../../dataset/enum/Observer'
import {
CONTROL_CONTEXT_ATTR,
Expand Down Expand Up @@ -383,6 +384,8 @@ export class Control {
const dateControl = new DateControl(element, this)
this.activeControl = dateControl
dateControl.awake()
} else if (control.type === ControlType.NUMBER) {
this.activeControl = new NumberControl(element, this)
}
// 缓存控件数据
this.updateActiveControlValue()
Expand Down Expand Up @@ -763,7 +766,9 @@ export class Control {
const nextElement = elementList[j]
if (nextElement.controlId !== element.controlId) break
if (
(type === ControlType.TEXT || type === ControlType.DATE) &&
(type === ControlType.TEXT ||
type === ControlType.DATE ||
type === ControlType.NUMBER) &&
nextElement.controlComponent === ControlComponent.VALUE
) {
textControlValue += nextElement.value
Expand All @@ -773,7 +778,11 @@ export class Control {
}
j++
}
if (type === ControlType.TEXT || type === ControlType.DATE) {
if (
type === ControlType.TEXT ||
type === ControlType.DATE ||
type === ControlType.NUMBER
) {
result.push({
...element.control,
zone,
Expand Down Expand Up @@ -917,6 +926,19 @@ export class Control {
} else {
date.clearSelect(controlContext, controlRule)
}
} else if (type === ControlType.NUMBER) {
const formatValue = Array.isArray(value) ? value : [{ value }]
formatElementList(formatValue, {
isHandleFirstElement: false,
editorOptions: this.options
})
const text = new NumberControl(element, this)
this.activeControl = text
if (value) {
text.setValue(formatValue, controlContext, controlRule)
} else {
text.clearValue(controlContext, controlRule)
}
}
// 模拟控件激活后销毁
this.activeControl = null
Expand Down
3 changes: 3 additions & 0 deletions src/editor/core/draw/control/number/NumberControl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { TextControl } from '../text/TextControl'

export class NumberControl extends TextControl {}
3 changes: 2 additions & 1 deletion src/editor/dataset/enum/Control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ export enum ControlType {
SELECT = 'select',
CHECKBOX = 'checkbox',
RADIO = 'radio',
DATE = 'date'
DATE = 'date',
NUMBER = 'number'
}

export enum ControlComponent {
Expand Down
42 changes: 42 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,48 @@ window.onload = function () {
}
})
break
case ControlType.NUMBER:
new Dialog({
title: '数值控件',
data: [
{
type: 'text',
label: '占位符',
name: 'placeholder',
required: true,
placeholder: '请输入占位符'
},
{
type: 'text',
label: '默认值',
name: 'value',
placeholder: '请输入默认值'
}
],
onConfirm: payload => {
const placeholder = payload.find(
p => p.name === 'placeholder'
)?.value
if (!placeholder) return
const value = payload.find(p => p.name === 'value')?.value || ''
instance.command.executeInsertControl({
type: ElementType.CONTROL,
value: '',
control: {
type,
value: value
? [
{
value
}
]
: null,
placeholder
}
})
}
})
break
default:
break
}
Expand Down

0 comments on commit aff4979

Please sign in to comment.