types(Grid): use tsx (#8122)

This commit is contained in:
neverland 2021-02-10 20:21:08 +08:00 committed by GitHub
parent 692631968d
commit 5b352d0339
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 8 deletions

View File

@ -1,9 +1,9 @@
import { computed } from 'vue'; import { computed, CSSProperties } from 'vue';
// Utils // Utils
import { createNamespace, addUnit } from '../utils'; import { createNamespace, addUnit } from '../utils';
import { BORDER } from '../utils/constant'; import { BORDER } from '../utils/constant';
import { GRID_KEY } from '../grid'; import { GRID_KEY, GridProvide } from '../grid';
// Composition // Composition
import { useParent } from '@vant/use'; import { useParent } from '@vant/use';
@ -26,13 +26,20 @@ export default createComponent({
}, },
setup(props, { slots }) { setup(props, { slots }) {
const { parent, index } = useParent(GRID_KEY); const { parent, index } = useParent<GridProvide>(GRID_KEY);
const route = useRoute(); const route = useRoute();
if (!parent) {
if (process.env.NODE_ENV !== 'production') {
console.error('[Vant] GridItem must be a child component of Grid.');
}
return;
}
const rootStyle = computed(() => { const rootStyle = computed(() => {
const { square, gutter, columnNum } = parent.props; const { square, gutter, columnNum } = parent.props;
const percent = `${100 / columnNum}%`; const percent = `${100 / +columnNum}%`;
const style = { const style: CSSProperties = {
flexBasis: percent, flexBasis: percent,
}; };
@ -128,10 +135,10 @@ export default createComponent({
return ( return (
<div class={[bem({ square })]} style={rootStyle.value}> <div class={[bem({ square })]} style={rootStyle.value}>
<div <div
role={clickable ? 'button' : null} role={clickable ? 'button' : undefined}
class={classes} class={classes}
style={contentStyle.value} style={contentStyle.value}
tabindex={clickable ? 0 : null} tabindex={clickable ? 0 : undefined}
onClick={route} onClick={route}
> >
{renderContent()} {renderContent()}

View File

@ -1,3 +1,4 @@
import { PropType } from 'vue';
import { createNamespace, addUnit } from '../utils'; import { createNamespace, addUnit } from '../utils';
import { BORDER_TOP } from '../utils/constant'; import { BORDER_TOP } from '../utils/constant';
import { useChildren } from '@vant/use'; import { useChildren } from '@vant/use';
@ -6,12 +7,27 @@ const [createComponent, bem] = createNamespace('grid');
export const GRID_KEY = 'vanGrid'; export const GRID_KEY = 'vanGrid';
export type GridDirection = 'horizontal' | 'vertical';
export type GridProvide = {
props: {
center: boolean;
border: boolean;
square?: boolean;
gutter?: number | string;
iconSize?: number | string;
columnNum: number | string;
direction?: GridDirection;
clickable?: boolean;
};
};
export default createComponent({ export default createComponent({
props: { props: {
square: Boolean, square: Boolean,
gutter: [Number, String], gutter: [Number, String],
iconSize: [Number, String], iconSize: [Number, String],
direction: String, direction: String as PropType<GridDirection>,
clickable: Boolean, clickable: Boolean,
columnNum: { columnNum: {
type: [Number, String], type: [Number, String],