-
Notifications
You must be signed in to change notification settings - Fork 31
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]: add gio-grid component #338
Merged
Merged
Changes from all commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
4ba98b9
feat: add grid
iahu efff50b
feat: add test cases
iahu 4c57128
fix: make ts happy again
iahu 846acda
chore: revert yarn.lock file
iahu 3a49ce2
chore: remove package-lock.json
iahu 119d484
fix: typo
iahu d4f9f65
Merge branch 'master' into grid
iahu fe15842
feat: time-picker (#292)
gh2049 d6e6628
feat(searchbar): add placeholder prop (#290)
75286b2
refactor(table): define when dataSource update, pagination behaviour …
960ed51
feat(date-picker): add rengePicker feature (#296)
berber1016 116bf06
docs: @gio-design/components 20.10.0 change log (#306)
661d5c2
docs: @gio-design/components 20.10.1 change log (#309)
aff4c0b
docs: @gio-design/components 20.10.2 change log
983e628
docs: update version number
228c993
fix: time-picker style (#307)
gh2049 eb82533
feat(date-picker): add dateRangePicker disabledDate methods (#314)
berber1016 0d9a860
fix: allow use value control selections; (#317)
6ea3163
style(form): 根据stylelint的提示,修改了form组件中index.less中部分属性的顺序 (#318)
WORLDI c8c5108
fix select text overflow and add icons (#319)
a9619d2
docs: @gio-design/components 20.10.3 change log (#320)
f4b295c
docs: @gio-designs/components release-20.10.4 change log (#322)
de49f90
feat: add grid
iahu ef52dbb
feat: add test cases
iahu 61db1ce
fix: make ts happy again
iahu 318ce9e
chore: revert yarn.lock file
iahu 9554339
chore: remove package-lock.json
iahu 5d68782
fix: typo
iahu 84dd64b
fix(upload): upload组件增加一个可选参数isBorder,控制图片上传成功后边框是否显示 (#331)
WORLDI a0a84cc
chore: create release pull request template
jack0pan 6cb9be5
chore: delete old pull request template
jack0pan 5354b3a
chore: create feature or fix pull request template
jack0pan 4dc7070
fix(dropdown): 修复Dropdown组件中placement方向的问题,由12个方向改为只有上下6个方向可选,默认方向为下 …
WORLDI d630222
style(select): 定义了select组件中input选择框内的文字尺寸 (#337)
WORLDI 679071a
test(alert): unit test of alert component is added (#339)
WORLDI 8373b90
fix(components): change folder name from camel case to kebab case (#329)
phyzess a70891e
Merge branch 'grid' of github.com:iahu/gio-design into grid
iahu 227b893
fix: index time-picker
iahu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
packages/components/src/components/grid/__test__/grid.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import React from 'react'; | ||
import { render, unmountComponentAtNode } from 'react-dom'; | ||
import { act } from 'react-dom/test-utils'; | ||
import Grid from '..'; | ||
|
||
let container: HTMLDivElement; | ||
beforeEach(() => { | ||
// 创建一个 DOM 元素作为渲染目标 | ||
container = document.createElement('div'); | ||
document.body.appendChild(container); | ||
}); | ||
|
||
afterEach(() => { | ||
// 退出时进行清理 | ||
unmountComponentAtNode(container); | ||
container.remove(); | ||
container = (undefined as unknown) as HTMLDivElement; | ||
}); | ||
|
||
describe('<Grid />', () => { | ||
it('should render correctly', () => { | ||
act(() => { | ||
render(<Grid />, container); | ||
}); | ||
expect(container.querySelector('.gio-grid')).not.toBeNull(); | ||
}); | ||
|
||
it('should render custom classNames', () => { | ||
act(() => { | ||
render(<Grid className="test1 test2" />, container); | ||
}); | ||
expect(container.querySelector('.gio-grid.test1.test2')).not.toBeNull(); | ||
}); | ||
|
||
it('should replace the root node with the passed `component` options', () => { | ||
act(() => { | ||
render(<Grid component="span" />, container); | ||
}); | ||
expect(container.querySelector('.gio-grid')).not.toBeNull(); | ||
}); | ||
|
||
it('should render nested', () => { | ||
act(() => { | ||
render( | ||
<Grid> | ||
<Grid /> | ||
<Grid> | ||
<Grid /> | ||
</Grid> | ||
</Grid>, | ||
container | ||
); | ||
}); | ||
expect(container.querySelectorAll('.gio-grid').length).toEqual(4); | ||
expect(container.querySelector('.gio-grid > .gio-grid > .gio-grid')).not.toBeNull(); | ||
}); | ||
|
||
it('should make css custom properties', () => { | ||
act(() => { | ||
render( | ||
<Grid | ||
span={12} | ||
gap={1} | ||
direction="row" | ||
wrap="wrap" | ||
justify="center" | ||
alignItems="center" | ||
alignContent="center" | ||
/>, | ||
container | ||
); | ||
}); | ||
|
||
const grid = container.querySelector('.gio-grid') as HTMLDivElement; | ||
expect(grid.style.getPropertyValue('--gio-grid-span')).toBe('12'); | ||
expect(grid.style.getPropertyValue('--gio-grid-gap')).toBe('1'); | ||
expect(grid.style.getPropertyValue('--gio-grid-wrap')).toBe('wrap'); | ||
expect(grid.style.getPropertyValue('--gio-grid-justify')).toBe('center'); | ||
expect(grid.style.getPropertyValue('--gio-grid-align-items')).toBe('center'); | ||
expect(grid.style.getPropertyValue('--gio-grid-align-content')).toBe('center'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import isNil from 'lodash/isNil'; | ||
import isNaN from 'lodash/isNaN'; | ||
import keys from 'lodash/keys'; | ||
import kebabCase from 'lodash/kebabCase'; | ||
|
||
export default {}; | ||
|
||
export const getAttrName = (cls: string, prefix?: string): string => { | ||
// if (!prefix) { | ||
// return cls; | ||
// } | ||
return `${prefix}-${kebabCase(cls)}`; | ||
}; | ||
|
||
interface ObjType { | ||
[key: string]: undefined | number | string | boolean; | ||
} | ||
|
||
type Ret<T> = { | ||
[P in keyof T]: string | boolean; | ||
}; | ||
|
||
const falsely = <T>(o: T) => isNil(o) || isNaN(o); | ||
|
||
export const dataMap = <T extends ObjType = ObjType>(obj: T, prefixCls: string): Ret<T> => { | ||
const cls = {} as Ret<T>; | ||
keys(obj).forEach((key) => { | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
cls[getAttrName(key, prefixCls) as keyof Ret<T>] = falsely(obj[key]) ? false : obj[key]!.toString(); | ||
}); | ||
|
||
return cls; | ||
}; | ||
|
||
export const clip = (min: number, max: number, value: number): number => { | ||
return Math.max(min, Math.min(max, value)); | ||
}; | ||
|
||
export const isNumber = <T = unknown>(n: T): boolean => typeof n === 'number'; | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import React, { useContext } from 'react'; | ||
import classNames from 'classnames'; | ||
|
||
import { ConfigContext } from '../config-provider/context'; | ||
import { GridProps } from './interface'; | ||
import { clip, dataMap, isNumber } from './help'; | ||
|
||
const Grid: React.FC<GridProps> = (props: React.PropsWithChildren<GridProps>) => { | ||
const { | ||
component: Component = 'div', | ||
prefixCls: customizePrefixCls, | ||
className, | ||
children, | ||
direction, | ||
justify, | ||
alignItems, | ||
alignContent, | ||
wrap, | ||
span, | ||
gap, | ||
container = false, | ||
collapse = false, | ||
style, | ||
} = props; | ||
const { getPrefixCls } = useContext(ConfigContext); | ||
const prefixCls = getPrefixCls('grid', customizePrefixCls); | ||
const cssProps = dataMap( | ||
{ | ||
direction, | ||
justify, | ||
alignItems, | ||
alignContent, | ||
span: isNumber(span) ? clip(0, 12, span as number) : span, | ||
gap, | ||
wrap, | ||
}, | ||
'--gio-grid' | ||
); | ||
|
||
return ( | ||
<Component | ||
className={classNames(prefixCls, className)} | ||
data-collapse={collapse} | ||
data-gap={!!gap} | ||
data-container={!!container} | ||
style={{ ...cssProps, ...style }} | ||
> | ||
{children} | ||
</Component> | ||
); | ||
}; | ||
|
||
export default Grid; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import React, { ElementType } from 'react'; | ||
|
||
interface ComponentProps { | ||
className?: string; | ||
style?: React.CSSProperties; | ||
} | ||
|
||
export interface GridProps<C extends ElementType = ElementType> extends ComponentProps { | ||
prefixCls?: string; | ||
component?: C; | ||
|
||
// flexbox types | ||
direction?: 'row' | 'row-reverse' | 'column' | 'column-reverse'; | ||
justify?: 'flex-start' | 'center' | 'flex-end' | 'space-between' | 'space-around' | 'space-evenly'; | ||
alignItems?: 'flex-start' | 'center' | 'flex-end' | 'stretch' | 'baseline'; | ||
alignContent?: 'stretch' | 'center' | 'flex-start' | 'flex-end' | 'space-between' | 'space-around'; | ||
wrap?: 'nowrap' | 'wrap' | 'wrap-reverse'; | ||
gap?: number | string; | ||
|
||
// grid property | ||
span?: number; | ||
container?: boolean; | ||
|
||
collapse?: boolean; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
@import '../../../stylesheet/index.less'; | ||
|
||
@component-cls: ~'@{component-prefix}-grid'; | ||
|
||
.@{component-cls} { | ||
display: flex; | ||
flex-wrap: var(--gio-grid-wrap, wrap); | ||
box-sizing: border-box; | ||
flex-direction: var(--gio-grid-direction, row); | ||
justify-content: var(--gio-grid-justify, flex-start); | ||
align-items: var(--gio-grid-align-items, stretch); | ||
align-content: var(--gio-grid-align-content, stretch); | ||
|
||
--gio-grid-gap-size: calc(var(--gio-grid-gap, 0) * 8px); | ||
|
||
&[data-container='true'] { | ||
position: relative; | ||
width: 100%; | ||
} | ||
|
||
.normal-gap { | ||
width: calc(100% * var(--gio-grid-span) / 12 - var(--gio-grid-gap-size)); | ||
|
||
&[data-gap='true'] { | ||
> * { | ||
--gio-grid-item-marign: calc(var(--gio-grid-gap-size) / 2); | ||
margin: var(--gio-grid-item-marign); | ||
} | ||
} | ||
} | ||
|
||
.css-grid-gap { | ||
gap: var(--gio-grid-gap-size); | ||
width: calc((100% + var(--gio-grid-gap-size)) / 12 * var(--gio-grid-span) - var(--gio-grid-gap-size)); | ||
|
||
&[data-gap='true'] { | ||
--gio-grid-item-marign: unset; | ||
// > * { | ||
// margin: unset !important; | ||
// } | ||
} | ||
} | ||
|
||
&[data-collapse='false'] { | ||
.normal-gap(); | ||
|
||
> [data-gap] { | ||
.normal-gap(); | ||
} | ||
} | ||
|
||
&[data-collapse='true'] { | ||
.css-grid-gap(); | ||
|
||
> [data-gap] { | ||
.css-grid-gap(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import './index.less'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import React from 'react'; | ||
import { Grid } from '@gio-design/components'; | ||
|
||
import '@gio-design/components/es/components/grid/style/index.css'; | ||
import './style.less'; | ||
|
||
const Base = (): JSX.Element => { | ||
return ( | ||
<Grid className="demo"> | ||
<Grid className="box" span={12}> | ||
span=12 | ||
</Grid> | ||
<Grid className="box" span={3}> | ||
span=3 | ||
</Grid> | ||
<Grid className="box" span={3}> | ||
span=3 | ||
</Grid> | ||
<Grid className="box" span={3}> | ||
span=3 | ||
</Grid> | ||
<Grid className="box" span={3}> | ||
span=3 | ||
</Grid> | ||
</Grid> | ||
); | ||
}; | ||
|
||
export default Base; |
23 changes: 23 additions & 0 deletions
23
packages/website/src/components/basic/grid/demo/collapse.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import React from 'react'; | ||
import { Grid } from '@gio-design/components'; | ||
|
||
import '@gio-design/components/es/components/grid/style/index.css'; | ||
import './style.less'; | ||
|
||
const Base = (): JSX.Element => { | ||
return ( | ||
<Grid collapse container gap={1} className="demo"> | ||
<Grid span={12}>span=12</Grid> | ||
<Grid span={3} gap={1} collapse container> | ||
<Grid span={12}>span=12</Grid> | ||
<Grid span={6}>span=6</Grid> | ||
<Grid span={6}>span=6</Grid> | ||
</Grid> | ||
<Grid span={3}>span=3</Grid> | ||
<Grid span={3}>span=3</Grid> | ||
<Grid span={3}>span=3</Grid> | ||
</Grid> | ||
); | ||
}; | ||
|
||
export default Base; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import '@gio-design/components/es/components/grid/style/index.css'; | ||
|
||
import React, { useState } from 'react'; | ||
|
||
import { Grid, Form, Radio, RadioGroup } from '@gio-design/components'; | ||
|
||
const { Item } = Form; | ||
|
||
const options = { | ||
direction: ['row', 'row-reverse', 'column', 'column-reverse'], | ||
justify: ['flex-start', 'center', 'flex-end', 'space-between', 'space-around', 'space-evenly'], | ||
alignItems: ['flex-start', 'center', 'flex-end', 'stretch', 'baseline'], | ||
alignContent: ['stretch', 'center', 'flex-start', 'flex-end', 'space-between', 'space-around'], | ||
wrap: ['wrap', 'nowrap', 'wrap-reverse'], | ||
} as const; | ||
|
||
type Options = typeof options; | ||
type OptionKey = keyof Options; | ||
const optionKeys = Object.keys(options) as Array<OptionKey>; | ||
|
||
type InitState = { | ||
[P in OptionKey]: Options[P][number]; | ||
}; | ||
const initState = {} as InitState; | ||
|
||
optionKeys.forEach((key) => { | ||
const [value] = options[key]; | ||
(initState[key] as InitState[OptionKey]) = value; | ||
}); | ||
|
||
const Base = (): JSX.Element => { | ||
const [formData, setFormData] = useState(initState); | ||
const onValuesChange = (value: Partial<InitState>) => setFormData({ ...formData, ...value }); | ||
|
||
return ( | ||
<div className="flex-props"> | ||
<Grid container gap={1} {...(formData as unknown)} className="box demo"> | ||
<Grid span={12}>span=12</Grid> | ||
<Grid span={3}>span=3</Grid> | ||
<Grid span={3}>span=6</Grid> | ||
<Grid span={3} style={{ height: 140 }}> | ||
span=3 | ||
</Grid> | ||
</Grid> | ||
|
||
<div className="box"> | ||
<Form initialValues={formData} onValuesChange={onValuesChange}> | ||
{optionKeys.map((key) => ( | ||
<Item key={key} name={key} label={`${key}:`}> | ||
<RadioGroup> | ||
{(options[key] as readonly string[]).map((d) => ( | ||
<Radio key={d} value={d}> | ||
{d} | ||
</Radio> | ||
))} | ||
</RadioGroup> | ||
</Item> | ||
))} | ||
</Form> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Base; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
有个isFinite可以判断非Nan和infinity的数字