Skip to content

Textarea Component ​

Used for entering multi-line text, such as chat input boxes, remarks, and other scenarios.

📌 Platform Differences ​

APP(vue)H5WeChat Mini ProgramAlipay Mini Program
✔✔✔✔

⚠️ Notes ​

Notes

  • modelValue is used for two-way binding of the input content
  • The focus property sets automatic focus; not supported on nvue, and on H5 it depends on the browser implementation
  • The formatter property is used to format input content, but WeChat Mini Program does not support passing functions via props; use the setFormatter method instead
  • The border property supports three types: surround (border on all sides), bottom (bottom border), none (no border)
  • When maxlength is set to -1, there is no limit on the maximum length
  • The component supports hy-form form validation, used via form-item
  • If you want the textarea to support line breaks, you need to set confirmType to return

🏯 Basic Usage Examples ​

html
<!-- Global usage -->
<hy-textarea v-model="value" placeholder="Please enter content"></hy-textarea>
typescript
import { ref } from 'vue';

const value = ref('');

Border Types ​

Set the border type via the border property:

html
<!-- Border on all sides (default) -->
<hy-textarea v-model="value1" border="surround" placeholder="Border on all sides"></hy-textarea>

<!-- Bottom border -->
<hy-textarea v-model="value2" border="bottom" placeholder="Bottom border"></hy-textarea>

<!-- No border -->
<hy-textarea v-model="value3" border="none" placeholder="No border"></hy-textarea>

Character Count ​

Set the count property to display a character count:

html
<hy-textarea v-model="value" placeholder="Please enter content" count></hy-textarea>

Auto Height ​

Set the autoHeight property to automatically increase the input height:

html
<hy-textarea v-model="value" autoHeight placeholder="Please enter content" count></hy-textarea>

Disabled State ​

Set the disabled property to disable the input:

html
<hy-textarea v-model="value" disabled placeholder="Disabled state"></hy-textarea>

Auto Focus ​

Set the focus property to automatically gain focus:

html
<hy-textarea v-model="value" :focus="isFocus" placeholder="Auto focus"></hy-textarea>
typescript
import { ref } from 'vue';

const isFocus = ref(true);

Custom Styles ​

Customize styles via customStyle or customClass:

html
<hy-textarea
    v-model="value"
    placeholder="Custom style"
    height="120"
    :customStyle="{ backgroundColor: '#f5f5f5', borderRadius: '10px' }"
></hy-textarea>

Formatting ​

Format input content via the formatter property:

html
<hy-textarea v-model="value" :formatter="formatter" placeholder="Only numbers allowed"></hy-textarea>
typescript
import { ref } from 'vue';

const value = ref('');

// Formatter: only allow numbers
const formatter = (val: string) => {
    return val.replace(/[^0-9]/g, '');
};

Using with Forms ​

The component supports hy-form form validation:

html
<hy-form ref="formRef">
    <hy-form-item label="Remark" prop="remark">
        <hy-textarea
            v-model="formData.remark"
            placeholder="Please enter a remark"
            :maxlength="200"
            count
        ></hy-textarea>
    </hy-form-item>
</hy-form>
typescript
import { ref, reactive } from 'vue';

const formRef = ref();
const formData = reactive({
    remark: '',
});

const rules = {
    remark: [
        { required: true, message: 'Please enter a remark' },
        { min: 5, message: 'Remark must be at least 5 characters' },
    ],
};

Chat Input Scenario ​

Combine with other components to create a chat input box:

html
<view class="chat-input">
    <hy-textarea
        v-model="message"
        autoHeight
        :maxlength="500"
        confirmType="send"
        placeholder="Type a message..."
        @confirm="sendMessage"
    ></hy-textarea>
    <hy-button type="primary" size="small" @click="sendMessage">Send</hy-button>
</view>

API ​

Textarea Props ​

ParameterDescriptionTypeDefault
modelValueContent of the inputstring-
placeholderPlaceholder shown when the input is emptystring-
placeholderClassStyle class for the placeholder[1]stringtextarea-placeholder
placeholderStyleStyle of the placeholder, in object formstring-
heightInput height, defaults to px for numeric valuesstring | number70
confirmTypeSets the text of the button at the bottom-right of the keyboard; only effective on WeChat Mini Program, App-vue, and H5stringdone
disabledWhether to disablebooleanfalse
countWhether to display the character countbooleanfalse
focusWhether to automatically gain focus; not supported on nvue, and on H5 it depends on the browser implementationbooleanfalse
autoHeightWhether to automatically increase heightbooleanfalse
fixedIf the textarea is inside a position:fixed area, you need to explicitly set the fixed property to truebooleanfalse
cursorSpacingSpecifies the distance between the cursor and the keyboardnumber0
cursorSpecifies the cursor position when focusednumber-
showConfirmBarWhether to show the bar above the keyboard with the "Done" buttonbooleantrue
selectionStartCursor start position, effective when auto-focused, must be used together with selection-endnumber-1
selectionEndCursor end position, effective when auto-focused, must be used together with selection-startnumber-1
adjustPositionWhether to automatically push the page up when the keyboard pops upbooleantrue
disableDefaultPaddingWhether to remove the default padding on iOS; only effective on WeChat Mini Programbooleanfalse
holdKeyboardWhen focused, tapping the page won't collapse the keyboard; only effective on WeChat Mini Programbooleanfalse
maxlengthMaximum input length; set to -1 for no limit on the maximum lengthnumber140
borderBorder type[2]surround | bottom | nonesurround
formatterInput filter or formatter function[3]function-
customStyleDefines external styles to be appliedCSSProperties-
customClassCustom external class namestring-

Events ​

Event NameDescriptionCallback
focusTriggered when the input gains focusevent
blurTriggered when the input loses focusevent
lineChangeTriggered when the number of lines in the input changesevent
changeTriggered on keyboard input (change event)value: string
update:modelValueTriggered on keyboard input (input event)value: string
confirmTriggered when Done is clicked (confirm event)event
keyboardheightchangeTriggered when the keyboard height changesevent

CSS Variables ​

Variable NameDescriptionDefault
--hy-textarea-min-heightMinimum height50rpx
--hy-textarea-max-heightMaximum height200rpx
--hy-textarea-font-sizeFont size15px
--hy-textarea-count-font-sizeCharacter count font size12px
--hy-textarea-colorInput text colorFollows --hy-text-color
--hy-textarea-disabled-colorDisabled text colorFollows --hy-text-color--disabled
--hy-textarea-radiusInput border radiusFollows --hy-radius-sm (8rpx)
--hy-textarea-count-radiusCharacter count border radiusFollows --hy-radius-sm (8rpx)
--hy-textarea-borderBorderFollows --hy-border-line
12:33
battery

  1. Note that when scoped is used in the page or component's style, you need to write /deep/ before the class name ↩︎

  2. surround: border on all sides; bottom: bottom border only; none: no border ↩︎

  3. WeChat Mini Program does not support passing functions via props; it is recommended to use the formatter property directly on the page (other platforms) ↩︎