@suyouwanggang/p-ui
Version:
`p-ui`是一套使用原生`Web Components`规范开发的跨框架UI组件库,基于`lit-elment`库开发。 [github项目地址](https://github.com/suyouwanggang/p-ui)
67 lines (58 loc) • 2.34 kB
text/typescript
type Constructor<T = {}> = new (...args: any[]) => T;
type validateType = 'number' | 'email' | 'date'|'';
export default function FormElementAble<LitElement extends Constructor>(Base: LitElement) {
return class extends Base {
form: HTMLFormElement=null;
novalidate: boolean = false;
disabled: boolean = false;
value: string = '';
pattern: string ='';
required: boolean = false;
readOnly: boolean = false;
errorTips: string = '';
type: validateType = '';
min: number = NaN;
max: number = NaN;
// tslint:disable-next-line: no-any
$customValidity: any;
invalid: boolean = false;
reset() {
this.invalid = false;
this.errorTips ="";
}
get customValidity() {
return this.$customValidity;
}
// tslint:disable-next-line: no-any
set customValidity(f: any) {
this.$customValidity = f;
}
checkValidity(): boolean {
if (this.novalidate || this.disabled || this.form && this.form.novalidate) {
return true;
}
return false;
}
get validity() {
if (this.pattern && this.pattern != "") {
const reg = new RegExp(this.pattern);
let result = reg.test(this.value);
if (!result) {
this.errorTips = `请于请求格式:${this.pattern}一致`;
return false;
}
}
if (this.type == 'number') {
const n = new Number(this.value);
let result = isNaN(n);
if (!result) {
this.errorTips = `请输入正确的数字`;
}
}
return this.customValidity.method(this);
}
connectedCallback(){
this.form=(this as any).closest('form');
}
}
}