fix(Badge): fix badge offset of different position (#11400)

* fix(Badge): fix badge offset of different position

* perf: improve code
This commit is contained in:
Gavin 2022-12-24 12:54:40 +08:00 committed by GitHub
parent e39f0c67d1
commit 78847b8375
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -71,6 +71,9 @@ export default defineComponent({
}
};
const getOffsetWithMinusString = (val: string) =>
val.startsWith('-') ? val.replace('-', '') : `-${val}`;
const style = computed(() => {
const style: CSSProperties = {
background: props.color,
@ -78,13 +81,23 @@ export default defineComponent({
if (props.offset) {
const [x, y] = props.offset;
const { position } = props;
const offsetY = position.indexOf('top') > -1 ? 'top' : 'bottom';
const offsetX = position.indexOf('left') > -1 ? 'left' : 'right';
if (slots.default) {
style.top = addUnit(y);
if (typeof y === 'number') {
style[offsetY] = addUnit(offsetY === 'top' ? y : -y);
} else {
style[offsetY] =
offsetY === 'top' ? addUnit(y) : getOffsetWithMinusString(y);
}
if (typeof x === 'number') {
style.right = addUnit(-x);
style[offsetX] = addUnit(offsetX === 'left' ? x : -x);
} else {
style.right = x.startsWith('-') ? x.replace('-', '') : `-${x}`;
style[offsetX] =
offsetX === 'left' ? addUnit(x) : getOffsetWithMinusString(x);
}
} else {
style.marginTop = addUnit(y);