-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #177 from open-formulieren/refactor/mode-toggle
Refactor the mode toggle into own component
- Loading branch information
Showing
6 changed files
with
147 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import {ArgTypes, Canvas, Meta} from '@storybook/blocks'; | ||
|
||
import * as ModeToggleStories from './ModeToggle.stories'; | ||
|
||
<Meta of={ModeToggleStories} /> | ||
|
||
# Mode toggle | ||
|
||
The mode toggle is a button group control to select a particular (presentation) mode. | ||
|
||
<Canvas of={ModeToggleStories.Default} /> | ||
|
||
## Props | ||
|
||
<ArgTypes /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import {Meta, StoryObj} from '@storybook/react'; | ||
import {fn} from '@storybook/test'; | ||
|
||
import ModeToggle from './ModeToggle'; | ||
|
||
export default { | ||
title: 'Generic/ModeToggle', | ||
component: ModeToggle, | ||
parameters: { | ||
modal: {noModal: true}, | ||
}, | ||
args: { | ||
name: 'story', | ||
modes: [ | ||
{value: 'mode1', label: 'Mode 1'}, | ||
{value: 'mode2', label: 'Mode 2'}, | ||
], | ||
currentMode: 'mode2', | ||
onToggle: fn(), | ||
}, | ||
} satisfies Meta<typeof ModeToggle>; | ||
|
||
type Story = StoryObj<typeof ModeToggle>; | ||
|
||
export const Default: Story = {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import clsx from 'clsx'; | ||
|
||
export interface Mode<T extends string> { | ||
value: T; | ||
label: React.ReactNode; | ||
} | ||
|
||
export interface ModeToggleProps<T extends string> { | ||
/** | ||
* Name attribute for the HTML radio inputs. | ||
*/ | ||
name: string; | ||
/** | ||
* Array of possible modes - a list of objects with keys `value` and `label`. | ||
*/ | ||
modes: Mode<T>[]; | ||
/** | ||
* Value of the currently active mode. | ||
*/ | ||
currentMode: T; | ||
/** | ||
* Callback invoked when a particular mode is selected. | ||
*/ | ||
onToggle: (mode: T) => void; | ||
/** | ||
* Any additional classnames to apply to the container element. | ||
*/ | ||
className?: string; | ||
/** | ||
* Toggle button classname(s). | ||
*/ | ||
btnClassName?: string; | ||
} | ||
|
||
/** | ||
* Render a button group with the available and active modes. | ||
* | ||
* This component is generic - pass a union of the possible mode values for strict type | ||
* checking: `<ModeToggle<'mode1' | 'mode2'>>` | ||
*/ | ||
function ModeToggle<T extends string>({ | ||
name, | ||
modes, | ||
currentMode, | ||
onToggle, | ||
className, | ||
btnClassName = 'btn-secondary', | ||
}: ModeToggleProps<T>) { | ||
return ( | ||
<div className={clsx('btn-group', 'btn-group-toggle', className)}> | ||
{modes.map(({value, label}) => ( | ||
<label className={clsx('btn', 'btn-sm', btnClassName, {active: value === currentMode})}> | ||
<input | ||
type="radio" | ||
name={name} | ||
value={value} | ||
autoComplete="off" | ||
checked={value === currentMode} | ||
onChange={event => onToggle(event.target.value as T)} | ||
/> | ||
{label} | ||
</label> | ||
))} | ||
</div> | ||
); | ||
} | ||
|
||
export default ModeToggle; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import {FormattedMessage} from 'react-intl'; | ||
|
||
import ModeToggle from '@/components/ModeToggle'; | ||
|
||
export type PreviewState = 'rich' | 'JSON'; | ||
|
||
export interface PreviewModeToggleProps { | ||
previewMode: PreviewState; | ||
setPreviewMode: (mode: PreviewState) => void; | ||
} | ||
|
||
const PreviewModeToggle: React.FC<PreviewModeToggleProps> = ({previewMode, setPreviewMode}) => ( | ||
<ModeToggle<PreviewState> | ||
name="previewMode" | ||
currentMode={previewMode} | ||
onToggle={mode => setPreviewMode(mode)} | ||
modes={[ | ||
{ | ||
value: 'rich', | ||
label: ( | ||
<FormattedMessage description="Component 'Rich' preview mode" defaultMessage="Form" /> | ||
), | ||
}, | ||
{ | ||
value: 'JSON', | ||
label: ( | ||
<FormattedMessage description="Component 'JSON' preview mode" defaultMessage="JSON" /> | ||
), | ||
}, | ||
]} | ||
/> | ||
); | ||
|
||
export default PreviewModeToggle; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters