space.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <template>
  2. <view
  3. :class="classObject"
  4. :style="[getStyle]"
  5. >
  6. <slot></slot>
  7. </view>
  8. </template>
  9. <script setup>
  10. import { ref, watch, onMounted } from "vue"
  11. const props = defineProps([
  12. 'direction',
  13. 'align',
  14. 'wrap',
  15. 'justify',
  16. 'size',
  17. 'height',
  18. 'padding',
  19. 'flex',
  20. 'border'
  21. ])
  22. console.log("--> % props:\n", props)
  23. const classObject = ref({})
  24. const handleClassObject = (props) => {
  25. console.log("--> % handleClassObject % props:\n", props.value)
  26. let className = 'yshop-space'
  27. let direction = props.direction
  28. let align = props.align
  29. let wrap = props.wrap
  30. let justify = props.justify
  31. let flex = props.flex
  32. let border = props.border
  33. if (border) {
  34. className += ' yshop-space-border'
  35. }
  36. if (direction) {
  37. className += ` yshop-space-${direction}`
  38. }
  39. if (wrap) {
  40. className += ` yshop-space-${wrap}`
  41. }
  42. if (align) {
  43. className += ` yshop-space-align-${align}`
  44. }
  45. if (justify) {
  46. className += ` yshop-space-justify-${justify}`
  47. }
  48. console.log("--> % handleClassObject % className:\n", className)
  49. classObject.value = className
  50. }
  51. const getStyle = ({
  52. size = 6,
  53. wrap,
  54. height,
  55. padding,
  56. flex
  57. }) => {
  58. let innerStyle = {}
  59. // let size = size.value
  60. // let wrap = wrap.value
  61. // let height = height.value
  62. // let padding = padding.value
  63. if (height) {
  64. innerStyle.height = `${height}rpx`
  65. }
  66. if (typeof size === 'number') {
  67. innerStyle.gap = size + 'px'
  68. }
  69. if (wrap) {
  70. innerStyle.flexWrap = 'wrap'
  71. }
  72. if (typeof size === 'string') {
  73. switch (size) {
  74. case 'small':
  75. innerStyle.gap = '8rpx'
  76. break
  77. case 'middle':
  78. innerStyle.gap = '16rpx'
  79. break
  80. case 'large':
  81. innerStyle.gap = '24rpx'
  82. break
  83. }
  84. }
  85. if (typeof padding === 'string') {
  86. innerStyle.padding = `${padding}rpx`
  87. }
  88. if (Object.prototype.toString.call(padding) === '[object Array]') {
  89. if (typeof padding === 'object') {
  90. if (padding.length == 1) {
  91. innerStyle.padding = `${padding[0]}rpx`
  92. }
  93. if (padding.length == 2) {
  94. innerStyle.padding = `${padding[0]}rpx ${padding[1]}rpx`
  95. }
  96. }
  97. }
  98. if (Object.prototype.toString.call(size) === '[object Array]') {
  99. if (typeof size === 'object') {
  100. if (size.length == 1) {
  101. innerStyle.gap = `${size[0]}rpx`
  102. }
  103. if (size.length == 2) {
  104. innerStyle.gap = `${size[0]}rpx ${size[1]}rpx`
  105. }
  106. }
  107. }
  108. if (flex) {
  109. innerStyle.flex = flex
  110. }
  111. return innerStyle
  112. }
  113. onMounted(() => {
  114. handleClassObject(props)
  115. getStyle(props)
  116. })
  117. </script>
  118. <style lang="less">
  119. space {
  120. width: 100%;
  121. }
  122. .yshop-space {
  123. display: flex;
  124. width: 100%;
  125. flex: 1;
  126. &-vertical {
  127. flex-direction: column;
  128. }
  129. &-align-center {
  130. align-items: center;
  131. }
  132. &-align-start {
  133. align-items: flex-start;
  134. }
  135. &-align-end {
  136. align-items: flex-end;
  137. }
  138. &-justify-center {
  139. justify-content: center;
  140. }
  141. &-justify-between {
  142. justify-content: space-between;
  143. }
  144. &-justify-around {
  145. justify-content: space-around;
  146. }
  147. &-align-end {
  148. align-items: flex-end;
  149. }
  150. &-align-baseline {
  151. align-items: baseline;
  152. }
  153. &-item:empty {
  154. display: none;
  155. }
  156. &-border {
  157. border-bottom: 1rpx solid #f9f9f9;
  158. }
  159. }
  160. // #ifdef APP-PLUS
  161. .yshop-space {
  162. &>view {
  163. margin-right: 8rpx;
  164. }
  165. &>text {
  166. margin-right: 8rpx;
  167. }
  168. &>image {
  169. margin-right: 8rpx;
  170. }
  171. .u-tag-wrapper {
  172. margin-right: 8rpx;
  173. }
  174. .u-tag {
  175. margin-right: 8rpx;
  176. }
  177. }
  178. // #endif
  179. </style>