|
|
@ -1,6 +1,10 @@ |
|
|
|
import * as React from 'react'; |
|
|
|
import React, { useCallback, useMemo, useState } from 'react'; |
|
|
|
import type { CSSProperties } from 'react'; |
|
|
|
import Icon, * as AntdIcons from '@ant-design/icons'; |
|
|
|
import type { SegmentedProps } from 'antd'; |
|
|
|
import type { IntlShape } from 'react-intl'; |
|
|
|
import { Segmented, Input, Empty, Affix } from 'antd'; |
|
|
|
import { css } from '@emotion/react'; |
|
|
|
import { useIntl } from 'dumi'; |
|
|
|
import debounce from 'lodash/debounce'; |
|
|
|
import Category from './Category'; |
|
|
@ -17,6 +21,32 @@ export enum ThemeType { |
|
|
|
|
|
|
|
const allIcons: { [key: string]: any } = AntdIcons; |
|
|
|
|
|
|
|
const useStyle = () => ({ |
|
|
|
iconSearchAffix: css` |
|
|
|
display: flex; |
|
|
|
transition: all 0.3s; |
|
|
|
justify-content: space-between; |
|
|
|
`,
|
|
|
|
}); |
|
|
|
|
|
|
|
const options = (intl: IntlShape): SegmentedProps['options'] => [ |
|
|
|
{ |
|
|
|
value: ThemeType.Outlined, |
|
|
|
icon: <Icon component={OutlinedIcon} />, |
|
|
|
label: intl.formatMessage({ id: 'app.docs.components.icon.outlined' }), |
|
|
|
}, |
|
|
|
{ |
|
|
|
value: ThemeType.Filled, |
|
|
|
icon: <Icon component={FilledIcon} />, |
|
|
|
label: intl.formatMessage({ id: 'app.docs.components.icon.filled' }), |
|
|
|
}, |
|
|
|
{ |
|
|
|
value: ThemeType.TwoTone, |
|
|
|
icon: <Icon component={TwoToneIcon} />, |
|
|
|
label: intl.formatMessage({ id: 'app.docs.components.icon.two-tone' }), |
|
|
|
}, |
|
|
|
]; |
|
|
|
|
|
|
|
interface IconSearchState { |
|
|
|
theme: ThemeType; |
|
|
|
searchKey: string; |
|
|
@ -24,25 +54,23 @@ interface IconSearchState { |
|
|
|
|
|
|
|
const IconSearch: React.FC = () => { |
|
|
|
const intl = useIntl(); |
|
|
|
const [displayState, setDisplayState] = React.useState<IconSearchState>({ |
|
|
|
theme: ThemeType.Outlined, |
|
|
|
const { iconSearchAffix } = useStyle(); |
|
|
|
const [displayState, setDisplayState] = useState<IconSearchState>({ |
|
|
|
searchKey: '', |
|
|
|
theme: ThemeType.Outlined, |
|
|
|
}); |
|
|
|
|
|
|
|
const newIconNames: string[] = []; |
|
|
|
|
|
|
|
const handleSearchIcon = React.useCallback( |
|
|
|
debounce((searchKey: string) => { |
|
|
|
setDisplayState((prevState) => ({ ...prevState, searchKey })); |
|
|
|
}), |
|
|
|
[], |
|
|
|
); |
|
|
|
const handleSearchIcon = debounce((e: React.ChangeEvent<HTMLInputElement>) => { |
|
|
|
setDisplayState((prevState) => ({ ...prevState, searchKey: e.target.value })); |
|
|
|
}, 300); |
|
|
|
|
|
|
|
const handleChangeTheme = React.useCallback((value) => { |
|
|
|
const handleChangeTheme = useCallback((value) => { |
|
|
|
setDisplayState((prevState) => ({ ...prevState, theme: value as ThemeType })); |
|
|
|
}, []); |
|
|
|
|
|
|
|
const renderCategories = React.useMemo<React.ReactNode | React.ReactNode[]>(() => { |
|
|
|
const renderCategories = useMemo<React.ReactNode | React.ReactNode[]>(() => { |
|
|
|
const { searchKey = '', theme } = displayState; |
|
|
|
|
|
|
|
const categoriesResult = Object.keys(categories) |
|
|
@ -77,62 +105,38 @@ const IconSearch: React.FC = () => { |
|
|
|
newIcons={newIconNames} |
|
|
|
/> |
|
|
|
)); |
|
|
|
return categoriesResult.length === 0 ? <Empty style={{ margin: '2em 0' }} /> : categoriesResult; |
|
|
|
return categoriesResult.length ? categoriesResult : <Empty style={{ margin: '2em 0' }} />; |
|
|
|
}, [displayState.searchKey, displayState.theme]); |
|
|
|
|
|
|
|
const [searchBarAffixed, setSearchBarAffixed] = React.useState(false); |
|
|
|
const [searchBarAffixed, setSearchBarAffixed] = useState<boolean>(false); |
|
|
|
const { token } = useSiteToken(); |
|
|
|
const { borderRadius, colorBgContainer } = token; |
|
|
|
const affixedStyle = searchBarAffixed |
|
|
|
? { |
|
|
|
boxShadow: 'rgba(50, 50, 93, 0.25) 0px 6px 12px -2px, rgba(0, 0, 0, 0.3) 0px 3px 7px -3px', |
|
|
|
padding: 8, |
|
|
|
margin: -8, |
|
|
|
borderRadius, |
|
|
|
background: colorBgContainer, |
|
|
|
} |
|
|
|
: {}; |
|
|
|
|
|
|
|
const affixedStyle: CSSProperties = { |
|
|
|
boxShadow: 'rgba(50, 50, 93, 0.25) 0 6px 12px -2px, rgba(0, 0, 0, 0.3) 0 3px 7px -3px', |
|
|
|
padding: 8, |
|
|
|
margin: -8, |
|
|
|
borderRadius, |
|
|
|
backgroundColor: colorBgContainer, |
|
|
|
}; |
|
|
|
|
|
|
|
return ( |
|
|
|
<div className="markdown"> |
|
|
|
<Affix offsetTop={24} onChange={(affixed) => setSearchBarAffixed(affixed)}> |
|
|
|
<div |
|
|
|
style={{ |
|
|
|
display: 'flex', |
|
|
|
justifyContent: 'space-between', |
|
|
|
transition: 'all .3s', |
|
|
|
...affixedStyle, |
|
|
|
}} |
|
|
|
> |
|
|
|
<Affix offsetTop={24} onChange={setSearchBarAffixed}> |
|
|
|
<div css={iconSearchAffix} style={searchBarAffixed ? affixedStyle : {}}> |
|
|
|
<Segmented |
|
|
|
size="large" |
|
|
|
value={displayState.theme} |
|
|
|
options={options(intl)} |
|
|
|
onChange={handleChangeTheme} |
|
|
|
size="large" |
|
|
|
options={[ |
|
|
|
{ |
|
|
|
icon: <Icon component={OutlinedIcon} />, |
|
|
|
label: intl.formatMessage({ id: 'app.docs.components.icon.outlined' }), |
|
|
|
value: ThemeType.Outlined, |
|
|
|
}, |
|
|
|
{ |
|
|
|
icon: <Icon component={FilledIcon} />, |
|
|
|
label: intl.formatMessage({ id: 'app.docs.components.icon.filled' }), |
|
|
|
value: ThemeType.Filled, |
|
|
|
}, |
|
|
|
{ |
|
|
|
icon: <Icon component={TwoToneIcon} />, |
|
|
|
label: intl.formatMessage({ id: 'app.docs.components.icon.two-tone' }), |
|
|
|
value: ThemeType.TwoTone, |
|
|
|
}, |
|
|
|
]} |
|
|
|
/> |
|
|
|
<Input.Search |
|
|
|
placeholder={intl.formatMessage({ id: 'app.docs.components.icon.search.placeholder' })} |
|
|
|
style={{ flex: 1, marginInlineStart: 16 }} |
|
|
|
allowClear |
|
|
|
onChange={(e) => handleSearchIcon(e.currentTarget.value)} |
|
|
|
size="large" |
|
|
|
autoFocus |
|
|
|
size="large" |
|
|
|
onChange={handleSearchIcon} |
|
|
|
/> |
|
|
|
</div> |
|
|
|
</Affix> |
|
|
|