FlexView.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. * 设置元素与其父元素之间的距离(支持数字或数组,等同于 top, right, bottom, left,
  99. * 但优先级比它们低),
  100. */
  101. inset?: (number|string)[]|string|number,
  102. /**
  103. * 圆角
  104. */
  105. radius?: number|string,
  106. /**
  107. * 间距
  108. */
  109. gap?: number|number[]|string,
  110. /**
  111. * 背景颜色
  112. */
  113. backgroundColor?: string,
  114. /**
  115. * 阴影。使用主题中的阴影预设。
  116. */
  117. shadow?: string,
  118. /**
  119. * 边框。使用主题中的边框预设。
  120. */
  121. border?: string,
  122. /**
  123. * 边框颜色
  124. */
  125. borderColor?: string,
  126. /**
  127. * 边框宽度
  128. */
  129. borderWidth?: number|string,
  130. /**
  131. * 边框样式
  132. */
  133. borderStyle?: string,
  134. /**
  135. * 宽度
  136. */
  137. width?: number|string,
  138. /**
  139. * 高度
  140. */
  141. height?: number|string,
  142. overflow?: 'visible'|'hidden'|'scroll'|'auto'
  143. /**
  144. * 层级
  145. */
  146. zIndex?: number,
  147. }
  148. const props = withDefaults(defineProps<FlexProps>(), {
  149. direction: "column",
  150. backgroundColor: '',
  151. });
  152. const { commonStyle } = useBaseViewStyleBuilder(props);
  153. const finalStyle = computed(() => {
  154. const obj : Record<string, any> = {};
  155. const o = {
  156. ...commonStyle.value,
  157. ...obj
  158. }
  159. for (const key in o) {
  160. if (o[key] === undefined)
  161. delete o[key];
  162. }
  163. return o;
  164. })
  165. defineOptions({
  166. options: {
  167. styleIsolation: "shared",
  168. virtualHost: true
  169. }
  170. });
  171. const instance = getCurrentInstance();
  172. const id = 'flex-item-' + RandomUtils.genNonDuplicateID(15);
  173. defineExpose({
  174. measure() {
  175. return new Promise((resolve) => {
  176. const tabItem = uni.createSelectorQuery()
  177. .in(instance)
  178. .select(`#${id}`);
  179. tabItem.boundingClientRect().exec((res) => {
  180. resolve(res)
  181. })
  182. });
  183. },
  184. })
  185. </script>
  186. <style>
  187. .nana-flex-layout {
  188. display: flex;
  189. }
  190. .nana-flex-row {
  191. flex-direction: row;
  192. }
  193. .nana-flex-column {
  194. flex-direction: column;
  195. }
  196. </style>