FlexView.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <template>
  2. <view
  3. :id="innerId ?? id"
  4. :class="[
  5. 'nana-flex-layout', {
  6. 'nana-flex-row': direction === 'row',
  7. 'nana-flex-column': direction === 'column'
  8. },
  9. innerClass,
  10. ]"
  11. :style="finalStyle"
  12. >
  13. <slot />
  14. </view>
  15. </template>
  16. <script setup lang="ts">
  17. /**
  18. * 组件说明:Flex组件,用于一些布局中快速写容器,是一系列盒子的基础组件。
  19. */
  20. import { computed, getCurrentInstance } from 'vue';
  21. import { type ThemePaddingMargin } from '../theme/ThemeDefine';
  22. import { RandomUtils } from '@imengyu/imengyu-utils';
  23. import { useBaseViewStyleBuilder } from './BaseView';
  24. export type FlexDirection = "row"|"column"|'row-reverse'|'column-reverse';
  25. export type FlexJustifyType = 'flex-start' | 'flex-end' | 'center' |'space-between' |'space-around' |'space-evenly';
  26. export type FlexAlignType = "stretch"|'center'|'start'|'end'|'flex-start' | 'flex-end' | 'center';
  27. export type StateType = 'default' | 'active' | 'pressed';
  28. export interface FlexProps {
  29. innerId?: string,
  30. /**
  31. * 盒子定位
  32. */
  33. position?: "absolute" | "relative" | 'fixed' | 'sticky',
  34. /**
  35. * 弹性盒子方向
  36. */
  37. direction?: FlexDirection,
  38. /**
  39. * 子元素在主轴上的对齐方式
  40. */
  41. justify?: FlexJustifyType,
  42. /**
  43. * 子元素在交叉轴上的对齐方式
  44. */
  45. align?: FlexAlignType|"auto",
  46. /**
  47. * 当前元素在主轴上的对齐方式
  48. */
  49. alignSelf?: FlexAlignType|"auto",
  50. /**
  51. * 主轴与交叉轴是否居中
  52. */
  53. center?: boolean,
  54. /**
  55. * 弹性布局是否换行
  56. */
  57. wrap?: boolean,
  58. /**
  59. * 特殊样式
  60. */
  61. innerStyle?: object,
  62. /**
  63. * 特殊类名
  64. */
  65. innerClass?: string|string[]|object,
  66. /**
  67. * flex 参数
  68. */
  69. flex?: number|string,
  70. /**
  71. * flexBasis 参数
  72. */
  73. flexBasis?: number|string,
  74. /**
  75. * flexGrow 参数
  76. */
  77. flexGrow?: number,
  78. /**
  79. * flexShrink 参数
  80. */
  81. flexShrink?: number,
  82. /**
  83. * 内边距参数(支持数字或数组)
  84. */
  85. padding?: number|Array<number>|ThemePaddingMargin,
  86. /**
  87. * 外边距参数(支持数字或数组)
  88. */
  89. margin?: number|Array<number>|ThemePaddingMargin,
  90. /**
  91. * 位置参数
  92. */
  93. top?: number|string,
  94. right?: number|string,
  95. bottom?: number|string,
  96. left?: number|string,
  97. /**
  98. * 圆角
  99. */
  100. radius?: number|string,
  101. /**
  102. * 间距
  103. */
  104. gap?: number|string,
  105. /**
  106. * 背景颜色
  107. */
  108. backgroundColor?: string,
  109. /**
  110. * 阴影。使用主题中的阴影预设。
  111. */
  112. shadow?: string,
  113. /**
  114. * 边框。使用主题中的边框预设。
  115. */
  116. border?: string,
  117. /**
  118. * 边框颜色
  119. */
  120. borderColor?: string,
  121. /**
  122. * 边框宽度
  123. */
  124. borderWidth?: number|string,
  125. /**
  126. * 边框样式
  127. */
  128. borderStyle?: string,
  129. /**
  130. * 宽度
  131. */
  132. width?: number|string,
  133. /**
  134. * 高度
  135. */
  136. height?: number|string,
  137. overflow?: 'visible'|'hidden'|'scroll'|'auto'
  138. /**
  139. * 层级
  140. */
  141. zIndex?: number,
  142. }
  143. const props = withDefaults(defineProps<FlexProps>(), {
  144. direction: "column",
  145. backgroundColor: '',
  146. });
  147. const { commonStyle } = useBaseViewStyleBuilder(props);
  148. const finalStyle = computed(() => {
  149. const obj : Record<string, any> = {};
  150. const o = {
  151. ...commonStyle.value,
  152. ...obj
  153. }
  154. for (const key in o) {
  155. if (o[key] === undefined)
  156. delete o[key];
  157. }
  158. return o;
  159. })
  160. defineOptions({
  161. options: {
  162. styleIsolation: "shared",
  163. virtualHost: true
  164. }
  165. });
  166. const instance = getCurrentInstance();
  167. const id = 'flex-item-' + RandomUtils.genNonDuplicateID(15);
  168. defineExpose({
  169. measure() {
  170. return new Promise((resolve) => {
  171. const tabItem = uni.createSelectorQuery()
  172. .in(instance)
  173. .select(`#${id}`);
  174. tabItem.boundingClientRect().exec((res) => {
  175. resolve(res)
  176. })
  177. });
  178. },
  179. })
  180. </script>
  181. <style>
  182. .nana-flex-layout {
  183. display: flex;
  184. }
  185. .nana-flex-row {
  186. flex-direction: row;
  187. }
  188. .nana-flex-column {
  189. flex-direction: column;
  190. }
  191. </style>