mirror of
https://github.com/reduxjs/redux-devtools.git
synced 2025-07-22 14:09:46 +03:00
Remove UNSAFE method from JSONTree
This commit is contained in:
parent
6436116048
commit
597b1d4d6c
|
@ -3,17 +3,11 @@
|
|||
// Dave Vedder <veddermatic@gmail.com> http://www.eskimospy.com/
|
||||
// port by Daniele Zannotti http://www.github.com/dzannotti <dzannotti@me.com>
|
||||
|
||||
import React from 'react';
|
||||
import PropTypes from 'prop-types';
|
||||
import React, { useMemo } from 'react';
|
||||
import JSONNode from './JSONNode';
|
||||
import createStylingFromTheme from './createStylingFromTheme';
|
||||
import { invertTheme } from 'react-base16-styling';
|
||||
import type {
|
||||
StylingConfig,
|
||||
StylingFunction,
|
||||
StylingValue,
|
||||
Theme,
|
||||
} from 'react-base16-styling';
|
||||
import type { StylingValue, Theme } from 'react-base16-styling';
|
||||
import { CircularPropsPassedThroughJSONTree } from './types';
|
||||
|
||||
interface Props extends CircularPropsPassedThroughJSONTree {
|
||||
|
@ -22,10 +16,6 @@ interface Props extends CircularPropsPassedThroughJSONTree {
|
|||
invertTheme: boolean;
|
||||
}
|
||||
|
||||
interface State {
|
||||
styling: StylingFunction;
|
||||
}
|
||||
|
||||
const identity = (value: any) => value;
|
||||
const expandRootNode = (
|
||||
keyPath: (string | number)[],
|
||||
|
@ -47,133 +37,45 @@ const defaultLabelRenderer = ([label]: (string | number)[]) => (
|
|||
);
|
||||
const noCustomNode = () => false;
|
||||
|
||||
function checkLegacyTheming(theme: Theme | undefined, props: Props) {
|
||||
const deprecatedStylingMethodsMap = {
|
||||
getArrowStyle: 'arrow',
|
||||
getListStyle: 'nestedNodeChildren',
|
||||
getItemStringStyle: 'nestedNodeItemString',
|
||||
getLabelStyle: 'label',
|
||||
getValueStyle: 'valueText',
|
||||
};
|
||||
export function JSONTree({
|
||||
data: value,
|
||||
theme,
|
||||
invertTheme: shouldInvertTheme,
|
||||
keyPath = ['root'],
|
||||
labelRenderer = defaultLabelRenderer,
|
||||
valueRenderer = identity,
|
||||
shouldExpandNode = expandRootNode,
|
||||
hideRoot = false,
|
||||
getItemString = defaultItemString,
|
||||
postprocessValue = identity,
|
||||
isCustomNode = noCustomNode,
|
||||
collectionLimit = 50,
|
||||
sortObjectKeys,
|
||||
}: Props) {
|
||||
const styling = useMemo(
|
||||
() =>
|
||||
createStylingFromTheme(shouldInvertTheme ? invertTheme(theme) : theme),
|
||||
[theme, shouldInvertTheme]
|
||||
);
|
||||
|
||||
const deprecatedStylingMethods = Object.keys(
|
||||
deprecatedStylingMethodsMap
|
||||
).filter((name) => props[name as keyof Props]);
|
||||
|
||||
if (deprecatedStylingMethods.length > 0) {
|
||||
if (typeof theme === 'string') {
|
||||
theme = {
|
||||
extend: theme,
|
||||
};
|
||||
} else {
|
||||
theme = { ...theme };
|
||||
}
|
||||
|
||||
deprecatedStylingMethods.forEach((name) => {
|
||||
// eslint-disable-next-line no-console
|
||||
console.error(
|
||||
`Styling method "${name}" is deprecated, use "theme" property instead`
|
||||
);
|
||||
|
||||
(theme as StylingConfig)[
|
||||
deprecatedStylingMethodsMap[
|
||||
name as keyof typeof deprecatedStylingMethodsMap
|
||||
]
|
||||
] = ({ style }, ...args) => ({
|
||||
style: {
|
||||
...style,
|
||||
...props[name as keyof Props](...args),
|
||||
},
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
return theme;
|
||||
}
|
||||
|
||||
function getStateFromProps(props: Props) {
|
||||
let theme = checkLegacyTheming(props.theme, props);
|
||||
if (props.invertTheme) {
|
||||
theme = invertTheme(theme);
|
||||
}
|
||||
|
||||
return {
|
||||
styling: createStylingFromTheme(theme),
|
||||
};
|
||||
}
|
||||
|
||||
export class JSONTree extends React.Component<Props, State> {
|
||||
static propTypes = {
|
||||
data: PropTypes.any,
|
||||
hideRoot: PropTypes.bool,
|
||||
theme: PropTypes.oneOfType([PropTypes.object, PropTypes.string]),
|
||||
invertTheme: PropTypes.bool,
|
||||
keyPath: PropTypes.arrayOf(
|
||||
PropTypes.oneOfType([PropTypes.string, PropTypes.number])
|
||||
),
|
||||
postprocessValue: PropTypes.func,
|
||||
sortObjectKeys: PropTypes.oneOfType([PropTypes.func, PropTypes.bool]),
|
||||
};
|
||||
|
||||
static defaultProps = {
|
||||
shouldExpandNode: expandRootNode,
|
||||
hideRoot: false,
|
||||
keyPath: ['root'],
|
||||
getItemString: defaultItemString,
|
||||
labelRenderer: defaultLabelRenderer,
|
||||
valueRenderer: identity,
|
||||
postprocessValue: identity,
|
||||
isCustomNode: noCustomNode,
|
||||
collectionLimit: 50,
|
||||
invertTheme: true,
|
||||
};
|
||||
|
||||
constructor(props: Props) {
|
||||
super(props);
|
||||
this.state = getStateFromProps(props);
|
||||
}
|
||||
|
||||
UNSAFE_componentWillReceiveProps(nextProps: Props) {
|
||||
if (
|
||||
['theme', 'invertTheme'].find(
|
||||
(k) => nextProps[k as keyof Props] !== this.props[k as keyof Props]
|
||||
)
|
||||
) {
|
||||
this.setState(getStateFromProps(nextProps));
|
||||
}
|
||||
}
|
||||
|
||||
shouldComponentUpdate(nextProps: Props) {
|
||||
return !!Object.keys(nextProps).find((k) =>
|
||||
k === 'keyPath'
|
||||
? nextProps[k].join('/') !== this.props[k].join('/')
|
||||
: nextProps[k as keyof Props] !== this.props[k as keyof Props]
|
||||
);
|
||||
}
|
||||
|
||||
render() {
|
||||
const {
|
||||
data: value,
|
||||
keyPath,
|
||||
postprocessValue,
|
||||
hideRoot,
|
||||
theme, // eslint-disable-line no-unused-vars
|
||||
invertTheme: _, // eslint-disable-line no-unused-vars
|
||||
...rest
|
||||
} = this.props;
|
||||
|
||||
const { styling } = this.state;
|
||||
|
||||
return (
|
||||
<ul {...styling('tree')}>
|
||||
<JSONNode
|
||||
{...{ postprocessValue, hideRoot, styling, ...rest }}
|
||||
keyPath={hideRoot ? [] : keyPath}
|
||||
value={postprocessValue(value)}
|
||||
/>
|
||||
</ul>
|
||||
);
|
||||
}
|
||||
return (
|
||||
<ul {...styling('tree')}>
|
||||
<JSONNode
|
||||
keyPath={hideRoot ? [] : keyPath}
|
||||
value={postprocessValue(value)}
|
||||
isCustomNode={isCustomNode}
|
||||
styling={styling}
|
||||
labelRenderer={labelRenderer}
|
||||
valueRenderer={valueRenderer}
|
||||
shouldExpandNode={shouldExpandNode}
|
||||
hideRoot={hideRoot}
|
||||
getItemString={getItemString}
|
||||
postprocessValue={postprocessValue}
|
||||
collectionLimit={collectionLimit}
|
||||
sortObjectKeys={sortObjectKeys}
|
||||
/>
|
||||
</ul>
|
||||
);
|
||||
}
|
||||
|
||||
export { StylingValue };
|
||||
|
|
Loading…
Reference in New Issue
Block a user