Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(badge): use composition api to refactor badge component #402

Merged
merged 3 commits into from Mar 8, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
103 changes: 56 additions & 47 deletions src/badge/badge.tsx
@@ -1,7 +1,7 @@
import { defineComponent, ComponentPublicInstance } from 'vue';
import { defineComponent, computed } from 'vue';
import { prefix } from '../config';
import props from './props';
import { renderTNodeJSX } from '../utils/render-tnode';
import { useTNodeJSX } from '../hooks/tnode';

const name = `${prefix}-badge`;
ChrisLee0211 marked this conversation as resolved.
Show resolved Hide resolved

Expand All @@ -10,64 +10,73 @@ export default defineComponent({

props: { ...props },

methods: {
getContent() {
if (this.dot) return '';
if (typeof this.count === 'function') {
return renderTNodeJSX(this, 'count');
setup(props) {
const renderTNodeJSX = useTNodeJSX();

/** 内容计算相关逻辑 start */
const content = computed(() => {
if (props.dot) return '';
if (typeof props.count === 'function') {
return renderTNodeJSX('count');
}
if (Number.isNaN(Number(this.count))) {
return this.count;
if (Number.isNaN(Number(props.count))) {
return props.count;
}
const count = Number(this.count);
return count > this.maxCount ? `${this.maxCount}+` : count;
},
isSmall() {
return this.size === 'small';
},
isZero() {
const content = this.getContent();
return content === 0 || content === '0';
},
isHidden() {
return !this.showZero && this.isZero();
},
getOffset() {
if (!this.offset) return {};
let [xOffset, yOffset]: Array<string | number> = this.offset;
const count = Number(props.count);
return count > props.maxCount ? `${props.maxCount}+` : count;
});

const getOffset = () => {
if (!props.offset) return {};
let [xOffset, yOffset]: Array<string | number> = props.offset;
xOffset = Number.isNaN(Number(xOffset)) ? xOffset : `${xOffset}px`;
yOffset = Number.isNaN(Number(yOffset)) ? yOffset : `${yOffset}px`;
return { xOffset, yOffset };
},
};
/** 内容计算相关逻辑 end */

/** 样式计算相关逻辑 start */
const isHidden = computed(() => {
return !props.showZero && (content.value === 0 || content.value === '0');
});

const badgeClassNames = computed(() => {
return [
{
[`${name}--dot`]: !!props.dot,
[`${name}--circle`]: !props.dot && props.shape === 'circle',
[`${name}--round`]: props.shape === 'round',
't-size-s': props.size === 'small',
},
];
});

const inlineStyle = computed(() => {
const { xOffset, yOffset } = getOffset();
return {
background: `${props.color}`,
right: xOffset,
top: yOffset,
};
});
/** 样式计算相关逻辑 end */
return {
content,
inlineStyle,
badgeClassNames,
isHidden,
};
},

render() {
const { dot, shape, color } = this.$props;

const content = this.getContent();
const isHidden = this.isHidden();
const children = this.$slots.default ? this.$slots.default(null) : '';
ChrisLee0211 marked this conversation as resolved.
Show resolved Hide resolved
const { xOffset, yOffset } = this.getOffset();
const badgeClassNames = [
{
[`${name}--dot`]: !!dot,
[`${name}--circle`]: !dot && shape === 'circle',
[`${name}--round`]: shape === 'round',
't-size-s': this.isSmall(),
},
];
const inlineStyle = {
background: `${color}`,
right: xOffset,
top: yOffset,
};

return (
<div class={name} {...this.$attrs}>
{children || null}
{isHidden ? null : (
<sup class={badgeClassNames} style={inlineStyle}>
{content}
{this.isHidden ? null : (
<sup class={this.badgeClassNames} style={this.inlineStyle}>
{this.content}
</sup>
)}
</div>
Expand Down