mirror of
https://github.com/zhigang1992/react.git
synced 2026-02-01 17:18:41 +08:00
* feat: optimize fonts rendering on windows (#385) * feat(styles): set Inter to highest font * docs(fonts): add guide for fonts rendering on windows * test: udpate snapshots * chore: release v2.1.0-canary.0 * feat(table): add update row action to Table (#378) * feat: add update to Table's actions. add test and doc fix(table): fix comments * feat(table): improve type for table actions chore: update docs chore: remove unused types chore(table): improve docs Co-authored-by: William Castandet <williamcastandet@williams-air.home> Co-authored-by: unix <unix.bio@gmail.com> * refactor(use-theme): move use-theme to the top directory (#397) * refactor(use-theme): move use-theme to the top directory * chore(jest): ignore use-theme of forwarding * chore: release v2.1.0-canary.1 * feat(select): add clearable option to select multiple with test and english doc (#396) * docs: add clearable option to select multiple with test and english doc * fix: fix types for onClear * fix: fix import path for use-theme add more test for coverage * docs(select): add chinese document Co-authored-by: unix <unix.bio@gmail.com> * chore: release v2.1.0-canary.2 * fix(tabs): scrollable (#404) docs(tabs): scroll behavior * feat(textarea): resize prop (#416) * feat: add resize prop to textarea * docs: add resize prop for textarea * docs(textarea): improve docs and attributes for cn * test(textarea): update snapshots Co-authored-by: unix <unix.bio@gmail.com> * fix(types): replace path aliases in type files (#432) * fix(types): replace path aliases in type files * chore(lint): upgrade eslint and optimize code style * chore: fix type error for context handler * test: update snapshots * fix: use ttsc to identify aliases in type paths * feat(hooks): add a tool hooks for react context (#439) * feat(hooks): add a tool hooks for react context * chore: move use-context-state to internal tools style: fix lint warning * chore: simplify the structure of the catalog * refactor(themes): refactor theme module to keep multiple themes (#440) * refactor(themes): refactor theme module to keep multiple themes * chore: migrate APIs to be compatible with new theme system * test: update snapshots * chore: migrate the path of the theme module * feat(themes): append static methods of themes * chore: hide custom theme when no custom content in the context * chore: manually add flush to preload styles in html * docs(themes): update to fit the new theme system * chore: release v2.1.0-canary.3 (#450) * docs: add link to GH discussions * chore: upgrade deps * chore: update code style for prettier * chore: release v2.1.0-canary.3 * chore(deps): upgrade babel * chore: replace enzyme adapter with community repo to fit react.17 * test: updatee snapshots for auto typesetting * test(config): ignore unexported parts of the tools Co-authored-by: William <wcastand@gmail.com> Co-authored-by: William Castandet <williamcastandet@williams-air.home> Co-authored-by: Vaibhav Acharya <vaibhavacharya111@gmail.com> Co-authored-by: Paul van Dyk <39598117+PaulPCIO@users.noreply.github.com>
195 lines
4.6 KiB
TypeScript
195 lines
4.6 KiB
TypeScript
import React, { useMemo } from 'react'
|
|
import useTheme from '../use-theme'
|
|
import { Justify, Direction, AlignItems, AlignContent } from './grid-types'
|
|
|
|
type BreakpointsValue = number | boolean
|
|
interface Props {
|
|
xs?: BreakpointsValue
|
|
sm?: BreakpointsValue
|
|
md?: BreakpointsValue
|
|
lg?: BreakpointsValue
|
|
xl?: BreakpointsValue
|
|
justify?: Justify
|
|
direction?: Direction
|
|
alignItems?: AlignItems
|
|
alignContent?: AlignContent
|
|
className?: string
|
|
}
|
|
|
|
const defaultProps = {
|
|
xs: false as BreakpointsValue,
|
|
sm: false as BreakpointsValue,
|
|
md: false as BreakpointsValue,
|
|
lg: false as BreakpointsValue,
|
|
xl: false as BreakpointsValue,
|
|
className: '',
|
|
}
|
|
|
|
type NativeAttrs = Omit<React.HTMLAttributes<any>, keyof Props>
|
|
export type GridBasicItemProps = Props & typeof defaultProps & NativeAttrs
|
|
|
|
type ItemLayoutValue = {
|
|
grow: number
|
|
width: string
|
|
basis: string
|
|
display: string
|
|
}
|
|
const getItemLayout = (val: BreakpointsValue): ItemLayoutValue => {
|
|
const display = val === 0 ? 'display: none;' : 'display: unset;'
|
|
if (typeof val === 'number') {
|
|
const width = (100 / 24) * val
|
|
const ratio = width > 100 ? '100%' : width < 0 ? '0' : `${width}%`
|
|
return {
|
|
grow: 0,
|
|
display,
|
|
width: ratio,
|
|
basis: ratio,
|
|
}
|
|
}
|
|
return {
|
|
grow: 1,
|
|
display,
|
|
width: '100%',
|
|
basis: '0',
|
|
}
|
|
}
|
|
|
|
const GridBasicItem: React.FC<React.PropsWithChildren<GridBasicItemProps>> = ({
|
|
xs,
|
|
sm,
|
|
md,
|
|
lg,
|
|
xl,
|
|
justify,
|
|
direction,
|
|
alignItems,
|
|
alignContent,
|
|
children,
|
|
className,
|
|
...props
|
|
}) => {
|
|
const theme = useTheme()
|
|
const classes = useMemo(() => {
|
|
const aligns: { [key: string]: any } = {
|
|
justify,
|
|
direction,
|
|
alignItems,
|
|
alignContent,
|
|
xs,
|
|
sm,
|
|
md,
|
|
lg,
|
|
xl,
|
|
}
|
|
const classString = Object.keys(aligns).reduce((pre, name) => {
|
|
if (aligns[name] !== undefined && aligns[name] !== false) return `${pre} ${name}`
|
|
return pre
|
|
}, '')
|
|
return classString.trim()
|
|
}, [justify, direction, alignItems, alignContent, xs, sm, md, lg, xl])
|
|
|
|
const layout = useMemo<
|
|
{
|
|
[key in ['xs', 'sm', 'md', 'lg', 'xl'][number]]: ItemLayoutValue
|
|
}
|
|
>(
|
|
() => ({
|
|
xs: getItemLayout(xs),
|
|
sm: getItemLayout(sm),
|
|
md: getItemLayout(md),
|
|
lg: getItemLayout(lg),
|
|
xl: getItemLayout(xl),
|
|
}),
|
|
[xs, sm, md, lg, xl],
|
|
)
|
|
|
|
return (
|
|
<div className={`item ${classes} ${className}`} {...props}>
|
|
{children}
|
|
<style jsx>{`
|
|
.item {
|
|
}
|
|
|
|
.justify {
|
|
justify-content: ${justify};
|
|
}
|
|
|
|
.direction {
|
|
flex-direction: ${direction};
|
|
}
|
|
|
|
.alignContent {
|
|
align-content: ${alignContent};
|
|
}
|
|
|
|
.alignItems {
|
|
align-items: ${alignItems};
|
|
}
|
|
|
|
.xs {
|
|
flex-grow: ${layout.xs.grow};
|
|
max-width: ${layout.xs.width};
|
|
flex-basis: ${layout.xs.basis};
|
|
${layout.xs.display}
|
|
}
|
|
|
|
@media only screen and (max-width: ${theme.breakpoints.xs.max}) {
|
|
.xs {
|
|
flex-grow: ${layout.xs.grow};
|
|
max-width: ${layout.xs.width};
|
|
flex-basis: ${layout.xs.basis};
|
|
${layout.xs.display}
|
|
}
|
|
}
|
|
|
|
@media only screen and (min-width: ${theme.breakpoints.sm.min}) {
|
|
.sm {
|
|
flex-grow: ${layout.sm.grow};
|
|
max-width: ${layout.sm.width};
|
|
flex-basis: ${layout.sm.basis};
|
|
${layout.sm.display}
|
|
}
|
|
}
|
|
|
|
@media only screen and (min-width: ${theme.breakpoints.md.min}) {
|
|
.md {
|
|
flex-grow: ${layout.md.grow};
|
|
max-width: ${layout.md.width};
|
|
flex-basis: ${layout.md.basis};
|
|
${layout.md.display}
|
|
}
|
|
}
|
|
|
|
@media only screen and (min-width: ${theme.breakpoints.lg.min}) {
|
|
.lg {
|
|
flex-grow: ${layout.lg.grow};
|
|
max-width: ${layout.lg.width};
|
|
flex-basis: ${layout.lg.basis};
|
|
${layout.lg.display}
|
|
}
|
|
}
|
|
|
|
@media only screen and (min-width: ${theme.breakpoints.xl.min}) {
|
|
.xl {
|
|
flex-grow: ${layout.xl.grow};
|
|
max-width: ${layout.xl.width};
|
|
flex-basis: ${layout.xl.basis};
|
|
${layout.xl.display}
|
|
}
|
|
}
|
|
`}</style>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
type MemoBasicItemComponent<P = {}> = React.NamedExoticComponent<P>
|
|
export type GridBasicItemComponentProps = Partial<typeof defaultProps> &
|
|
Omit<Props, keyof typeof defaultProps> &
|
|
NativeAttrs
|
|
|
|
GridBasicItem.defaultProps = defaultProps
|
|
|
|
export default React.memo(
|
|
GridBasicItem,
|
|
) as MemoBasicItemComponent<GridBasicItemComponentProps>
|