'use strict';
import React, { PropTypes } from 'react';
import ReactNative, { StyleSheet, View, Platform } from 'react-native';
import ReactElement from 'react/lib/ReactElement';
import LAB from 'lab4';
export default class BlockList extends LAB.Component {
static propTypes = {
separateTop: PropTypes.bool,
separateBottom: PropTypes.bool,
line: PropTypes.object,
blank: PropTypes.object,
};
// static defaultProps = {};
constructor(props, context) {
super(props, context);
}
render() {
const rendered = (
<View
{...this.props}
style={[this.getStyle('container'), this.props.style]}
>
{this._renderChildrenWithSeparate()}
</View>
);
this._separate = undefined;
return rendered;
}
_renderChildrenWithSeparate() {
const arr = [];
if (this.props.separateTop) {
this._renderSeparate(arr, 'top-sp');
}
React.Children.forEach(this.props.children, (element, index) => {
if (element) {
if (element.key == null) {
arr.push(ReactElement.cloneAndReplaceKey(element, 'c-' + index));
} else {
arr.push(element);
}
this._renderSeparate(arr, 'sp-' + index);
}
});
if (!this.props.separateBottom && this._separate) {
arr.pop();
}
return arr;
}
_renderSeparate(arr, key) {
if (this._separate === undefined) {
if (this.props.line) {
this._separate = LAB.render(this.props.line, { key });
arr.push(this._separate);
} else if (this.props.blank) {
this._separate = LAB.render(this.props.blank, { key });
arr.push(this._separate);
} else {
this._separate = null;
}
} else if (this._separate) {
arr.push(ReactElement.cloneAndReplaceKey(this._separate, key));
}
}
}
const styles = StyleSheet.create({
container: {
flexGrow: 1,
flexShrink: 1,
},
});
BlockList.defaultStyles = styles;
import REACT_ELEMENT_TYPE from './ReactElementSymbol'
//è¿æ»¤å±æ§
const RESERVED_PROPS = {
key: true,
ref: true,
__self: true,
__source: true
};
//å½åè°ç¨ç»ä»¶æé
const ReactCurrentOwner = {
current: null
};
//selfåownerï¼refç¨ç产ä¸ç¨ self, source
const ReactElement = function (type, key, ref, self, source, owner, props) {
const element = {
$$typeof: REACT_ELEMENT_TYPE, //æ£æµreact对象ï¼é²XSS
//å
ç´ çå
ç½®å±æ§
type: type,
key: key,
ref: ref,
props: props,
// è®°å½å建æ¤å
ç´ çç»ä»¶ã
_owner: owner
};
return element;
};
//top-level-api createElement æ ¼å¼åJSX对象
ReactElement.createElement = function (type, config, children) {
//åå§å屿§
const props = {};
let propName;
let key = null;
let ref = null;
let self = null;
let source = null;
if (config != null) {
ref = config.ref === undefined ? null : config.ref;
key = config.key === undefined ? '' + null : config.key;
self = config.__self === undefined ? null : config.__self;
source = config.__source === undefined ? null : config.__source;
// å¤çå
¶å®å±æ§
for (propName in config) {
if (hasOwnProperty.call(config, propName) && !RESERVED_PROPS.hasOwnProperty(propName)) {
props[propName] = config[propName];
}
}
}
// å¤çchildren,大äº1个childï¼è½¬æ¢ä¸ºæ°ç»
const childrenLength = arguments.length - 2;
if (childrenLength === 1) {
props.children = children;
} else if (childrenLength > 1) {
const childArray = Array(childrenLength);
for (let i = 0; i < childrenLength; i++) {
childArray[i] = arguments[i + 2];
}
props.children = childArray;
}
// é»è®¤å±æ§
if (type && type.defaultProps) {
const defaultProps = type.defaultProps;
for (propName in defaultProps) {
if (props[propName] === undefined) {
props[propName] = defaultProps[propName];
}
}
}
return ReactElement(type, key, ref, self, source, ReactCurrentOwner.current, props);
};
//top-level-api createFactory æ´é²type屿§
ReactElement.createFactory = function (type) {
const factory = ReactElement.createElement.bind(null, type);
factory.type = type;
return factory;
};
//cloneAndReplaceKey å
鿢key
ReactElement.cloneAndReplaceKey = function (oldElement, newKey) {
const newElement = ReactElement(oldElement.type, newKey, oldElement.ref, oldElement._self, oldElement._source, oldElement._owner, oldElement.props);
return newElement;
};
//top-level-api å
éå
ç´
ReactElement.cloneElement = function (element, config, children) {
// åprops
const props = Object.assign({}, element.props);
let propName;
let key = element.key;
let ref = element.ref;
let self = element._self;
let source = element._source;
let owner = element._owner;
if (config != null) {
ref = config.ref === undefined ? null : config.ref;
key = config.key === undefined ? '' + null : config.key;
// é»è®¤å±æ§
let defaultProps;
if (element.type && element.type.defaultProps) {
defaultProps = element.type.defaultProps;
}
for (propName in config) {
if (hasOwnProperty.call(config, propName) && !RESERVED_PROPS.hasOwnProperty(propName)) {
if (config[propName] === undefined && defaultProps !== undefined) {
props[propName] = defaultProps[propName];
} else {
props[propName] = config[propName];
}
}
}
}
const childrenLength = arguments.length - 2;
if (childrenLength === 1) {
props.children = children;
} else if (childrenLength > 1) {
const childArray = Array(childrenLength);
for (let i = 0; i < childrenLength; i++) {
childArray[i] = arguments[i + 2];
}
props.children = childArray;
}
return ReactElement(element.type, key, ref, self, source, owner, props);
};
//top-level-api æ£æµå
ç´ æ¯å¦æ¯reactå
ç´
ReactElement.isValidElement = function (object) {
return typeof object === 'object' && object !== null && object.$$typeof === REACT_ELEMENT_TYPE;
};
export default ReactElement;
import { isValidElement, cloneAndReplaceKey } from './ReactElement';
import {
getIteratorFn,
REACT_ELEMENT_TYPE,
REACT_PORTAL_TYPE,
} from '../../ReactSymbols';
const SEPARATOR = '.';
const SUBSEPARATOR = ':';
const escape = (key) => {
const escapeRegex = /[=:]/g;
const escaperLookup = {
'=': '=0',
':': '=2',
};
const escapedString = key.replace(
escapeRegex,
(match) => escaperLookup[match]
);
return '$' + escapedString;
};
const escapeUserProvidedKey = (text) => text.replace(/\/+/g, '$&/');
const getElementKey = (element, index) => {
if (typeof element === 'object' && element !== null && element.key != null)
return escape('' + element.key);
return index.toString(36);
};
const mapIntoArray = (children, array, escapedPrefix, nameSoFar, callback) => {
const type = typeof children;
if (type === 'undefined' || type === 'boolean') children = null;
let invokeCallback = false;
if (children === null) {
invokeCallback = true;
} else {
switch (type) {
case 'string':
case 'number':
invokeCallback = true;
break;
case 'object':
switch (children.$$typeof) {
case REACT_ELEMENT_TYPE:
case REACT_PORTAL_TYPE:
invokeCallback = true;
}
}
}
if (invokeCallback) {
const child = children;
let mappedChild = callback(child);
const childKey =
nameSoFar === '' ? SEPARATOR + getElementKey(child, 0) : nameSoFar;
if (Array.isArray(mappedChild)) {
let escapedChildKey = '';
if (childKey != null)
escapedChildKey = escapeUserProvidedKey(childKey) + '/';
mapIntoArray(mappedChild, array, escapedChildKey, '', (c) => c);
} else if (mappedChild !== null) {
if (isValidElement(mappedChild)) {
mappedChild = cloneAndReplaceKey(
mappedChild,
escapedPrefix +
(mappedChild.key && (!child || child.key !== mappedChild.key)
? escapeUserProvidedKey('' + mappedChild.key) + '/'
: '') +
childKey
);
}
array.push(mappedChild);
}
return 1;
}
let child;
let nextName;
let subtreeCount = 0;
const nextNamePrefix =
nameSoFar === '' ? SEPARATOR : nameSoFar + SUBSEPARATOR;
if (Array.isArray(children))
children.forEach((child, i) => {
nextName = nextNamePrefix + getElementKey(child, i);
subtreeCount += mapIntoArray(
child,
array,
escapedPrefix,
nextName,
callback
);
});
else {
const iteratorFn = getIteratorFn(children);
if (typeof iteratorFn === 'function') {
const iterableChildren = children;
const iterator = iteratorFn.call(iterableChildren);
let step;
let ii = 0;
while (!(step = iterator.next()).done) {
child = step.value;
nextName = nextNamePrefix + getElementKey(child, ii++);
subtreeCount += mapIntoArray(
child,
array,
escapedPrefix,
nextName,
callback
);
}
} else if (type === 'object') {
const childrenString = String(children);
throw new Error(
`Objects are not valid as a React child ${childrenString}`
);
}
}
return subtreeCount;
};
function mapChildren(children, func, context) {
if (children === null) return children;
const result = [];
let count = 0;
mapIntoArray(children, result, '', '', function (child) {
return func.call(context, child, count++);
});
return result;
}
function forEachChildren(children, forEachFunc, forEachContext) {
mapChildren(
children,
function (...args) {
forEachFunc.apply(this, args);
},
forEachContext
);
}
const countChildren = (children) => {
let n = 0;
mapChildren(children, () => n++);
return n;
};
const toArray = (children) => mapChildren(children, (child) => child) || [];
const onlyChild = (children) => {
if (!isValidElement(children)) {
throw new Error(
'React.Children.only expected to receive a single React element child.'
);
}
return children;
};
export {
forEachChildren as forEach,
mapChildren as map,
countChildren as count,
onlyChild as only,
toArray,
};
Accelerate Your Automation Test Cycles With LambdaTest
Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.