目录

增强输入框组件

简介

增强输入框组件是原生input组件的增强 ,主要用于填写信息、搜索商品等场景。增强输入框具有如下特点:

增强输入框(easyinput)组件结构可按照布局分为两个部分。一部分是图标,另一部分是输入框。

布局结构如下图:

基本布局代码如下:

<text >左右图标</text>
<div class="example-body">
    <easyinput preimgsrc = "{{searchimgsrc}}" deletesrc = "{{deleteimgsrc}}"  @handleclick="handleClick"  @clearclick="clearClick"></easyinput>
</div>

开发指引

[h2]自定义子组件

  1. 定义布局样式。

    增强输入框整体外观都是通用的,但是内部具体的样式有区别,在设计的时候,不能把内部样式固定,否则一旦数字输入框的UI有变化,会导致子组件也要修改,违背了对外开源的初衷。

    所以,在子组件easyinput.ux中,开放多种样式属性,方便开发者自定义样式。

    <template>
      <div class="container">
            <image  src="{{this.preimgsrc}}"  class="photo" onclick="handle()" ></image>       
            <input  disabled="{{this.disabled}}"   class="selectionInput"  placeholder="{{this.placeholderinput}}" maxlength="100"  
                    value="{{this.inputValue}}"  onchange="updateValue" onselectionchange="*"  >
            </input>   
            <image  src="{{this.sufimgsrc}}"  class="photo" onclick="handle()" ></image>     
            <image  src="{{this.deletesrc}}"  class="photo" onclick="resetValue()"></image>    
      </div>
    </template>
  2. 规划属性和支持的事件。
    支持的属性:

    属性

    类型

    默认值

    描述

    preimgsrc

    String

    -

    左“搜索”图标路径。

    sufimgsrc

    String

    -

    右“搜索”图标路径(左、右“搜索”图标只能存在一个)。

    deletesrc

    String

    -

    “删除”图标路径。

    placeholderinput

    String

    请输入内容

    提示输入内容。

    disabled

    Boolean

    false

    是否禁用。

    支持的事件:

    事件名称

    参数

    描述

    handleclick

    left|right

    点击左、右“搜索”图标的事件。

    clearclick

    {}

    清空当前输入框内容的事件。

[h2]父子组件通信

  1. 父组件给子组件传递数据。
    1. 子组件通过在props定义参数,接收来自父组件的传值数据,如preimgsrc、sufimgsrc、disabled等。
      props: { 
          preimgsrc: {
              type: String,
              default: ''  
          },
          sufimgsrc: {
              type: String,
              default: ''  
          },
          deletesrc: {
              type: String,
              default: ''  
          },
          placeholderinput: {
              type: String,
              default: '请输入内容:'  
          },
          disabled: {
              type: Boolean,
              default: false
          },
      },
    2. 父组件给子组件传值,如果是布尔值,需要在data中定义,而不能直接在组件中定义。
      <easyinput disabled="{{this.disabled}}" placeholderinput="{{this.placeholderinput}}"></easyinput>
      data() {
          return {
              placeholderinput:'禁用',
              inputValue: '',
              sufimgsrc:'',
              preimgsrc:'',
              deleteimgsrc:'',
              disabled:'true'
          }
      },
  2. 子组件给父组件传递数据和方法。
    1. 父组件中注册特定方法的监听,等待子组件通知触发。
      <easyinput preimgsrc = "{{searchimgsrc}}" deletesrc = "{{deleteimgsrc}}"  @handleclick="handleClick"  @clearclick="clearClick"></easyinput>
      handleClick(e) {
          console.log(JSON.stringify(e));
          prompt.showToast({
              message: "点击了"+ e.detail+"侧的图标",
              duration: 2000,
              gravity: 'center'
          })
      },
      clearClick(e){
          console.log(JSON.stringify(e));  
          this.inputValue = '' 
      }
    2. 子组件通过this.$emit方法携带事件及参数通知父组件。
      handle() {
          console.log("handleclick", this.sufimgsrc,this.preimgsrc);
          let direction;
          if (this.preimgsrc !== '') {
              direction ="left"
          }else if(this.sufimgsrc !== ''){
              direction ="right"
          }
          this.$emit('handleclick',direction)
      },        
      resetValue() {
          this.inputValue = ''
          if(this.disabled){
              this.placeholderinput = '请输入内容:' 
          }
          this.$emit('clearclick', {});
      },

示例代码

增强输入框easyinput.ux代码:

<template>
  <div class="container">
        <image  src="{{this.preimgsrc}}"  class="photo" onclick="handle()" ></image>       
        <input  disabled="{{this.disabled}}"   class="selectionInput"  placeholder="{{this.placeholderinput}}" maxlength="100"  
                value="{{this.inputValue}}"  onchange="updateValue" onselectionchange="*"  >
        </input>   
        <image  src="{{this.sufimgsrc}}"  class="photo" onclick="handle()" ></image>     
        <image  src="{{this.deletesrc}}"  class="photo" onclick="resetValue()"></image>    
  </div>
</template>
<script>
    module.exports = {
        props: { 
            preimgsrc: {
                type: String,
                default: ''  
            },
            sufimgsrc: {
                type: String,
                default: ''  
            },
            deletesrc: {
                type: String,
                default: ''  
            },
            placeholderinput: {
                type: String,
                default: '请输入内容:'  
            },
            disabled: {
                type: Boolean,
                default: false
            },
        },
        data: {
            inputValue: '',
            direction:'',
        },

        onInit() {
            this.$page.setTitleBar({ text: '输入框' })
        },

        updateValue(e) {
            this.inputValue = e.value;
            console.log(e.value)
        },

        handle() {
            console.log("handleclick", this.sufimgsrc,this.preimgsrc);
            let direction;
            if (this.preimgsrc !== '') {
                direction ="left"
            }else if(this.sufimgsrc !== ''){
                direction ="right"
            }
            this.$emit('handleclick',direction)
        },

        resetValue() {
            this.inputValue = ''
            if(this.disabled){
              this.placeholderinput = '请输入内容:' 
            }
            this.$emit('clearclick', {});
        },
    }
</script>
<style>
    .container{
        width: 100%; 
        border-top-width: 1px;
        border-bottom-width: 1px;   
        border-left-width: 1px;
        border-right-width: 1px;
        border-color: #999999;
    }

    .selectionInput{
        width: 100%;
        placeholder-color:rgb(161, 152, 152);
        caret-color:#7b68ee;
    }
</style>

页面hello.ux代码:

<import name="easyinput" src="../Easyinput/easyinput.ux"></import>
<template>
      <div class="container">
        <text class="example-info ">easyinput组件是对原生input组件的增强。easyinput内置了边框、图标等,同时包含input所有功能。</text>

        <text >左图标</text>
        <div class="example-body">
            <easyinput preimgsrc = "{{searchimgsrc}}"  @handleclick="handleClick" ></easyinput>
        </div>

        <text >右图标</text>
        <div class="example-body">
            <easyinput sufimgsrc = "{{searchimgsrc}}"  @handleclick="handleClick" ></easyinput>
        </div>

        <text >禁用</text>
        <div class="example-body">
            <easyinput disabled="{{this.disabled}}" placeholderinput="{{this.placeholderinput}}"  deletesrc = "{{deleteimgsrc}}" @clearclick="clearClick"></easyinput>
        </div>

        <text >左右图标</text>
        <div class="example-body">
            <easyinput preimgsrc = "{{searchimgsrc}}" deletesrc = "{{deleteimgsrc}}"  @handleclick="handleClick"  @clearclick="clearClick"></easyinput>
        </div>
    </div>
</template>
<script>
    import prompt from '@system.prompt'
    export default {
        data() {
            return {
                placeholderinput:'禁用',
                inputValue: '',
                sufimgsrc:'',
                preimgsrc:'',
                deleteimgsrc:'',
                disabled:'true'
            }
        },
        onInit() {
            this.$page.setTitleBar({
                text: 'menu',
            });

            this.searchimgsrc ='/Common/search.png'
            this.deleteimgsrc = '/Common/delete.png'
        },
        handleClick(e) {
            console.log(JSON.stringify(e));
            prompt.showToast({
                message: "点击了"+ e.detail+"侧的图标",
                duration: 2000,
                gravity: 'center'
            })
        },
        clearClick(e){
            console.log(JSON.stringify(e));  
            this.inputValue = '' 
        }
    }
</script>
<style>
    .container {
        flex: 1;
        flex-direction: column;
        background-color: #efeff4;
    }

    .text {
        flex: 1;
        font-size: 130px;
        line-height: 150px;
    }

    .example-info {
        padding: 15px;
        color: #3b4144;
        background-color: #ffffff;
    }

    .example-body {
        flex-direction: column;
        padding: 15px;
        background-color: #ffffff;
        height: 100px;
    }
</style>