<style lang="less">
.component-input {
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  position: relative;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
  background: rgba(196, 196, 196, 0.2);
  border-radius: 44rpx;
  padding-left: 80rpx;
}

.icon-search-wrap {
  position: absolute;
  left: 28rpx;
  top: 28rpx;
  .icon {
    width: 32rpx;
    height: 32rpx;
    display: block;
  }
}
.component-input .input {
  height: 100%;
  flex: 1;
  box-sizing: border-box;
  padding: 0;
  background: transparent;
  font-size: 48rpx;
  font-size: 30rpx;
  line-height: 100%;
  color: #000;
}

.component-input .clear {
  height: 100%;
  width: 72rpx;
  display: flex;
  flex-direction: column;
  justify-content: center;
}
</style>

<template>
  <div class="component-input">
    <label v-if="isShowSearch" class="icon-search-wrap" for="component-input">
      <image class="icon" src="{{h5StaticPath+ '/icon-search.svg'}}" />
    </label>
    <input
      id="component-input"
      class="input"
      :placeholder="placeholder"
      :value="value"
      bindinput="changeValue"
      placeholder-style="{{placeholderStyle || 'color:#AFAFC1;'}}"
    />
    <div class="clear" v-if="value" v-on:click="clear()">
      <icon type="clear" size="16" class="icon-close"></icon>
    </div>
  </div>
</template>

<script>
import wepy from '@wepy/core';
import config from '@/config';

wepy.component({
  data: {
    h5StaticPath: config.h5StaticPath
  },
  props: {
    placeholder: String,
    value: String,
    placeholderStyle: String,
    isShowSearch: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    clear() {
      this.changeValueFun('');
    },
    changeValue(e) {
      let value = e.$wx.detail.value;
      this.changeValueFun(value);
    },
    changeValueFun(text) {
      this.$emit('component-input-with-clear-change', text);
    }
  }
});
</script>