(component): switch组件新增 尺寸选项

This commit is contained in:
0o张不歪o0 2022-07-19 10:25:46 +08:00
parent 80a793d151
commit b21410440a
3 changed files with 90 additions and 10 deletions

View File

@ -1,3 +1,54 @@
@lg: 28px;
@md: 22px;
@sm: 16px;
@xs: 13px;
@lg-min-width: 55px;
@md-min-width: 35px;
@sm-min-width: 26px;
@xs-min-width: 20px;
.set-size(@size, @min-width) {
& {
.layui-form-switch {
height: @size;
min-width: @min-width;
span{
width: calc(@size - 4px);
height: calc(@size - 4px);
transition:all 0.1s linear;
}
em{
margin-left: calc(@size - 3px);
}
}
.layui-form-onswitch{
span{
left: calc(100% - calc(@size - 1px));
}
em{
margin-right: calc(@size - 3px);
margin-left: 0px;
}
}
}
}
.layui-switch-container{
&[size="lg"] {
.set-size(@lg,@lg-min-width);
}
&[size="md"] {
.set-size(@md,@md-min-width);
}
&[size="sm"] {
.set-size(@sm,@sm-min-width);
}
&[size="xs"] {
.set-size(@xs,@xs-min-width);
}
}
.layui-form-switch { .layui-form-switch {
position: relative; position: relative;
height: 22px; height: 22px;
@ -7,8 +58,8 @@
border-radius: 20px; border-radius: 20px;
cursor: pointer; cursor: pointer;
background-color: var(--global-neutral-color-6); background-color: var(--global-neutral-color-6);
-webkit-transition: 0.1s linear; -webkit-transition:all 0.1s linear;
transition: 0.1s linear; transition:all 0.1s linear;
} }
.layui-form-switch span { .layui-form-switch span {
@ -24,15 +75,14 @@
border-radius: 20px; border-radius: 20px;
background-color: #fff; background-color: #fff;
box-shadow: 0 2px 4px #00230b33; box-shadow: 0 2px 4px #00230b33;
-webkit-transition: 0.1s linear; -webkit-transition:all 0.1s linear;
transition: 0.1s linear; transition:all 0.1s linear;
} }
.layui-form-switch em { .layui-form-switch em {
position: relative; position: relative;
top: 0; top: 0;
width: 25px; width: 25px;
margin-left: 21px;
padding: 0 !important; padding: 0 !important;
text-align: center !important; text-align: center !important;
color: #999 !important; color: #999 !important;
@ -46,14 +96,10 @@
} }
.layui-form-onswitch span { .layui-form-onswitch span {
left: 100%;
margin-left: -21px;
background-color: #fff; background-color: #fff;
} }
.layui-form-onswitch em { .layui-form-onswitch em {
margin-left: 5px;
margin-right: 21px;
color: #fff !important; color: #fff !important;
} }

View File

@ -17,12 +17,14 @@ export interface LaySwitchProps {
unswitchColor?: string; unswitchColor?: string;
onswitchValue?: string | number | boolean; onswitchValue?: string | number | boolean;
unswitchValue?: string | number | boolean; unswitchValue?: string | number | boolean;
size?: "lg" | "md" | "sm" | "xs";
} }
const props = withDefaults(defineProps<LaySwitchProps>(), { const props = withDefaults(defineProps<LaySwitchProps>(), {
disabled: false, disabled: false,
onswitchValue: true, onswitchValue: true,
unswitchValue: false, unswitchValue: false,
size:'md'
}); });
const emit = defineEmits(["update:modelValue", "change"]); const emit = defineEmits(["update:modelValue", "change"]);
@ -58,7 +60,7 @@ const styles = computed(() => {
</script> </script>
<template> <template>
<span @click.stop="handleClick"> <span @click.stop="handleClick" class="layui-switch-container" :size="size">
<div <div
class="layui-unselect layui-form-switch" class="layui-unselect layui-form-switch"
:style="styles" :style="styles"

View File

@ -172,6 +172,37 @@ export default {
::: :::
::: title 尺寸
:::
::: demo
<template>
<div style='display:flex;align-items: flex-end;'>
<lay-switch v-model="active7" size='lg'></lay-switch>
<lay-switch v-model="active7" size='md' style='margin-left:10px'></lay-switch>
<lay-switch v-model="active7" size='sm' style='margin-left:10px'></lay-switch>
<lay-switch v-model="active7" size='xs' style='margin-left:10px'></lay-switch>
</div>
</template>
<script>
import { ref } from 'vue'
export default {
setup() {
const active7 = ref(true)
return {
active7
}
}
}
</script>
:::
::: title Switch 属性 ::: title Switch 属性
::: :::
@ -186,6 +217,7 @@ export default {
| unswitch-text | 禁用描述 | `禁用` | | unswitch-text | 禁用描述 | `禁用` |
| onswitch-value | 启用值 | `true` | | onswitch-value | 启用值 | `true` |
| unswitch-value | 禁用值 | `false` | | unswitch-value | 禁用值 | `false` |
| size | 尺寸 | `lg` `md` `sm` `xs` |
::: :::