diff --git a/components/auto-complete/__tests__/__snapshots__/index.test.tsx.snap b/components/auto-complete/__tests__/__snapshots__/index.test.tsx.snap index 1aaf2097e..eaf1c54ba 100644 --- a/components/auto-complete/__tests__/__snapshots__/index.test.tsx.snap +++ b/components/auto-complete/__tests__/__snapshots__/index.test.tsx.snap @@ -4,6 +4,7 @@ exports[`AutoComplete should render correctly 1`] = ` { it('should render correctly', () => { @@ -32,11 +33,13 @@ describe('AutoComplete', () => { expect((input as HTMLInputElement).value).toEqual('value2') }) - it('should render clear icon', () => { + it('should render clear icon', async () => { const wrapper = mount() expect(wrapper.find('svg').length).toBe(0) - wrapper.setProps({ clearable: true }) + await act(async () => { + wrapper.setProps({ clearable: true }) + }) expect(wrapper.find('svg').length).toBe(1) wrapper.find('svg').simulate('click', nativeEvent) @@ -44,11 +47,13 @@ describe('AutoComplete', () => { expect((input as HTMLInputElement).value).toEqual('') }) - it('should reponse width change', () => { + it('should reponse width change', async () => { const wrapper = mount() expect(wrapper.prop('width')).toEqual('100px') + await act(async () => { + wrapper.setProps({ width: '200px' }) + }) - wrapper.setProps({ width: '200px' }) expect(wrapper.prop('width')).toEqual('200px') }) }) diff --git a/components/auto-complete/__tests__/search.test.tsx b/components/auto-complete/__tests__/search.test.tsx index 6b55b910c..13b325f99 100644 --- a/components/auto-complete/__tests__/search.test.tsx +++ b/components/auto-complete/__tests__/search.test.tsx @@ -1,7 +1,8 @@ import React from 'react' import { mount, render } from 'enzyme' import { AutoComplete } from 'components' -import { nativeEvent } from 'tests/utils' +import { nativeEvent, updateWrapper } from 'tests/utils' +import { act } from 'react-dom/test-utils' const mockOptions = [{ label: 'London', value: 'london' }] describe('AutoComplete Search', () => { @@ -33,9 +34,11 @@ describe('AutoComplete Search', () => { expect(value).not.toEqual('london') }) - it('should render searching component', () => { + it('should render searching component', async () => { let wrapper = mount() - wrapper.setProps({ searching: true }) + await act(async () => { + wrapper.setProps({ searching: true }) + }) wrapper.find('input').at(0).simulate('focus') let dropdown = wrapper.find('.auto-complete-dropdown') expect(dropdown.text()).not.toContain('london') @@ -136,4 +139,15 @@ describe('AutoComplete Search', () => { const wrapper = mount() expect(() => wrapper.unmount()).not.toThrow() }) + + it('value should be reset when freeSolo disabled', async () => { + const wrapper = mount() + const input = wrapper.find('input').at(0) + input.simulate('focus') + input.simulate('change', { target: { value: 'test' } }) + expect((input.getDOMNode() as HTMLInputElement).value).toEqual('test') + input.simulate('blur') + await updateWrapper(wrapper, 200) + expect((input.getDOMNode() as HTMLInputElement).value).toEqual('value') + }) }) diff --git a/components/auto-complete/__tests__/use-input.test.tsx b/components/auto-complete/__tests__/use-input.test.tsx new file mode 100644 index 000000000..21ebd95c6 --- /dev/null +++ b/components/auto-complete/__tests__/use-input.test.tsx @@ -0,0 +1,60 @@ +import React, { useEffect } from 'react' +import { mount } from 'enzyme' +import { AutoComplete, useInput } from 'components' + +describe('UseInput', () => { + it('should follow change with use-input', () => { + let log = '' + const logSpy = jest.spyOn(console, 'log').mockImplementation(msg => (log = msg)) + const MockInput: React.FC<{ value?: string }> = ({ value }) => { + const { state, setState, bindings } = useInput('') + useEffect(() => { + if (value) setState(value) + }, [value]) + useEffect(() => { + if (state) console.log(state) + }, [state]) + return + } + + const wrapper = mount() + wrapper.setProps({ value: 'test' }) + const input = wrapper.find('input').at(0).getDOMNode() as HTMLInputElement + + expect(input.value).toEqual('test') + expect(log).toContain('test') + + log = '' + wrapper + .find('input') + .at(0) + .simulate('change', { target: { value: 'test-change' } }) + expect(log).toContain('test-change') + logSpy.mockRestore() + }) + + it('should follow change with use-input', () => { + const MockInput: React.FC<{ value?: string; resetValue?: boolean }> = ({ + value, + resetValue, + }) => { + const { reset, setState, bindings } = useInput('') + useEffect(() => { + if (value) setState(value) + }, [value]) + useEffect(() => { + if (resetValue) reset() + }, [resetValue]) + return + } + + const wrapper = mount() + wrapper.setProps({ value: 'test' }) + let input = wrapper.find('input').at(0).getDOMNode() as HTMLInputElement + expect(input.value).toEqual('test') + + wrapper.setProps({ resetValue: true }) + input = wrapper.find('input').at(0).getDOMNode() as HTMLInputElement + expect(input.value).toEqual('') + }) +}) diff --git a/components/auto-complete/auto-complete-item.tsx b/components/auto-complete/auto-complete-item.tsx index 63b102b61..6d6fb08cc 100644 --- a/components/auto-complete/auto-complete-item.tsx +++ b/components/auto-complete/auto-complete-item.tsx @@ -28,9 +28,10 @@ const AutoCompleteItem: React.FC> children, }) => { const theme = useTheme() - const { value, updateValue, size } = useAutoCompleteContext() + const { value, updateValue, size, updateVisible } = useAutoCompleteContext() const selectHandler = () => { updateValue && updateValue(identValue) + updateVisible && updateVisible(false) } const isActive = useMemo(() => value === identValue, [identValue, value]) diff --git a/components/auto-complete/auto-complete.tsx b/components/auto-complete/auto-complete.tsx index cb5b55179..0e51a45ea 100644 --- a/components/auto-complete/auto-complete.tsx +++ b/components/auto-complete/auto-complete.tsx @@ -8,6 +8,7 @@ import { AutoCompleteContext, AutoCompleteConfig } from './auto-complete-context import { NormalSizes, NormalTypes } from '../utils/prop-types' import Loading from '../loading' import { pickChild } from '../utils/collections' +import useCurrentState from '../utils/use-current-state' export type AutoCompleteOption = { label: string @@ -31,6 +32,7 @@ interface Props { dropdownClassName?: string dropdownStyle?: object disableMatchWidth?: boolean + disableFreeSolo?: boolean className?: string } @@ -41,6 +43,7 @@ const defaultProps = { clearable: false, size: 'medium' as NormalSizes, disableMatchWidth: false, + disableFreeSolo: false, className: '', } @@ -83,11 +86,16 @@ const AutoComplete: React.FC> = ({ dropdownClassName, dropdownStyle, disableMatchWidth, + disableFreeSolo, ...props }) => { const ref = useRef(null) - const [state, setState] = useState(customInitialValue) + const inputRef = useRef(null) + const resetTimer = useRef() + const [state, setState, stateRef] = useCurrentState(customInitialValue) + const [selectVal, setSelectVal] = useState(customInitialValue) const [visible, setVisible] = useState(false) + const [, searchChild] = pickChild(children, AutoCompleteSearching) const [, emptyChild] = pickChild(children, AutoCompleteEmpty) const autoCompleteItems = useMemo(() => { @@ -110,14 +118,24 @@ const AutoComplete: React.FC> = ({ const updateValue = (val: string) => { if (disabled) return + setSelectVal(val) onSelect && onSelect(val) setState(val) + inputRef.current && inputRef.current.focus() } const updateVisible = (next: boolean) => setVisible(next) const onInputChange = (event: React.ChangeEvent) => { + setVisible(true) onSearch && onSearch(event.target.value) setState(event.target.value) } + const resetInputValue = () => { + if (!disableFreeSolo) return + if (!state || state === '') return + if (state !== selectVal) { + setState(selectVal) + } + } useEffect(() => { onChange && onChange(state) @@ -140,9 +158,15 @@ const AutoComplete: React.FC> = ({ ) const toggleFocusHandler = (next: boolean) => { + clearTimeout(resetTimer.current) setVisible(next) if (next) { - onSearch && onSearch(state) + onSearch && onSearch(stateRef.current) + } else { + resetTimer.current = window.setTimeout(() => { + resetInputValue() + clearTimeout(resetTimer.current) + }, 100) } } @@ -157,6 +181,7 @@ const AutoComplete: React.FC> = ({
test-1
*
test-2" +`; + +exports[`Breadcrumbs should render correctly 1`] = ` +"" +`; diff --git a/components/breadcrumbs/__tests__/breadcrumbs.test.tsx b/components/breadcrumbs/__tests__/breadcrumbs.test.tsx new file mode 100644 index 000000000..b0ff8cf6c --- /dev/null +++ b/components/breadcrumbs/__tests__/breadcrumbs.test.tsx @@ -0,0 +1,75 @@ +import React from 'react' +import { mount } from 'enzyme' +import { Breadcrumbs } from 'components' + +describe('Breadcrumbs', () => { + it('should render correctly', () => { + const wrapper = mount( + + test-1 + , + ) + expect(wrapper.html()).toMatchSnapshot() + expect(() => wrapper.unmount()).not.toThrow() + }) + + it('should redefined all separators', () => { + const wrapper = mount( + + test-1 + test-2 + , + ) + expect(wrapper.html()).toMatchSnapshot() + expect(wrapper.html()).toContain('*') + expect(() => wrapper.unmount()).not.toThrow() + }) + + it('the specified separator should be redefined', () => { + const wrapper = mount( + + test-1 + % + test-2 + , + ) + expect(wrapper.html()).toContain('%') + }) + + it('should render string when href missing', () => { + let wrapper = mount( + + test-1 + , + ) + let dom = wrapper.find('.breadcrums-item').at(0).getDOMNode() + expect(dom.tagName).toEqual('SPAN') + + wrapper = mount( + + test-1 + , + ) + dom = wrapper.find('.breadcrums-item').at(0).getDOMNode() + expect(dom.tagName).toEqual('A') + + wrapper = mount( + + test-1 + , + ) + dom = wrapper.find('.breadcrums-item').at(0).getDOMNode() + expect(dom.tagName).toEqual('A') + }) + + it('should trigger click event', () => { + const handler = jest.fn() + const wrapper = mount( + + test-1 + , + ) + wrapper.find('.breadcrums-item').at(0).simulate('click') + expect(handler).toHaveBeenCalled() + }) +}) diff --git a/components/breadcrumbs/breadcrumbs-item.tsx b/components/breadcrumbs/breadcrumbs-item.tsx new file mode 100644 index 000000000..2ab1e95c6 --- /dev/null +++ b/components/breadcrumbs/breadcrumbs-item.tsx @@ -0,0 +1,61 @@ +import Link from '../link' +import { Props as LinkBasicProps } from '../link/link' +import React, { useMemo } from 'react' +import withDefaults from '../utils/with-defaults' +import { pickChild } from '../utils/collections' +import BreadcrumbsSeparator from './breadcrumbs-separator' + +interface Props { + href?: string + nextLink?: boolean + onClick?: (event: React.MouseEvent) => void + className?: string +} + +const defaultProps = { + nextLink: false, + className: '', +} + +type NativeAttrs = Omit, keyof Props> +type NativeLinkAttrs = Omit +export type BreadcrumbsProps = Props & typeof defaultProps & NativeLinkAttrs + +const BreadcrumbsItem = React.forwardRef< + HTMLAnchorElement, + React.PropsWithChildren +>( + ( + { href, nextLink, onClick, children, className, ...props }, + ref: React.Ref, + ) => { + const isLink = useMemo(() => href !== undefined || nextLink, [href, nextLink]) + const [withoutSepChildren] = pickChild(children, BreadcrumbsSeparator) + const clickHandler = (event: React.MouseEvent) => { + onClick && onClick(event) + } + + if (!isLink) { + return ( + + {withoutSepChildren} + + ) + } + + return ( + + {withoutSepChildren} + + ) + }, +) + +const MemoBreadcrumbsItem = React.memo(BreadcrumbsItem) + +export default withDefaults(MemoBreadcrumbsItem, defaultProps) diff --git a/components/breadcrumbs/breadcrumbs-separator.tsx b/components/breadcrumbs/breadcrumbs-separator.tsx new file mode 100644 index 000000000..80e12f5b2 --- /dev/null +++ b/components/breadcrumbs/breadcrumbs-separator.tsx @@ -0,0 +1,37 @@ +import React from 'react' +import withDefaults from '../utils/with-defaults' + +interface Props { + className?: string +} + +const defaultProps = { + className: '', +} + +type NativeAttrs = Omit, keyof Props> +export type BreadcrumbsProps = Props & typeof defaultProps & NativeAttrs + +const BreadcrumbsSeparator: React.FC> = ({ + children, + className, +}) => { + return ( +
+ {children} + +
+ ) +} + +const MemoBreadcrumbsSeparator = React.memo(BreadcrumbsSeparator) + +export default withDefaults(MemoBreadcrumbsSeparator, defaultProps) diff --git a/components/breadcrumbs/breadcrumbs.tsx b/components/breadcrumbs/breadcrumbs.tsx new file mode 100644 index 000000000..7850b4de5 --- /dev/null +++ b/components/breadcrumbs/breadcrumbs.tsx @@ -0,0 +1,114 @@ +import React, { ReactNode, useMemo } from 'react' +import useTheme from '../styles/use-theme' +import BreadcrumbsItem from './breadcrumbs-item' +import BreadcrumbsSeparator from './breadcrumbs-separator' +import { addColorAlpha } from '../utils/color' +import { NormalSizes } from '../utils/prop-types' + +interface Props { + size: NormalSizes + separator?: string | ReactNode + className?: string +} + +const defaultProps = { + size: 'medium' as NormalSizes, + separator: '/', + className: '', +} + +type NativeAttrs = Omit, keyof Props> +export type BreadcrumbsProps = Props & typeof defaultProps & NativeAttrs + +const getSize = (size: NormalSizes) => { + const sizes: { [key in NormalSizes]: string } = { + mini: '.75rem', + small: '.875rem', + medium: '1rem', + large: '1.125rem', + } + return sizes[size] +} + +const Breadcrumbs: React.FC> = ({ + size, + separator, + children, + className, +}) => { + const theme = useTheme() + const fontSize = useMemo(() => getSize(size), [size]) + const hoverColor = useMemo(() => { + return addColorAlpha(theme.palette.link, 0.85) + }, [theme.palette.link]) + + const childrenArray = React.Children.toArray(children) + const withSeparatorChildren = childrenArray.map((item, index) => { + if (!React.isValidElement(item)) return item + const last = childrenArray[index - 1] + const lastIsSeparator = React.isValidElement(last) && last.type === BreadcrumbsSeparator + const currentIsSeparator = item.type === BreadcrumbsSeparator + if (!lastIsSeparator && !currentIsSeparator && index > 0) { + return ( + + {separator} + {item} + + ) + } + return item + }) + + return ( + + ) +} + +type MemoBreadcrumbsComponent

= React.NamedExoticComponent

& { + Item: typeof BreadcrumbsItem + Separator: typeof BreadcrumbsSeparator +} +type ComponentProps = Partial & + Omit & + NativeAttrs + +Breadcrumbs.defaultProps = defaultProps + +export default React.memo(Breadcrumbs) as MemoBreadcrumbsComponent diff --git a/components/breadcrumbs/index.ts b/components/breadcrumbs/index.ts new file mode 100644 index 000000000..fe5edd80d --- /dev/null +++ b/components/breadcrumbs/index.ts @@ -0,0 +1,8 @@ +import Breadcrumbs from './breadcrumbs' +import BreadcrumbsItem from './breadcrumbs-item' +import BreadcrumbsSeparator from './breadcrumbs-separator' + +Breadcrumbs.Item = BreadcrumbsItem +Breadcrumbs.Separator = BreadcrumbsSeparator + +export default Breadcrumbs diff --git a/components/css-baseline/__tests__/__snapshots__/baseline.test.tsx.snap b/components/css-baseline/__tests__/__snapshots__/baseline.test.tsx.snap index 4c5ff672e..07fc08442 100644 --- a/components/css-baseline/__tests__/__snapshots__/baseline.test.tsx.snap +++ b/components/css-baseline/__tests__/__snapshots__/baseline.test.tsx.snap @@ -213,7 +213,7 @@ initialize { pre code { color: #000; - font-size: 0.75rem; + font-size: 0.8125rem; line-height: 1.25rem; white-space: pre; } @@ -547,7 +547,7 @@ initialize { pre code { color: #fff; - font-size: 0.75rem; + font-size: 0.8125rem; line-height: 1.25rem; white-space: pre; } diff --git a/components/css-baseline/css-baseline.tsx b/components/css-baseline/css-baseline.tsx index 3eeadfbf6..9f65267d6 100644 --- a/components/css-baseline/css-baseline.tsx +++ b/components/css-baseline/css-baseline.tsx @@ -216,7 +216,7 @@ const CssBaseline: React.FC> = ({ children }) => { pre code { color: ${theme.palette.foreground}; - font-size: 0.75rem; + font-size: 0.8125rem; line-height: 1.25rem; white-space: pre; } diff --git a/components/grid/__tests__/__snapshots__/index.test.tsx.snap b/components/grid/__tests__/__snapshots__/index.test.tsx.snap index 1f8e91768..c0253ff72 100644 --- a/components/grid/__tests__/__snapshots__/index.test.tsx.snap +++ b/components/grid/__tests__/__snapshots__/index.test.tsx.snap @@ -25,6 +25,7 @@ exports[`Grid all breakpoint values should be supported 1`] = ` flex-grow: 0; max-width: 4.166666666666667%; flex-basis: 4.166666666666667%; + display: flex; } @media only screen and (max-width: 650px) { @@ -32,6 +33,7 @@ exports[`Grid all breakpoint values should be supported 1`] = ` flex-grow: 0; max-width: 4.166666666666667%; flex-basis: 4.166666666666667%; + display: flex; } } @@ -40,6 +42,7 @@ exports[`Grid all breakpoint values should be supported 1`] = ` flex-grow: 0; max-width: 8.333333333333334%; flex-basis: 8.333333333333334%; + display: flex; } } @@ -48,6 +51,7 @@ exports[`Grid all breakpoint values should be supported 1`] = ` flex-grow: 0; max-width: 12.5%; flex-basis: 12.5%; + display: flex; } } @@ -56,6 +60,7 @@ exports[`Grid all breakpoint values should be supported 1`] = ` flex-grow: 0; max-width: 16.666666666666668%; flex-basis: 16.666666666666668%; + display: flex; } } @@ -64,6 +69,7 @@ exports[`Grid all breakpoint values should be supported 1`] = ` flex-grow: 0; max-width: 20.833333333333336%; flex-basis: 20.833333333333336%; + display: flex; } }

" @@ -159,6 +171,7 @@ exports[`Grid css value should be passed through 1`] = ` flex-grow: 1; max-width: 100%; flex-basis: 0; + display: flex; } @media only screen and (max-width: 650px) { @@ -166,6 +179,7 @@ exports[`Grid css value should be passed through 1`] = ` flex-grow: 1; max-width: 100%; flex-basis: 0; + display: flex; } } @@ -174,6 +188,7 @@ exports[`Grid css value should be passed through 1`] = ` flex-grow: 1; max-width: 100%; flex-basis: 0; + display: flex; } } @@ -182,6 +197,7 @@ exports[`Grid css value should be passed through 1`] = ` flex-grow: 1; max-width: 100%; flex-basis: 0; + display: flex; } } @@ -190,6 +206,7 @@ exports[`Grid css value should be passed through 1`] = ` flex-grow: 1; max-width: 100%; flex-basis: 0; + display: flex; } } @@ -198,6 +215,7 @@ exports[`Grid css value should be passed through 1`] = ` flex-grow: 1; max-width: 100%; flex-basis: 0; + display: flex; } } " @@ -293,6 +317,7 @@ exports[`Grid decimal spacing should be supported 1`] = ` flex-grow: 1; max-width: 100%; flex-basis: 0; + display: flex; } @media only screen and (max-width: 650px) { @@ -300,6 +325,7 @@ exports[`Grid decimal spacing should be supported 1`] = ` flex-grow: 1; max-width: 100%; flex-basis: 0; + display: flex; } } @@ -308,6 +334,7 @@ exports[`Grid decimal spacing should be supported 1`] = ` flex-grow: 1; max-width: 100%; flex-basis: 0; + display: flex; } } @@ -316,6 +343,7 @@ exports[`Grid decimal spacing should be supported 1`] = ` flex-grow: 1; max-width: 100%; flex-basis: 0; + display: flex; } } @@ -324,6 +352,7 @@ exports[`Grid decimal spacing should be supported 1`] = ` flex-grow: 1; max-width: 100%; flex-basis: 0; + display: flex; } } @@ -332,6 +361,7 @@ exports[`Grid decimal spacing should be supported 1`] = ` flex-grow: 1; max-width: 100%; flex-basis: 0; + display: flex; } } " @@ -427,6 +463,7 @@ exports[`Grid nested components should be supported 1`] = ` flex-grow: 1; max-width: 100%; flex-basis: 0; + display: flex; } @media only screen and (max-width: 650px) { @@ -434,6 +471,7 @@ exports[`Grid nested components should be supported 1`] = ` flex-grow: 1; max-width: 100%; flex-basis: 0; + display: flex; } } @@ -442,6 +480,7 @@ exports[`Grid nested components should be supported 1`] = ` flex-grow: 1; max-width: 100%; flex-basis: 0; + display: flex; } } @@ -450,6 +489,7 @@ exports[`Grid nested components should be supported 1`] = ` flex-grow: 1; max-width: 100%; flex-basis: 0; + display: flex; } } @@ -458,6 +498,7 @@ exports[`Grid nested components should be supported 1`] = ` flex-grow: 1; max-width: 100%; flex-basis: 0; + display: flex; } } @@ -466,6 +507,7 @@ exports[`Grid nested components should be supported 1`] = ` flex-grow: 1; max-width: 100%; flex-basis: 0; + display: flex; } }
test
test
test
,
,
," @@ -951,6 +1035,7 @@ exports[`Grid should render correctly 1`] = ` flex-grow: 1; max-width: 100%; flex-basis: 0; + display: flex; } @media only screen and (max-width: 650px) { @@ -958,6 +1043,7 @@ exports[`Grid should render correctly 1`] = ` flex-grow: 1; max-width: 100%; flex-basis: 0; + display: flex; } } @@ -966,6 +1052,7 @@ exports[`Grid should render correctly 1`] = ` flex-grow: 1; max-width: 100%; flex-basis: 0; + display: flex; } } @@ -974,6 +1061,7 @@ exports[`Grid should render correctly 1`] = ` flex-grow: 1; max-width: 100%; flex-basis: 0; + display: flex; } } @@ -982,6 +1070,7 @@ exports[`Grid should render correctly 1`] = ` flex-grow: 1; max-width: 100%; flex-basis: 0; + display: flex; } } @@ -990,6 +1079,7 @@ exports[`Grid should render correctly 1`] = ` flex-grow: 1; max-width: 100%; flex-basis: 0; + display: flex; } }
test
" @@ -1150,6 +1252,7 @@ exports[`Grid should work correctly when size exceeds 1`] = ` flex-grow: 0; max-width: 100%; flex-basis: 100%; + display: flex; } @media only screen and (max-width: 650px) { @@ -1157,6 +1260,7 @@ exports[`Grid should work correctly when size exceeds 1`] = ` flex-grow: 0; max-width: 100%; flex-basis: 100%; + display: flex; } } @@ -1165,6 +1269,7 @@ exports[`Grid should work correctly when size exceeds 1`] = ` flex-grow: 1; max-width: 100%; flex-basis: 0; + display: flex; } } @@ -1173,6 +1278,7 @@ exports[`Grid should work correctly when size exceeds 1`] = ` flex-grow: 1; max-width: 100%; flex-basis: 0; + display: flex; } } @@ -1181,6 +1287,7 @@ exports[`Grid should work correctly when size exceeds 1`] = ` flex-grow: 1; max-width: 100%; flex-basis: 0; + display: flex; } } @@ -1189,6 +1296,7 @@ exports[`Grid should work correctly when size exceeds 1`] = ` flex-grow: 1; max-width: 100%; flex-basis: 0; + display: flex; } }
test
" diff --git a/components/grid/__tests__/index.test.tsx b/components/grid/__tests__/index.test.tsx index 72250d0bf..f40b00a89 100644 --- a/components/grid/__tests__/index.test.tsx +++ b/components/grid/__tests__/index.test.tsx @@ -111,4 +111,10 @@ describe('Grid', () => { expect(wrapper.html()).toMatchSnapshot() expect(() => wrapper.unmount()).not.toThrow() }) + + it('Grid should be hidden when value is 0', () => { + let wrapper = mount() + expect(wrapper.find('.item').hasClass('xs')).toBeTruthy() + expect(wrapper.find('.item').html()).toContain('display: none') + }) }) diff --git a/components/grid/basic-item.tsx b/components/grid/basic-item.tsx index 6e0fb97fa..1233b3e7f 100644 --- a/components/grid/basic-item.tsx +++ b/components/grid/basic-item.tsx @@ -32,19 +32,23 @@ type ItemLayoutValue = { grow: number width: string basis: string + display: string } const getItemLayout = (val: BreakpointsValue): ItemLayoutValue => { + const display = val === 0 ? 'none' : 'flex' 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', } @@ -77,11 +81,11 @@ const GridBasicItem: React.FC> = ({ xl, } const classString = Object.keys(aligns).reduce((pre, name) => { - if (Boolean(aligns[name]) && aligns[name] !== 0) return `${pre} ${name}` + if (aligns[name] !== undefined && aligns[name] !== false) return `${pre} ${name}` return pre }, '') return classString.trim() - }, [justify, direction, alignItems, alignContent]) + }, [justify, direction, alignItems, alignContent, xs, sm, md, lg, xl]) const layout = useMemo< { @@ -125,6 +129,7 @@ const GridBasicItem: React.FC> = ({ flex-grow: ${layout.xs.grow}; max-width: ${layout.xs.width}; flex-basis: ${layout.xs.basis}; + display: ${layout.xs.display}; } @media only screen and (max-width: ${theme.breakpoints.xs.max}) { @@ -132,6 +137,7 @@ const GridBasicItem: React.FC> = ({ flex-grow: ${layout.xs.grow}; max-width: ${layout.xs.width}; flex-basis: ${layout.xs.basis}; + display: ${layout.xs.display}; } } @@ -140,6 +146,7 @@ const GridBasicItem: React.FC> = ({ flex-grow: ${layout.sm.grow}; max-width: ${layout.sm.width}; flex-basis: ${layout.sm.basis}; + display: ${layout.sm.display}; } } @@ -148,6 +155,7 @@ const GridBasicItem: React.FC> = ({ flex-grow: ${layout.md.grow}; max-width: ${layout.md.width}; flex-basis: ${layout.md.basis}; + display: ${layout.md.display}; } } @@ -156,6 +164,7 @@ const GridBasicItem: React.FC> = ({ flex-grow: ${layout.lg.grow}; max-width: ${layout.lg.width}; flex-basis: ${layout.lg.basis}; + display: ${layout.lg.display}; } } @@ -164,6 +173,7 @@ const GridBasicItem: React.FC> = ({ flex-grow: ${layout.xl.grow}; max-width: ${layout.xl.width}; flex-basis: ${layout.xl.basis}; + display: ${layout.xl.display}; } } `} diff --git a/components/image/__tests__/browser.test.tsx b/components/image/__tests__/browser.test.tsx index 7533daa57..db6c82882 100644 --- a/components/image/__tests__/browser.test.tsx +++ b/components/image/__tests__/browser.test.tsx @@ -59,4 +59,15 @@ describe('Image Browser', () => { const wrapper = mount() expect(() => wrapper.unmount()).not.toThrow() }) + + it('anchor props should be passed through', () => { + const anchorRel = 'noreferrer' + const wrapper = mount( + + + , + ) + const rel = wrapper.find('a').getDOMNode().getAttribute('rel') + expect(anchorRel).toEqual(anchorRel) + }) }) diff --git a/components/image/image-browser.tsx b/components/image/image-browser.tsx index 14f5e1a51..ce420f93c 100644 --- a/components/image/image-browser.tsx +++ b/components/image/image-browser.tsx @@ -1,21 +1,26 @@ import React, { useMemo } from 'react' import Link from '../link' +import { Props as LinkProps } from '../link/link' import useTheme from '../styles/use-theme' import withDefaults from '../utils/with-defaults' import ImageBrowserHttpsIcon from './image-browser-https-icon' import { getBrowserColors, BrowserColors } from './styles' +type AnchorProps = Omit, keyof LinkProps> + interface Props { title?: string url?: string showFullLink?: boolean invert?: boolean + anchorProps?: AnchorProps className?: string } const defaultProps = { className: '', showFullLink: false, + anchorProps: {} as AnchorProps, invert: false, } @@ -42,12 +47,17 @@ const getTitle = (title: string, colors: BrowserColors) => ( ) -const getAddressInput = (url: string, showFullLink: boolean, colors: BrowserColors) => ( +const getAddressInput = ( + url: string, + showFullLink: boolean, + colors: BrowserColors, + anchorProps: AnchorProps, +) => (
- + {showFullLink ? url : getHostFromUrl(url)}
  • " +`; diff --git a/components/pagination/__tests__/pagination.test.tsx b/components/pagination/__tests__/pagination.test.tsx new file mode 100644 index 000000000..8bf9dfb3c --- /dev/null +++ b/components/pagination/__tests__/pagination.test.tsx @@ -0,0 +1,148 @@ +import React from 'react' +import { mount } from 'enzyme' +import { Pagination } from 'components' +import { act } from 'react-dom/test-utils' +import { updateWrapper } from 'tests/utils' + +describe('Pagination', () => { + it('should render correctly', () => { + const wrapper = mount() + expect(wrapper.html()).toMatchSnapshot() + expect(() => wrapper.unmount()).not.toThrow() + }) + + it('the specified page should be activated', async () => { + const wrapper = mount() + expect(wrapper.find('.active').text()).toEqual('2') + await act(async () => { + wrapper.setProps({ page: 10 }) + }) + await updateWrapper(wrapper, 200) + expect(wrapper.find('.active').text()).toEqual('10') + }) + + it('should trigger change event', async () => { + let current = 1 + const handler = jest.fn().mockImplementation(val => (current = val)) + const wrapper = mount() + + await act(async () => { + wrapper.setProps({ page: 10 }) + }) + await updateWrapper(wrapper, 200) + expect(handler).toHaveBeenCalled() + expect(current).toEqual(10) + + const btns = wrapper.find('button') + btns.at(0).simulate('click') + await updateWrapper(wrapper, 200) + expect(current).toEqual(9) + + btns.at(btns.length - 1).simulate('click') + btns.at(btns.length - 1).simulate('click') + btns.at(btns.length - 1).simulate('click') + btns.at(btns.length - 1).simulate('click') + await updateWrapper(wrapper, 200) + expect(current).toEqual(10) + handler.mockRestore() + }) + + it('the page should be rendered to follow the specified limit', async () => { + const wrapper = mount() + expect(wrapper.find('button').length).toBeGreaterThanOrEqual(20) + await act(async () => { + wrapper.setProps({ limit: 5 }) + }) + await updateWrapper(wrapper, 200) + expect(wrapper.find('button').length).toBeLessThanOrEqual(10) + }) + + it('should be render all pages when limit is greater than the total', async () => { + const handler = jest.fn() + const wrapper = mount() + expect(wrapper.find('button').length).toBeGreaterThanOrEqual(15) + wrapper.find('button').at(10).simulate('click') + await updateWrapper(wrapper, 200) + + expect(handler).toHaveBeenCalled() + handler.mockRestore() + }) + + it('omit pages by limit value', async () => { + const wrapper = mount() + const btn4 = wrapper.find('button').at(4) + expect(btn4.text()).toEqual('4') + btn4.simulate('click') + await updateWrapper(wrapper, 200) + let btns = wrapper.find('button').map(btn => btn.text()) + expect(btns.includes('2')).not.toBeTruthy() + expect(btns.includes('1')).toBeTruthy() + expect(btns.includes('3')).toBeTruthy() + expect(btns.includes('4')).toBeTruthy() + expect(btns.includes('5')).toBeTruthy() + expect(btns.includes('6')).not.toBeTruthy() + expect(btns.includes('20')).toBeTruthy() + + const btn5 = wrapper.find('button').at(5) + expect(btn5.text()).toEqual('5') + btn5.simulate('click') + await updateWrapper(wrapper, 200) + btns = wrapper.find('button').map(btn => btn.text()) + expect(btns.includes('1')).toBeTruthy() + expect(btns.includes('2')).not.toBeTruthy() + expect(btns.includes('3')).not.toBeTruthy() + expect(btns.includes('4')).toBeTruthy() + expect(btns.includes('5')).toBeTruthy() + expect(btns.includes('6')).toBeTruthy() + expect(btns.includes('7')).not.toBeTruthy() + expect(btns.includes('8')).not.toBeTruthy() + expect(btns.includes('20')).toBeTruthy() + }) + + it('should trigger change event when ellipsis clicked', async () => { + let current = 20 + const handler = jest.fn().mockImplementation(val => (current = val)) + const wrapper = mount() + const btn = wrapper.find('svg').at(0).parents('button') + btn.at(0).simulate('click') + await updateWrapper(wrapper, 200) + expect(handler).toHaveBeenCalled() + expect(current).toEqual(15) + + await act(async () => { + wrapper.setProps({ page: 1 }) + }) + await updateWrapper(wrapper, 200) + const lastBtn = wrapper.find('svg').at(0).parents('button') + lastBtn.at(0).simulate('click') + await updateWrapper(wrapper, 200) + expect(current).toEqual(1 + 5) + }) + + it('another SVG should be displayed when the mouse is moved in', async () => { + const wrapper = mount() + const svg = wrapper.find('svg').at(0) + const btn = svg.parents('button') + + const html = svg.html() + btn.simulate('mouseEnter') + await updateWrapper(wrapper) + expect(html).not.toEqual(wrapper.find('svg').at(0).html()) + + btn.simulate('mouseLeave') + await updateWrapper(wrapper) + expect(html).toEqual(wrapper.find('svg').at(0).html()) + }) + + it('custom buttons should be display', () => { + const wrapper = mount( + + custom-prev + custom-next + , + ) + const btns = wrapper.find('button') + expect(btns.at(0).text()).toEqual('custom-prev') + expect(btns.at(btns.length - 1).text()).toEqual('custom-next') + }) +}) diff --git a/components/pagination/index.ts b/components/pagination/index.ts new file mode 100644 index 000000000..9a6011225 --- /dev/null +++ b/components/pagination/index.ts @@ -0,0 +1,8 @@ +import Pagination from './pagination' +import PaginationPrevious from './pagination-previous' +import PaginationNext from './pagination-next' + +Pagination.Previous = PaginationPrevious +Pagination.Next = PaginationNext + +export default Pagination diff --git a/components/pagination/pagination-context.ts b/components/pagination/pagination-context.ts new file mode 100644 index 000000000..8342ec758 --- /dev/null +++ b/components/pagination/pagination-context.ts @@ -0,0 +1,18 @@ +import React from 'react' +import { tuple } from '../utils/prop-types' +const paginationUpdateTypes = tuple('prev', 'next', 'click') + +export type PaginationUpdateType = typeof paginationUpdateTypes[number] + +export interface PaginationConfig { + isFirst?: boolean + isLast?: boolean + update?: (type: PaginationUpdateType) => void +} + +const defaultContext = {} + +export const PaginationContext = React.createContext(defaultContext) + +export const usePaginationContext = (): PaginationConfig => + React.useContext(PaginationContext) diff --git a/components/pagination/pagination-ellipsis.tsx b/components/pagination/pagination-ellipsis.tsx new file mode 100644 index 000000000..e4d759db6 --- /dev/null +++ b/components/pagination/pagination-ellipsis.tsx @@ -0,0 +1,60 @@ +import React, { useState } from 'react' +import PaginationItem from './pagination-item' + +interface Props { + isBefore?: boolean + onClick?: (e: React.MouseEvent) => void +} + +const PaginationEllipsis: React.FC = ({ isBefore, onClick }) => { + const [showMore, setShowMore] = useState(false) + + return ( + onClick && onClick(e)} + onMouseEnter={() => setShowMore(true)} + onMouseLeave={() => setShowMore(false)}> + {showMore ? ( + + + + + ) : ( + + + + + + )} + + + + ) +} + +export default PaginationEllipsis diff --git a/components/pagination/pagination-item.tsx b/components/pagination/pagination-item.tsx new file mode 100644 index 000000000..74824d76d --- /dev/null +++ b/components/pagination/pagination-item.tsx @@ -0,0 +1,101 @@ +import React, { useMemo } from 'react' +import useTheme from '../styles/use-theme' +import { addColorAlpha } from '../utils/color' + +interface Props { + active?: boolean + disabled?: boolean + onClick?: (e: React.MouseEvent) => void +} + +type NativeAttrs = Omit, keyof Props> +export type PaginationItemProps = Props & NativeAttrs + +const PaginationItem: React.FC> = ({ + active, + children, + disabled, + onClick, + ...props +}) => { + const theme = useTheme() + const [hover, activeHover] = useMemo( + () => [addColorAlpha(theme.palette.success, 0.1), addColorAlpha(theme.palette.success, 0.8)], + [theme.palette.success], + ) + const clickHandler = (event: React.MouseEvent) => { + if (disabled) return + onClick && onClick(event) + } + + return ( +
  • + + +
  • + ) +} + +export default PaginationItem diff --git a/components/pagination/pagination-next.tsx b/components/pagination/pagination-next.tsx new file mode 100644 index 000000000..d01f893fe --- /dev/null +++ b/components/pagination/pagination-next.tsx @@ -0,0 +1,19 @@ +import React from 'react' +import PaginationItem from './pagination-item' +import { usePaginationContext } from './pagination-context' + +export type PaginationNextProps = React.ButtonHTMLAttributes + +const PaginationNext: React.FC> = ({ + children, + ...props +}) => { + const { update, isLast } = usePaginationContext() + return ( + update && update('next')} disabled={isLast} {...props}> + {children} + + ) +} + +export default PaginationNext diff --git a/components/pagination/pagination-pages.tsx b/components/pagination/pagination-pages.tsx new file mode 100644 index 000000000..a90f58fdc --- /dev/null +++ b/components/pagination/pagination-pages.tsx @@ -0,0 +1,104 @@ +import React, { Dispatch, SetStateAction, useCallback, useMemo } from 'react' +import PaginationItem from './pagination-item' +import PaginationEllipsis from './pagination-ellipsis' + +interface Props { + limit: number + count: number + current: number + setPage: Dispatch> +} + +const PaginationPages: React.FC = ({ limit, count, current, setPage }) => { + const showPages = useMemo(() => { + const oddLimit = limit % 2 === 0 ? limit - 1 : limit + return oddLimit - 2 + }, [limit]) + const middleNumber = (showPages + 1) / 2 + + const [showBeforeEllipsis, showAfterEllipsis] = useMemo(() => { + const showEllipsis = count > limit + return [ + showEllipsis && current > middleNumber + 1, + showEllipsis && current < count - middleNumber, + ] + }, [current, showPages, middleNumber, count, limit]) + const pagesArray = useMemo(() => [...new Array(showPages)], [showPages]) + + const renderItem = useCallback( + (value: number, active: number) => ( + setPage(value)}> + {value} + + ), + [], + ) + const startPages = pagesArray.map((_, index) => { + const value = index + 2 + return renderItem(value, current) + }) + const middlePages = pagesArray.map((_, index) => { + const middleIndexNumber = middleNumber - (index + 1) + const value = current - middleIndexNumber + return ( + setPage(value)}> + {value} + + ) + }) + const endPages = pagesArray.map((_, index) => { + const value = count - (showPages - index) + return renderItem(value, current) + }) + if (count <= limit) { + /* eslint-disable react/jsx-no-useless-fragment */ + return ( + <> + {[...new Array(count)].map((_, index) => { + const value = index + 1 + return ( + setPage(value)}> + {value} + + ) + })} + + ) + /* eslint-enable */ + } + return ( + <> + {renderItem(1, current)} + {showBeforeEllipsis && ( + setPage(last => (last - 5 >= 1 ? last - 5 : 1))} + /> + )} + {showBeforeEllipsis && showAfterEllipsis + ? middlePages + : showBeforeEllipsis + ? endPages + : startPages} + {showAfterEllipsis && ( + setPage(last => (last + 5 <= count ? last + 5 : count))} + /> + )} + {renderItem(count, current)} + + ) +} + +export default PaginationPages diff --git a/components/pagination/pagination-previous.tsx b/components/pagination/pagination-previous.tsx new file mode 100644 index 000000000..79b2bf961 --- /dev/null +++ b/components/pagination/pagination-previous.tsx @@ -0,0 +1,19 @@ +import React from 'react' +import PaginationItem from './pagination-item' +import { usePaginationContext } from './pagination-context' + +export type PaginationNextProps = React.ButtonHTMLAttributes + +const PaginationPrevious: React.FC> = ({ + children, + ...props +}) => { + const { update, isFirst } = usePaginationContext() + return ( + update && update('prev')} disabled={isFirst} {...props}> + {children} + + ) +} + +export default PaginationPrevious diff --git a/components/pagination/pagination.tsx b/components/pagination/pagination.tsx new file mode 100644 index 000000000..80727857e --- /dev/null +++ b/components/pagination/pagination.tsx @@ -0,0 +1,142 @@ +import React, { useEffect, useMemo } from 'react' +import PaginationPrevious from './pagination-previous' +import PaginationNext from './pagination-next' +import PaginationPages from './pagination-pages' +import { PaginationContext, PaginationConfig, PaginationUpdateType } from './pagination-context' +import useCurrentState from '../utils/use-current-state' +import { pickChild } from '../utils/collections' +import { NormalSizes } from '../utils/prop-types' + +interface Props { + size?: NormalSizes + page?: number + initialPage?: number + count?: number + limit?: number + onChange?: (val: number) => void +} + +const defaultProps = { + size: 'medium' as NormalSizes, + initialPage: 1, + count: 1, + limit: 7, +} + +type NativeAttrs = Omit, keyof Props> +export type PaginationProps = Props & typeof defaultProps & NativeAttrs + +type PaginationSize = { + font: string + width: string +} + +const getPaginationSizes = (size: NormalSizes) => { + const sizes: { [key in NormalSizes]: PaginationSize } = { + mini: { + font: '.75rem', + width: '1.25rem', + }, + small: { + font: '.75rem', + width: '1.65rem', + }, + medium: { + font: '.875rem', + width: '2rem', + }, + large: { + font: '1rem', + width: '2.4rem', + }, + } + return sizes[size] +} + +const Pagination: React.FC> = ({ + page: customPage, + initialPage, + count, + limit, + size, + children, + onChange, +}) => { + const [page, setPage, pageRef] = useCurrentState(initialPage) + const [, prevChildren] = pickChild(children, PaginationPrevious) + const [, nextChildren] = pickChild(children, PaginationNext) + + const [prevItem, nextItem] = useMemo(() => { + const hasChildren = (c: any) => React.Children.count(c) > 0 + const prevDefault = prev + const nextDefault = next + return [ + hasChildren(prevChildren) ? prevChildren : prevDefault, + hasChildren(nextChildren) ? nextChildren : nextDefault, + ] + }, [prevChildren, nextChildren]) + const { font, width } = useMemo(() => getPaginationSizes(size), [size]) + + const update = (type: PaginationUpdateType) => { + if (type === 'prev' && pageRef.current > 1) { + setPage(last => last - 1) + } + if (type === 'next' && pageRef.current < count) { + setPage(last => last + 1) + } + } + const values = useMemo( + () => ({ + isFirst: page <= 1, + isLast: page >= count, + update, + }), + [page], + ) + + useEffect(() => { + onChange && onChange(page) + }, [page]) + useEffect(() => { + if (customPage !== undefined) { + setPage(customPage) + } + }, [customPage]) + + return ( + + + + + ) +} + +type MemoPaginationComponent

    = React.NamedExoticComponent

    & { + Previous: typeof PaginationPrevious + Next: typeof PaginationNext +} + +type ComponentProps = Partial & + Omit & + NativeAttrs + +Pagination.defaultProps = defaultProps + +export default React.memo(Pagination) as MemoPaginationComponent diff --git a/components/radio/__tests__/group.test.tsx b/components/radio/__tests__/group.test.tsx index 9cb15c5e7..22bd1e1ab 100644 --- a/components/radio/__tests__/group.test.tsx +++ b/components/radio/__tests__/group.test.tsx @@ -61,6 +61,28 @@ describe('Radio Group', () => { changeHandler.mockRestore() }) + it('the radio value should be support number', () => { + let value = '' + const changeHandler = jest.fn().mockImplementation(val => (value = val)) + const wrapper = mount( + + Option 1 + Option 2 + , + ) + + wrapper + .find('input') + .at(0) + .simulate('change', { + ...nativeEvent, + target: { checked: true }, + }) + expect(changeHandler).toHaveBeenCalled() + expect(value).toEqual(5) + changeHandler.mockRestore() + }) + it('should ignore events when disabled', () => { const changeHandler = jest.fn() const wrapper = mount( diff --git a/components/radio/radio-context.ts b/components/radio/radio-context.ts index b29271bce..41fcce416 100644 --- a/components/radio/radio-context.ts +++ b/components/radio/radio-context.ts @@ -1,9 +1,9 @@ import React from 'react' export interface RadioConfig { - updateState?: (value: string) => void + updateState?: (value: string | number) => void disabledAll: boolean - value?: string + value?: string | number inGroup: boolean } diff --git a/components/radio/radio-group.tsx b/components/radio/radio-group.tsx index 802d3ce6d..21954f117 100644 --- a/components/radio/radio-group.tsx +++ b/components/radio/radio-group.tsx @@ -4,11 +4,11 @@ import { RadioContext } from './radio-context' import { NormalSizes } from 'components/utils/prop-types' interface Props { - value?: string - initialValue?: string + value?: string | number + initialValue?: string | number disabled?: boolean size?: NormalSizes - onChange?: (value: string) => void + onChange?: (value: string | number) => void className?: string useRow?: boolean } @@ -44,8 +44,8 @@ const RadioGroup: React.FC> = ({ useRow, ...props }) => { - const [selfVal, setSelfVal] = useState(initialValue) - const updateState = (nextValue: string) => { + const [selfVal, setSelfVal] = useState(initialValue) + const updateState = (nextValue: string | number) => { setSelfVal(nextValue) onChange && onChange(nextValue) } diff --git a/components/radio/radio.tsx b/components/radio/radio.tsx index b449ad7e8..15109e4e8 100644 --- a/components/radio/radio.tsx +++ b/components/radio/radio.tsx @@ -20,7 +20,7 @@ export interface RadioEvent { interface Props { checked?: boolean - value?: string + value?: string | number size?: NormalSizes className?: string disabled?: boolean @@ -77,7 +77,7 @@ const Radio: React.FC> = ({ } setSelfChecked(!selfChecked) if (inGroup) { - updateState && updateState(radioValue as string) + updateState && updateState(radioValue as string | number) } onChange && onChange(selfEvent) } diff --git a/components/select/__tests__/__snapshots__/multiple.test.tsx.snap b/components/select/__tests__/__snapshots__/multiple.test.tsx.snap index fa8a6270c..f850dbe87 100644 --- a/components/select/__tests__/__snapshots__/multiple.test.tsx.snap +++ b/components/select/__tests__/__snapshots__/multiple.test.tsx.snap @@ -32,6 +32,7 @@ exports[`Select Multiple should render correctly 1`] = ` flex-grow: 1; max-width: 100%; flex-basis: 0; + display: flex; } @media only screen and (max-width: 650px) { @@ -39,6 +40,7 @@ exports[`Select Multiple should render correctly 1`] = ` flex-grow: 1; max-width: 100%; flex-basis: 0; + display: flex; } } @@ -47,6 +49,7 @@ exports[`Select Multiple should render correctly 1`] = ` flex-grow: 1; max-width: 100%; flex-basis: 0; + display: flex; } } @@ -55,6 +58,7 @@ exports[`Select Multiple should render correctly 1`] = ` flex-grow: 1; max-width: 100%; flex-basis: 0; + display: flex; } } @@ -63,6 +67,7 @@ exports[`Select Multiple should render correctly 1`] = ` flex-grow: 1; max-width: 100%; flex-basis: 0; + display: flex; } } @@ -71,6 +76,7 @@ exports[`Select Multiple should render correctly 1`] = ` flex-grow: 1; max-width: 100%; flex-basis: 0; + display: flex; } }