Chuyển tới nội dung chính

Input / Form

Cú pháp

<zmp-list-input> <zmp-input>

Zalo Mini App framework cung cấp các component input để người dùng nhập dữ liệu. Các component này được sử dụng trong <form> hoặc <zmp-list>, và có thể sử dụng Form Methods để lấy data từ input.

Properties

Input properties

PropTypeDefaultDescription
wrapbooleantrueXác định input element được bao bởi <div className="input"> element hoặc không.
typestringInput type. Tất cả input type mặc định của HTML5: text, password, number,... Ngoài ra cò có một số type đặc biệt:
  • textarea - Render Textarea
  • select - Render Select
  • datepicker - Render Date Picker
  • colorpicker - Render Color Picker
  • texteditor - Render Text Editor
resizablebooleanfalseTrường hợp type là textarea thì resizable cho phép textarea element có khả năng thay đổi kích thước
min-rowsnumberSử dụng khi type="textarea"resizable. Biểu thị số dòng khởi tạo của textarea trước khi resize ( giá trị từ 1 đến 20)
max-rowsnumberSử dụng khi type="textarea"resizable. Biểu thị số dòng đạt được sẽ kết thúc resize ( giá trị từ 1 đến 20)
input-styleobjectTùy chỉnh style cho input element
clear-buttonbooleanfalseThêm nút xóa giá trị của input khi người dùng click/press
validatebooleanfalseCho phép input element thực hiện kiểm tra hợp lệ khi giá trị input thay đổi dựa vao pattern cụ thể hoặc mặc định của HTML5 . Trường hợp muốn tùy chỉnh việc kiểm tra hợ lệ, set validation thành false và sử dụng error-message cùng với error-message-force props.
validate-on-blurbooleanfalseThực hiện kiểm tra hợp lệ khi người dùng bỏ focus vào input element
error-messagestringTùy chỉnh Error message hiển thị khi giá trị của input không hợp lệ
error-message-forcebooleanfalseBuộc input element thông báo lỗi không hợp lệ
infostringThêm thông tin thêm về input element
namestringInput name
placeholderstringInput placeholder
idstringGiá trị thuộc tính ID cho Input wrapper element
input-idstringGiá trị thuộc tính ID cho Input element
valuestring | numberGiá trị của input.
  • Trường hợp type="datepicker" Giá trị sẽ là mảng các ngày, ví dụ :value="[new Date()]"
  • Trường hợp type="colorpicker" Giá trị là Object thông tin colors, ví dụ :value="{ hex: '#ff0000' }"
  • Trường hợp type="texteditor" giá trị sẽ là HTML string
default-valuestring | numberGiá trị mặc định của input, trong trường hợp sử dụng uncontrolled component
inputmodestringGiá trị thuộc tính input's native "inputmode"
sizestring | numberGiá trị thuộc tính input's native "size"
patternstringGiá trị thuộc tính input's native "pattern"
acceptstring | numberGiá trị thuộc tính input's native "accept"
autocompletestringGiá trị thuộc tính input's native "autocomplete"
autofocusbooleanfalseGiá trị thuộc tính input's native "autofocus"
autosavestringGiá trị thuộc tính input's native "autosave"
disabledbooleanfalseVô hiệu hóa input element
maxstring | numberGiá trị thuộc tính input's native "max"
minstring | numberGiá trị thuộc tính input's native "min"
stepstring | numberGiá trị thuộc tính input's native "step"
maxlengthstring | numberGiá trị thuộc tính input's native "maxlength"
minlengthstring | numberGiá trị thuộc tính input's native "minlength"
multiplebooleanfalseGiá trị thuộc tính input's native "multiple"
readonlybooleanfalseReadonly Input
requiredbooleanfalseRequired Input
tabindexstring | numberGiá trị thuộc tính input's native "tabindex"
calendar-paramsobjectObject với các Calendar Parameters sử dung trong trường hợp type="datepicker"
color-picker-paramsobjectObject với các Color Picker Parameters sử dung trong trường hợp type="colorpicker"
text-editor-paramsobjectObject với các Text Editor Parameters sử dung trong trường hợp type="texteditor"

List Input properties

PropTypeDefaultDescription
labelstringInput's label
inputbooleantrueRender input element hoặc không
typestringInput type. Tất cả input type mặc định của HTML5: text, password, number,... Ngoài ra cò có một số type đặc biệt:
  • textarea - Render Textarea
  • select - Render Select
  • datepicker - Render Date Picker
  • colorpicker - Render Color Picker
  • texteditor - Render Text Editor
resizablebooleanfalseTrường hợp type là textarea thì resizable cho phép textarea element có khả năng thay đổi kích thước
min-rowsnumberSử dụng khi type="textarea"resizable. Biểu thị số dòng khởi tạo của textarea trước khi resize ( giá trị từ 1 đến 20)
max-rowsnumberSử dụng khi type="textarea"resizable. Biểu thị số dòng đạt được sẽ kết thúc resize ( giá trị từ 1 đến 20)
input-styleobjectTùy chỉnh style cho input element
clear-buttonbooleanfalseThêm nút xóa giá trị của input khi người dùng click/press
validatebooleanfalseCho phép input element thực hiện kiểm tra hợp lệ khi giá trị input thay đổi dựa vao pattern cụ thể hoặc mặc định của HTML5 . Trường hợp muốn tùy chỉnh việc kiểm tra hợ lệ, set validation thành false và sử dụng error-message cùng với error-message-force props.
validate-on-blurbooleanfalseThực hiện kiểm tra hợp lệ khi người dùng bỏ focus vào input element
error-messagestringTùy chỉnh Error message hiển thị khi giá trị của input không hợp lệ
error-message-forcebooleanfalseBuộc input element thông báo lỗi không hợp lệ
infostringThêm thông tin thêm về input element
namestringInput name
placeholderstringInput placeholder
idstringGiá trị thuộc tính ID cho Input wrapper element
input-idstringGiá trị thuộc tính ID cho Input element
valuestring | numberGiá trị của input, tương tự property value của component Input
default-valuestring |numberGiá trị mặc định của input, trong trường hợp sử dụng uncontrolled component
inputmodestringGiá trị thuộc tính input's native "inputmode"
sizestring
numberGiá trị thuộc tính input's native "size"
patternstringGiá trị thuộc tính input's native "pattern"
acceptstring | numberGiá trị thuộc tính input's native "accept"
autocompletestringGiá trị thuộc tính input's native "autocomplete"
autofocusbooleanfalseGiá trị thuộc tính input's native "autofocus"
autosavestringGiá trị thuộc tính input's native "autosave"
disabledbooleanfalseVô hiệu hóa input element
maxstring | numberGiá trị thuộc tính input's native "max"
minstring |numberGiá trị thuộc tính input's native "min"
stepstring | numberGiá trị thuộc tính input's native "step"
maxlengthstring |numberGiá trị thuộc tính input's native "maxlength"
minlengthstring | numberGiá trị thuộc tính input's native "minlength"
multiplebooleanfalseGiá trị thuộc tính input's native "multiple"
readonlybooleanfalseReadonly Input
requiredbooleanfalseRequired Input
tabindexstring | numberGiá trị thuộc tính input's native "tabindex"
wrapbooleantrueListInput element sẽ được bao bởi <li> element
calendar-paramsobjectObject với các Calendar Parameters sử dụng trong trường hợp type="datepicker"
color-picker-paramsobjectObject với các Color Picker Parameters sử dụng trong trường hợp type="colorpicker"
text-editor-paramsobjectObject với các Text Editor Parameters sử dụng trong trường hợp type="texteditor"

Events

EventDescription
@focusKích hoạt khi người dùng focus vào input.
@blurKích hoạt khi người dùng không còn focus vào input.
@inputKích hoạt khi giá trị của input thay đổi. Lưu Ý: Input event triggers sau beforeinput, keypress, keyup, keydown events.
@changeKích hoạt khi giá trị của input thay đổi sau khi người dùng không còn focus (blur) vào input
@input-clearKích hoạt khi người dùng click vào button xóa dữ liệu
@textarea-resizeKích hoạt khi textarea thay đổi kích thước. event.detail là object bao gồm initialHeight, currentHeight và scrollHeight properties
@input-emptyKích hoạt khi giá trị của input element thành trống
@input-notemptyKích hoạt khi giá trị của input element khong còn trống
@calendar-changeKích hoạt type="datepicker" khi giá trị của calendar thay đổi. Nhận vào mảng với các ngày được chọn.
@text-editor-changeKích hoạt khi type="texteditor" giá trị của Text Editor thay đổi. Nhận vào giá trị của text editor (HTML string).

Slots

SlotDescription
defaultĐối với type="select" hoặc type="textarea" - element sẽ được chèn vào trong select họăc textarea tags.
infoSlot sẽ được chèn vào trong container info message
error-messageSlot sẽ được chèn vào trong container error message
labelSlot sẽ được chèn vào trong container input label
inputSlot sẽ thay thế input element (input phải set thành false)
root-startSlot đuợc chèn vào đầu của <li> element
root / root-endSlot được chèn vào cuối của <li> element
content-startSlot đuợc chèn vào đầu của <div className="item-content"> element
content / content-endSlot đuợc chèn vào cuôi của <div className="item-content"> element
inner-startSlot đuợc chèn vào đầu của <div className="item-inner"> element
inner / inner-endSlot đuợc chèn vào cuối của <div className="item-inner"> element

Form Methods

Zalo Mini App cung cấp các methods để làm việc với input và form, trong đó convertToDatafillFormData là 2 methods hữu ích giúp lấy form data và điền nội dung cho form:

import { zmp } from "zmp-vue";

Lấy form data dưới dạng object

zmp.form.convertToData(form);
  • form (required) - HTMLElement hoặc string (CSS Selector) của form cần lấy data.
  • Method trả về object

Điền nội dung form từ data

zmp.form.fillFromData(form, data);
  • form (required) - HTMLElement hoặc string (CSS Selector) của form cần điền data.
  • data (required) - object object chứa form data.
  • Lưu ý mỗi input cần cung cấp thuộc tính name, name sẽ trở thành object filed khi lấy data cũng như điền nội data
  • Checkboxes và "multiple" selects biểu diễn dưới dạng mảng (Arrays)

Ví dụ

Code demo fallback when rendering server side!