refactor: replace defaultProps with JavaScript default parameters (#9464)

This change in specific to React components and
React@18 version.

Refs #9456
This commit is contained in:
Vladimír Gorej
2024-01-08 10:45:09 +01:00
committed by GitHub
parent 15fb960658
commit 252c81ae8e
3 changed files with 50 additions and 67 deletions

View File

@@ -1,21 +1,24 @@
/**
* @prettier
*/
import React from "react" import React from "react"
import PropTypes from "prop-types" import PropTypes from "prop-types"
export default class App extends React.Component { class App extends React.Component {
getLayout() { getLayout() {
let { getComponent, layoutSelectors } = this.props const { getComponent, layoutSelectors } = this.props
const layoutName = layoutSelectors.current() const layoutName = layoutSelectors.current()
const Component = getComponent(layoutName, true) const Component = getComponent(layoutName, true)
return Component ? Component : ()=> <h1> No layout defined for &quot;{layoutName}&quot; </h1>
return Component
? Component
: () => <h1> No layout defined for &quot;{layoutName}&quot; </h1>
} }
render() { render() {
const Layout = this.getLayout() const Layout = this.getLayout()
return ( return <Layout />
<Layout/>
)
} }
} }
@@ -24,5 +27,4 @@ App.propTypes = {
layoutSelectors: PropTypes.object.isRequired, layoutSelectors: PropTypes.object.isRequired,
} }
App.defaultProps = { export default App
}

View File

@@ -20,54 +20,33 @@ const getModelName = (uri) => {
return null return null
} }
const Model = forwardRef(({ schema, getComponent, onToggle }, ref) => { const Model = forwardRef(
const JSONSchema202012 = getComponent("JSONSchema202012") ({ schema, getComponent, onToggle = () => {} }, ref) => {
const name = getModelName(schema.get("$$ref")) const JSONSchema202012 = getComponent("JSONSchema202012")
const name = getModelName(schema.get("$$ref"))
const handleExpand = useCallback( const handleExpand = useCallback(
(e, expanded) => { (e, expanded) => {
onToggle(name, expanded) onToggle(name, expanded)
}, },
[name, onToggle] [name, onToggle]
) )
return ( return (
<JSONSchema202012 <JSONSchema202012
name={name} name={name}
schema={schema.toJS()} schema={schema.toJS()}
ref={ref} ref={ref}
onExpand={handleExpand} onExpand={handleExpand}
/> />
) )
}) }
)
Model.propTypes = { Model.propTypes = {
schema: ImPropTypes.map.isRequired, schema: ImPropTypes.map.isRequired,
getComponent: PropTypes.func.isRequired, getComponent: PropTypes.func.isRequired,
getConfigs: PropTypes.func.isRequired,
specSelectors: PropTypes.object.isRequired,
specPath: ImPropTypes.list.isRequired,
name: PropTypes.string,
displayName: PropTypes.string,
isRef: PropTypes.bool,
required: PropTypes.bool,
expandDepth: PropTypes.number,
depth: PropTypes.number,
includeReadOnly: PropTypes.bool,
includeWriteOnly: PropTypes.bool,
onToggle: PropTypes.func, onToggle: PropTypes.func,
} }
Model.defaultProps = {
name: "",
displayName: "",
isRef: false,
required: false,
expandDepth: 0,
depth: 1,
includeReadOnly: false,
includeWriteOnly: false,
onToggle: () => {},
}
export default Model export default Model

View File

@@ -5,6 +5,25 @@ import { componentDidCatch } from "../fn"
import Fallback from "./fallback" import Fallback from "./fallback"
export class ErrorBoundary extends Component { export class ErrorBoundary extends Component {
static propTypes = {
targetName: PropTypes.string,
getComponent: PropTypes.func,
fn: PropTypes.object,
children: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.node),
PropTypes.node,
])
}
static defaultProps = {
targetName: "this component",
getComponent: () => Fallback,
fn: {
componentDidCatch,
},
children: null,
}
static getDerivedStateFromError(error) { static getDerivedStateFromError(error) {
return { hasError: true, error } return { hasError: true, error }
} }
@@ -29,22 +48,5 @@ export class ErrorBoundary extends Component {
return children return children
} }
} }
ErrorBoundary.propTypes = {
targetName: PropTypes.string,
getComponent: PropTypes.func,
fn: PropTypes.object,
children: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.node),
PropTypes.node,
])
}
ErrorBoundary.defaultProps = {
targetName: "this component",
getComponent: () => Fallback,
fn: {
componentDidCatch,
},
children: null,
}
export default ErrorBoundary export default ErrorBoundary