parent
464c8e3c34
commit
4d48eb2cde
@ -0,0 +1,85 @@
|
||||
import { ControlComponent } from '../../../../dataset/enum/Control'
|
||||
import { KeyMap } from '../../../../dataset/enum/Keymap'
|
||||
import { IControlInstance } from '../../../../interface/Control'
|
||||
import { IElement } from '../../../../interface/Element'
|
||||
import { Control } from '../Control'
|
||||
|
||||
export class CheckboxControl implements IControlInstance {
|
||||
|
||||
private element: IElement
|
||||
private control: Control
|
||||
|
||||
constructor(element: IElement, control: Control) {
|
||||
this.element = element
|
||||
this.control = control
|
||||
}
|
||||
|
||||
public getElement(): IElement {
|
||||
return this.element
|
||||
}
|
||||
|
||||
public getCode(): string | null {
|
||||
return this.element.control?.code || null
|
||||
}
|
||||
|
||||
public getValue(): IElement[] {
|
||||
const elementList = this.control.getElementList()
|
||||
const { startIndex } = this.control.getRange()
|
||||
const startElement = elementList[startIndex]
|
||||
const data: IElement[] = []
|
||||
// 向左查找
|
||||
let preIndex = startIndex
|
||||
while (preIndex > 0) {
|
||||
const preElement = elementList[preIndex]
|
||||
if (
|
||||
preElement.controlId !== startElement.controlId ||
|
||||
preElement.controlComponent === ControlComponent.PREFIX
|
||||
) {
|
||||
break
|
||||
}
|
||||
if (preElement.controlComponent === ControlComponent.VALUE) {
|
||||
data.unshift(preElement)
|
||||
}
|
||||
preIndex--
|
||||
}
|
||||
// 向右查找
|
||||
let nextIndex = startIndex + 1
|
||||
while (nextIndex < elementList.length) {
|
||||
const nextElement = elementList[nextIndex]
|
||||
if (
|
||||
nextElement.controlId !== startElement.controlId ||
|
||||
nextElement.controlComponent === ControlComponent.POSTFIX
|
||||
) {
|
||||
break
|
||||
}
|
||||
if (nextElement.controlComponent === ControlComponent.VALUE) {
|
||||
data.push(nextElement)
|
||||
}
|
||||
nextIndex++
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
public setValue(): number {
|
||||
const { endIndex } = this.control.getRange()
|
||||
return endIndex
|
||||
}
|
||||
|
||||
public keydown(evt: KeyboardEvent): number {
|
||||
const range = this.control.getRange()
|
||||
// 收缩边界到Value内
|
||||
this.control.shrinkBoundary()
|
||||
const { startIndex, endIndex } = range
|
||||
// 删除
|
||||
if (evt.key === KeyMap.Backspace || evt.key === KeyMap.Delete) {
|
||||
return this.control.removeControl(startIndex)
|
||||
}
|
||||
return endIndex
|
||||
}
|
||||
|
||||
public cut(): number {
|
||||
const { endIndex } = this.control.getRange()
|
||||
return endIndex
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,11 +1,13 @@
|
||||
export enum ControlType {
|
||||
TEXT = 'text',
|
||||
SELECT = 'select'
|
||||
SELECT = 'select',
|
||||
CHECKBOX = 'checkbox'
|
||||
}
|
||||
|
||||
export enum ControlComponent {
|
||||
PREFIX = 'prefix',
|
||||
POSTFIX = 'postfix',
|
||||
PLACEHOLDER = 'placeholder',
|
||||
VALUE = 'value'
|
||||
VALUE = 'value',
|
||||
CHECKBOX = 'checkbox'
|
||||
}
|
||||
Loading…
Reference in new issue