[暂存] 重构 switch 逻辑

This commit is contained in:
就眠儀式 2021-11-18 11:21:57 +08:00
parent 0886ee3a1f
commit b0a3bd0ac9
6 changed files with 139 additions and 100 deletions

View File

@ -3,6 +3,29 @@
::: demo ::: demo
<template>
<lay-switch></lay-switch>
</template>
<script>
import { ref } from 'vue'
export default {
setup() {
return {
}
}
}
</script>
:::
::: title 基础使用
:::
::: demo
<template> <template>
<lay-switch v-model="active1"></lay-switch> <lay-switch v-model="active1"></lay-switch>
</template> </template>

View File

@ -86,6 +86,12 @@ export default {
subTitle: 'change log', subTitle: 'change log',
path: '/zh-CN/guide/changelog', path: '/zh-CN/guide/changelog',
}, },
{
id: 4,
title: '规范',
subTitle: 'norms',
path: '/zh-CN/guide/norms'
}
], ],
}, },
] ]

View File

@ -11,19 +11,19 @@ import { computed } from "vue";
export interface LayButtonProps { export interface LayButtonProps {
type?: "primary" | "normal" | "warm" | "danger"; type?: "primary" | "normal" | "warm" | "danger";
size?: "lg" | "sm" | "xs"; size?: "lg" | "sm" | "xs";
fluid?: boolean; fluid?: boolean | string;
radius?: boolean; radius?: boolean | string;
border?: "green" | "blue" | "orange" | "red" | "black"; border?: "green" | "blue" | "orange" | "red" | "black";
disabled?: boolean; disabled?: boolean | string;
loading?: boolean; loading?: boolean | string;
nativeType?: "button" | "submit" | "reset"; nativeType?: "button" | "submit" | "reset";
} }
const props = withDefaults(defineProps<LayButtonProps>(), { const props = withDefaults(defineProps<LayButtonProps>(), {
fluid: false, fluid: false,
radius: false, radius: false,
disabled: false,
loading: false, loading: false,
disabled: false,
nativeType: "button", nativeType: "button",
}); });
@ -43,7 +43,7 @@ const classes = computed(() => {
{ {
'layui-btn-fluid': fluid, 'layui-btn-fluid': fluid,
'layui-btn-radius': radius, 'layui-btn-radius': radius,
'layui-btn-disabled': disabled 'layui-btn-disabled': disabled,
}, },
classes, classes,
]" ]"

View File

@ -83,52 +83,52 @@
<script lang="ts"> <script lang="ts">
export default { export default {
name: 'LayLayer', name: "LayLayer",
} };
</script> </script>
<script lang="ts" setup> <script lang="ts" setup>
import { onMounted, onUpdated, ref, useSlots, watch } from 'vue' import { onMounted, onUpdated, ref, useSlots, watch } from "vue";
import useMove from '../../hooks/useMove' import useMove from "../../hooks/useMove";
import { guid } from '../../tools/guidUtil' import { guid } from "../../tools/guidUtil";
const slot = useSlots() const slot = useSlots();
onMounted(() => { onMounted(() => {
moveHandle() moveHandle();
}) });
onUpdated(() => { onUpdated(() => {
moveHandle() moveHandle();
}) });
const props = withDefaults( export interface LayLayerProps {
defineProps<{ id?: string;
id?: string zIndex?: number;
zIndex?: number title?: string;
title?: string offset?: string[];
offset?: string[] width?: string;
width?: string height?: string;
height?: string visible?: boolean | string;
visible?: boolean | string maxmin?: boolean | string;
maxmin?: boolean | string btn?: Record<string, unknown>[];
btn?: Record<string, unknown>[] move?: boolean | string;
move?: boolean | string type?: number;
type?: number content?: string;
content?: string shade?: boolean | string;
shade?: boolean | string shadeClose?: boolean | string;
shadeClose?: boolean | string closeBtn?: boolean | string;
closeBtn?: boolean | string btnAlign?: string;
btnAlign?: string anim?: number;
anim?: number }
}>(),
{ const props = withDefaults(defineProps<LayLayerProps>(), {
id: 'layer-' + guid(), id: "layer-" + guid(),
zIndex: 99999999, zIndex: 99999999,
title: '标题', title: "标题",
offset: () => ['50%', '50%'], offset: () => ["50%", "50%"],
width: '390px', width: "390px",
height: '330px', height: "330px",
visible: true, visible: true,
maxmin: false, maxmin: false,
move: false, move: false,
@ -137,75 +137,74 @@ const props = withDefaults(
shade: false, shade: false,
shadeClose: true, shadeClose: true,
closeBtn: true, closeBtn: true,
btnAlign: 'l', btnAlign: "l",
anim: 0, anim: 0,
content: '', content: "",
} });
)
const top = ref(props.offset[0]) const top = ref(props.offset[0]);
const left = ref(props.offset[1]) const left = ref(props.offset[1]);
const width = ref(props.width) const width = ref(props.width);
const height = ref(props.height) const height = ref(props.height);
const max = ref(false) const max = ref(false);
const contentHeight = ref( const contentHeight = ref(
props.btn.length > 0 props.btn.length > 0
? 'calc(' + height.value + ' - 100px)' ? "calc(" + height.value + " - 100px)"
: 'calc(' + height.value + ' - 50px)' : "calc(" + height.value + " - 50px)"
) );
watch(max, function () { watch(max, function () {
if (max.value) { if (max.value) {
contentHeight.value = contentHeight.value =
props.btn.length > 0 ? 'calc(100% - 100px)' : 'calc(100% - 50px)' props.btn.length > 0 ? "calc(100% - 100px)" : "calc(100% - 50px)";
} else { } else {
contentHeight.value = contentHeight.value =
props.btn.length > 0 props.btn.length > 0
? 'calc(' + height.value + ' - 100px)' ? "calc(" + height.value + " - 100px)"
: 'calc(' + height.value + ' - 50px)' : "calc(" + height.value + " - 50px)";
} }
}) });
const emit = defineEmits(['close', 'update:visible']) const emit = defineEmits(["close", "update:visible"]);
const moveHandle = function () { const moveHandle = function () {
if (props.move) { if (props.move) {
const el = document.getElementById(props.id) const el = document.getElementById(props.id);
if (el != null) { if (el != null) {
useMove(el) useMove(el);
} }
} }
} };
const shadeHandle = function () { const shadeHandle = function () {
if (props.shadeClose) { if (props.shadeClose) {
emit('close') emit("close");
emit('update:visible', false) emit("update:visible", false);
} }
} };
const closeHandle = function () { const closeHandle = function () {
emit('close') emit("close");
emit('update:visible', false) emit("update:visible", false);
} };
const minHandle = function () { const minHandle = function () {
emit('close') emit("close");
emit('update:visible', false) emit("update:visible", false);
} };
const maxHandle = function () { const maxHandle = function () {
if (max.value) { if (max.value) {
width.value = props.width width.value = props.width;
height.value = props.height height.value = props.height;
top.value = props.offset[0] top.value = props.offset[0];
left.value = props.offset[1] left.value = props.offset[1];
} else { } else {
width.value = '100%' width.value = "100%";
height.value = '100%' height.value = "100%";
top.value = '0px' top.value = "0px";
left.value = '0px' left.value = "0px";
} }
max.value = !max.value max.value = !max.value;
} };
</script> </script>

View File

@ -5,27 +5,37 @@ export default {
</script> </script>
<script setup lang="ts"> <script setup lang="ts">
import { defineProps, defineEmits } from "vue"; import { defineProps, defineEmits, computed, ref } from "vue";
import "./index.less" import "./index.less";
export interface LaySwitchProps { export interface LaySwitchProps {
modelValue: boolean;
disabled?: boolean; disabled?: boolean;
activeText?: string; activeText?: string;
modelValue?: boolean;
inactiveText?: string; inactiveText?: string;
} }
const props = withDefaults(defineProps<LaySwitchProps>(), { const props = withDefaults(defineProps<LaySwitchProps>(), {
disabled: false,
activeText: "启用", activeText: "启用",
inactiveText: "禁用", inactiveText: "禁用",
}); });
const emit = defineEmits(["update:modelValue", "change"]); const emit = defineEmits(["update:modelValue", "change"]);
const isActive = computed({
get() {
return props.modelValue;
},
set(val) {
emit("change", val);
emit("update:modelValue", val);
},
});
const handleClick = function () { const handleClick = function () {
if (!props.disabled) { if (!props.disabled) {
emit("update:modelValue", !props.modelValue); isActive.value = !isActive.value;
emit("change", !props.modelValue);
} }
}; };
</script> </script>
@ -35,11 +45,12 @@ const handleClick = function () {
<div <div
class="layui-unselect layui-form-switch" class="layui-unselect layui-form-switch"
:class="{ :class="{
'layui-form-onswitch': modelValue, 'layui-disabled': disabled,
'layui-checkbox-disbaled layui-disabled': disabled, 'layui-form-onswitch': isActive,
'layui-checkbox-disbaled': disabled,
}" }"
> >
<em>{{ modelValue == true ? activeText : inactiveText }}</em> <em>{{ isActive == true ? activeText : inactiveText }}</em>
<i /> <i />
</div> </div>
</span> </span>