文本框

默认小部件,一般用于字符串元素。

代码演示

基础样例

最简单的用法。

expand code expand code
import { Component, inject } from '@angular/core';

import { YelonFormModule, SFSchema, SFStringWidgetSchema, SFValueChange } from '@yelon/form';
import { NzMessageService } from 'ng-zorro-antd/message';

@Component({
  selector: 'form-string-simple',
  template: `<sf [schema]="schema" (formValueChange)="valueChange($event)" (formSubmit)="submit($event)" />`,
  standalone: true,
  imports: [YelonFormModule]
})
export class FormStringSimpleComponent {
  private readonly msg = inject(NzMessageService);
  schema: SFSchema = {
    properties: {
      name: {
        type: 'string',
        title: 'Name',
        ui: {
          addOnAfter: 'RMB',
          placeholder: 'RMB结算',
          change: val => console.log(val),
          focus: e => console.log('focus', e),
          blur: e => console.log('blur', e),
          enter: e => console.log('enter', e)
        } as SFStringWidgetSchema
      },
      mobile: {
        type: 'string',
        format: 'mobile',
        title: '手机号'
      },
      sfz: {
        type: 'string',
        format: 'id-card',
        title: '身份证号'
      },
      regex: {
        type: 'string',
        pattern: `^[abc]+$`,
        title: '正则表达式',
        ui: {
          placeholder: `^[abc]+$`
        } as SFStringWidgetSchema
      },
      color: {
        type: 'string',
        format: 'color',
        title: '颜色',
        ui: {
          optionalHelp: {
            text: '我是有背景颜色的喔',
            bgColor: '#f50'
          }
        }
      }
    },
    required: ['name']
  };

  submit(value: {}): void {
    this.msg.success(JSON.stringify(value));
  }

  valueChange(res: SFValueChange): void {
    this.msg.info(JSON.stringify(res));
  }
}
抖动与顺序

使用 changeDebounceTime 启用节流控制,并利用 changeMapchange 配合完成节流、请求顺序。

expand code expand code
import { Component, ViewChild, inject } from '@angular/core';
import { of } from 'rxjs';

import { YelonFormModule, SFComponent, SFSchema, SFStringWidgetSchema } from '@yelon/form';
import { _HttpClient } from '@yelon/theme';
import { NzMessageService } from 'ng-zorro-antd/message';

interface UserItem {
  id: number;
  email: string;
}

@Component({
  selector: 'form-string-time',
  template: `<sf #sf [schema]="schema" (formSubmit)="submit($event)" />`,
  standalone: true,
  imports: [YelonFormModule]
})
export class FormStringTimeComponent {
  private readonly msg = inject(NzMessageService);
  private readonly http = inject(_HttpClient);
  @ViewChild('sf') private readonly sf!: SFComponent;

  schema: SFSchema = {
    properties: {
      q: {
        type: 'string',
        title: 'Key',
        ui: {
          changeDebounceTime: 500,
          changeMap: v => (v?.length > 0 ? this.http.get(`/users?q=${v}&ps=6`) : of({ list: [] })),
          change: (res: { list: UserItem[] }) => {
            if (res.list.length <= 0) return;
            const property = this.sf.getProperty('/items')!;
            property.schema.enum = res.list.map(item => item.email);
            property.widget.reset(res.list[0].email);
          }
        } as SFStringWidgetSchema
      },
      items: {
        type: 'string',
        enum: ['a', 'b', 'c']
      }
    }
  };

  submit(value: {}): void {
    this.msg.success(JSON.stringify(value));
  }
}

API

schema属性

成员说明类型默认值
[maxLength]表单最大长度number-
[readOnly]禁用状态boolean-

ui属性

成员说明类型默认值
[type]等同 input 的 type 值,例如:passwordstring-
[placeholder]在文字框中显示提示讯息string-
[borderless]是否隐藏边框booleanfalse
[autocomplete]自动完成功能的表单HTML Attribute-
[autofocus]当页面加载时获得焦点HTML Attribute-
[addOnBefore]前置标签,等同 nzAddOnBeforestring-
[addOnAfter]后置标签,等同 nzAddOnAfterstring-
[addOnBeforeIcon]前置Icon,等同 nzAddOnBeforeIconstring-
[addOnAfterIcon]后置Icon,等同 nzAddOnAfterIconstring-
[prefix]带有前缀图标的 input,等同 nzPrefixstring-
[prefixIcon]前缀图标,等同 nzPrefixIconstring-
[suffix]带有后缀图标的 input,等同 nzSuffixstring-
[suffixIcon]后缀图标,等同 nzSuffixIconstring-
[changeDebounceTime]change 事件节流与顺序控制的阀值number-
[changeMap]转换数据,相当于 switchMap 操作(val: string) => Observable<any>-
[change]内容变更事件(val: string) => void-
[focus]焦点事件(e: FocusEvent) => void-
[blur]失焦事件(e: FocusEvent) => void-
[enter]回车事件(e: KeyboardEvent) => void-
Loading...