mirror of
https://github.com/zhigang1992/react.git
synced 2026-03-26 22:42:51 +08:00
44 lines
1.0 KiB
TypeScript
44 lines
1.0 KiB
TypeScript
import React from 'react'
|
|
import withDefaults from '../utils/with-defaults'
|
|
|
|
interface Props {
|
|
span?: number
|
|
offset?: number
|
|
component?: keyof JSX.IntrinsicElements
|
|
className?: string
|
|
}
|
|
|
|
const defaultProps = {
|
|
span: 24,
|
|
offset: 0,
|
|
component: 'div' as keyof JSX.IntrinsicElements,
|
|
className: '',
|
|
}
|
|
|
|
type NativeAttrs = Omit<React.HTMLAttributes<any>, keyof Props>
|
|
export type ColProps = Props & typeof defaultProps & NativeAttrs
|
|
|
|
const Col: React.FC<React.PropsWithChildren<ColProps>> = React.memo(({
|
|
component, children, span, offset, className, ...props
|
|
}) => {
|
|
const Component = component
|
|
|
|
return (
|
|
<Component className={`col ${className}`} {...props}>
|
|
{children}
|
|
<style jsx>{`
|
|
.col {
|
|
float: left;
|
|
box-sizing: border-box;
|
|
padding-left: calc(var(--row-gap) / 2);
|
|
padding-right: calc(var(--row-gap) / 2);
|
|
width: ${100 / 24 * span}%;
|
|
margin-left: ${100 / 24 * offset}%;
|
|
}
|
|
`}</style>
|
|
</Component>
|
|
)
|
|
})
|
|
|
|
export default withDefaults(Col, defaultProps)
|