-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2260 from XiaoMi/feature/spin-loading
feat(spinner&loading): 增加环形loading组件
- Loading branch information
Showing
12 changed files
with
183 additions
and
63 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { useCallback, useEffect, useRef, useState } from 'react' | ||
import { debounce } from '@hi-ui/func-utils' | ||
import type { DebounceReturn } from '@hi-ui/func-utils' | ||
|
||
export const useLoading = ({ visible = true, delay = -1 }: UseLoadingProps) => { | ||
const [internalVisible, setInternalVisible] = useState(false) | ||
|
||
// Real trigger loading update | ||
const updateLoadingStatus = useCallback(() => { | ||
if (internalVisible === visible) return | ||
setInternalVisible(visible) | ||
}, [internalVisible, visible]) | ||
|
||
const prevDebouncedUpdateRef = useRef<null | DebounceReturn>(null) | ||
|
||
const cancelWaitingLoading = () => { | ||
prevDebouncedUpdateRef.current?.cancel() | ||
} | ||
|
||
const shouldDelay = visible && delay >= 0 | ||
|
||
const debouncedLoadingUpdater = useCallback(() => { | ||
cancelWaitingLoading() | ||
|
||
if (shouldDelay) { | ||
const debouncedUpdateLoading = debounce(updateLoadingStatus, delay) | ||
prevDebouncedUpdateRef.current = debouncedUpdateLoading | ||
|
||
debouncedUpdateLoading() | ||
} else { | ||
updateLoadingStatus() | ||
prevDebouncedUpdateRef.current = null | ||
} | ||
}, [delay, shouldDelay, updateLoadingStatus]) | ||
|
||
useEffect(() => { | ||
debouncedLoadingUpdater() | ||
|
||
return () => { | ||
cancelWaitingLoading() | ||
} | ||
}, [debouncedLoadingUpdater]) | ||
|
||
return { | ||
internalVisible, | ||
setInternalVisible, | ||
} | ||
} | ||
|
||
export interface UseLoadingProps { | ||
visible?: boolean | ||
delay?: number | ||
} | ||
|
||
export type useInputReturn = ReturnType<typeof useLoading> |
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,30 @@ | ||
import React from 'react' | ||
import Loading from '../src' | ||
|
||
/** | ||
* @title 设置加载指示符类型 | ||
*/ | ||
export const Type = () => { | ||
return ( | ||
<> | ||
<h1>Type</h1> | ||
<div | ||
className="loading-basic__wrap" | ||
style={{ position: 'relative', width: 500, height: 300 }} | ||
> | ||
<Loading type="spin"> | ||
<div | ||
style={{ | ||
width: 500, | ||
height: 300, | ||
boxSizing: 'border-box', | ||
background: '#f5f7fa', | ||
padding: 20, | ||
border: '20px solid #5f6a7a', | ||
}} | ||
/> | ||
</Loading> | ||
</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
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 |
---|---|---|
@@ -1,8 +1,11 @@ | ||
import React from 'react' | ||
import Spinner from '../src' | ||
|
||
export * from './basic.stories' | ||
export * from './size.stories' | ||
|
||
export default { | ||
title: 'Private(暂不对外)/Spinner', | ||
component: Spinner, | ||
decorators: [(story: Function) => <div>{story()}</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,21 @@ | ||
import React from 'react' | ||
import Space from '@hi-ui/space' | ||
import Spinner from '../src' | ||
|
||
/** | ||
* @title 设置大小 | ||
*/ | ||
export const Size = () => { | ||
return ( | ||
<> | ||
<h1>Size</h1> | ||
<div className="spinner-size__wrap"> | ||
<Space size={20}> | ||
<Spinner size={12} /> | ||
<Spinner size={24} /> | ||
<Spinner size="lg" /> | ||
</Space> | ||
</div> | ||
</> | ||
) | ||
} |