Skip to content

Commit

Permalink
refactor: replace defaultProps with JavaScript default parameters (#9464
Browse files Browse the repository at this point in the history
)

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

Refs #9456
  • Loading branch information
char0n committed Jan 8, 2024
1 parent 15fb960 commit 252c81a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 67 deletions.
20 changes: 11 additions & 9 deletions src/core/components/app.jsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
/**
* @prettier
*/
import React from "react"
import PropTypes from "prop-types"

export default class App extends React.Component {

class App extends React.Component {
getLayout() {
let { getComponent, layoutSelectors } = this.props
const { getComponent, layoutSelectors } = this.props
const layoutName = layoutSelectors.current()
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() {
const Layout = this.getLayout()

return (
<Layout/>
)
return <Layout />
}
}

Expand All @@ -24,5 +27,4 @@ App.propTypes = {
layoutSelectors: PropTypes.object.isRequired,
}

App.defaultProps = {
}
export default App
61 changes: 20 additions & 41 deletions src/core/plugins/oas31/components/model/model.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,54 +20,33 @@ const getModelName = (uri) => {
return null
}

const Model = forwardRef(({ schema, getComponent, onToggle }, ref) => {
const JSONSchema202012 = getComponent("JSONSchema202012")
const name = getModelName(schema.get("$$ref"))
const Model = forwardRef(
({ schema, getComponent, onToggle = () => {} }, ref) => {
const JSONSchema202012 = getComponent("JSONSchema202012")
const name = getModelName(schema.get("$$ref"))

const handleExpand = useCallback(
(e, expanded) => {
onToggle(name, expanded)
},
[name, onToggle]
)
const handleExpand = useCallback(
(e, expanded) => {
onToggle(name, expanded)
},
[name, onToggle]
)

return (
<JSONSchema202012
name={name}
schema={schema.toJS()}
ref={ref}
onExpand={handleExpand}
/>
)
})
return (
<JSONSchema202012
name={name}
schema={schema.toJS()}
ref={ref}
onExpand={handleExpand}
/>
)
}
)

Model.propTypes = {
schema: ImPropTypes.map.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,
}

Model.defaultProps = {
name: "",
displayName: "",
isRef: false,
required: false,
expandDepth: 0,
depth: 1,
includeReadOnly: false,
includeWriteOnly: false,
onToggle: () => {},
}

export default Model
36 changes: 19 additions & 17 deletions src/core/plugins/safe-render/components/error-boundary.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,25 @@ import { componentDidCatch } from "../fn"
import Fallback from "./fallback"

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) {
return { hasError: true, error }
}
Expand All @@ -29,22 +48,5 @@ export class ErrorBoundary extends Component {
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

0 comments on commit 252c81a

Please sign in to comment.