[improvement] Tag: tsx (#2770)

This commit is contained in:
neverland 2019-02-17 18:49:22 +08:00 committed by GitHub
parent 82b29267a2
commit 890bd72268
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 9 deletions

View File

@ -33,4 +33,4 @@ CellGroup.props = {
} }
}; };
export default sfc(CellGroup); export default sfc<CellGroupProps>(CellGroup);

View File

@ -26,4 +26,4 @@ Info.props = {
info: [String, Number] info: [String, Number]
}; };
export default sfc(Info); export default sfc<InfoProps>(Info);

View File

@ -2,18 +2,21 @@ import { use } from '../utils';
import { inherit } from '../utils/functional'; import { inherit } from '../utils/functional';
import { RED, BLUE, GREEN, GRAY_DARK } from '../utils/color'; import { RED, BLUE, GREEN, GRAY_DARK } from '../utils/color';
// Types
import { FunctionalComponent } from '../utils/use/sfc';
const [sfc, bem] = use('tag'); const [sfc, bem] = use('tag');
const COLOR_MAP = { const COLOR_MAP: { [key: string]: string } = {
danger: RED, danger: RED,
primary: BLUE, primary: BLUE,
success: GREEN success: GREEN
}; };
function Tag(h, props, slots, ctx) { const Tag: FunctionalComponent<TagProps> = function(h, props, slots, ctx) {
const { mark, plain, round, size } = ctx.props; const { type, mark, plain, round, size } = ctx.props;
const color = props.color || COLOR_MAP[props.type] || GRAY_DARK; const color = props.color || (type && COLOR_MAP[type]) || GRAY_DARK;
const key = plain ? 'color' : 'backgroundColor'; const key = plain ? 'color' : 'backgroundColor';
const style = { [key]: color }; const style = { [key]: color };
@ -21,11 +24,16 @@ function Tag(h, props, slots, ctx) {
style.color = props.textColor; style.color = props.textColor;
} }
const classes: { [key: string]: any } = { mark, plain, round };
if (size) {
classes[size] = size;
}
return ( return (
<span <span
style={style} style={style}
class={[ class={[
bem({ mark, plain, round, [size]: size }), bem(classes),
{ {
'van-hairline--surround': plain 'van-hairline--surround': plain
} }
@ -35,7 +43,17 @@ function Tag(h, props, slots, ctx) {
{slots.default && slots.default()} {slots.default && slots.default()}
</span> </span>
); );
} };
export type TagProps = {
size?: string;
type?: string;
mark?: boolean;
color?: string;
plain?: boolean;
round?: boolean;
textColor?: string;
};
Tag.props = { Tag.props = {
size: String, size: String,
@ -47,4 +65,4 @@ Tag.props = {
textColor: String textColor: String
}; };
export default sfc(Tag); export default sfc<TagProps>(Tag);