Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️ Conform element types #829

Merged
merged 20 commits into from
Nov 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions apps/storybook-react/stories/Divider.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-unsafe-call */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
import React from 'react'
import styled from 'styled-components'
import { Divider, DividerProps } from '@equinor/eds-core-react'
Expand Down
23 changes: 1 addition & 22 deletions apps/storybook-react/stories/Popover.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,6 @@ export default {
PopoverContent,
CardActions,
},
argTypes: {
placement: {
control: {
type: 'select',
options: [
'topLeft',
'top',
'topRight',
'rightTop',
'right',
'rightBottom',
'bottomLeft',
'bottom',
'bottomRight',
'leftTop',
'left',
'leftBottom',
],
},
},
},
} as Meta

export const Default: Story<PopoverProps> = (args) => {
Expand All @@ -67,7 +46,7 @@ export const Default: Story<PopoverProps> = (args) => {
}
return (
<div style={{ margin: '10em' }}>
<Popover {...args} onClose={handleToggle} open={active || args.open}>
<Popover {...args} onClose={handleToggle} open={active}>
<PopoverAnchor>
<Button id="1" onClick={handleToggle}>
Click me!
Expand Down
6 changes: 3 additions & 3 deletions libraries/core-react/src/Accordion/Accordion.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React, { forwardRef, useMemo, ReactElement } from 'react'
import React, { forwardRef, useMemo, ReactElement, HTMLAttributes } from 'react'
import createId from 'lodash/uniqueId'
import type { AccordionProps } from './Accordion.types'

const Accordion = forwardRef<
HTMLDivElement,
AccordionProps & JSX.IntrinsicElements['div']
>(function EdsAccordion(
AccordionProps & HTMLAttributes<HTMLDivElement>
>(function Accordion(
{ headerLevel = 'h2', chevronPosition = 'left', children, ...props },
ref,
) {
Expand Down
165 changes: 85 additions & 80 deletions libraries/core-react/src/Accordion/AccordionHeader.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import React, { forwardRef, isValidElement, ReactElement } from 'react'
import React, {
forwardRef,
HTMLAttributes,
isValidElement,
ReactElement,
} from 'react'
import styled from 'styled-components'
import type { CSSObject } from 'styled-components'
// eslint-disable-next-line camelcase
Expand Down Expand Up @@ -90,94 +95,94 @@ type AccordionHeaderProps = {
disabled?: boolean
/** @ignore */
toggleExpanded?: () => void
} & AccordionProps
} & AccordionProps &
HTMLAttributes<HTMLDivElement>

type AccordionChild = {
type: { displayName: string }
} & ReactElement

const AccordionHeader = forwardRef<
HTMLDivElement,
AccordionHeaderProps & JSX.IntrinsicElements['div']
>(function AccordionHeader(
{
parentIndex,
headerLevel,
chevronPosition,
panelId,
id,
isExpanded = false,
children,
toggleExpanded,
disabled,
...props
},
ref,
) {
const handleClick = () => {
if (!disabled) {
toggleExpanded()
}
}

const handleKeyDown = (event: React.KeyboardEvent) => {
const { key } = event
if (key === 'Enter' || key === ' ') {
toggleExpanded()
event.preventDefault()
}
}

const chevron = (
<StyledIcon
key={`${id}-icon`}
name={isExpanded ? 'chevron_up' : 'chevron_down'}
size={16}
chevronPosition={chevronPosition}
color={disabled ? chevronDisabledColor : chevronDefaultColor}
/>
)

const headerChildren = React.Children.map(
children,
(child: AccordionChild) => {
if (typeof child === 'string') {
return (
<AccordionHeaderTitle isExpanded={isExpanded} disabled={disabled}>
{child}
</AccordionHeaderTitle>
)
const AccordionHeader = forwardRef<HTMLDivElement, AccordionHeaderProps>(
function AccordionHeader(
{
parentIndex,
headerLevel,
chevronPosition,
panelId,
id,
isExpanded = false,
children,
toggleExpanded,
disabled,
...props
},
ref,
) {
const handleClick = () => {
if (!disabled) {
toggleExpanded()
}
}

if (isValidElement(child) && child.type === AccordionHeaderTitle) {
return React.cloneElement(child, {
isExpanded,
disabled,
})
const handleKeyDown = (event: React.KeyboardEvent) => {
const { key } = event
if (key === 'Enter' || key === ' ') {
toggleExpanded()
event.preventDefault()
}
}

return child
},
)

const newChildren = [chevron, headerChildren]

return (
<StyledAccordionHeader
isExpanded={isExpanded}
parentIndex={parentIndex}
as={headerLevel}
disabled={disabled}
{...props}
panelId={panelId}
onClick={handleClick}
onKeyDown={handleKeyDown}
ref={ref}
>
{chevronPosition === 'left' ? newChildren : newChildren.reverse()}
</StyledAccordionHeader>
)
})
const chevron = (
<StyledIcon
key={`${id}-icon`}
name={isExpanded ? 'chevron_up' : 'chevron_down'}
size={16}
chevronPosition={chevronPosition}
color={disabled ? chevronDisabledColor : chevronDefaultColor}
/>
)

const headerChildren = React.Children.map(
children,
(child: AccordionChild) => {
if (typeof child === 'string') {
return (
<AccordionHeaderTitle isExpanded={isExpanded} disabled={disabled}>
{child}
</AccordionHeaderTitle>
)
}

if (isValidElement(child) && child.type === AccordionHeaderTitle) {
return React.cloneElement(child, {
isExpanded,
disabled,
})
}

return child
},
)

const newChildren = [chevron, headerChildren]

return (
<StyledAccordionHeader
isExpanded={isExpanded}
parentIndex={parentIndex}
as={headerLevel}
disabled={disabled}
{...props}
panelId={panelId}
onClick={handleClick}
onKeyDown={handleKeyDown}
ref={ref}
>
{chevronPosition === 'left' ? newChildren : newChildren.reverse()}
</StyledAccordionHeader>
)
},
)

// AccordionHeader.displayName = 'EdsAccordionHeader'

Expand Down
4 changes: 2 additions & 2 deletions libraries/core-react/src/Accordion/AccordionHeaderTitle.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { forwardRef } from 'react'
import React, { forwardRef, HTMLAttributes } from 'react'
import styled from 'styled-components'
import { accordion as tokens } from './Accordion.tokens'

Expand All @@ -7,7 +7,7 @@ type AccordionHeaderTitleProps = {
isExpanded?: boolean
/** Accordion item is disabled */
disabled?: boolean
} & JSX.IntrinsicElements['span']
} & HTMLAttributes<HTMLSpanElement>

const StyledAccordionHeaderTitle = styled.span<AccordionHeaderTitleProps>`
flex: 1;
Expand Down
10 changes: 8 additions & 2 deletions libraries/core-react/src/Accordion/AccordionItem.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import React, { forwardRef, useState, useEffect, ReactElement } from 'react'
import React, {
forwardRef,
useState,
useEffect,
ReactElement,
HTMLAttributes,
} from 'react'
import type { AccordionProps } from './Accordion.types'

type Props = {
Expand All @@ -8,7 +14,7 @@ type Props = {
isExpanded?: boolean
/** accordion item is disabled */
disabled?: boolean
} & JSX.IntrinsicElements['div'] &
} & HTMLAttributes<HTMLDivElement> &
AccordionProps

const AccordionItem = forwardRef<HTMLDivElement, Props>(function AccordionItem(
Expand Down
4 changes: 2 additions & 2 deletions libraries/core-react/src/Accordion/AccordionPanel.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { forwardRef } from 'react'
import React, { forwardRef, HTMLAttributes } from 'react'
import styled from 'styled-components'
import { accordion as tokens } from './Accordion.tokens'

Expand All @@ -16,7 +16,7 @@ type AccordionPanelProps = {
id?: string
/** If `true`, the panel will be hidden. */
hidden?: boolean
} & JSX.IntrinsicElements['div']
} & HTMLAttributes<HTMLDivElement>

type StyledAccordionPanelProps = Pick<AccordionPanelProps, 'headerId'>

Expand Down
6 changes: 3 additions & 3 deletions libraries/core-react/src/Avatar/Avatar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { forwardRef } from 'react'
import React, { forwardRef, HTMLAttributes } from 'react'
import styled, { css } from 'styled-components'
import { avatar as tokens } from './Avatar.tokens'

Expand Down Expand Up @@ -55,9 +55,9 @@ export type AvatarProps = {
size?: 16 | 24 | 32 | 40 | 48
/** @default false */
disabled?: boolean
}
} & HTMLAttributes<HTMLDivElement>

export const Avatar = forwardRef<HTMLHRElement, AvatarProps>(function Avatar(
export const Avatar = forwardRef<HTMLDivElement, AvatarProps>(function Avatar(
{ src = null, alt, size = 24, disabled = false, ...rest },
ref,
) {
Expand Down
4 changes: 2 additions & 2 deletions libraries/core-react/src/Banner/BannerActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ const StyledBannerActions = styled.div<StyledBannerActionsProps>`
}}
`

type Props = {
type BannerActionProps = {
children: ReactNode
placement?: BannerActionsPlacement
} & HTMLAttributes<HTMLDivElement>

export const BannerActions: FC<Props> = ({
export const BannerActions: FC<BannerActionProps> = ({
children,
placement = 'left',
className,
Expand Down
4 changes: 2 additions & 2 deletions libraries/core-react/src/Banner/BannerIcon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ const StyledBannerIcon = styled.span<StyledBannerIconProps>`
margin-right: ${enabled.spacings};
`

type Props = {
type BannerIconProps = {
/** Which icon background and fill color to use. Info = green, warning = red */
variant?: BannerIconVariant
/** @ignore */
children: ReactNode
} & HTMLAttributes<HTMLSpanElement>

export const BannerIcon: FC<Props> = ({
export const BannerIcon: FC<BannerIconProps> = ({
children,
variant = 'info',
...props
Expand Down
4 changes: 2 additions & 2 deletions libraries/core-react/src/Breadcrumbs/Breadcrumb.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { forwardRef } from 'react'
import React, { forwardRef, HTMLAttributes } from 'react'
import styled, { css } from 'styled-components'
import { Typography } from '../Typography'
import { Tooltip } from '../Tooltip'
Expand Down Expand Up @@ -35,7 +35,7 @@ type BreadcrumbProps = {
onClick?: (e: MouseEvent | KeyboardEvent) => void
/** Children is breadcrumb text */
children: string
} & JSX.IntrinsicElements['div']
} & HTMLAttributes<HTMLDivElement>

export const Breadcrumb = forwardRef<HTMLDivElement, BreadcrumbProps>(
function Breadcrumb({ children, maxWidth, ...other }, ref) {
Expand Down
6 changes: 3 additions & 3 deletions libraries/core-react/src/Card/Card.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { forwardRef } from 'react'
import React, { forwardRef, HTMLAttributes } from 'react'
import styled from 'styled-components'
import { card as tokens } from './Card.tokens'
import { spacingsTemplate } from '../_common/templates'
Expand All @@ -8,12 +8,12 @@ const { spacings, shape } = tokens
type StyledCardProps = {
background: string
cursor: string
} & React.HTMLAttributes<HTMLDivElement>
} & HTMLAttributes<HTMLDivElement>

export type CardProps = {
/** Variant */
variant?: 'default' | 'info' | 'warning' | 'danger'
} & JSX.IntrinsicElements['div']
} & HTMLAttributes<HTMLDivElement>

const StyledCard = styled.div<StyledCardProps>`
height: fit-content;
Expand Down
4 changes: 2 additions & 2 deletions libraries/core-react/src/Card/CardActions.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { forwardRef } from 'react'
import React, { forwardRef, HTMLAttributes } from 'react'
import styled from 'styled-components'
import { Typography } from '../Typography'

Expand All @@ -7,7 +7,7 @@ export type CardActionProps = {
alignRight?: boolean
/** Meta information */
meta?: string
} & JSX.IntrinsicElements['div']
} & HTMLAttributes<HTMLDivElement>

const StyledCardActions = styled.div<React.CSSProperties>`
display: grid;
Expand Down
Loading