-
Notifications
You must be signed in to change notification settings - Fork 957
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* feat(ui): popover components * feat(playground): popover component demos * test(ui): popover model tests * fix(playground): bundler and compiler issues * refactor(ui): popover redundant cloneElement * refactor(ui): popover content as prop * refactor(ui): document popover component * feat(ui): popover - add ability to pass placement as object * feat(ui): popover - apply mappings
- Loading branch information
Showing
16 changed files
with
1,857 additions
and
316 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,35 @@ | ||
import React from 'react'; | ||
import { | ||
View, | ||
ViewProps, | ||
} from 'react-native'; | ||
import { StyleType } from '@kitten/theme'; | ||
|
||
export type Props = ViewProps; | ||
|
||
export class Arrow extends React.Component<Props> { | ||
|
||
private getComponentStyle = (source: StyleType): StyleType => { | ||
return { | ||
borderLeftWidth: source.width, | ||
borderRightWidth: source.width, | ||
borderBottomWidth: source.height, | ||
borderBottomColor: source.backgroundColor, | ||
borderLeftColor: 'transparent', | ||
borderRightColor: 'transparent', | ||
backgroundColor: 'transparent', | ||
}; | ||
}; | ||
|
||
public render(): React.ReactElement<ViewProps> { | ||
const { style, ...props } = this.props; | ||
const componentStyle = this.getComponentStyle(style); | ||
|
||
return ( | ||
<View | ||
{...props} | ||
style={[style, componentStyle]} | ||
/> | ||
); | ||
} | ||
} |
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,140 @@ | ||
import React from 'react'; | ||
import { | ||
findNodeHandle, | ||
UIManager, | ||
View, | ||
ViewProps, | ||
Dimensions, | ||
ScaledSize, | ||
} from 'react-native'; | ||
import { Frame } from './type'; | ||
|
||
export type MeasuringElement = React.ReactElement<MeasuringElementProps>; | ||
export type MeasuringElementProps = { tag: string | number } & any; | ||
export type MeasuringNode = React.ReactElement<MeasureNodeProps>; | ||
export type MeasuringNodeProps = MeasureNodeProps & ViewProps; | ||
|
||
export type MeasuredElementProps = MeasuringElementProps & { frame: Frame }; | ||
export type MeasuredElement = React.ReactElement<MeasuredElementProps>; | ||
|
||
export interface MeasureResult { | ||
[tag: string]: Frame; | ||
} | ||
|
||
export interface MeasureElementProps { | ||
onResult: (result: MeasuredElement) => void; | ||
children: MeasuringElement; | ||
} | ||
|
||
export interface MeasureNodeProps { | ||
onResult: (result: MeasureResult) => void; | ||
children: MeasuringElement[]; | ||
} | ||
|
||
/** | ||
* Measures child element size and it's screen position asynchronously. | ||
* Returns measure result in `onResult` callback. | ||
* | ||
* Usage: | ||
* | ||
* const onMeasure = (element: ElementToMeasure): void => { | ||
* const { x, y, width, height } = element.props.frame; | ||
* ... | ||
* }; | ||
* | ||
* <MeasureElement onResult={this.onMeasure}> | ||
* <ElementToMeasure tag='@measure/measure-me!'/> | ||
* </MeasureElement> | ||
*/ | ||
export const MeasureElement = (props: MeasureElementProps): MeasuringElement => { | ||
|
||
const ref: React.RefObject<any> = React.createRef(); | ||
|
||
const { children, onResult } = props; | ||
|
||
const bindToWindow = (frame: Frame, window: ScaledSize): Frame => { | ||
if (frame.origin.x < window.width) { | ||
return frame; | ||
} | ||
|
||
const boundFrame: Frame = new Frame( | ||
frame.origin.x - window.width, | ||
frame.origin.y, | ||
frame.size.width, | ||
frame.size.height, | ||
); | ||
|
||
return bindToWindow(boundFrame, window); | ||
}; | ||
|
||
const measureSelf = () => { | ||
const node: number = findNodeHandle(ref.current); | ||
|
||
UIManager.measureInWindow(node, (x: number, y: number, w: number, h: number) => { | ||
const window: ScaledSize = Dimensions.get('window'); | ||
const frame: Frame = bindToWindow(new Frame(x, y, w, h), window); | ||
|
||
const measuredElement: MeasuredElement = React.cloneElement(children, { frame }); | ||
|
||
onResult(measuredElement); | ||
}); | ||
}; | ||
|
||
return React.cloneElement(children, { | ||
ref, | ||
onLayout: measureSelf, | ||
}); | ||
}; | ||
|
||
/** | ||
* Measures passed child elements size and it's screen position asynchronously. | ||
* Returns measure result in `onResult` callback. | ||
* | ||
* Does the same as `MeasureElement` but calls `onResult` after all children are measured. | ||
* | ||
* Usage: | ||
* | ||
* const onMeasure = (result: MeasureResult): void => { | ||
* const { [0]: firstElementFrame, [1]: secondElementFrame } = result; | ||
* const { x, y, width, height } = firstElementFrame; | ||
* ... | ||
* }; | ||
* | ||
* <MeasureNode onResult={this.onMeasure}> | ||
* <ElementToMeasure tag={0}/> | ||
* <ElementToMeasure tag={1}/> | ||
* </MeasureNode> | ||
*/ | ||
export const MeasureNode = (props: MeasuringNodeProps): MeasuringNode => { | ||
|
||
const result: MeasureResult = {}; | ||
|
||
const { children, onResult, ...derivedProps } = props; | ||
|
||
const onChildMeasure = (element: MeasuredElement) => { | ||
const { tag, frame } = element.props; | ||
|
||
result[tag] = frame; | ||
|
||
if (Object.keys(result).length === children.length) { | ||
onResult(result); | ||
} | ||
}; | ||
|
||
const createMeasuringElement = (element: MeasuringElement, index: number): MeasuringElement => { | ||
return ( | ||
<MeasureElement | ||
key={index} | ||
onResult={onChildMeasure}> | ||
{element} | ||
</MeasureElement> | ||
); | ||
}; | ||
|
||
return ( | ||
<View | ||
{...derivedProps}> | ||
{children.map(createMeasuringElement)} | ||
</View> | ||
); | ||
}; |
Oops, something went wrong.