123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- <template>
- <view class="uni-id-combox">
- <!--<view v-if="label" class="uni-id-combox__label" :style="labelStyle">
- <text>{{label}}</text>
- </view>-->
- <view class="uni-id-combox__input-box">
- <label class="uni-id-combox__input" @click="toggleSelector">{{inputVal}}
- <uni-icons color="#C8C8C8" type="arrowright"/>
- </label>
- <!-- <input class="uni-id-combox__input" type="text" disabled="true" :placeholder="placeholder" v-model="inputVal" @input="onInput"/>-->
- <!-- @focus="onFocus" @blur="onBlur" />-->
-
- <!-- <label class="iconfont icon-triangle-down " style="color: grey;" @click="toggleSelector"></label>-->
- <!-- <uni-icons class="uni-id-combox__input-arrow" type="arrowdown" size="14" @click="toggleSelector"></uni-icons>-->
- <view class="uni-id-combox__selector " v-if="showSelector">
- <scroll-view scroll-y="true" class="uni-id-combox__selector-scroll">
- <view class="uni-id-combox__selector-empty" v-if="filterCandidatesLength === 0">
- <label>{{emptyTips}}</label>
- </view>
- <!-- <view class="uni-id-combox__selector-item" v-for="(item,index) in filterCandidates" :key="index" @click="onSelectorClick(index)">-->
- <view class="uni-id-combox__selector-item" v-for="(item,index) in candidates" :key="index" @click="onSelectorClick(index)">
- <label>{{item.value}}</label>
- <!-- <text>{{item.value}}</text>-->
- </view>
- </scroll-view>
- </view>
- </view>
- </view>
- </template>
- <script>
- /**
- * Combox 组合输入框
- * @description 组合输入框一般用于既可以输入也可以选择的场景
- * @tutorial https://ext.dcloud.net.cn/plugin?id=1261
- * @property {String} label 左侧文字
- * @property {String} labelWidth 左侧内容宽度
- * @property {String} placeholder 输入框占位符
- * @property {Array} candidates 候选项列表
- * @property {String} emptyTips 筛选结果为空时显示的文字
- * @property {String} value 组合框的值
- */
- export default {
- name: 'uniIdCombox',
- emits:['input','update:modelValue'],
- props: {
- label: {
- type: String,
- default: ''
- },
- labelWidth: {
- type: String,
- default: 'auto'
- },
- placeholder: {
- type: String,
- default: ''
- },
- candidates: {
- type: Array,
- default () {
- return []
- }
- },
- emptyTips: {
- type: String,
- default: '无匹配项'
- },
- // #ifndef VUE3
- value: {
- type: [String, Number],
- default: ''
- },
- // #endif
- // #ifdef VUE3
- modelValue: {
- type: [String, Number],
- default: ''
- },
- // #endif
- },
- data() {
- return {
- showSelector: false,
- inputVal: '',
- id:'',
- }
- },
- computed: {
- labelStyle() {
- if (this.labelWidth === 'auto') {
- return {}
- }
- return {
- width: this.labelWidth
- }
- },
- filterCandidates() {
- return this.candidates.filter((item) => {
- let ret = item.value.toString().indexOf(this.inputVal) > -1;
- //console.log('value:' +item.value + ' ret:' + ret);
- return ret
- })
- },
- filterCandidatesLength() {
- return this.filterCandidates.length
- }
- },
- watch: {
- // #ifndef VUE3
- value: {
- handler(newVal) {
- this.inputVal = newVal
- },
- immediate: true
- },
- // #endif
- // #ifdef VUE3
- modelValue: {
- handler(newVal) {
- this.inputVal = newVal
- },
- immediate: true
- },
- // #endif
- },
- methods: {
- toggleSelector() {
- this.showSelector = !this.showSelector
- },
- onFocus() {
- this.showSelector = true
- },
- onBlur() {
- setTimeout(() => {
- this.showSelector = false
- }, 153)
- },
- onSelectorClick(index) {
- this.inputVal = this.candidates[index].value;
- this.id = this.candidates[index].id;
- //this.filterCandidates[index].value
- this.showSelector = false
- this.$emit('input', this.inputVal)
- this.$emit('update:modelValue', {id:this.id,value:this.inputVal})
- },
- onInput() {
-
- setTimeout(() => {
- this.$emit('input', this.inputVal)
- this.$emit('update:modelValue', this.inputVal)
- })
- }
- }
- }
- </script>
- <style lang="scss" scoped>
- @import url("/static/font/iconfont.css");
- .uni-id-combox {
- ///* #ifndef APP-NVUE */
- display: flex;
- ///* #endif */
- height: 80rpx;
- flex-direction: row;
- align-items: center;
- // border-bottom: solid 1px #DDDDDD;
- }
- .uni-id-combox__label {
- font-size: 32rpx;
- line-height: 44rpx;
- //padding-right: 20rpx;
- color: #999999;
- }
- .uni-id-combox__input-box {
- position: relative;
- ///* #ifndef APP-NVUE */
- display: flex;
- ////* #endif */
- //flex: 1;
- flex-direction: row;
- align-items: center;
- justify-content: center;
- width:100%
- //background-color: #007AFF;
- }
- .uni-id-combox__input {
- //flex: 1;
- font-size: 32rpx;
- height: 44rpx;
- line-height: 44rpx;
- //justify-content: flex-end;
- }
- .uni-id-combox__input-arrow {
- //padding: 10rpx;
- }
- .uni-id-combox__selector {
- ///* #ifndef APP-NVUE */
- box-sizing: border-box;
- ///* #endif */
- position: absolute;
- top: 42rpx;
- left: 0;
- width: 100%;
- background-color: #FFFFFF;
- border-radius: 6rpx;
- box-shadow: #DDDDDD 4rpx 4rpx 8rpx, #DDDDDD -4rpx -4rpx 8rpx;
- z-index: 2;
- }
- .uni-id-combox__selector-scroll {
- ///* #ifndef APP-NVUE */
- max-height: 800rpx;
- box-sizing: border-box;
- ///* #endif */
- }
- .uni-id-combox__selector::before {
- ///* #ifndef APP-NVUE */
- content: '';
- ///* #endif */
- position: absolute;
- width: 0;
- height: 0;
- border-bottom: solid 6rpx #FFFFFF;
- border-right: solid 6rpx transparent;
- border-left: solid 6rpx transparent;
- //left: 50%;
- top: -6rpx;
- //margin-left: -6rpx;
- }
- .uni-id-combox__selector-empty,
- .uni-id-combox__selector-item {
- ///* #ifndef APP-NVUE */
- display: flex;
- cursor: pointer;
- ///* #endif */
- line-height: 72rpx;
- font-size: 28rpx;
- text-align: center;
- border-bottom: solid 1rpx #DDDDDD;
- //margin: 0px 20rpx;
- justify-content: center;
- }
- .uni-id-combox__selector-empty:last-child,
- .uni-id-combox__selector-item:last-child {
- ///* #ifndef APP-NVUE */
- border-bottom: none;
- ///* #endif */
- }
- </style>
|