generated from react-component/footer
-
Notifications
You must be signed in to change notification settings - Fork 26
feat: add position props #257
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
Closed
Closed
Changes from 5 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
c72f523
feat: add position props
972ebc5
feat: del console
a5e4c62
feat: 优化代码
624c6b9
feat: 增加单测
9541ca1
feat: 还原注释
9bdb1ec
feat: 增加注释
6eb0782
feat: unit test
f32da7e
feat: add unit test
624e2c5
feat: add unit test
c51cf9c
feat: add unit test
46268a3
feat: del unit
b282eb7
fix: 优化三元表达式
57cc233
fix: 先去掉增加的单测
8106d4c
feat: unit test
3341781
feat: 三元 to if
4d55691
feat: add unit test
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
This file contains hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -9,6 +9,9 @@ | |
left: number; | ||
right: number; | ||
width: number; | ||
top: number; | ||
bottom: number; | ||
height: number; | ||
} | null; | ||
|
||
export interface MotionThumbInterface { | ||
|
@@ -20,23 +23,52 @@ | |
onMotionStart: VoidFunction; | ||
onMotionEnd: VoidFunction; | ||
direction?: 'ltr' | 'rtl'; | ||
vertical?: boolean; | ||
} | ||
|
||
const calcThumbStyle = ( | ||
targetElement: HTMLElement | null | undefined, | ||
): ThumbReact => | ||
targetElement | ||
? { | ||
left: targetElement.offsetLeft, | ||
right: | ||
(targetElement.parentElement!.clientWidth as number) - | ||
targetElement.clientWidth - | ||
targetElement.offsetLeft, | ||
width: targetElement.clientWidth, | ||
} | ||
: null; | ||
vertical?: boolean, | ||
): ThumbReact => { | ||
if (!targetElement) return null; | ||
|
||
const style: ThumbReact = { | ||
left: targetElement.offsetLeft, | ||
right: | ||
(targetElement.parentElement!.clientWidth as number) - | ||
targetElement.clientWidth - | ||
targetElement.offsetLeft, | ||
width: targetElement.clientWidth, | ||
top: targetElement.offsetTop, | ||
bottom: | ||
(targetElement.parentElement!.clientHeight as number) - | ||
targetElement.clientHeight - | ||
targetElement.offsetTop, | ||
height: targetElement.clientHeight, | ||
}; | ||
|
||
if (vertical) { | ||
return { | ||
left: 0, | ||
right: 0, | ||
width: 0, | ||
top: style.top, | ||
bottom: style.bottom, | ||
height: style.height, | ||
}; | ||
} | ||
|
||
const toPX = (value: number) => | ||
return { | ||
left: style.left, | ||
right: style.right, | ||
width: style.width, | ||
top: 0, | ||
bottom: 0, | ||
height: 0, | ||
}; | ||
}; | ||
|
||
const toPX = (value: number | undefined): string | undefined => | ||
value !== undefined ? `${value}px` : undefined; | ||
|
||
export default function MotionThumb(props: MotionThumbInterface) { | ||
|
@@ -49,6 +81,7 @@ | |
onMotionStart, | ||
onMotionEnd, | ||
direction, | ||
vertical = false, | ||
} = props; | ||
|
||
const thumbRef = React.useRef<HTMLDivElement>(null); | ||
|
@@ -57,11 +90,9 @@ | |
// =========================== Effect =========================== | ||
const findValueElement = (val: SegmentedValue) => { | ||
const index = getValueIndex(val); | ||
|
||
const ele = containerRef.current?.querySelectorAll<HTMLDivElement>( | ||
`.${prefixCls}-item`, | ||
)[index]; | ||
|
||
return ele?.offsetParent && ele; | ||
}; | ||
|
||
|
@@ -73,8 +104,8 @@ | |
const prev = findValueElement(prevValue); | ||
const next = findValueElement(value); | ||
|
||
const calcPrevStyle = calcThumbStyle(prev); | ||
const calcNextStyle = calcThumbStyle(next); | ||
const calcPrevStyle = calcThumbStyle(prev, vertical); | ||
const calcNextStyle = calcThumbStyle(next, vertical); | ||
|
||
setPrevValue(value); | ||
setPrevStyle(calcPrevStyle); | ||
|
@@ -90,32 +121,47 @@ | |
|
||
const thumbStart = React.useMemo( | ||
() => | ||
direction === 'rtl' | ||
vertical | ||
? toPX(prevStyle?.top ?? 0) | ||
: direction === 'rtl' | ||
? toPX(-(prevStyle?.right as number)) | ||
: toPX(prevStyle?.left as number), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 内嵌的三元表达式不是很推荐。 |
||
[direction, prevStyle], | ||
[vertical, direction, prevStyle], | ||
); | ||
|
||
const thumbActive = React.useMemo( | ||
() => | ||
direction === 'rtl' | ||
vertical | ||
? toPX(nextStyle?.top ?? 0) | ||
: direction === 'rtl' | ||
? toPX(-(nextStyle?.right as number)) | ||
: toPX(nextStyle?.left as number), | ||
[direction, nextStyle], | ||
[vertical, direction, nextStyle], | ||
); | ||
|
||
// =========================== Motion =========================== | ||
const onAppearStart = () => { | ||
return { | ||
transform: `translateX(var(--thumb-start-left))`, | ||
width: `var(--thumb-start-width)`, | ||
}; | ||
}; | ||
const onAppearActive = () => { | ||
return { | ||
transform: `translateX(var(--thumb-active-left))`, | ||
width: `var(--thumb-active-width)`, | ||
}; | ||
}; | ||
const onAppearStart = () => | ||
vertical | ||
? { | ||
transform: 'translateY(var(--thumb-start-top))', | ||
height: 'var(--thumb-start-height)', | ||
} | ||
: { | ||
transform: 'translateX(var(--thumb-start-left))', | ||
width: 'var(--thumb-start-width)', | ||
}; | ||
|
||
const onAppearActive = () => | ||
vertical | ||
? { | ||
transform: 'translateY(var(--thumb-active-top))', | ||
height: 'var(--thumb-active-height)', | ||
} | ||
: { | ||
transform: 'translateX(var(--thumb-active-left))', | ||
width: 'var(--thumb-active-width)', | ||
}; | ||
|
||
const onVisibleChanged = () => { | ||
setPrevStyle(null); | ||
setNextStyle(null); | ||
|
@@ -144,6 +190,10 @@ | |
'--thumb-start-width': toPX(prevStyle?.width), | ||
'--thumb-active-left': thumbActive, | ||
'--thumb-active-width': toPX(nextStyle?.width), | ||
'--thumb-start-top': thumbStart, | ||
'--thumb-start-height': toPX(prevStyle?.height), | ||
'--thumb-active-top': thumbActive, | ||
'--thumb-active-height': toPX(nextStyle?.height), | ||
} as React.CSSProperties; | ||
|
||
// It's little ugly which should be refactor when @umi/test update to latest jsdom | ||
|
This file contains hidden or 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.
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.
Uh oh!
There was an error while loading. Please reload this page.