Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(layout): 新增 layout 组件 #452

Merged
merged 5 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions site/mobile/mobile.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ export default {
name: 'icon',
component: () => import('tdesign-mobile-react/icon/_example/index.tsx'),
},
{
title: 'Layout 布局',
name: 'layout',
component: () => import('tdesign-mobile-react/layout/_example/index.tsx'),
},
{
title: 'Tabs 选项卡',
name: 'tabs',
Expand Down
6 changes: 6 additions & 0 deletions site/web/site.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ export default {
path: '/mobile-react/components/icon',
component: () => import('tdesign-mobile-react/icon/icon.md'),
},
{
title: 'Layout 布局',
name: 'layout',
path: '/mobile-react/components/layout',
component: () => import('tdesign-mobile-react/layout/layout.md'),
},
{
title: 'Link 链接',
name: 'link',
Expand Down
6 changes: 6 additions & 0 deletions src/_util/convertUnit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import isNumber from 'lodash/isNumber';

export const convertUnit = (val: string | number | undefined) => {
if (val == null) return 0;
return isNumber(val) ? `${val}px` : val;
};
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export * from './button';
export * from './divider';
export * from './fab';
export * from './progress';
export * from './layout';
export * from './link';

/**
Expand Down
58 changes: 58 additions & 0 deletions src/layout/Col.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import React, { forwardRef, useContext } from 'react';
import classNames from 'classnames';

import { TdColProps } from './type';
import { colDefaultProps } from './defaultProps';

import { StyledProps } from '../common';

import { convertUnit } from '../_util/convertUnit';
import parseTNode from '../_util/parseTNode';
import useDefaultProps from '../hooks/useDefaultProps';
import { usePrefixClass } from '../hooks/useClass';

import { RowContext, RowProps } from './Row';

export interface ColProps extends TdColProps, StyledProps {
children?: React.ReactNode;
}

const calcColPadding = (gutter: RowProps['gutter']) => {
const styles: React.CSSProperties = {};

if (!gutter) {
return styles;
}
const gutterVal = convertUnit(Number(gutter) / 2);
styles.paddingRight = gutterVal;
styles.paddingLeft = gutterVal;
return styles;
};

const Col = forwardRef<HTMLDivElement, ColProps>((props, ref) => {
const { offset, span, className, children, style: propStyle } = useDefaultProps<ColProps>(props, colDefaultProps);

const colClass = usePrefixClass('col');

const { gutter } = useContext(RowContext);

const colClassNames = classNames(colClass, className, {
[`${colClass}--${span}`]: span !== undefined,
[`${colClass}--offset-${offset}`]: parseInt(offset as string, 10) >= 0,
});

const colStyle = {
...calcColPadding(gutter),
...propStyle,
};

return (
<div className={colClassNames} ref={ref} style={colStyle}>
{parseTNode(children)}
</div>
);
});

Col.displayName = 'Col';

export default Col;
46 changes: 46 additions & 0 deletions src/layout/Row.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import React, { createContext, forwardRef, useMemo } from 'react';
import classNames from 'classnames';

import type { TdRowProps } from './type';

import { convertUnit } from '../_util/convertUnit';
import parseTNode from '../_util/parseTNode';
import useDefaultProps from '../hooks/useDefaultProps';
import { usePrefixClass } from '../hooks/useClass';
import { StyledProps } from '../common';

import { rowDefaultProps } from './defaultProps';

export interface RowProps extends TdRowProps, StyledProps {
children?: React.ReactNode;
}

export const RowContext = createContext<{ gutter: RowProps['gutter'] }>({ gutter: undefined });

const Row = forwardRef<HTMLDivElement, RowProps>((props, ref) => {
const { children, className = '', gutter, style } = useDefaultProps<RowProps>(props, rowDefaultProps);

const colClass = usePrefixClass('row');

const computedStyle = useMemo(() => {
const mergedStyle = { ...style };
if (!gutter) {
return mergedStyle;
}

const gutterVal = convertUnit(-gutter / 2);

mergedStyle.marginRight = gutterVal;
mergedStyle.marginLeft = gutterVal;

return mergedStyle;
}, [gutter, style]);

return (
<div className={classNames([colClass], className)} ref={ref} style={computedStyle}>
<RowContext.Provider value={{ gutter }}>{parseTNode(children)}</RowContext.Provider>
</div>
);
});

export default Row;
Loading
Loading