mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-05-22 22:49:15 +08:00
[improvement] Button: tsx (#2766)
This commit is contained in:
parent
2e308ffc62
commit
2ebcf2a36a
@ -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);
|
@ -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,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user