-
Notifications
You must be signed in to change notification settings - Fork 19
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
13 changed files
with
229 additions
and
98 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
// tslint:disable no-any | ||
import {Component} from 'react'; | ||
import * as React from 'react'; | ||
import {IBaseComponent} from '../template/component'; | ||
import { RenderInRootDom } from '../utils/'; | ||
import Helpers from '../Helpers'; | ||
import Transitions from '../Transitions'; | ||
|
||
const { Scale } = Transitions; | ||
const { ClickOutside } = Helpers; | ||
|
||
export interface IPopOverProps extends IBaseComponent { | ||
/** | ||
* 面板是否展开 | ||
*/ | ||
open: boolean; | ||
/** | ||
* 面板展开回调 | ||
*/ | ||
onOpenChange?: (v: boolean) => void; | ||
/** | ||
* 浮层内容 | ||
*/ | ||
pop: any; | ||
/** | ||
* 进入后 | ||
*/ | ||
onEntered?: () => void; | ||
} | ||
|
||
export interface IPopOverState { | ||
popPos: { | ||
left: number; | ||
top: number; | ||
}; | ||
} | ||
|
||
/** | ||
* **组件中文名称**-组件描述。 | ||
*/ | ||
export class PopOver extends Component<IPopOverProps, IPopOverState> { | ||
preCls = 'yoshino-pop-over'; | ||
refPopOverContainer: Element; | ||
|
||
static defaultProps = { | ||
}; | ||
|
||
state = { | ||
popPos: { | ||
left: 0, | ||
top: 0, | ||
}, | ||
}; | ||
|
||
setPopOverPosition = () => { | ||
if (!this.refPopOverContainer) { | ||
return; | ||
} | ||
const rect = this.refPopOverContainer.getBoundingClientRect(); | ||
const pageY = window.pageYOffset; // 当前滚动条y轴偏移量 | ||
const pageX = window.pageXOffset; // 当前滚动条x轴偏移量 | ||
this.setState({ | ||
popPos: { | ||
left: rect.left + pageX, | ||
top: rect.top + pageY, | ||
} | ||
}); | ||
} | ||
|
||
onOpenChange = (open: boolean) => { | ||
const { onOpenChange } = this.props; | ||
if (onOpenChange) { | ||
onOpenChange(open); | ||
} | ||
// 调整面板位置 | ||
this.setPopOverPosition(); | ||
} | ||
|
||
render() { | ||
const { | ||
children, pop, open, | ||
onEntered, | ||
} = this.props; | ||
const preCls = this.preCls; | ||
const child = React.Children.only(children); | ||
return ( | ||
<React.Fragment> | ||
{ | ||
React.cloneElement(child, { | ||
onClick: () => { | ||
if (child.props.onClick) { | ||
child.props.onClick(); | ||
} | ||
this.onOpenChange(true); | ||
}, | ||
ref: (v: any) => { | ||
if (child.props.ref) { | ||
child.props.ref(v); | ||
} | ||
if (v) { | ||
this.refPopOverContainer = v; | ||
} | ||
} | ||
}) | ||
} | ||
<RenderInRootDom> | ||
<ClickOutside | ||
clickOutside={() => { | ||
this.onOpenChange(false); | ||
}} | ||
> | ||
<div className={`${preCls}-box`} style={this.state.popPos}> | ||
<Scale | ||
timeout={300} | ||
active={open} | ||
onEntered={() => { | ||
if (onEntered) { | ||
onEntered(); | ||
} | ||
}} | ||
> | ||
{pop} | ||
</Scale> | ||
</div> | ||
</ClickOutside> | ||
</RenderInRootDom> | ||
</React.Fragment> | ||
); | ||
} | ||
} | ||
|
||
export default PopOver; |
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,12 @@ | ||
import * as React from 'react'; | ||
import {Simulate, renderIntoDocument, scryRenderedComponentsWithType} from 'react-dom/test-utils'; | ||
import {findDOMNode} from 'react-dom'; | ||
import PopOver from '../index'; | ||
|
||
describe('多选', () => { | ||
test('点击其中一个选项后可通过 onChange 拿到最新的值', () => { | ||
const component = renderIntoDocument( | ||
<PopOver/>, | ||
) as PopOver; | ||
}); | ||
}); |
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,14 @@ | ||
import * as renderer from 'react-test-renderer'; | ||
import * as React from 'react'; | ||
import PopOver from '../index'; | ||
|
||
describe('Props', () => { | ||
|
||
test('默认', () => { | ||
const component = renderer.create( | ||
<PopOver/>, | ||
); | ||
const tree = component.toJSON(); | ||
expect(tree).toMatchSnapshot(); | ||
}); | ||
}); |
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,3 @@ | ||
import PopOver from './PopOver'; | ||
|
||
export default PopOver; |
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,3 @@ | ||
import '../../Transitions/style/'; | ||
import '../../Helpers/style/'; | ||
import './index.css'; |
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,9 @@ | ||
@import './var.less'; | ||
|
||
@popOver-prefix-cls: ~"@{css-prefix}-pop-over"; | ||
|
||
.@{popOver-prefix-cls} { | ||
&-box { | ||
position: absolute; | ||
} | ||
} |
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,3 @@ | ||
import '../../Transitions/style/less.js'; | ||
import '../../Helpers/style/less.js'; | ||
import './index.less' |
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 @@ | ||
@import '../../styles/var.less'; |
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,5 +1,4 @@ | ||
import '../../Icon/style/'; | ||
import '../../Input/style/'; | ||
import '../../Transitions/style/'; | ||
import '../../Helpers/style/'; | ||
import '../../PopOver/style/'; | ||
import './index.css'; |
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,5 +1,4 @@ | ||
import '../../Icon/style/less.js'; | ||
import '../../Input/style/less.js'; | ||
import '../../Transitions/style/less.js'; | ||
import '../../Helpers/style/less.js'; | ||
import '../../PopOver/style/less.js'; | ||
import './index.less'; |
Oops, something went wrong.