u
This commit is contained in:
1 parent
4d0d2f875e
commit
27263365ad
13 files changed
+265
-21
No files matched your search
@@ -0,0 +1,87 @@
|
||||
@use '../_styles/_variables.scss' as *;
|
||||
@use 'sass:color';
|
||||
|
||||
$switch-prefix: '#{$prefix}-switch';
|
||||
|
||||
.#{$switch-prefix} {
|
||||
position: relative;
|
||||
box-sizing: border-box;
|
||||
min-width: 44px;
|
||||
height: 22px;
|
||||
padding: 0;
|
||||
overflow: hidden;
|
||||
line-height: 22px;
|
||||
vertical-align: middle;
|
||||
background-color: $bg-color-secondarycomponent;
|
||||
border: none;
|
||||
border-radius: 12px;
|
||||
outline: none;
|
||||
cursor: pointer;
|
||||
transition: background-color 0.2s cubic-bezier(0.34, 0.69, 0.1, 1);
|
||||
|
||||
// 普通 hover,只在非 disabled 下生效
|
||||
&:not(.#{$switch-prefix}-disabled):not(.#{$switch-prefix}-loading):hover {
|
||||
background-color: $bg-color-secondarycomponent-hover;
|
||||
}
|
||||
|
||||
// checked 状态 hover,只在非 disabled 下生效
|
||||
&.#{$switch-prefix}-checked:not(.#{$switch-prefix}-disabled):not(.#{$switch-prefix}-loading):hover {
|
||||
background-color: $primary-color-hover;
|
||||
}
|
||||
|
||||
&.#{$switch-prefix}-checked {
|
||||
background-color: $primary-color;
|
||||
|
||||
.#{$switch-prefix}-handle {
|
||||
left: calc(100% - 4px - 14px); // 100% - 右边距 - handle宽度
|
||||
}
|
||||
|
||||
.#{$switch-prefix}-text {
|
||||
right: auto;
|
||||
left: calc(4px + 4px); // left + left
|
||||
}
|
||||
.#{$switch-prefix}-text-holder {
|
||||
margin: 0 24px 0 8px;
|
||||
}
|
||||
}
|
||||
|
||||
&.#{$switch-prefix}-loading {
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
&.#{$switch-prefix}-disabled {
|
||||
cursor: not-allowed;
|
||||
opacity: 0.5;
|
||||
}
|
||||
|
||||
.#{$switch-prefix}-handle {
|
||||
position: absolute;
|
||||
top: 50%;
|
||||
left: 4px;
|
||||
transform: translateY(-50%);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
width: 14px;
|
||||
height: 14px;
|
||||
font-size: 12px;
|
||||
background-color: #fff;
|
||||
border-radius: 50%;
|
||||
transition: all 0.2s cubic-bezier(0.34, 0.69, 0.1, 1);
|
||||
box-shadow: $shadow-switch;
|
||||
}
|
||||
|
||||
.#{$switch-prefix}-text-holder {
|
||||
margin: 0 8px 0 24px; // 8 = 4 + 4 22 = 14 + 4 + 4 + 2
|
||||
font-size: 12px;
|
||||
opacity: 0;
|
||||
}
|
||||
|
||||
.#{$switch-prefix}-text {
|
||||
position: absolute;
|
||||
top: 0;
|
||||
right: calc(4px + 4px);
|
||||
color: #fff;
|
||||
font-size: 12px;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
import type { PropType, VNode } from 'vue'
|
||||
import { computed, defineComponent, ref, watch } from 'vue'
|
||||
import { getPrefixCls } from '../_utils/getPrefixCls'
|
||||
import Icon from '../icon/index'
|
||||
import Spin from '../spin/index'
|
||||
|
||||
export default defineComponent({
|
||||
name: 'Switch',
|
||||
props: {
|
||||
modelValue: {
|
||||
type: [String, Number, Boolean] as PropType<string | number | boolean>,
|
||||
default: false,
|
||||
},
|
||||
checkedValue: {
|
||||
type: [String, Number, Boolean] as PropType<string | number | boolean>,
|
||||
default: true,
|
||||
},
|
||||
uncheckedValue: {
|
||||
type: [String, Number, Boolean] as PropType<string | number | boolean>,
|
||||
default: false,
|
||||
},
|
||||
checkedContent: {
|
||||
type: [String, Object, Function] as unknown as () => string | VNode | (() => VNode),
|
||||
default: '',
|
||||
},
|
||||
uncheckedContent: {
|
||||
type: [String, Object, Function] as unknown as () => string | VNode | (() => VNode),
|
||||
default: '',
|
||||
},
|
||||
disabled: {
|
||||
type: Boolean,
|
||||
default: false,
|
||||
},
|
||||
loading: {
|
||||
type: Boolean,
|
||||
default: false,
|
||||
},
|
||||
class: {
|
||||
type: [String, Array, Object],
|
||||
default: '',
|
||||
},
|
||||
style: {
|
||||
type: Object,
|
||||
default: () => ({}),
|
||||
},
|
||||
},
|
||||
emits: ['update:modelValue', 'change'],
|
||||
setup(props, { emit, slots }) {
|
||||
const prefixCls = getPrefixCls('switch')
|
||||
|
||||
const _checked = ref(props.modelValue)
|
||||
|
||||
watch(
|
||||
() => props.modelValue,
|
||||
(val) => {
|
||||
_checked.value = val
|
||||
},
|
||||
)
|
||||
const isChecked = computed(() => _checked.value === props.checkedValue)
|
||||
|
||||
const toggle = (e: Event) => {
|
||||
const newValue = isChecked.value ? props.uncheckedValue : props.checkedValue
|
||||
_checked.value = newValue
|
||||
|
||||
emit('update:modelValue', newValue)
|
||||
emit('change', newValue, e)
|
||||
}
|
||||
|
||||
const displayContent = computed(() => {
|
||||
// 优先使用插槽
|
||||
if (isChecked.value && slots.checkedContent) {
|
||||
return slots.checkedContent()
|
||||
}
|
||||
else if (!isChecked.value && slots.uncheckedContent) {
|
||||
return slots.uncheckedContent()
|
||||
}
|
||||
|
||||
// fallback 到 props
|
||||
return isChecked.value
|
||||
? typeof props.checkedContent === 'function'
|
||||
? props.checkedContent()
|
||||
: props.checkedContent
|
||||
: typeof props.uncheckedContent === 'function'
|
||||
? props.uncheckedContent()
|
||||
: props.uncheckedContent
|
||||
})
|
||||
|
||||
return () => (
|
||||
<button
|
||||
type="button"
|
||||
role="switch"
|
||||
disabled={props.disabled || props.loading}
|
||||
class={[
|
||||
prefixCls,
|
||||
props.class,
|
||||
{
|
||||
[`${prefixCls}-checked`]: isChecked.value,
|
||||
[`${prefixCls}-disabled`]: props.disabled,
|
||||
[`${prefixCls}-loading`]: props.loading,
|
||||
},
|
||||
]}
|
||||
onClick={toggle}
|
||||
>
|
||||
<span class={`${prefixCls}-handle`}>
|
||||
{props.loading && <Spin loading size={12} />}
|
||||
</span>
|
||||
<span class={`${prefixCls}-text-holder`}>{displayContent.value}</span>
|
||||
<span class={`${prefixCls}-text`}>{displayContent.value}</span>
|
||||
</button>
|
||||
)
|
||||
},
|
||||
})
|
||||
Reference in new issue
Block a user