123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- <template>
- <view
- class="input"
- :style="isActive? {...activeStyle}: ''"
- @click.stop="openList"
- >
- <!-- 文字显示区 -->
- <view
- class="select-text"
- :style="{
- color: (placeholder? '#606266': '#bbb')
- }">
- {{ selectText || initValue || placeholder }}
- </view>
- <!-- 图标 -->
- <view
- class="icon"
- :class="isActive? 'animate-open-rotate': 'animate-close-rotate'"
- >
- <uni-icons
- :type="rightIcon"
- :size="rightIconSize"
- :color="rightIconColor">
- </uni-icons>
- </view>
- <!-- 展示列表 -->
- <view
- v-if="isActive"
- class="list-wrap"
- :class="isActive? 'fadeShow': 'fadeHidden'"
- >
- <!-- 特殊格式长*宽*高 -->
- <view class="list" v-if="itemFormat == '*'" :style="{height: `${list.length * itemHeight}rpx`}">
- <view
- class="item"
- :style="{...(item.selected? itemActive: {}),height: `${itemHeight}rpx`,lineHeight: `${itemHeight}rpx`}"
- v-for="(item, index) in list"
- @click.stop="select(item)"
- >
- {{ `${item.width}*${item.height}*${item.length}` }}
- </view>
- </view>
- <!-- 按照绑定得字段来显示选中字段 :style="{height: `${list.length * itemHeight}rpx`}"-->
- <view v-else class="list" >
- <view
- class="item"
- :style="{...(item.selected? itemActive: {}),height: `${itemHeight}rpx`,lineHeight: `${itemHeight}rpx`}"
- v-for="(item, index) in list"
- @click.stop="select(item)"
- >
- {{ `${item[itemFormat]}` }}
- </view>
- </view>
- </view>
- </view>
- </template>
- <script>
- export default {
- data() {
- return {
- selectText: '',
- isActive: false
- };
- },
- methods: {
- // 下拉框选择
- select(item) {
- this.clearListStatus()
- item.selected = true
- this.itemFormat == '*'?this.selectText = `${item.width}*${item.height}*${item.length}`: this.selectText = item.text
- setTimeout(() => {
- this.toggleSelectStyle()
- }, 100)
- this.$emit('change', {
- newVal: item.value,
- orignItem: item
- })
- },
- openList() {
- this.toggleSelectStyle()
- },
- // 切换下拉框状态
- toggleSelectStyle() {
- this.isActive = !this.isActive
- },
- // 清空输入框
- clearInput() {
- this.selectText = ''
- this.isActive = false
- this.clearListStatus()
- },
- // 清空列表的选中样式
- clearListStatus() {
- for(let item of this.list) {
- item.selected = false
- }
- }
- },
- props: {
- // 下拉框点击显示的绑定字段
- itemFormat: {
- type: String,
- default: 'text'
- },
- // 数据列表
- list: {
- type: Array,
- default: () => {
- return [
- {text: '1楼', value: 1},
- {text: '4楼', value: 2}
- ]
- }
- },
- // 输入框选中样式
- activeStyle: {
- type: Object,
- default: () => {
- return {
- borderColor: '#007aff'
- }
- }
- },
- itemActive: {
- type: Object,
- default: () => {
- return {
- backgroundColor: '#f5f7fa',
- color: '#007aff',
- }
- }
- },
- itemHeight: {
- type: Number,
- default: 72
- },
- // 右侧图标颜色
- rightIconColor: {
- type: String,
- default: '#bbb'
- },
- // 右侧图标
- rightIcon: {
- type: String,
- default: 'arrowdown'
- },
- // 右侧图标大小
- rightIconSize: {
- type: Number,
- default: 12
- },
- // 占位符
- placeholder: {
- type: String,
- default: '1楼'
- },
- initValue: {
- type: String,
- default: ''
- }
- },
- onLoad() {
-
- }
- }
- </script>
- <style lang="scss">
- .input {
- position: relative;
- padding: 0 5%;
- height: 72rpx;
- line-height: 72rpx;
- width: 100%;
- border-radius: 5rpx;
- border: 1px solid #6A6B6C;
- .select-text {
- padding-right: 50rpx;
- display: flex;
- align-items: center;
- width: 100%;
- height: 100%;
- overflow: hidden;
- text-overflow: ellipsis;
- }
- .icon {
- position: absolute;
- right: 20rpx;
- top: 0;
- height: 100%;
- }
- .list-wrap {
- position: absolute;
- top: calc(100% + 10px);
- left: 0;
- z-index: 999;
- width: 100%;
- border: 1px solid #eee;
- box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
- background-color: #FFFFFF;
- &::before {
- content: '';
- position: absolute;
- top: -14rpx;
- left: 50rpx;
- z-index: 999;
- width: 0;
- height: 0;
- border-left: 12rpx solid transparent;
- border-right: 12rpx solid transparent;
- border-bottom: 14rpx solid #eee;
- }
- &::after {
- content: '';
- position: absolute;
- top: -13rpx;
- left: 52rpx;
- z-index: 999;
- width: 0;
- height: 0;
- border-left: 11rpx solid transparent;
- border-right: 11rpx solid transparent;
- border-bottom: 13rpx solid #fff;
- }
- .list {
- padding: 10rpx 0;
- max-height: 300rpx;
- height: 300rpx;
- overflow: auto;
- border-radius: 5rpx;
- .item {
- padding: 0 5%;
- }
- }
- }
- .animate-open-rotate {
- transform: rotate(180deg);
- transition: all .5s ease;
- }
- .animate-close-rotate {
- transform: rotate(0);
- transition: all .5s ease;
- }
- .fadeShow {
- animation: fadeShow .2s linear;
- }
- .fadeHidden {
- animation: fadeHidden .2s linear;
- }
- }
- @keyframes fadeShow {
- 0%{opacity: 0;}
- 100%{opacity: 1}
- }
- @keyframes fadeHidden {
- 0%{opacity: 1;}
- 100%{opacity: 0}
- }
- </style>
|