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

Components: migrate ConfirmDialog component's Stories from knobs to controls #40164

Merged
merged 24 commits into from
Apr 29, 2022
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
2c03913
ConfirmDialog Stories: implement a template, migrate 'Default' and 'W…
chad1008 Apr 4, 2022
79a2873
ConfirmDialog Stories: migrate 'Very Long Message' story from knobs t…
chad1008 Apr 4, 2022
2f3a1c1
ConfirmDialog Stories: migrate 'With JSX Message' story from knobs to…
chad1008 Apr 4, 2022
a11d635
ConfirmDialog Stories: migrate 'Uncontrolled with explicit cancel' st…
chad1008 Apr 4, 2022
9f06e0a
ConfirmDialog: add Story demonstrating 'Title' prop
chad1008 Apr 5, 2022
e25d150
ConfirmDialog: Update 'Controlled' story description
chad1008 Apr 5, 2022
0f91938
ConfirmDialog Stories: add argTypes data
chad1008 Apr 5, 2022
986f4f8
ConfirmDialog: convert 'With JSX Message' story to provide actual JSX…
chad1008 Apr 6, 2022
7a83d46
ConfirmDialog: use default args for stories where appropriate
chad1008 Apr 6, 2022
feaf0af
Revert "ConfirmDialog: add Story demonstrating 'Title' prop"
chad1008 Apr 7, 2022
16cc885
ConfirmDialog Stories: clean up story descriptions
chad1008 Apr 7, 2022
8ff9d15
ConfirmDialog Stories: clean up argTypes data
chad1008 Apr 7, 2022
da3b312
ConfirmDialog stories: set code snippets to open
chad1008 Apr 11, 2022
8fcdf8c
ConfirmDialog Stories: remove 'Uncontrolled with explicit onCancel' s…
chad1008 Apr 11, 2022
64fcc77
ConfrirmDialog Stories: set all stories to be controlled by default (…
chad1008 Apr 11, 2022
8b8dd4b
ConfirmDialog: remove initial confirmValue text
chad1008 Apr 11, 2022
c9520c6
ConfirmDialog Stories: remove 'Very Long Message' story
chad1008 Apr 11, 2022
420d7dd
ConfirmDialog Stories: remove 'JSX Message' story
chad1008 Apr 11, 2022
1dd56c0
ConfirmDialog Stories: remove daText() knob
chad1008 Apr 11, 2022
be34689
ConfirmDialoge Stories: remove JSX control remnants
chad1008 Apr 11, 2022
67fe8d5
ConfirmDialog Stories: update Controlled story to use controls and th…
chad1008 Apr 11, 2022
6deb56b
replace the Controlled story with Uncontrolled now that Controlled is…
chad1008 Apr 25, 2022
98b6642
ConfirmDialog Stories: remove Uncontrolled Story
chad1008 Apr 28, 2022
168674a
ConfirmDialog Stories: simplify args in template code
chad1008 Apr 28, 2022
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
186 changes: 87 additions & 99 deletions packages/components/src/confirm-dialog/stories/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { text } from '@storybook/addon-knobs';

/**
* WordPress dependencies
*/
Expand All @@ -15,102 +10,42 @@ import Button from '../../button';
import { Heading } from '../../heading';
import { ConfirmDialog } from '..';

export default {
const meta = {
component: ConfirmDialog,
title: 'Components (Experimental)/ConfirmDialog',
argTypes: {
children: {
control: { type: 'text' },
},
confirmButtonText: {
control: { type: 'text' },
},
cancelButtonText: {
control: { type: 'text' },
},
isOpen: {
control: { type: null },
},
onConfirm: {
control: { type: null },
},
onCancel: {
control: { type: null },
},
},
args: {
children: 'Would you like to privately publish the post now?',
},
parameters: {
chad1008 marked this conversation as resolved.
Show resolved Hide resolved
knobs: { disable: false },
docs: { source: { state: 'open' } },
},
};

const daText = () =>
text( 'message', 'Would you like to privately publish the post now?' );
const daCancelText = () => text( 'cancel button', 'No thanks' );
const daConfirmText = () => text( 'confirm button', 'Yes please!' );

// Simplest usage: just declare the component with the required `onConfirm` prop.
export const _default = () => {
const [ confirmVal, setConfirmVal ] = useState( "Hasn't confirmed yet" );

return (
<>
<ConfirmDialog onConfirm={ () => setConfirmVal( 'Confirmed!' ) }>
{ daText() }
</ConfirmDialog>
<Heading level={ 1 }>{ confirmVal }</Heading>
</>
);
};

export const WithCustomButtonLabels = () => {
const [ confirmVal, setConfirmVal ] = useState( "Hasn't confirmed yet" );

return (
<>
<ConfirmDialog
onConfirm={ () => setConfirmVal( 'Confirmed!' ) }
cancelButtonText={ daCancelText() }
confirmButtonText={ daConfirmText() }
>
{ daText() }
</ConfirmDialog>
<Heading level={ 1 }>{ confirmVal }</Heading>
</>
);
};

export const WithJSXMessage = () => {
const [ confirmVal, setConfirmVal ] = useState( "Hasn't confirmed yet" );

return (
<>
<ConfirmDialog onConfirm={ () => setConfirmVal( 'Confirmed!' ) }>
<Heading level={ 2 }>{ daText() }</Heading>
</ConfirmDialog>
<Heading level={ 1 }>{ confirmVal }</Heading>
</>
);
};

export const VeeeryLongMessage = () => {
const [ confirmVal, setConfirmVal ] = useState( "Hasn't confirmed yet" );
export default meta;

return (
<>
<ConfirmDialog onConfirm={ () => setConfirmVal( 'Confirmed!' ) }>
{ daText().repeat( 20 ) }
</ConfirmDialog>
<Heading level={ 1 }>{ confirmVal }</Heading>
</>
);
};

export const UncontrolledAndWithExplicitOnCancel = () => {
const [ confirmVal, setConfirmVal ] = useState(
"Hasn't confirmed or cancelled yet"
);

return (
<>
<ConfirmDialog
onConfirm={ () => setConfirmVal( 'Confirmed!' ) }
onCancel={ () => setConfirmVal( 'Cancelled' ) }
>
{ daText() }
</ConfirmDialog>
<Heading level={ 1 }>{ confirmVal }</Heading>
</>
);
};

// Controlled `ConfirmDialog`s require both `onConfirm` *and* `onCancel to be passed
// It's expected that the user will then use it to hide the dialog, too (see the
// `setIsOpen` calls below).
export const Controlled = () => {
const Template = ( args ) => {
const [ isOpen, setIsOpen ] = useState( false );
const [ confirmVal, setConfirmVal ] = useState(
"Hasn't confirmed or cancelled yet"
);
const [ confirmVal, setConfirmVal ] = useState( '' );

const handleConfirm = () => {
setConfirmVal( 'Confirmed!' );
Expand All @@ -121,22 +56,75 @@ export const Controlled = () => {
setConfirmVal( 'Cancelled' );
setIsOpen( false );
};

return (
<>
<Button variant="primary" onClick={ () => setIsOpen( true ) }>
Open ConfirmDialog
</Button>

<ConfirmDialog
{ ...args }
isOpen={ isOpen }
onConfirm={ handleConfirm }
onCancel={ handleCancel }
>
{ daText() }
{ args.children }
</ConfirmDialog>

<Heading level={ 1 }>{ confirmVal }</Heading>

<Button variant="primary" onClick={ () => setIsOpen( true ) }>
Open ConfirmDialog
</Button>
</>
);
};

// Simplest usage: just declare the component with the required `onConfirm` prop. Note: the `onCancel` prop is optional here, unless you'd like to render the component in Controlled mode (see below)
export const _default = Template.bind( {} );
const _defaultSnippet = `() => {
const [ isOpen, setIsOpen ] = useState( false );
const [ confirmVal, setConfirmVal ] = useState('');

const handleConfirm = () => {
setConfirmVal( 'Confirmed!' );
setIsOpen( false );
};

const handleCancel = () => {
setConfirmVal( 'Cancelled' );
setIsOpen( false );
};

return (
<>
<ConfirmDialog
isOpen={ isOpen }
onConfirm={ handleConfirm }
onCancel={ handleCancel }
>
Would you like to privately publish the post now?
</ConfirmDialog>

<Heading level={ 1 }>{ confirmVal }</Heading>

<Button variant="primary" onClick={ () => setIsOpen( true ) }>
Open ConfirmDialog
</Button>
</>
);
};`;
_default.args = {};
_default.parameters = {
docs: {
source: {
code: _defaultSnippet,
language: 'jsx',
type: 'auto',
format: 'true',
},
},
};

// To customize button text, pass the `cancelButtonText` and/or `confirmButtonText` props.
export const withCustomButtonLabels = Template.bind( {} );
withCustomButtonLabels.args = {
cancelButtonText: 'No thanks',
confirmButtonText: 'Yes please!',
};