Radio.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <template>
  2. <Touchable
  3. :touchable="!disabled"
  4. :activeOpacity="activeOpacity"
  5. :innerStyle="{
  6. ...(block ? themeStyles.checkBoxFull.value : themeStyles.checkBox.value),
  7. ...innerStyle
  8. }"
  9. innerClass="nana-check"
  10. center
  11. direction="row"
  12. @click.stop="switchOn"
  13. >
  14. <slot v-if="checkPosition === 'left'" icon="check" :on="value" :disabled="disabled" :shape="shape">
  15. <CheckBoxDefaultButton
  16. :on="value"
  17. :disabled="disabled"
  18. :shape="shape"
  19. :size="checkSize"
  20. :checkedBackgroundColor="themeContext.resolveThemeColor(color)"
  21. :checkedBorderColor="themeContext.resolveThemeColor(color)"
  22. :borderColor="themeContext.resolveThemeColor(borderColor)"
  23. :checkColor="themeContext.resolveThemeColor(checkColor)"
  24. :icon="props.icon"
  25. :iconSize="checkSize - 15"
  26. :type="props.icon != 'check-mark' ? 'icon' : 'radio'"
  27. />
  28. </slot>
  29. <slot>
  30. <Text :innerStyle="{
  31. ...themeStyles.checkText.value,
  32. ...textStyle,
  33. color: themeContext.resolveThemeColor(props.disabled === true ? disabledTextColor : textColor),
  34. display: StringUtils.isNullOrEmpty(text) ? 'none' : 'flex',
  35. }"
  36. :text="text"
  37. />
  38. </slot>
  39. <slot v-if="checkPosition === 'right'" icon="check" :on="value" :disabled="disabled" :shape="shape">
  40. <CheckBoxDefaultButton
  41. :on="value"
  42. :disabled="disabled"
  43. :shape="shape"
  44. :size="checkSize"
  45. :checkedBackgroundColor="themeContext.resolveThemeColor(color)"
  46. :checkedBorderColor="themeContext.resolveThemeColor(color)"
  47. :borderColor="themeContext.resolveThemeColor(borderColor)"
  48. :checkColor="themeContext.resolveThemeColor(checkColor)"
  49. :icon="props.icon"
  50. :iconSize="checkSize - 15"
  51. :type="props.icon != 'check-mark' ? 'icon' : 'radio'"
  52. />
  53. </slot>
  54. </Touchable>
  55. </template>
  56. <script setup lang="ts">
  57. import { computed, inject, onMounted } from 'vue';
  58. import { propGetThemeVar, useTheme, type TextStyle, type ViewStyle } from '../theme/ThemeDefine';
  59. import { StringUtils } from '@imengyu/imengyu-utils';
  60. import { DynamicSize } from '../theme/ThemeTools';
  61. import CheckBoxDefaultButton from './CheckBoxDefaultButton.vue';
  62. import Text from '../basic/Text.vue';
  63. import type { RadioBoxGroupContextInfo } from './RadioGroup.vue';
  64. import { useCellContext } from '../basic/CellContext';
  65. import Touchable from '../feedback/Touchable.vue';
  66. export interface RadioBoxProps {
  67. /**
  68. * 当前复选框在复选框组中的value,不能与其他重复
  69. */
  70. name: string|number|boolean;
  71. /**
  72. * 复选框的文字
  73. */
  74. text?: string;
  75. /**
  76. * 复选框的形状
  77. * @default 'round'
  78. */
  79. shape?: "square"|"round";
  80. /**
  81. * 复选框占满整个父元素,默认否
  82. * @default false
  83. */
  84. block?: boolean,
  85. /**
  86. * 复选框按钮位置,默认在左
  87. * @default 'left'
  88. */
  89. checkPosition?: "left"|"right";
  90. /**
  91. * 复选框未选择时的边框颜色
  92. * @default border.input
  93. */
  94. borderColor?: string|undefined;
  95. /**
  96. * 复选框选中时勾的颜色
  97. * @default white
  98. */
  99. checkColor?: string|undefined;
  100. /**
  101. * 复选框按钮大小,默认是 36
  102. * @default 36
  103. */
  104. checkSize?: number|undefined;
  105. /**
  106. * 按下时透明度
  107. * @default 0.75
  108. */
  109. activeOpacity?: number,
  110. /**
  111. * 复选框的颜色,默认是 primary
  112. * @default primary
  113. */
  114. color?: string;
  115. /**
  116. * 是否禁用复选框
  117. * @default false
  118. */
  119. disabled?: boolean;
  120. /**
  121. * 选择勾的图标
  122. * @default 'check-mark'
  123. */
  124. icon?: string;
  125. /**
  126. * 文字颜色
  127. * @default text.content
  128. */
  129. textColor?: string;
  130. /**
  131. * 禁用状态下文字颜色
  132. * @default text.second
  133. */
  134. disabledTextColor?: string;
  135. /**
  136. * 自定义文字样式
  137. */
  138. textStyle?: TextStyle;
  139. /**
  140. * 自定义样式
  141. */
  142. innerStyle?: ViewStyle;
  143. }
  144. const props = withDefaults(defineProps<RadioBoxProps>(), {
  145. modelValue: false,
  146. shape: () => propGetThemeVar('RadioBoxShape', 'round'),
  147. block: false,
  148. checkPosition: () => propGetThemeVar('RadioBoxCheckPosition', 'left'),
  149. borderColor: () => propGetThemeVar('RadioBoxBorderColor', 'border.input'),
  150. checkColor: () => propGetThemeVar('RadioBoxCheckColor', 'white'),
  151. textColor: () => propGetThemeVar('RadioBoxTextColor', 'text.content'),
  152. disabledTextColor: () => propGetThemeVar('RadioBoxDisabledTextColor', 'text.second'),
  153. color: () => propGetThemeVar('RadioBoxColor', 'primary'),
  154. checkSize: () => propGetThemeVar('RadioBoxCheckSize', 36),
  155. activeOpacity: () => propGetThemeVar('RadioBoxActiveOpacity', 0.75),
  156. disabled: false,
  157. icon: 'check-mark',
  158. });
  159. const disabled = computed(() => props.disabled || groupContext?.topDisabled.value);
  160. const themeContext = useTheme();
  161. const themeStyles = themeContext.useThemeStyles({
  162. checkBox: {
  163. alignSelf: 'flex-start',
  164. marginHorizontal: DynamicSize('RadioBoxMarginHorizontal', 4),
  165. },
  166. checkBoxFull: {
  167. alignSelf: 'stretch',
  168. width: '100%',
  169. justifyContent: 'space-between',
  170. marginHorizontal: DynamicSize('RadioBoxMarginHorizontal', 4),
  171. },
  172. check: {
  173. alignSelf: 'flex-start',
  174. alignItems: 'center',
  175. justifyContent: 'center',
  176. },
  177. checkText: {
  178. fontSize: DynamicSize('RadioBoxTextFontSize', 28),
  179. },
  180. });
  181. const groupContext = inject<RadioBoxGroupContextInfo>('RadioBoxGroupContext', null as any);
  182. const cellContext = useCellContext();;
  183. const value = computed(() => {
  184. if (!groupContext)
  185. return false;
  186. return groupContext.value.value === props.name;
  187. });
  188. function switchOn() {
  189. if (disabled.value)
  190. return;
  191. groupContext!.onValueChange(props.name, true);
  192. }
  193. onMounted(() => {
  194. if (cellContext)
  195. cellContext.setOnClickListener(() => switchOn());
  196. })
  197. defineOptions({
  198. options: {
  199. styleIsolation: "shared",
  200. virtualHost: true,
  201. }
  202. })
  203. </script>