Merge pull request #114 from utopia-os/remove-defaultProps

fix(source): use JavaScript default parameters instead defaultProps
This commit is contained in:
antontranelis 2025-02-11 10:56:29 +00:00 committed by GitHub
commit 14dc0e38ba
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 15 additions and 27 deletions

View File

@ -1,4 +1,3 @@
import { node, string } from 'prop-types'
import { Children, cloneElement, isValidElement, useEffect } from 'react'
import type { Item } from '#types/Item'
@ -33,11 +32,4 @@ export const ItemForm = ({
)
}
ItemForm.propTypes = {
children: node,
__TYPE: string,
}
ItemForm.defaultProps = {
__TYPE: 'ItemForm',
}
ItemForm.__TYPE = 'ItemForm'

View File

@ -1,4 +1,3 @@
import { node, string } from 'prop-types'
import { Children, cloneElement, isValidElement } from 'react'
import type { Item } from '#types/Item'
@ -8,18 +7,11 @@ export const ItemView = ({ children, item }: { children?: React.ReactNode; item?
<div>
{children
? Children.toArray(children).map((child) =>
isValidElement<{ item: Item }>(child) ? cloneElement(child, { item }) : '',
isValidElement<{ item: Item }>(child) ? cloneElement(child, { item }) : null,
)
: ''}
: null}
</div>
)
}
ItemView.propTypes = {
children: node,
__TYPE: string,
}
ItemView.defaultProps = {
__TYPE: 'ItemView',
}
ItemView.__TYPE = 'ItemView'

View File

@ -32,6 +32,7 @@ import type { Item } from '#types/Item'
import type { LayerProps } from '#types/LayerProps'
import type { Tag } from '#types/Tag'
import type { Popup } from 'leaflet'
import type { ReactElement, ReactNode } from 'react'
export const Layer = ({
data,
@ -284,10 +285,10 @@ export const Layer = ({
>
{children &&
Children.toArray(children).some(
(child) => isValidElement(child) && child.props.__TYPE === 'ItemView',
(child) => isComponentWithType(child) && child.type.__TYPE === 'ItemView',
) ? (
Children.toArray(children).map((child) =>
isValidElement(child) && child.props.__TYPE === 'ItemView' ? (
isComponentWithType(child) && child.type.__TYPE === 'ItemView' ? (
<ItemViewPopup
ref={(r) => {
if (!(item.id in leafletRefs && leafletRefs[item.id].popup === r)) {
@ -300,9 +301,7 @@ export const Layer = ({
>
{child}
</ItemViewPopup>
) : (
''
),
) : null,
)
) : (
<>
@ -318,6 +317,7 @@ export const Layer = ({
/>
</>
)}
<Tooltip offset={[0, -38]} direction='top'>
{item.name ? item.name : getValue(item, itemNameField)}
</Tooltip>
@ -332,10 +332,10 @@ export const Layer = ({
itemFormPopup.layer.name === name &&
(children &&
Children.toArray(children).some(
(child) => isValidElement(child) && child.props.__TYPE === 'ItemForm',
(child) => isComponentWithType(child) && child.type.__TYPE === 'ItemForm',
) ? (
Children.toArray(children).map((child) =>
isValidElement(child) && child.props.__TYPE === 'ItemForm' ? (
isComponentWithType(child) && child.type.__TYPE === 'ItemForm' ? (
<ItemFormPopup
key={setItemFormPopup?.name}
position={itemFormPopup.position}
@ -362,3 +362,7 @@ export const Layer = ({
</>
)
}
function isComponentWithType(node: ReactNode): node is ReactElement & { type: { __TYPE: string } } {
return isValidElement(node) && typeof node.type !== 'string' && '__TYPE' in node.type
}