Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | 3385x 179x 3206x 420x 2786x 3273x 569x 2704x 756x 244x 512x 3010x 3010x 3010x 259x 259x 376x 376x 379x 379x 1x 379x 4x 379x 8x 2x 6x 6x | /*
Copyright (c) 2018-2019 Uber Technologies, Inc.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
// @flow
import * as React from 'react';
import {isValidElementType} from 'react-is';
import deepMerge from '../utils/deep-merge.js';
export type StyleOverrideT = {} | (({}) => ?{});
export type OverrideObjectT<T> = {|
component?: ?React.ComponentType<T>,
props?: ?{},
style?: ?StyleOverrideT,
|};
export type OverrideT<T> = OverrideObjectT<T> | React.ComponentType<T>;
export type OverridesT<T> = {
[string]: OverrideT<T>,
};
/**
* Given an override argument, returns the component implementation override if it exists
*/
// eslint-disable-next-line flowtype/no-weak-types
export function getOverride(override: any): any {
if (isValidElementType(override)) {
return override;
}
// Check if override is OverrideObjectT
if (override && typeof override === 'object') {
// Remove this 'any' once this flow issue is fixed:
// https://github.com/facebook/flow/issues/6666
// eslint-disable-next-line flowtype/no-weak-types
return (override: any).component;
}
// null/undefined
return override;
}
/**
* Given an override argument, returns the override props that should be passed
* to the component when rendering it.
*/
export function getOverrideProps<T>(override: ?OverrideT<T>) {
if (override && typeof override === 'object') {
return {
...override.props,
$style: override.style,
};
}
return {};
}
/**
* Coerces an override argument into an override object
* (sometimes it is just an override component)
*/
export function toObjectOverride<T>(
override: OverrideT<T>,
): OverrideObjectT<T> {
if (isValidElementType(override)) {
return {
// eslint-disable-next-line flowtype/no-weak-types
component: ((override: any): React.ComponentType<T>),
};
}
// Flow can't figure out that typeof 'function' above will
// catch React.StatelessFunctionalComponent
// (probably related to https://github.com/facebook/flow/issues/6666)
// eslint-disable-next-line flowtype/no-weak-types
return ((override || {}: any): OverrideObjectT<T>);
}
/**
* Get a convenient override array that will always have [component, props]
*/
/* eslint-disable flowtype/no-weak-types */
export function getOverrides(
override: any,
defaultComponent: React.ComponentType<any>,
): [React.ComponentType<any>, {}] {
const component = getOverride(override) || defaultComponent;
const props = getOverrideProps(override);
return [component, props];
}
/* eslint-enable flowtype/no-weak-types */
/**
* Merges two overrides objects – this is useful if you want to inject your own
* overrides into a child component, but also accept further overrides from
* from upstream. See `mergeOverride` below.
*/
export function mergeOverrides<T>(
target?: OverridesT<T> = {},
source?: OverridesT<T> = {},
): OverridesT<T> {
const allIdentifiers = Object.keys({...target, ...source});
return allIdentifiers.reduce((acc, name) => {
acc[name] = mergeOverride(
toObjectOverride(target[name]),
toObjectOverride(source[name]),
);
return acc;
}, {});
}
/**
* Merges two override objects using the following behavior:
* - Component implementation from the source (parent) replaces target
* - Props and styles are both deep merged
*/
export function mergeOverride<T>(
target: OverrideObjectT<T>,
source: OverrideObjectT<T>,
): OverrideObjectT<T> {
// Shallow merge should handle `component`
const merged = {...target, ...source};
// Props just use deep merge
if (target.props && source.props) {
merged.props = deepMerge({}, target.props, source.props);
}
// Style overrides need special merging since they may be functions
if (target.style && source.style) {
merged.style = mergeStyleOverrides(target.style, source.style);
}
return merged;
}
/**
* Since style overrides can be an object *or* a function, we need to handle
* the case that one of them is a function. We do this by returning a new
* function that deep merges the result of each style override
*/
export function mergeStyleOverrides(
target: StyleOverrideT,
source: StyleOverrideT,
): StyleOverrideT {
// Simple case of both objects
if (typeof target === 'object' && typeof source === 'object') {
return deepMerge({}, target, source);
}
// At least one is a function, return a new composite function
return (...args) => {
return deepMerge(
{},
typeof target === 'function' ? target(...args) : target,
typeof source === 'function' ? source(...args) : source,
);
};
}
|