Skip to content

Revert "[Dialog] Replace deprecated Button for current one" #3527

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

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
6 changes: 0 additions & 6 deletions .changeset/cool-schools-cheer.md

This file was deleted.

20 changes: 13 additions & 7 deletions src/Dialog/Dialog.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, {useCallback, useEffect, useRef, useState} from 'react'
import styled from 'styled-components'
import {Button, ButtonProps} from '../Button'
import Button, {ButtonPrimary, ButtonDanger, ButtonProps} from '../deprecated/Button'
import Box from '../Box'
import {get} from '../constants'
import {useOnEscapePress, useProvidedRefOrCreate} from '../hooks'
Expand All @@ -20,11 +20,11 @@ const ANIMATION_DURATION = '200ms'
* Props that characterize a button to be rendered into the footer of
* a Dialog.
*/
export type DialogButtonProps = Omit<ButtonProps, 'children'> & {
export type DialogButtonProps = ButtonProps & {
/**
* The type of Button element to use
*/
buttonType?: 'default' | 'primary' | 'danger' | 'normal'
buttonType?: 'normal' | 'primary' | 'danger'

/**
* The Button's inner text
Expand Down Expand Up @@ -366,6 +366,11 @@ const Footer = styled.div<SxProp>`
${sx};
`

const buttonTypes = {
normal: Button,
primary: ButtonPrimary,
danger: ButtonDanger,
}
const Buttons: React.FC<React.PropsWithChildren<{buttons: DialogButtonProps[]}>> = ({buttons}) => {
const autoFocusRef = useProvidedRefOrCreate<HTMLButtonElement>(buttons.find(button => button.autoFocus)?.ref)
let autoFocusCount = 0
Expand All @@ -382,16 +387,17 @@ const Buttons: React.FC<React.PropsWithChildren<{buttons: DialogButtonProps[]}>>
return (
<>
{buttons.map((dialogButtonProps, index) => {
const {content, buttonType = 'default', autoFocus = false, ...buttonProps} = dialogButtonProps
const {content, buttonType = 'normal', autoFocus = false, ...buttonProps} = dialogButtonProps
const ButtonElement = buttonTypes[buttonType]
return (
<Button
<ButtonElement
key={index}
{...buttonProps}
variant={buttonType === 'normal' ? 'default' : buttonType}
variant={buttonType}
ref={autoFocus && autoFocusCount === 0 ? (autoFocusCount++, autoFocusRef) : null}
>
{content}
</Button>
</ButtonElement>
)
})}
</>
Expand Down
8 changes: 4 additions & 4 deletions src/SelectPanel/__snapshots__/SelectPanel.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ exports[`SelectPanel renders consistently 1`] = `
color: #1F2328;
}

.c2 {
margin-left: 4px;
}

.c1 {
position: relative;
display: inline-block;
Expand Down Expand Up @@ -75,6 +71,10 @@ exports[`SelectPanel renders consistently 1`] = `
border-color: rgba(31,35,40,0.15);
}

.c2 {
margin-left: 4px;
}

<div
className="c0"
color="fg.default"
Expand Down
37 changes: 16 additions & 21 deletions src/__tests__/ConfirmationDialog.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import {render as HTMLRender, fireEvent} from '@testing-library/react'
import {axe} from 'jest-axe'
import React, {useCallback, useRef, useState} from 'react'

import {ActionMenu} from '../ActionMenu'
import {ActionList} from '../ActionList'
import {ActionMenu} from '../deprecated/ActionMenu'
import BaseStyles from '../BaseStyles'
import Box from '../Box'
import {Button} from '../Button'
import Button from '../deprecated/Button/Button'
import {ConfirmationDialog, useConfirm} from '../Dialog/ConfirmationDialog'
import theme from '../theme'
import {ThemeProvider} from '../ThemeProvider'
Expand Down Expand Up @@ -63,14 +62,10 @@ const ShorthandHookFromActionMenu = () => {
<SSRProvider>
<BaseStyles>
<Box display="flex" flexDirection="column" alignItems="flex-start">
<ActionMenu>
<ActionMenu.Button>{text}</ActionMenu.Button>
<ActionMenu.Overlay>
<ActionList>
<ActionList.Item onSelect={onButtonClick}>Show dialog</ActionList.Item>
</ActionList>
</ActionMenu.Overlay>
</ActionMenu>
<ActionMenu
renderAnchor={props => <Button {...props}>{text}</Button>}
items={[{text: 'Show dialog', onAction: onButtonClick}]}
/>
</Box>
</BaseStyles>
</SSRProvider>
Expand Down Expand Up @@ -100,36 +95,36 @@ describe('ConfirmationDialog', () => {
})

it('focuses the primary action when opened and the confirmButtonType is not set', async () => {
const {getByText, getByRole} = HTMLRender(<Basic />)
const {getByText} = HTMLRender(<Basic />)
fireEvent.click(getByText('Show dialog'))
expect(getByRole('button', {name: 'Primary'})).toEqual(document.activeElement)
expect(getByText('Primary')).toEqual(document.activeElement)
expect(getByText('Secondary')).not.toEqual(document.activeElement)
})

it('focuses the primary action when opened and the confirmButtonType is not danger', async () => {
const {getByText, getByRole} = HTMLRender(<Basic confirmButtonType="primary" />)
const {getByText} = HTMLRender(<Basic confirmButtonType="primary" />)
fireEvent.click(getByText('Show dialog'))
expect(getByRole('button', {name: 'Primary'})).toEqual(document.activeElement)
expect(getByText('Primary')).toEqual(document.activeElement)
expect(getByText('Secondary')).not.toEqual(document.activeElement)
})

it('focuses the secondary action when opened and the confirmButtonType is danger', async () => {
const {getByText, getByRole} = HTMLRender(<Basic confirmButtonType="danger" />)
const {getByText} = HTMLRender(<Basic confirmButtonType="danger" />)
fireEvent.click(getByText('Show dialog'))
expect(getByRole('button', {name: 'Primary'})).not.toEqual(document.activeElement)
expect(getByRole('button', {name: 'Secondary'})).toEqual(document.activeElement)
expect(getByText('Primary')).not.toEqual(document.activeElement)
expect(getByText('Secondary')).toEqual(document.activeElement)
})

it('supports nested `focusTrap`s', async () => {
const spy = jest.spyOn(console, 'error').mockImplementationOnce(() => {})

const {getByText, getByRole} = HTMLRender(<ShorthandHookFromActionMenu />)
const {getByText} = HTMLRender(<ShorthandHookFromActionMenu />)

fireEvent.click(getByText('Show menu'))
fireEvent.click(getByText('Show dialog'))

expect(getByRole('button', {name: 'Primary'})).toEqual(document.activeElement)
expect(getByRole('button', {name: 'Secondary'})).not.toEqual(document.activeElement)
expect(getByText('Primary')).toEqual(document.activeElement)
expect(getByText('Secondary')).not.toEqual(document.activeElement)

// REACT_VERSION_LATEST should be treated as a constant for the test
// environment
Expand Down
38 changes: 19 additions & 19 deletions src/__tests__/__snapshots__/AnchoredOverlay.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -100,25 +100,6 @@ exports[`AnchoredOverlay should render consistently when open 1`] = `
color: #1F2328;
}

.c2 {
background-color: #ffffff;
box-shadow: 0 1px 3px rgba(31,35,40,0.12),0 8px 24px rgba(66,74,83,0.12);
position: absolute;
min-width: 192px;
max-width: 640px;
height: auto;
width: auto;
border-radius: 12px;
overflow: hidden;
-webkit-animation: overlay-appear 200ms cubic-bezier(0.33,1,0.68,1);
animation: overlay-appear 200ms cubic-bezier(0.33,1,0.68,1);
visibility: var(--styled-overlay-visibility);
}

.c2:focus {
outline: none;
}

.c1 {
position: relative;
display: inline-block;
Expand Down Expand Up @@ -183,6 +164,25 @@ exports[`AnchoredOverlay should render consistently when open 1`] = `
border-color: rgba(31,35,40,0.15);
}

.c2 {
background-color: #ffffff;
box-shadow: 0 1px 3px rgba(31,35,40,0.12),0 8px 24px rgba(66,74,83,0.12);
position: absolute;
min-width: 192px;
max-width: 640px;
height: auto;
width: auto;
border-radius: 12px;
overflow: hidden;
-webkit-animation: overlay-appear 200ms cubic-bezier(0.33,1,0.68,1);
animation: overlay-appear 200ms cubic-bezier(0.33,1,0.68,1);
visibility: var(--styled-overlay-visibility);
}

.c2:focus {
outline: none;
}

@media (forced-colors:active) {
.c2 {
outline: solid 1px transparent;
Expand Down
Loading