CheckBox.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 name="check" 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. />
  26. </slot>
  27. <slot>
  28. <Text
  29. :innerStyle="{
  30. ...themeStyles.checkText.value,
  31. ...textStyle,
  32. color: themeContext.resolveThemeColor(props.disabled === true ? disabledTextColor : textColor),
  33. display: StringUtils.isNullOrEmpty(text) ? 'none' : 'flex',
  34. }"
  35. :text="text"
  36. />
  37. </slot>
  38. <slot name="check" v-if="checkPosition === 'right'" icon="check" :on="value" :disabled="disabled" :shape="shape">
  39. <CheckBoxDefaultButton
  40. :on="value"
  41. :disabled="disabled"
  42. :shape="shape"
  43. :size="checkSize"
  44. :checkedBackgroundColor="themeContext.resolveThemeColor(color)"
  45. :checkedBorderColor="themeContext.resolveThemeColor(color)"
  46. :borderColor="themeContext.resolveThemeColor(borderColor)"
  47. :checkColor="themeContext.resolveThemeColor(checkColor)"
  48. :icon="props.icon"
  49. />
  50. </slot>
  51. </Touchable>
  52. </template>
  53. <script setup lang="ts">
  54. import { computed, inject, onMounted, toRef, watch } from 'vue';
  55. import { useCellContext } from '../basic/CellContext';
  56. import { propGetThemeVar, useTheme, type TextStyle, type ViewStyle } from '../theme/ThemeDefine';
  57. import { useFieldChildValueInjector } from './FormContext';
  58. import { DynamicSize } from '../theme/ThemeTools';
  59. import { StringUtils } from '@imengyu/imengyu-utils';
  60. import type { CheckBoxGroupContextInfo } from './CheckBoxGroup.vue';
  61. import CheckBoxDefaultButton from './CheckBoxDefaultButton.vue';
  62. import Text from '../basic/Text.vue';
  63. import Touchable from '../feedback/Touchable.vue';
  64. export interface CheckBoxProps {
  65. /**
  66. * 是否选中复选框
  67. * @default false
  68. */
  69. modelValue?: boolean;
  70. /**
  71. * 当前复选框在复选框组中的value,不能与其他重复
  72. */
  73. name?: string;
  74. /**
  75. * 复选框的文字
  76. */
  77. text?: string;
  78. /**
  79. * 复选框的形状
  80. * @default 'square'
  81. */
  82. shape?: "square"|"round";
  83. /**
  84. * 复选框占满整个父元素,默认否
  85. * @default false
  86. */
  87. block?: boolean,
  88. /**
  89. * 复选框按钮位置,默认在左
  90. * @default 'left'
  91. */
  92. checkPosition?: "left"|"right";
  93. /**
  94. * 复选框未选择时的边框颜色
  95. * @default border.input
  96. */
  97. borderColor?: string|undefined;
  98. /**
  99. * 复选框选中时勾的颜色
  100. * @default white
  101. */
  102. checkColor?: string|undefined;
  103. /**
  104. * 复选框按钮大小,默认是 36
  105. * @default 36
  106. */
  107. checkSize?: number|undefined;
  108. /**
  109. * 按下时透明度
  110. * @default 0.75
  111. */
  112. activeOpacity?: number,
  113. /**
  114. * 复选框的颜色,默认是 primary
  115. * @default primary
  116. */
  117. color?: string;
  118. /**
  119. * 是否禁用复选框
  120. * @default false
  121. */
  122. disabled?: boolean;
  123. /**
  124. * 选择勾的图标
  125. * @default 'check-mark'
  126. */
  127. icon?: string;
  128. /**
  129. * 文字颜色
  130. * @default text.content
  131. */
  132. textColor?: string;
  133. /**
  134. * 禁用状态下文字颜色
  135. * @default text.second
  136. */
  137. disabledTextColor?: string;
  138. /**
  139. * 自定义文字样式
  140. */
  141. textStyle?: TextStyle;
  142. /**
  143. * 自定义样式
  144. */
  145. innerStyle?: ViewStyle;
  146. }
  147. const props = withDefaults(defineProps<CheckBoxProps>(), {
  148. modelValue: false,
  149. shape: () => propGetThemeVar('CheckBoxShape', 'square'),
  150. block: false,
  151. checkPosition: () => propGetThemeVar('CheckBoxCheckPosition', 'left'),
  152. borderColor: () => propGetThemeVar('CheckBoxBorderColor', 'border.input'),
  153. checkColor: () => propGetThemeVar('CheckBoxCheckColor', 'white'),
  154. textColor: () => propGetThemeVar('CheckBoxTextColor', 'text.content'),
  155. disabledTextColor: () => propGetThemeVar('CheckBoxDisabledTextColor', 'text.second'),
  156. color: () => propGetThemeVar('CheckBoxColor', 'primary'),
  157. checkSize: () => propGetThemeVar('CheckBoxCheckSize', 36),
  158. activeOpacity: () => propGetThemeVar('CheckBoxActiveOpacity', 0.75),
  159. disabled: false,
  160. icon: 'check-mark',
  161. });
  162. const emit = defineEmits<{
  163. (e: 'update:modelValue', value: boolean): void;
  164. }>();
  165. const themeContext = useTheme();
  166. const themeStyles = themeContext.useThemeStyles({
  167. checkBox: {
  168. alignSelf: 'flex-start',
  169. marginHorizontal: DynamicSize('CheckBoxMarginHorizontal', 4),
  170. },
  171. checkBoxFull: {
  172. alignSelf: 'stretch',
  173. width: '100%',
  174. justifyContent: 'space-between',
  175. marginHorizontal: DynamicSize('CheckBoxMarginHorizontal', 4),
  176. },
  177. check: {
  178. alignSelf: 'flex-start',
  179. alignItems: 'center',
  180. justifyContent: 'center',
  181. },
  182. checkText: {
  183. fontSize: DynamicSize('CheckBoxTextFontSize', 28),
  184. },
  185. });
  186. const groupContext = inject<CheckBoxGroupContextInfo>('checkBoxGroupContext', null as any);
  187. const cellContext = useCellContext();
  188. const disabled = computed(() => props.disabled === true || groupContext?.disabled.value);
  189. watch(() => [ props.name, props.disabled ], () => {
  190. if (props.name)
  191. groupContext?.onAddItem(props.name, props.disabled);
  192. }, { immediate: true })
  193. const {
  194. value,
  195. updateValue,
  196. } = useFieldChildValueInjector(
  197. toRef(props, 'modelValue'),
  198. (v) => emit('update:modelValue', v),
  199. groupContext ? {
  200. getValue: () => groupContext!.value.value && groupContext.value.value.indexOf(props.name ?? '') >= 0,
  201. updateValue: (v) => groupContext!.onValueChange(props.name, !value.value),
  202. } : undefined,
  203. );
  204. function switchOn() {
  205. if (props.disabled)
  206. return;
  207. updateValue(!value.value);
  208. }
  209. onMounted(() => {
  210. if (cellContext)
  211. cellContext.setOnClickListener(() => switchOn());
  212. })
  213. defineOptions({
  214. options: {
  215. styleIsolation: "shared",
  216. virtualHost: true,
  217. }
  218. })
  219. </script>