[improvement] Button: tsx (#2766)

This commit is contained in:
neverland 2019-02-16 22:03:28 +08:00 committed by GitHub
parent 2e308ffc62
commit 2ebcf2a36a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 7 deletions

View File

@ -3,12 +3,21 @@ import { emit, inherit } from '../utils/functional';
import { routeProps, functionalRoute } from '../mixins/router'; import { routeProps, functionalRoute } from '../mixins/router';
import Loading from '../loading'; import Loading from '../loading';
// Types
import { RouteProps } from '../mixins/router';
import { FunctionalComponent } from '../utils/use/sfc';
const [sfc, bem] = use('button'); const [sfc, bem] = use('button');
function Button(h, props, slots, ctx) { const Button: FunctionalComponent<ButtonProps> = function(
const { type, disabled, loading, loadingText } = props; h,
props,
slots,
ctx
) {
const { tag, type, disabled, loading, loadingText } = props;
const onClick = event => { const onClick = (event: Event) => {
if (!loading && !disabled) { if (!loading && !disabled) {
emit(ctx, 'click', event); emit(ctx, 'click', event);
functionalRoute(ctx); functionalRoute(ctx);
@ -16,7 +25,7 @@ function Button(h, props, slots, ctx) {
}; };
return ( return (
<props.tag <tag
type={props.nativeType} type={props.nativeType}
disabled={disabled} disabled={disabled}
class={bem([ class={bem([
@ -45,9 +54,25 @@ function Button(h, props, slots, ctx) {
{slots.default ? slots.default() : props.text} {slots.default ? slots.default() : props.text}
</span> </span>
)} )}
</props.tag> </tag>
); );
} };
export type ButtonProps = RouteProps & {
tag?: string;
type?: string;
size?: string;
text?: string;
block?: boolean;
plain?: boolean;
round?: boolean;
square?: boolean;
loading?: boolean;
disabled?: boolean;
nativeType?: string;
loadingText?: string;
bottomAction?: boolean;
};
Button.props = { Button.props = {
...routeProps, ...routeProps,
@ -75,4 +100,4 @@ Button.props = {
} }
}; };
export default sfc(Button); export default sfc<ButtonProps>(Button);

View File

@ -24,6 +24,12 @@ export function functionalRoute(context: RenderContext) {
route(context.parent && context.parent.$router, context.props); route(context.parent && context.parent.$router, context.props);
} }
export type RouteProps = {
url?: string,
replace?: boolean;
to?: RawLocation;
}
export const routeProps = { export const routeProps = {
url: String, url: String,
replace: Boolean, replace: Boolean,