123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- <template>
- <view class="evan-radio" @click="onRadioClick">
- <slot v-if="$slots.icon" name="icon"></slot>
- <template v-else>
- <uni-icons v-if="icon" :type="icon" :size="iconSize" :color="iconColor"></uni-icons>
- <view v-else class="evan-radio__inner" :class="['evan-radio__inner--'+shape]" :style="{width:iconSize+'px',height:iconSize+'px',backgroundColor:innerBackgroundColor,borderColor:innerBorderColor}">
- <uni-icons v-if="isChecked" type="checkmarkempty" :size="iconSize" :color="isDisabled?'#c8c9cc':'#fff'"></uni-icons>
- </view>
- </template>
- <text v-if="$slots.default" class="evan-radio__label" :style="mTitleStlye">
- <slot></slot>
- </text>
- </view>
- </template>
- <script>
- import UniIcons from '@/components/uni-icons/uni-icons.vue'
- export default {
- name: 'EvanRadio',
- components: {
- UniIcons
- },
- props: {
- shape: {
- type: String,
- default: 'round'
- },
- value: {
- type: [String, Number, Boolean],
- default: null
- },
- label: {
- type: [String, Number, Boolean],
- default: null
- },
- disabled: {
- type: Boolean,
- default: false
- },
- icon: {
- type: String,
- default: null
- },
- iconSize: {
- type: Number,
- default: 20
- },
- primaryColor: {
- type: String,
- default: '#108ee9'
- },
- titleStyle: {
- type: Object,
- default: () => {
- return {}
- }
- },
- preventClick: {
- type: Boolean,
- default: false
- },
- clearable: {
- type: Boolean,
- default: false
- }
- },
- computed: {
- isGroup() {
- let parent = this.getParent()
- if (parent) {
- return true
- }
- return false
- },
- isDisabled() {
- if (this.isGroup) {
- return this.getParent().disabled || this.disabled
- }
- return this.disabled
- },
- mTitleStlye() {
- let titleStyle = Object.assign({}, this.titleStyle || {})
- let arr = Object.keys(titleStyle).map((key) => {
- if (key === 'color' && this.disabled) {
- return null
- }
- return `${key}:${titleStyle[key]}`
- }).filter((v) => v)
- return arr.join(';')
- },
- isChecked() {
- let parent = this.getParent()
- if ((this.isGroup && parent.value === this.label) || (!this.isGroup && this.currentValue === this.label)) {
- return true
- }
- return false
- },
- innerBackgroundColor() {
- if (this.isDisabled) {
- return '#ebedf0'
- }
- let parent = this.getParent()
- if (this.isChecked) {
- return this.primaryColor
- }
- return '#fff'
- },
- innerBorderColor() {
- if (this.isDisabled) {
- return '#c8c9cc'
- }
- if (this.isChecked) {
- return this.primaryColor
- }
- return '#c8c9cc'
- },
- iconColor() {
- if (this.isDisabled) {
- return '#ebedf0'
- }
- if (this.isChecked) {
- return this.primaryColor
- }
- return '#c8c9cc'
- }
- },
- watch: {
- value: {
- immediate: true,
- handler(value) {
- this.currentValue = value
- }
- }
- },
- data() {
- return {
- currentValue: null
- }
- },
- methods: {
- // 获取EvanRadioGroup组件
- getParent() {
- let parent = this.$parent
- if (parent) {
- let parentName = parent.$options.name
- while (parentName !== 'EvanRadioGroup') {
- parent = parent.$parent
- if (parent) {
- parentName = parent.$options.name
- } else {
- return null
- }
- }
- return parent
- }
- return null
- },
- onRadioClick() {
- if (!this.isDisabled && !this.preventClick) {
- this.choose()
- }
- },
- select() {
- if (!this.isDisabled) {
- this.choose()
- }
- },
- choose() {
- if (this.currentValue !== this.label) {
- this.currentValue = this.label
- this.$emit('input', this.currentValue)
- if (this.isGroup) {
- let parent = this.getParent()
- parent.onRadioChange(this.label)
- }
- } else if (this.clearable) {
- this.currentValue = null
- this.$emit('input', this.currentValue)
- if (this.isGroup) {
- let parent = this.getParent()
- parent.onRadioChange(null)
- }
- }
- },
- setValue(groupValue) {
- this.currentValue = groupValue
- }
- },
- created() {
- let parent = this.getParent()
- if (parent) {
- this.setValue(parent.value)
- }
- }
- }
- </script>
- <style lang="scss" scoped>
- .evan-radio {
- /* #ifndef APP-NVUE */
- display: flex;
- /* #endif */
- flex-direction: row;
- align-items: center;
- }
- .evan-radio__label {
- font-size: 16px;
- margin-left: 8px;
- color: #333;
- }
- .evan-radio__inner {
- border-width: 1px;
- border-style: solid;
- background-color: #fff;
- /* #ifndef APP-NVUE */
- display: flex;
- box-sizing: border-box;
- /* #endif */
- flex-direction: column;
- align-items: center;
- justify-content: center;
- }
- .evan-radio__inner--round {
- border-radius: 50%;
- }
- </style>
|