-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
361 additions
and
44 deletions.
There are no files selected for viewing
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
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
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,106 @@ | ||
import type { DTransitionState } from '../_transition'; | ||
|
||
import { isNumber, isUndefined } from 'lodash'; | ||
import { useEffect, useRef } from 'react'; | ||
|
||
import { usePrefixConfig, useComponentConfig, useAsync, useForceUpdate } from '../../hooks'; | ||
import { registerComponentMate, getClassName, checkNodeExist } from '../../utils'; | ||
import { TTANSITION_DURING_BASE } from '../../utils/global'; | ||
import { DTransition } from '../_transition'; | ||
|
||
export interface DLoadingProps extends React.HTMLAttributes<HTMLDivElement> { | ||
dVisible?: boolean; | ||
dText?: React.ReactNode; | ||
dDelay?: number; | ||
dSize?: number; | ||
afterVisibleChange?: (visible: boolean) => void; | ||
} | ||
|
||
const { COMPONENT_NAME } = registerComponentMate({ COMPONENT_NAME: 'DLoading' }); | ||
export function DLoading(props: DLoadingProps): JSX.Element | null { | ||
const { | ||
children, | ||
dVisible = true, | ||
dText, | ||
dDelay, | ||
dSize = '28px', | ||
afterVisibleChange, | ||
|
||
...restProps | ||
} = useComponentConfig(COMPONENT_NAME, props); | ||
|
||
//#region Context | ||
const dPrefix = usePrefixConfig(); | ||
//#endregion | ||
|
||
const asyncCapture = useAsync(); | ||
const forceUpdate = useForceUpdate(); | ||
|
||
const delayVisible = useRef(false); | ||
if (dVisible === false) { | ||
delayVisible.current = false; | ||
} | ||
const visible = isUndefined(dDelay) ? dVisible : delayVisible.current; | ||
|
||
const transitionStyles: Partial<Record<DTransitionState, React.CSSProperties>> = { | ||
enter: { opacity: 0 }, | ||
entering: { | ||
transition: ['opacity'].map((attr) => `${attr} ${TTANSITION_DURING_BASE}ms linear`).join(', '), | ||
}, | ||
leaving: { | ||
opacity: 0, | ||
transition: ['opacity'].map((attr) => `${attr} ${TTANSITION_DURING_BASE}ms linear`).join(', '), | ||
}, | ||
leaved: { display: 'none' }, | ||
}; | ||
|
||
useEffect(() => { | ||
if (isNumber(dDelay) && dVisible) { | ||
const [asyncGroup, asyncId] = asyncCapture.createGroup(); | ||
|
||
asyncGroup.setTimeout(() => { | ||
delayVisible.current = true; | ||
forceUpdate(); | ||
}, dDelay); | ||
|
||
return () => { | ||
asyncCapture.deleteGroup(asyncId); | ||
}; | ||
} | ||
}, [asyncCapture, dDelay, dVisible, forceUpdate]); | ||
|
||
return ( | ||
<DTransition | ||
dIn={visible} | ||
dDuring={TTANSITION_DURING_BASE} | ||
afterEnter={() => { | ||
afterVisibleChange?.(true); | ||
}} | ||
afterLeave={() => { | ||
afterVisibleChange?.(false); | ||
}} | ||
> | ||
{(state) => ( | ||
<div | ||
{...restProps} | ||
className={getClassName(restProps.className, `${dPrefix}loading`)} | ||
style={{ | ||
...restProps.style, | ||
...transitionStyles[state], | ||
}} | ||
> | ||
<div className={`${dPrefix}loading__icon`} style={{ fontSize: dSize }}> | ||
{checkNodeExist(children) ? ( | ||
children | ||
) : ( | ||
<svg className={`${dPrefix}loading__spinner`} width="1em" height="1em" viewBox="0 0 50 50"> | ||
<circle cx="25" cy="25" r="20" fill="none" stroke="currentColor" strokeWidth="4" strokeLinecap="round"></circle> | ||
</svg> | ||
)} | ||
</div> | ||
{checkNodeExist(dText) && <div className={`${dPrefix}loading__text`}>{dText}</div>} | ||
</div> | ||
)} | ||
</DTransition> | ||
); | ||
} |
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,6 @@ | ||
--- | ||
group: Feedback | ||
title: Loading | ||
--- | ||
|
||
## API |
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,5 @@ | ||
--- | ||
title: 加载中 | ||
--- | ||
|
||
## API |
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,37 @@ | ||
--- | ||
title: | ||
en-US: Basic | ||
zh-Hant: 基本 | ||
--- | ||
|
||
# en-US | ||
|
||
The simplest usage. | ||
|
||
# zh-Hant | ||
|
||
最简单的用法。 | ||
|
||
```tsx | ||
import { useState } from 'react'; | ||
|
||
import { DLoading, DAlert, DSwitch } from '@react-devui/ui'; | ||
|
||
export default function Demo() { | ||
const [loading, setLoading] = useState(true); | ||
|
||
return ( | ||
<> | ||
<DSwitch dModel={loading} onModelChange={setLoading}> | ||
Loading | ||
</DSwitch> | ||
<br /> | ||
<br /> | ||
<div style={{ position: 'relative' }}> | ||
<DLoading dVisible={loading}></DLoading> | ||
<DAlert dTitle="I love DevUI so much!" dDescription="Detailed description and advice about DevUI." dType="info"></DAlert> | ||
</div> | ||
</> | ||
); | ||
} | ||
``` |
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,37 @@ | ||
--- | ||
title: | ||
en-US: Add text | ||
zh-Hant: 添加文字 | ||
--- | ||
|
||
# en-US | ||
|
||
Add text via `dText`. | ||
|
||
# zh-Hant | ||
|
||
通过 `dText` 添加文字。 | ||
|
||
```tsx | ||
import { useState } from 'react'; | ||
|
||
import { DLoading, DAlert, DSwitch } from '@react-devui/ui'; | ||
|
||
export default function Demo() { | ||
const [loading, setLoading] = useState(true); | ||
|
||
return ( | ||
<> | ||
<DSwitch dModel={loading} onModelChange={setLoading}> | ||
Loading | ||
</DSwitch> | ||
<br /> | ||
<br /> | ||
<div style={{ position: 'relative' }}> | ||
<DLoading dVisible={loading} dText="Loading..."></DLoading> | ||
<DAlert dTitle="I love DevUI so much!" dDescription="Detailed description and advice about DevUI." dType="info"></DAlert> | ||
</div> | ||
</> | ||
); | ||
} | ||
``` |
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,40 @@ | ||
--- | ||
title: | ||
en-US: Custom icon | ||
zh-Hant: 自定义图标 | ||
--- | ||
|
||
# en-US | ||
|
||
Custom loading icon. | ||
|
||
# zh-Hant | ||
|
||
自定义加载图标。 | ||
|
||
```tsx | ||
import { useState } from 'react'; | ||
|
||
import { DLoading, DAlert, DSwitch } from '@react-devui/ui'; | ||
import { LoadingOutlined } from '@react-devui/ui/icons'; | ||
|
||
export default function Demo() { | ||
const [loading, setLoading] = useState(true); | ||
|
||
return ( | ||
<> | ||
<DSwitch dModel={loading} onModelChange={setLoading}> | ||
Loading | ||
</DSwitch> | ||
<br /> | ||
<br /> | ||
<div style={{ position: 'relative' }}> | ||
<DLoading dVisible={loading}> | ||
<LoadingOutlined dSpin /> | ||
</DLoading> | ||
<DAlert dTitle="I love DevUI so much!" dDescription="Detailed description and advice about DevUI." dType="info"></DAlert> | ||
</div> | ||
</> | ||
); | ||
} | ||
``` |
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,37 @@ | ||
--- | ||
title: | ||
en-US: Delayed display | ||
zh-Hant: 延迟显示 | ||
--- | ||
|
||
# en-US | ||
|
||
Delay display loading to prevent screen flickering. | ||
|
||
# zh-Hant | ||
|
||
延迟显示加载,防止画面闪烁。 | ||
|
||
```tsx | ||
import { useState } from 'react'; | ||
|
||
import { DLoading, DAlert, DSwitch } from '@react-devui/ui'; | ||
|
||
export default function Demo() { | ||
const [loading, setLoading] = useState(false); | ||
|
||
return ( | ||
<> | ||
<DSwitch dModel={loading} onModelChange={setLoading}> | ||
Loading | ||
</DSwitch> | ||
<br /> | ||
<br /> | ||
<div style={{ position: 'relative' }}> | ||
<DLoading dVisible={loading} dDelay={500}></DLoading> | ||
<DAlert dTitle="I love DevUI so much!" dDescription="Detailed description and advice about DevUI." dType="info"></DAlert> | ||
</div> | ||
</> | ||
); | ||
} | ||
``` |
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 @@ | ||
export * from './Loading'; |
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
Oops, something went wrong.