Skip to content

CodeInput Verification Code Input Component

This component is generally used in scenarios for verifying users' SMS verification codes, and can also be used in combination with Huayue's keyboard component

📌 Platform Difference Notes

APP(vue)H5WeChat Mini ProgramAlipay Mini Program

⚠️ Notes

Notes

  • When disabledKeyboard is set to true, tapping the input box will not bring up the system keyboard, which is suitable for use with a custom keyboard
  • When the dot parameter is set to true, the input content will be displayed as dots, but event callbacks will return the real value
  • mode supports two modes: box (box mode) and line (bottom line mode)
  • The disabledDot parameter controls whether inputting a decimal point is disabled; the default is true (input disabled)

🏯 Basic Usage Example

html
<template>
    <hy-code-input v-model="value"></hy-code-input>
</template>

<script setup>
    import { ref } from 'vue';

    const value = ref('');
</script>

Custom Length

html
<template>
    <hy-code-input v-model="value" :maxlength="4"></hy-code-input>
</template>

<script setup>
    import { ref } from 'vue';

    const value = ref('');
</script>

Cell Spacing

html
<template>
    <hy-code-input v-model="value" :space="20"></hy-code-input>
</template>

<script setup>
    import { ref } from 'vue';

    const value = ref('');
</script>

Replace Input Content with Dots

html
<template>
    <hy-code-input v-model="value" dot></hy-code-input>
</template>

<script setup>
    import { ref } from 'vue';

    const value = ref('');
</script>

Line Mode

html
<template>
    <hy-code-input v-model="value" mode="line"></hy-code-input>
</template>

<script setup>
    import { ref } from 'vue';

    const value = ref('');
</script>

Adjust Colors

html
<template>
    <hy-code-input v-model="value" color="#f56c6c" borderColor="#f56c6c"></hy-code-input>
</template>

<script setup>
    import { ref } from 'vue';

    const value = ref('');
</script>

Hairline Border

html
<template>
    <hy-code-input v-model="value" hairline></hy-code-input>
</template>

<script setup>
    import { ref } from 'vue';

    const value = ref('');
</script>

Auto Focus

html
<template>
    <hy-code-input v-model="value" focus></hy-code-input>
</template>

<script setup>
    import { ref } from 'vue';

    const value = ref('');
</script>

No Border

html
<template>
    <hy-code-input v-model="value" :border="false" size="50"></hy-code-input>
</template>

<script setup>
    import { ref } from 'vue';

    const value = ref('');
</script>

Working with a Custom Keyboard

When you need to use a custom keyboard, set disabledKeyboard to true to prevent the system keyboard from conflicting with the custom keyboard. The custom keyboard opens automatically when the input box is tapped and gains focus.

html
<template>
    <view>
        <hy-code-input
            v-model="keyboardValue"
            :disabledKeyboard="true"
            mode="box"
            @finish="handleFinish"
            @focus="showKeyboard = true"
        ></hy-code-input>

        <hy-keyboard
            v-model:show="showKeyboard"
            mode="default"
            v-model="keyboardValue"
            :show-dot-key="false"
            close-text="Done"
            @close="showKeyboard = false"
        ></hy-keyboard>
    </view>
</template>

<script setup>
    import { ref } from 'vue';

    const keyboardValue = ref('');
    const showKeyboard = ref(false);

    const handleFinish = (value) => {
        console.log('Input complete:', value);
        showKeyboard.value = false;
        uni.showToast({
            title: `Input complete: ${value}`,
            icon: 'none',
        });
    };
</script>

Listening to Input Events

html
<template>
    <hy-code-input v-model="value" @change="handleChange" @finish="handleFinish"></hy-code-input>
</template>

<script setup>
    import { ref } from 'vue';

    const value = ref('');

    const handleChange = (value) => {
        console.log('Input content changed:', value);
    };

    const handleFinish = (value) => {
        console.log('Input complete:', value);
    };
</script>

Listening to Focus Events

html
<template>
    <hy-code-input v-model="value" @focus="handleFocus" @blur="handleBlur"></hy-code-input>
</template>

<script setup>
    import { ref } from 'vue';

    const value = ref('');

    const handleFocus = () => {
        console.log('Input box focused');
        // You can open a custom keyboard here
    };

    const handleBlur = () => {
        console.log('Input box lost focus');
    };
</script>

API

CodeInput Props

ParameterDescriptionTypeDefault
v-modelPreset valuestring|number-
adjustPositionWhether to automatically push the page up when the keyboard pops upbooleantrue
maxlengthNumber of input charactersnumber6
borderInput box without borderbooleantrue
dotWhether to fill with dotsbooleantrue
modeMode selection, see "Basic Usage" above for detailsbox|linebox
hairlineWhether to use a hairline borderbooleanfalse
spaceDistance between charactersnumber10
focusWhether to automatically gain focusbooleanfalse
boldWhether the font and input line are boldbooleanfalse
colorFont colorstring-
fontSizeFont size, unit rpxstring|number18
sizeSize of the input box, width equals height, numeric value defaults to pxstring|number35
disabledKeyboardDisable bringing up the system keyboard when tapping the input boxbooleanfalse
borderColorBorder and line colorstring-
disabledDotWhether to disable inputting the "." symbolbooleantrue
customStyleCustom external styles to be appliedCSSProperties-
customClassCustom external class namestring-

Events

Event NameDescriptionCallback Parameters
changeTriggered when the input content changes, see above for detailsvalue: current input value
finishTriggered when the number of input characters reaches maxlength, see abovevalue: current input value
focusTriggered when the input box gains focus-
blurTriggered when the input box loses focus-
03:29