mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
[improvement] Cell: tsx (#2771)
This commit is contained in:
parent
890bd72268
commit
34e30aac5e
@ -4,9 +4,14 @@ import { emit, inherit } from '../utils/functional';
|
|||||||
import { routeProps, functionalRoute } from '../mixins/router';
|
import { routeProps, functionalRoute } from '../mixins/router';
|
||||||
import Icon from '../icon';
|
import Icon from '../icon';
|
||||||
|
|
||||||
|
// Types
|
||||||
|
import { SharedCellProps } from './shared';
|
||||||
|
import { FunctionalComponent } from '../utils/use/sfc';
|
||||||
|
import { Mods } from '../utils/use/bem';
|
||||||
|
|
||||||
const [sfc, bem] = use('cell');
|
const [sfc, bem] = use('cell');
|
||||||
|
|
||||||
function Cell(h, props, slots, ctx) {
|
const Cell: FunctionalComponent<CellProps> = function(h, props, slots, ctx) {
|
||||||
const { icon, size, title, label, value, isLink, arrowDirection } = props;
|
const { icon, size, title, label, value, isLink, arrowDirection } = props;
|
||||||
|
|
||||||
const showTitle = slots.title || isDef(title);
|
const showTitle = slots.title || isDef(title);
|
||||||
@ -34,8 +39,9 @@ function Cell(h, props, slots, ctx) {
|
|||||||
? slots.icon()
|
? slots.icon()
|
||||||
: icon && <Icon class={bem('left-icon')} name={icon} />;
|
: icon && <Icon class={bem('left-icon')} name={icon} />;
|
||||||
|
|
||||||
const RightIcon = slots['right-icon']
|
const rightIconSlot = slots['right-icon'];
|
||||||
? slots['right-icon']()
|
const RightIcon = rightIconSlot
|
||||||
|
? rightIconSlot()
|
||||||
: isLink && (
|
: isLink && (
|
||||||
<Icon
|
<Icon
|
||||||
class={bem('right-icon')}
|
class={bem('right-icon')}
|
||||||
@ -43,20 +49,25 @@ function Cell(h, props, slots, ctx) {
|
|||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
|
|
||||||
const onClick = event => {
|
const onClick = (event: Event) => {
|
||||||
emit(ctx, 'click', event);
|
emit(ctx, 'click', event);
|
||||||
functionalRoute(ctx);
|
functionalRoute(ctx);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const classes: Mods = {
|
||||||
|
center: props.center,
|
||||||
|
required: props.required,
|
||||||
|
borderless: !props.border,
|
||||||
|
clickable: isLink || props.clickable
|
||||||
|
};
|
||||||
|
|
||||||
|
if (size) {
|
||||||
|
classes[size] = size;
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
class={bem({
|
class={bem(classes)}
|
||||||
center: props.center,
|
|
||||||
required: props.required,
|
|
||||||
borderless: !props.border,
|
|
||||||
clickable: isLink || props.clickable,
|
|
||||||
[size]: size
|
|
||||||
})}
|
|
||||||
onClick={onClick}
|
onClick={onClick}
|
||||||
{...inherit(ctx)}
|
{...inherit(ctx)}
|
||||||
>
|
>
|
||||||
@ -69,6 +80,12 @@ function Cell(h, props, slots, ctx) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type CellProps = SharedCellProps & {
|
||||||
|
size?: string;
|
||||||
|
clickable?: boolean;
|
||||||
|
arrowDirection?: string;
|
||||||
|
}
|
||||||
|
|
||||||
Cell.props = {
|
Cell.props = {
|
||||||
...cellProps,
|
...cellProps,
|
||||||
...routeProps,
|
...routeProps,
|
||||||
@ -77,4 +94,4 @@ Cell.props = {
|
|||||||
arrowDirection: String
|
arrowDirection: String
|
||||||
};
|
};
|
||||||
|
|
||||||
export default sfc(Cell);
|
export default sfc<CellProps>(Cell);
|
@ -1,3 +1,17 @@
|
|||||||
|
export type SharedCellProps = {
|
||||||
|
icon?: string;
|
||||||
|
border?: boolean;
|
||||||
|
center?: boolean;
|
||||||
|
isLink?: boolean;
|
||||||
|
required?: boolean;
|
||||||
|
titleClass?: string;
|
||||||
|
valueClass?: string;
|
||||||
|
labelClass?: string;
|
||||||
|
title?: string | number;
|
||||||
|
value?: string | number;
|
||||||
|
label?: string | number;
|
||||||
|
}
|
||||||
|
|
||||||
export const cellProps = {
|
export const cellProps = {
|
||||||
icon: String,
|
icon: String,
|
||||||
center: Boolean,
|
center: Boolean,
|
||||||
|
@ -7,8 +7,8 @@
|
|||||||
* b(['disabled', 'primary']) // 'button button--disabled button--primary'
|
* b(['disabled', 'primary']) // 'button button--disabled button--primary'
|
||||||
*/
|
*/
|
||||||
|
|
||||||
type Mod = string | { [key: string]: any };
|
export type Mod = string | { [key: string]: any };
|
||||||
type Mods = Mod | Mod[];
|
export type Mods = Mod | Mod[];
|
||||||
|
|
||||||
const ELEMENT = '__';
|
const ELEMENT = '__';
|
||||||
const MODS = '--';
|
const MODS = '--';
|
||||||
|
@ -47,7 +47,11 @@ export type FunctionalComponent<
|
|||||||
inject?: InjectOptions;
|
inject?: InjectOptions;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type VantTsxComponent<T> = (props: T) => VNode;
|
export type TsxBaseProps = {
|
||||||
|
class?: any;
|
||||||
|
style?: any;
|
||||||
|
}
|
||||||
|
export type TsxComponent<T> = (props: T & TsxBaseProps) => VNode;
|
||||||
|
|
||||||
const arrayProp = {
|
const arrayProp = {
|
||||||
type: Array,
|
type: Array,
|
||||||
@ -104,7 +108,7 @@ function transformFunctionalComponent(
|
|||||||
|
|
||||||
export default (name: string) => <T = DefaultProps>(
|
export default (name: string) => <T = DefaultProps>(
|
||||||
sfc: VantComponentOptions | FunctionalComponent
|
sfc: VantComponentOptions | FunctionalComponent
|
||||||
): VantTsxComponent<T> => {
|
): TsxComponent<T> => {
|
||||||
if (typeof sfc === 'function') {
|
if (typeof sfc === 'function') {
|
||||||
sfc = transformFunctionalComponent(sfc);
|
sfc = transformFunctionalComponent(sfc);
|
||||||
}
|
}
|
||||||
@ -121,5 +125,5 @@ export default (name: string) => <T = DefaultProps>(
|
|||||||
sfc.name = name;
|
sfc.name = name;
|
||||||
sfc.install = install;
|
sfc.install = install;
|
||||||
|
|
||||||
return sfc as VantTsxComponent<T>;
|
return sfc as TsxComponent<T>;
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user