目录

number_box

Overview

The number_box element is an input box with increase and decrease buttons. The element is mainly used for setting the number of limited goods, goods to be added to the cart, and so on. This element has the following features:

The number_box element can be divided into three parts in the layout. On the left and right is the decrease button and increase button respectively. The input box is in the middle of the two buttons. The following figure shows the layout.

The sample code for the layout is as follows:

<text>Set the minimum number to 2, the maximum number to 10, and the step to 0.1.</text>.
<number_box  value="{{value}}" min="2" max="10"  step="0.1" @change="handlechange" @focus="handlefocus" @blur="handleblur"></number_box> 

Development Procedure

[h2]Customizing the Subelements

  1. Design the layout.

    The number box UI is universal, but the content layout is not. The content layout cannot be fixed. Otherwise, once the UI of the content part changes, the subelements have to be modified, which violates the open-closed principle in programming.

    Therefore, multiple style attributes are supported in number_box for developers to customize the style.

    <template>
      <div class="uni-numbox ">
        <div class="{{this.extremum1 ? 'uni-numbox__minus1' : 'uni-numbox__minus'}}" onclick="_calcValue('minus')">
          <text class="{{this.extremum1?'uni-numbox--text1':'uni-numbox--text'}}" disabled="{{disabled}}">-</text>
        </div>
    
        <input value="{{value}}" type="number" class="uni-numbox__value" onchange="updateValue" enterkeytype="done"  
          onenterkeyclick="enterkeyclick" disabled="{{disabled}}" onfocus="handlefocus" onblur="handleblur"></input>
    
        <div class="{{this.extremum ? 'uni-numbox__plus1' : 'uni-numbox__plus'}}" onclick="_calcValue('plus')">
          <text class="{{this.extremum ?'uni-numbox--text1':'uni-numbox--text'}}" disabled="{{disabled}}">+</text>
        </div>
      </div>
    </template>
  2. Customize the attributes and supported events.

    Supported attributes

    Attribute

    Type

    Default Value

    Description

    value

    Number

    0

    Current value in the input box.

    min

    Number

    0

    Minimum number.

    max

    Number

    100

    Maximum number.

    step

    Number

    1

    Increased or decreased number each time when the buttons are tapped.

    disabled

    Boolean

    false

    Indicates whether the number box is disabled.

    Supported events

    Event

    Parameter

    Triggering Condition

    change

    value

    The number in the input box is changed.

    focus

    e

    The input box is focused.

    blur

    e

    The input box is not focused.

[h2]Implementing Data Communication Between the Parent Element and Subelements

  1. Pass the data from the parent element to the subelements.
    1. Define parameters in props of the subelements.
      props: {
        value: {
          type: [Number, String],
          default: 1
        },
        min: {
          type: Number,
          default: 0
        },
        max: {
          type: Number,
          default: 100
        },
        step: {
          type: Number,
          default: 1
        },
        disabled: {
          type: Boolean,
          default: false
        }
      },
    2. Pass the values from the parent element to the subelements. If the data type is Boolean, you cannot define it in the element directly. Define it in data instead.
      <text>Disabled status.</text>
      <number_box disabled="{{disabled}}"></number_box>
      data: {
        componentData: {},
        value: 8,
        disabled:true    
      },
  2. Pass the method from the parent element to the subelements.
    1. When any area of the parent element is tapped, the following onclick method is triggered. Call this.$broadcast to pass messages to the subelements.
      <div class="container"  onclick="onclick">
      </div>
      onclick(e){
        this.$broadcast('checkValue')  
      }
    2. Call this.$on() in the subelements to listen to the input number and verify it.
      this.$on('checkValue', this.checkValue)
      checkValue(e) {
        console.log("checkValue")
        console.log(JSON.stringify(e))
        if (this.disabled) {
          return
        }
        if (Number(this.value) >= Number(this.max)) {
          this.value = this.max;
        } else if (Number(this.value) <= Number(this.min)) {
          this.value = this.min;
        } else {
          this.value = this.value
        }
      }
  3. Pass data and method from the subelements to the parent element.
    1. In the parent element, register a listener on the events to be passed by the subelements.
      <number_box id="number1" value="{{value}}" @change="handlechange" @focus="handlefocus" @blur="handleblur" ></number_box>
      handlechange(e) {
        this.value=e.detail
      },
      handlefocus() {
      },
      handleblur() {
      },
    2. Call this.$emit in the subelements to notify the parent element of the events and the parameters.
      updateValue(e) {
        if (Number(e.value) >= Number(this.max)) {
          prompt.showToast({
            message: 'Enter a number less than or equal to ' + this.max,
          })
          this.value = e.value
          this.extremum = true;
          this.extremum1 = false;
        } else if (Number(e.value) <= Number(this.min)) {
          prompt.showToast({
            message: 'Enter a number greater than or equal to ' + this.min,
          })
          this.value = e.value
          this.extremum1 = true;
          this.extremum = false;
        } else {
          this.value = e.value
          this.extremum = false;
          this.extremum1 = false;
        }
        this.$emit('change', this.value)
      },
      handlefocus(e) {
        this.$emit('focus', e)
      },
      handleblur(e) {
        this.$emit('blur', e)
      }, 

Sample Code

number_box.ux file:

<template>
  <div class="uni-numbox ">
    <div class="{{this.extremum1 ? 'uni-numbox__minus1' : 'uni-numbox__minus'}}" onclick="_calcValue('minus')">
      <text class="{{this.extremum1?'uni-numbox--text1':'uni-numbox--text'}}" disabled="{{disabled}}">-</text>
    </div>

    <input value="{{value}}" type="number" class="uni-numbox__value" onchange="updateValue" enterkeytype="done"  
      onenterkeyclick="enterkeyclick" disabled="{{disabled}}" onfocus="handlefocus" onblur="handleblur"></input>

    <div class="{{this.extremum ? 'uni-numbox__plus1' : 'uni-numbox__plus'}}" onclick="_calcValue('plus')">
      <text class="{{this.extremum ?'uni-numbox--text1':'uni-numbox--text'}}" disabled="{{disabled}}">+</text>
    </div>
  </div>
</template>
<style>
  .uni-numbox__value:disabled {
    color: #c0c0c0;
  }
  .uni-numbox {
    display: flex;

    flex-direction: row;
    height: 100px;
    line-height: 100px;
    width: 300px;
  }
  .uni-numbox__minus {
    /* #ifndef APP-NVUE */
    display: flex;
    /* #endif */
    flex-direction: row;
    align-items: center;
    justify-content: center;
    width: 100px;
    height: 100px;
    font-size: 50px;
    color: #333;
    background-color: #f8f8f8;
     border-width: 1px; 
    border-style: solid;
    border-color: #e5e5e5;
    border-top-left-radius: 3px;
    border-bottom-left-radius: 3px;
    border-right-width: 0;
  }
  .uni-numbox__minus1 {
    /* #ifndef APP-NVUE */
    display: flex;
    /* #endif */
    flex-direction: row;
    align-items: center;
    justify-content: center;
    width: 100px;
    height: 100px;
    font-size: 50px;
    color: #333;
    background-color: #f8f8f8;
    border-width: 1px;
    border-style: solid;
    border-color: #e5e5e5;
    border-top-left-radius: 3px;
    border-bottom-left-radius: 3px;
    border-right-width: 0;
  }
  .uni-numbox--text {
    font-size: 50px;
    color: #333;
    background-color: #f8f8f8;
  }
  .uni-numbox--text1 {
    font-size: 50px;
    color: #c0c0c0;
    background-color: #f8f8f8;
  }
  .uni-numbox--text:disabled {
    font-size: 50px;
    color: #c0c0c0;
  }
  .uni-numbox--text1:disabled {
    font-size: 50px;
    color: #c0c0c0;
  }
  .uni-numbox__plus {
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: center;
    width: 100px;
    height: 100px;
    font-size: 50px;
    border-width: 1px;
    border-style: solid;
    border-color: #e5e5e5;
    border-top-right-radius: 3px;
    border-bottom-right-radius: 3px;
    background-color: #f8f8f8;
    border-left-width: 0;
  }
  .uni-numbox__plus1 {
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: center;
    width: 100px;
    height: 100px;
    font-size: 50px;
    border-width: 1px;
    border-style: solid;
    border-color: #e5e5e5;
    border-top-right-radius: 3px;
    border-bottom-right-radius: 3px;
    background-color: #f8f8f8;
    border-left-width: 0;
  }
  .uni-numbox__value {
    background-color: #ffffff;
    width: 100px;
    height: 100px;
    text-align: center;
    line-height: 100px;
    font-size: 50px;
    border-width: 1px;
    border-style: solid;
    border-color: #e5e5e5;
    border-left-width: 0;
    border-right-width: 0;
  }
</style>
<script>
  import prompt from '@system.prompt';
  module.exports = {
    props: {
      value: {
        type: [Number, String],
        default: 1
      },
      min: {
        type: Number,
        default: 0
      },
      max: {
        type: Number,
        default: 100
      },
      step: {
        type: Number,
        default: 1
      },
      disabled: {
        type: Boolean,
        default: false
      }
    },
    data() {
      return {
        extremum: false,
        extremum1: false,

      };
    },
    onInit() {
      this.$page.setTitleBar({ text: '' })
      if (Number(this.value) < Number(this.min)) {
        this.value = this.min
      }
      if (this.disabled) {
        this.value = " "
      }
      this.$on('checkValue', this.checkValue)
    },
    updateValue(e) {
      console.log("Number" + Number(undefined))
      console.log(JSON.stringify(e))
      console.log(e.value)

      if (Number(e.value) >= Number(this.max)) {
        prompt.showToast({
          message: 'Enter a number less than or equal to ' + this.max,
        })
        this.value = e.value
        this.extremum = true;
        this.extremum1 = false;
      } else if (Number(e.value) <= Number(this.min)) {
        prompt.showToast({
          message: 'Enter a number greater than or equal to ' + this.min,
        })
        this.value = e.value
        this.extremum1 = true;
        this.extremum = false;
      } else {
        this.value = e.value
        this.extremum = false;
        this.extremum1 = false;
      }
      console.log("onchange in the subelement" + this.value);
      this.$emit('change', this.value)
    },

    _calcValue(type) {
      console.log(this.disabled);
      if (this.disabled) {
        return
      }
      const scale = this.getDecimalScale();
      let calcvalue = this.value * scale;
      let calcstep = this.step * scale;
      if (type === "minus") {
        console.log("minus");
        console.log(this.value);
        this.value = (calcvalue - calcstep) / scale.toFixed(String(scale).length - 1);
        if (Number(this.value) <= Number(this.min)) {
          this.value = this.min
        }
        if (Number(this.value) >= Number(this.max)) {
          this.value = this.max
        }
      }
      if (type === "plus") {
        console.log("plus");
        this.value = (calcvalue + calcstep) / scale.toFixed(String(scale).length - 1);
        if (Number(this.value) >= Number(this.max)) {
          this.value = this.max
        }
        if (Number(this.value) < Number(this.min)) {
          this.value = this.min
        }
      }
      this.$emit('change', this.value)
    },
    getDecimalScale() {
      let scale = 1;
      if (this.step !== 1) {
        scale = Math.pow(10, String(this.step).split(".")[1].length);
      }
      return scale;
    },
    enterkeyclick(e) {
      console.log("enterkeyclick" + e.value)
      if (Number(e.value) >= Number(this.max)) {
        this.value = this.max
      } else if (Number(e.value) <= Number(this.min)) {
        this.value = this.min
      } else {
        this.value = e.value
      }
    },
    handlefocus(e) {
      console.log("handlefocus in the subelement" + this.value);
      this.$emit('focus', e)
      console.log(JSON.stringify(e))
    },
    handleblur(e) {
      console.log("handleblur in the subelement" + this.value);
      this.$emit('blur', e)
      console.log(JSON.stringify(e))
      console.log("e.currentTarget.attr.value" + e.currentTarget.attr.value)
    }, 
    checkValue(e) {
      console.log("checkValue")
      console.log(JSON.stringify(e))
      if (this.disabled) {
        return
      }
      if (Number(this.value) >= Number(this.max)) {
        this.value = this.max;
      } else if (Number(this.value) <= Number(this.min)) {
        this.value = this.min;
      } else {
        this.value = this.value
      }
    }
  }
</script>

hello.ux file:

<import name="number_box" src="../Number_box/number_box.ux"></import>
<template>
  <!-- Only one root node is allowed in template. -->
  <div class="container"  onclick="onclick">
    <text>Use v-model"{{ value }}".</text>
    <number_box id="number1" value="{{value}}" @change="handlechange" @focus="handlefocus" @blur="handleblur" ></number_box>
    <text>Set the maximum number to 10 and the minimum number to 2.</text>
    <number_box min="2" max="10" ></number_box>
    <text>The step is 0.1.</text>
    <number_box step="0.1"></number_box>
<text>Disabled status.</text>
    <number_box disabled="{{disabled}}"></number_box>
  </div>
</template>

<style>
  .container {
    flex-direction: column;
    justify-content: center;
    align-items: center;
  }
  .title {
    font-size: 100px;
  }
</style>

<script>
  module.exports = {
  data: {
    componentData: {},
    value: 8,
    disabled:true    
  },
    onInit() {
      this.$page.setTitleBar({
        text: 'menu',
        textColor: '#ffffff',
        backgroundColor: '#007DFF',
        backgroundOpacity: 0.5,
        menu: true
      });
    },
    handlechange(e) {
      console.log("handlechange in the parent element");
      console.log(JSON.stringify(e));
      this.value=e.detail
    },
    handlefocus() {
      console.log("handlefocus in the parent element");
    },
    handleblur() {
      console.log("handleblur in the parent element");
    },
    onclick(e){
     this.$broadcast('checkValue')  
    }
  }
</script>