uv-list-item.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. <template>
  2. <!-- #ifdef APP-NVUE -->
  3. <cell :keep-scroll-position="keepScrollPosition">
  4. <!-- #endif -->
  5. <view :class="{ 'uv-list-item--disabled': disabled }" :style="{'background-color':customStyle.backgroundColor?customStyle.backgroundColor:'#fff'}"
  6. :hover-class="(!clickable && !link) || disabled || showSwitch ? '' : 'uv-list-item--hover'"
  7. class="uv-list-item" @click="onClick">
  8. <view v-if="!isFirstChild" class="border--left" :class="{ 'uv-list--border': border }"></view>
  9. <view class="uv-list-item__wrapper">
  10. <slot>
  11. <view class="uv-list-item__container"
  12. :class="{ 'container--right': showArrow || link, 'flex--direction': directionData === 'column'}"
  13. :style="{paddingTop:padding.top,paddingLeft:padding.left,paddingRight:padding.right,paddingBottom:padding.bottom}">
  14. <slot name="header">
  15. <view class="uv-list-item__header">
  16. <view v-if="thumb" class="uv-list-item__icon">
  17. <image :src="thumb" class="uv-list-item__icon-img" :class="['uv-list--' + thumbSize]" />
  18. </view>
  19. <view v-else-if="showExtraIcon" class="uv-list-item__icon">
  20. <uv-icon :name="extraIcon.icon" :customPrefix="extraIcon.customPrefix" :color="extraIcon.color" :size="extraIcon.size" />
  21. </view>
  22. </view>
  23. </slot>
  24. <slot name="body">
  25. <view class="uv-list-item__content"
  26. :class="{ 'uv-list-item__content--center': thumb || showExtraIcon || showBadge || showSwitch }">
  27. <text v-if="title" class="uv-list-item__content-title"
  28. :class="[ellipsis !== 0 && ellipsis <= 2 ? 'uv-ellipsis-' + ellipsis : '']">{{ title }}</text>
  29. <text v-if="note" class="uv-list-item__content-note">{{ note }}</text>
  30. </view>
  31. </slot>
  32. <slot name="footer">
  33. <view v-if="rightText || showBadge || showSwitch" class="uv-list-item__extra"
  34. :class="{ 'flex--justify': directionData === 'column' }">
  35. <text v-if="rightText" class="uv-list-item__extra-text">{{ rightText }}</text>
  36. <uv-badge
  37. v-if="showBadge"
  38. :show="!!(badge.show || badge.isDot || badge.value)"
  39. :isDot="badge.isDot"
  40. :value="badge.value"
  41. :max="badge.max"
  42. :type="badge.type"
  43. :showZero="badge.showZero"
  44. :bgColor="badge.bgColor"
  45. :color="badge.color"
  46. :shape="badge.shape"
  47. :numberType="badge.numberType"
  48. :inverted="badge.inverted"
  49. customStyle="margin-left: 4px;"
  50. ></uv-badge>
  51. <uv-switch v-if="showSwitch" :value="switchChecked" :disabled="disabled" @change="onSwitchChange"></uv-switch>
  52. </view>
  53. </slot>
  54. </view>
  55. </slot>
  56. </view>
  57. <uv-icon v-if="showArrow || link" size="34rpx" class="uv-icon-wrapper" color="#bbb" name="arrow-right" />
  58. </view>
  59. <!-- #ifdef APP-NVUE -->
  60. </cell>
  61. <!-- #endif -->
  62. </template>
  63. <script>
  64. import mpMixin from '@/uni_modules/uv-ui-tools/libs/mixin/mpMixin.js'
  65. import mixin from '@/uni_modules/uv-ui-tools/libs/mixin/mixin.js'
  66. /**
  67. * ListItem 列表子组件
  68. * @description 列表子组件
  69. * @tutorial https://ext.dcloud.net.cn/plugin?id=24
  70. * @property {String} title 标题
  71. * @property {String} note 描述
  72. * @property {String} thumb 左侧缩略图,若thumb有值,则不会显示扩展图标
  73. * @property {String} thumbSize = [lg|base|sm] 略缩图大小
  74. * @value lg 大图
  75. * @value base 一般
  76. * @value sm 小图
  77. * @property {String} rightText 右侧文字内容
  78. * @property {Boolean} disabled = [true|false] 是否禁用
  79. * @property {Boolean} clickable = [true|false] 是否开启点击反馈
  80. * @property {String} link = [navigateTo|redirectTo|reLaunch|switchTab] 是否展示右侧箭头并开启点击反馈
  81. * @value navigateTo 同 uni.navigateTo()
  82. * @value redirectTo 同 uni.redirectTo()
  83. * @value reLaunch 同 uni.reLaunch()
  84. * @value switchTab 同 uni.switchTab()
  85. * @property {String | PageURIString} to 跳转目标页面
  86. * @property {Boolean} showBadge = [true|false] 是否显示数字角标
  87. * @property {Object} badge 扩展数字角标的参数,格式为 :badge="{value: 122}"
  88. * @property {Boolean} showSwitch = [true|false] 是否显示Switch
  89. * @property {Boolean} switchChecked = [true|false] Switch是否被选中
  90. * @property {Boolean} showExtraIcon = [true|false] 左侧是否显示扩展图标
  91. * @property {Object} extraIcon 扩展图标参数,格式为 :extraIcon="{icon: 'photo',size: '30px'}"
  92. * @property {String} direction = [row|column] 排版方向
  93. * @value row 水平排列
  94. * @value column 垂直排列
  95. * @event {Function} click 点击 uniListItem 触发事件
  96. * @event {Function} switchChange 点击切换 Switch 时触发
  97. */
  98. export default {
  99. name: 'uv-list-item',
  100. mixins: [mpMixin, mixin],
  101. emits: ['click', 'switchChange'],
  102. props: {
  103. direction: {
  104. type: String,
  105. default: 'row'
  106. },
  107. title: {
  108. type: String,
  109. default: ''
  110. },
  111. note: {
  112. type: String,
  113. default: ''
  114. },
  115. ellipsis: {
  116. type: [Number, String],
  117. default: 0
  118. },
  119. disabled: {
  120. type: [Boolean, String],
  121. default: false
  122. },
  123. clickable: {
  124. type: Boolean,
  125. default: false
  126. },
  127. showArrow: {
  128. type: [Boolean, String],
  129. default: false
  130. },
  131. link: {
  132. type: [Boolean, String],
  133. default: false
  134. },
  135. to: {
  136. type: String,
  137. default: ''
  138. },
  139. showSwitch: {
  140. type: [Boolean, String],
  141. default: false
  142. },
  143. switchChecked: {
  144. type: [Boolean, String],
  145. default: false
  146. },
  147. showBadge: {
  148. type: [Boolean, String],
  149. default: false
  150. },
  151. badge: {
  152. type: Object,
  153. default () {
  154. return {}
  155. }
  156. },
  157. rightText: {
  158. type: String,
  159. default: ''
  160. },
  161. thumb: {
  162. type: String,
  163. default: ''
  164. },
  165. thumbSize: {
  166. type: String,
  167. default: 'base'
  168. },
  169. showExtraIcon: {
  170. type: [Boolean, String],
  171. default: false
  172. },
  173. extraIcon: {
  174. type: Object,
  175. default () {
  176. return {
  177. name: '',
  178. color: '#000000',
  179. size: 20,
  180. customPrefix: ''
  181. };
  182. }
  183. },
  184. border: {
  185. type: Boolean,
  186. default: false
  187. },
  188. customStyle: {
  189. type: Object,
  190. default () {
  191. return {
  192. padding: '',
  193. backgroundColor: '#FFFFFF'
  194. }
  195. }
  196. },
  197. keepScrollPosition: {
  198. type: Boolean,
  199. default: false
  200. }
  201. },
  202. computed: {
  203. directionData(){
  204. return this.direction ? this.direction : (this.parentData.direction ? this.parentData.direction : 'row');
  205. }
  206. },
  207. watch: {
  208. 'customStyle.padding': {
  209. handler(padding) {
  210. this.setPadding(padding);
  211. },
  212. immediate: true
  213. }
  214. },
  215. data() {
  216. return {
  217. isFirstChild: false,
  218. padding: {
  219. top: "",
  220. right: "",
  221. bottom: "",
  222. left: ""
  223. },
  224. parentData: {
  225. direction: 'row',
  226. padding: 0
  227. }
  228. };
  229. },
  230. created() {
  231. this.updateParentData();
  232. },
  233. mounted() {
  234. this.init();
  235. this.list = this.getForm()
  236. // 判断是否存在 uv-list 组件
  237. if (this.list) {
  238. if (!this.list.firstChildAppend) {
  239. this.list.firstChildAppend = true;
  240. this.isFirstChild = true;
  241. }
  242. }
  243. },
  244. methods: {
  245. init(){
  246. if (!this.parent) {
  247. this.$uv.error('uv-list-item必须搭配uv-list组件使用');
  248. }
  249. this.$nextTick(()=>{
  250. if(!(this.padding.top || this.padding.right|| this.padding.bottom|| this.padding.left)){
  251. this.setPadding(this.parentData.padding);
  252. }
  253. })
  254. },
  255. updateParentData() {
  256. this.getParentData('uv-list');
  257. },
  258. setPadding(padding){
  259. if(typeof padding == 'number'){
  260. padding += ''
  261. }
  262. let paddingArr = padding.split(' ')
  263. if (paddingArr.length === 1) {
  264. const allPadding = paddingArr[0]
  265. this.padding = {
  266. "top": allPadding,
  267. "right": allPadding,
  268. "bottom": allPadding,
  269. "left": allPadding
  270. }
  271. } else if (paddingArr.length === 2) {
  272. const [verticalPadding, horizontalPadding] = paddingArr;
  273. this.padding = {
  274. "top": verticalPadding,
  275. "right": horizontalPadding,
  276. "bottom": verticalPadding,
  277. "left": horizontalPadding
  278. }
  279. } else if (paddingArr.length === 4) {
  280. const [topPadding, rightPadding, bottomPadding, leftPadding] = paddingArr;
  281. this.padding = {
  282. "top": topPadding,
  283. "right": rightPadding,
  284. "bottom": bottomPadding,
  285. "left": leftPadding
  286. }
  287. }
  288. },
  289. /**
  290. * 获取父元素实例
  291. */
  292. getForm(name = 'uniList') {
  293. let parent = this.$parent;
  294. let parentName = parent.$options.name;
  295. while (parentName !== name) {
  296. parent = parent.$parent;
  297. if (!parent) return false
  298. parentName = parent.$options.name;
  299. }
  300. return parent;
  301. },
  302. onClick() {
  303. if (this.to !== '') {
  304. this.openPage();
  305. return;
  306. }
  307. if (this.clickable || this.link) {
  308. this.$emit('click', {
  309. data: {}
  310. });
  311. }
  312. },
  313. onSwitchChange(e) {
  314. this.$emit('switchChange', e);
  315. },
  316. openPage() {
  317. if (['navigateTo', 'redirectTo', 'reLaunch', 'switchTab'].indexOf(this.link) !== -1) {
  318. this.pageApi(this.link);
  319. } else {
  320. this.pageApi('navigateTo');
  321. }
  322. },
  323. pageApi(api) {
  324. let callback = {
  325. url: this.to,
  326. success: res => {
  327. this.$emit('click', {
  328. data: res
  329. });
  330. },
  331. fail: err => {
  332. this.$emit('click', {
  333. data: err
  334. });
  335. }
  336. }
  337. switch (api) {
  338. case 'navigateTo':
  339. uni.navigateTo(callback)
  340. break
  341. case 'redirectTo':
  342. uni.redirectTo(callback)
  343. break
  344. case 'reLaunch':
  345. uni.reLaunch(callback)
  346. break
  347. case 'switchTab':
  348. uni.switchTab(callback)
  349. break
  350. default:
  351. uni.navigateTo(callback)
  352. }
  353. }
  354. }
  355. };
  356. </script>
  357. <style lang="scss" scoped>
  358. @import '@/uni_modules/uv-ui-tools/libs/css/components.scss';
  359. $uv-font-size-sm:12px;
  360. $uv-font-size-base:14px;
  361. $uv-font-size-lg:16px;
  362. $uv-spacing-col-lg: 12px;
  363. $uv-spacing-row-lg: 15px;
  364. $uv-img-size-sm:20px;
  365. $uv-img-size-base:26px;
  366. $uv-img-size-lg:40px;
  367. $uv-border-color:#e5e5e5;
  368. $uv-bg-color-hover:#f1f1f1;
  369. $uv-text-color-grey:#999;
  370. $list-item-pd: $uv-spacing-col-lg $uv-spacing-row-lg;
  371. .uv-list-item {
  372. @include flex(row);
  373. font-size: $uv-font-size-lg;
  374. position: relative;
  375. justify-content: space-between;
  376. align-items: center;
  377. background-color: #fff;
  378. /* #ifdef H5 */
  379. cursor: pointer;
  380. /* #endif */
  381. }
  382. .uv-list-item--disabled {
  383. opacity: 0.3;
  384. }
  385. .uv-list-item--hover {
  386. background-color: $uv-bg-color-hover !important;
  387. }
  388. .uv-list-item__wrapper {
  389. @include flex(column);
  390. flex: 1;
  391. }
  392. .uv-list-item__container {
  393. position: relative;
  394. @include flex(row);
  395. padding: $list-item-pd;
  396. padding-left: $uv-spacing-row-lg;
  397. flex: 1;
  398. overflow: hidden;
  399. }
  400. .container--right {
  401. padding-right: 0;
  402. }
  403. .uv-list--border {
  404. position: absolute;
  405. top: 0;
  406. right: 0;
  407. left: 0;
  408. /* #ifdef APP-NVUE */
  409. border-top-color: $uv-border-color;
  410. border-top-style: solid;
  411. border-top-width: 0.5px;
  412. /* #endif */
  413. }
  414. /* #ifndef APP-NVUE */
  415. .uv-list--border:after {
  416. position: absolute;
  417. top: 0;
  418. right: 0;
  419. left: 0;
  420. height: 1px;
  421. content: '';
  422. -webkit-transform: scaleY(0.5);
  423. transform: scaleY(0.5);
  424. background-color: $uv-border-color;
  425. }
  426. /* #endif */
  427. .uv-list-item__content {
  428. @include flex(column);
  429. padding-right: 8px;
  430. flex: 1;
  431. color: #3b4144;
  432. justify-content: space-between;
  433. overflow: hidden;
  434. }
  435. .uv-list-item__content--center {
  436. justify-content: center;
  437. }
  438. .uv-list-item__content-title {
  439. font-size: $uv-font-size-base;
  440. color: #3b4144;
  441. overflow: hidden;
  442. }
  443. .uv-list-item__content-note {
  444. margin-top: 6rpx;
  445. color: $uv-text-color-grey;
  446. font-size: $uv-font-size-sm;
  447. overflow: hidden;
  448. }
  449. .uv-list-item__extra {
  450. @include flex(row);
  451. justify-content: flex-end;
  452. align-items: center;
  453. }
  454. .uv-list-item__header {
  455. @include flex(row);
  456. align-items: center;
  457. }
  458. .uv-list-item__icon {
  459. margin-right: 18rpx;
  460. flex-direction: row;
  461. justify-content: center;
  462. align-items: center;
  463. }
  464. .uv-list-item__icon-img {
  465. /* #ifndef APP-NVUE */
  466. display: block;
  467. /* #endif */
  468. height: $uv-img-size-base;
  469. width: $uv-img-size-base;
  470. margin-right: 10px;
  471. }
  472. .uv-icon-wrapper {
  473. /* #ifndef APP-NVUE */
  474. display: flex;
  475. /* #endif */
  476. align-items: center;
  477. padding: 0 10px;
  478. }
  479. .flex--direction {
  480. flex-direction: column;
  481. /* #ifndef APP-NVUE */
  482. align-items: initial;
  483. /* #endif */
  484. }
  485. .flex--justify {
  486. /* #ifndef APP-NVUE */
  487. justify-content: initial;
  488. /* #endif */
  489. }
  490. .uv-list--lg {
  491. height: $uv-img-size-lg;
  492. width: $uv-img-size-lg;
  493. }
  494. .uv-list--base {
  495. height: $uv-img-size-base;
  496. width: $uv-img-size-base;
  497. }
  498. .uv-list--sm {
  499. height: $uv-img-size-sm;
  500. width: $uv-img-size-sm;
  501. }
  502. .uv-list-item__extra-text {
  503. color: $uv-text-color-grey;
  504. font-size: $uv-font-size-sm;
  505. }
  506. .uv-ellipsis-1 {
  507. /* #ifndef APP-NVUE */
  508. overflow: hidden;
  509. white-space: nowrap;
  510. text-overflow: ellipsis;
  511. /* #endif */
  512. /* #ifdef APP-NVUE */
  513. lines: 1;
  514. text-overflow: ellipsis;
  515. /* #endif */
  516. }
  517. .uv-ellipsis-2 {
  518. /* #ifndef APP-NVUE */
  519. overflow: hidden;
  520. text-overflow: ellipsis;
  521. display: -webkit-box;
  522. -webkit-line-clamp: 2;
  523. -webkit-box-orient: vertical;
  524. /* #endif */
  525. /* #ifdef APP-NVUE */
  526. lines: 2;
  527. text-overflow: ellipsis;
  528. /* #endif */
  529. }
  530. </style>