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