wd-radio.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <template>
  2. <view
  3. :class="`wd-radio ${cellValue ? 'is-cell-radio' : ''} ${cellValue && shapeValue == 'button' ? 'is-button-radio' : ''} ${
  4. sizeValue ? 'is-' + sizeValue : ''
  5. } ${inlineValue ? 'is-inline' : ''} ${isChecked ? 'is-checked' : ''} ${shapeValue !== 'check' ? 'is-' + shapeValue : ''} ${
  6. disabledValue ? 'is-disabled' : ''
  7. } icon-placement-${iconPlacement} ${customClass}`"
  8. :style="customStyle"
  9. @click="handleClick"
  10. >
  11. <view
  12. class="wd-radio__label"
  13. :style="`${maxWidth ? 'max-width:' + maxWidth : ''}; ${
  14. isChecked && shapeValue === 'button' && !disabledValue ? 'color :' + checkedColorValue : ''
  15. }`"
  16. >
  17. <slot></slot>
  18. </view>
  19. <view class="wd-radio__shape" :style="isChecked && !disabledValue ? 'color: ' + checkedColorValue : ''">
  20. <wd-icon v-if="shapeValue === 'check'" :style="isChecked && !disabledValue ? 'color: ' + checkedColorValue : ''" name="check"></wd-icon>
  21. </view>
  22. </view>
  23. </template>
  24. <script lang="ts">
  25. export default {
  26. name: 'wd-radio',
  27. options: {
  28. virtualHost: true,
  29. addGlobalClass: true,
  30. styleIsolation: 'shared'
  31. }
  32. }
  33. </script>
  34. <script lang="ts" setup>
  35. import wdIcon from '../wd-icon/wd-icon.vue'
  36. import { computed, watch } from 'vue'
  37. import { useParent } from '../composables/useParent'
  38. import { RADIO_GROUP_KEY } from '../wd-radio-group/types'
  39. import { radioProps, type RadioIconPlacement } from './types'
  40. import { getPropByPath, isDef } from '../common/util'
  41. const props = defineProps(radioProps)
  42. const { parent: radioGroup } = useParent(RADIO_GROUP_KEY)
  43. const isChecked = computed(() => {
  44. if (radioGroup) {
  45. return props.value === radioGroup.props.modelValue
  46. } else {
  47. return false
  48. }
  49. })
  50. const shapeValue = computed(() => {
  51. return props.shape || getPropByPath(radioGroup, 'props.shape')
  52. })
  53. const checkedColorValue = computed(() => {
  54. return props.checkedColor || getPropByPath(radioGroup, 'props.checkedColor')
  55. })
  56. const disabledValue = computed(() => {
  57. if (isDef(props.disabled)) {
  58. return props.disabled
  59. } else {
  60. return getPropByPath(radioGroup, 'props.disabled')
  61. }
  62. })
  63. const inlineValue = computed(() => {
  64. if (isDef(props.inline)) {
  65. return props.inline
  66. } else {
  67. return getPropByPath(radioGroup, 'props.inline')
  68. }
  69. })
  70. const sizeValue = computed(() => {
  71. return props.size || getPropByPath(radioGroup, 'props.size')
  72. })
  73. const cellValue = computed(() => {
  74. if (isDef(props.cell)) {
  75. return props.cell
  76. } else {
  77. return getPropByPath(radioGroup, 'props.cell')
  78. }
  79. })
  80. const iconPlacement = computed<RadioIconPlacement>(() => {
  81. if (isDef(props.iconPlacement)) {
  82. return props.iconPlacement
  83. } else {
  84. return getPropByPath(radioGroup, 'props.iconPlacement')
  85. }
  86. })
  87. watch(
  88. () => props.shape,
  89. (newValue) => {
  90. const type = ['check', 'dot', 'button']
  91. if (!newValue || type.indexOf(newValue) === -1) console.error(`shape must be one of ${type.toString()}`)
  92. }
  93. )
  94. /**
  95. * 点击子元素,通知父元素触发change事件
  96. */
  97. function handleClick() {
  98. const { value } = props
  99. if (!disabledValue.value && radioGroup && isDef(value)) {
  100. radioGroup.updateValue(value)
  101. }
  102. }
  103. </script>
  104. <style lang="scss" scoped>
  105. @import './index.scss';
  106. </style>