-
Notifications
You must be signed in to change notification settings - Fork 0
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 #89 from perimetre/7.8.0
7.8.0
- Loading branch information
Showing
6 changed files
with
220 additions
and
2 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
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,124 @@ | ||
import React from 'react'; | ||
import { useState } from 'react'; | ||
// also exported from '@storybook/react' if you can deal with breaking changes in 6.1 | ||
import { Meta, Story } from '@storybook/react/types-6-0'; | ||
import classnames from 'classnames'; | ||
import { ButtonStack, ButtonStackProps } from '.'; | ||
import { colorOptions } from '../../prebuiltTailwindTheme'; | ||
import { puiColorClassnameMap } from '../../storybookMappers'; | ||
import { BellIcon } from '../Icons'; | ||
|
||
export default { | ||
title: 'Components/ButtonStack', | ||
component: ButtonStack, | ||
argTypes: { | ||
color: { | ||
defaultValue: 'pui-primary', | ||
control: { | ||
type: 'select', | ||
options: colorOptions | ||
} | ||
}, | ||
disabled: { | ||
defaultValue: false, | ||
control: { | ||
type: 'boolean' | ||
} | ||
}, | ||
className: { | ||
control: { | ||
type: 'text' | ||
} | ||
}, | ||
onClick: { action: 'onClick' } | ||
} | ||
} as Meta; | ||
|
||
/** | ||
* A story that displays a button stack example | ||
* | ||
* @param props the story props | ||
* @param props.color the color property set on controls | ||
* @param props.disabled the disabled color set on controls | ||
*/ | ||
const WithTooltipTemplate: Story<ButtonStackProps & { border?: string; color?: string; disabled?: boolean }> = ({ | ||
color, | ||
disabled, | ||
...props | ||
}) => { | ||
const [activeKey, setActiveKey] = useState('button-1'); | ||
|
||
const dropdownContentItems = Array(4) | ||
.fill(null) | ||
.map((_, i) => ({ | ||
key: `button-${i + 1}`, | ||
/** | ||
* The icon example | ||
*/ | ||
icon: () => <BellIcon />, | ||
buttonProps: { | ||
/** | ||
* Callback for when the button is clicked | ||
*/ | ||
onClick: () => setActiveKey(`button-${i + 1}`), | ||
disabled | ||
} | ||
})); | ||
|
||
return ( | ||
<ButtonStack | ||
{...props} | ||
containerClassname={classnames({ | ||
[puiColorClassnameMap[color || 'transparent']]: color !== 'pui-primary' | ||
})} | ||
activeKey={activeKey} | ||
items={dropdownContentItems} | ||
/> | ||
); | ||
}; | ||
|
||
export const WithTooltip = WithTooltipTemplate.bind({}); | ||
|
||
/** | ||
* A story that displays a button stack example | ||
* | ||
* @param props the story props | ||
* @param props.color the color property set on controls | ||
* @param props.disabled the disabled color set on controls | ||
*/ | ||
const NoTooltipTemplate: Story<ButtonStackProps & { border?: string; color?: string; disabled?: boolean }> = ({ | ||
color, | ||
disabled, | ||
...props | ||
}) => { | ||
const [activeKey, setActiveKey] = useState('button-1'); | ||
|
||
const dropdownContentItems = Array(4) | ||
.fill(null) | ||
.map((_, i) => ({ | ||
key: `button-${i + 1}`, | ||
/** | ||
* The icon example | ||
*/ | ||
icon: () => <BellIcon />, | ||
/** | ||
* Callback for when the button is clicked | ||
*/ | ||
onClick: () => setActiveKey(`button-${i + 1}`), | ||
disabled | ||
})); | ||
|
||
return ( | ||
<ButtonStack | ||
{...props} | ||
hasTooltip={false} | ||
containerClassname={classnames({ | ||
[puiColorClassnameMap[color || 'transparent']]: color !== 'pui-primary' | ||
})} | ||
activeKey={activeKey} | ||
items={dropdownContentItems} | ||
/> | ||
); | ||
}; | ||
|
||
export const NoTooltip = NoTooltipTemplate.bind({}); |
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,87 @@ | ||
import classNames from 'classnames'; | ||
import { startCase } from 'lodash'; | ||
import React from 'react'; | ||
import { Button, ButtonProps } from '../Button'; | ||
import { Tooltip, TooltipProps } from '../Tooltip'; | ||
|
||
export type ButtonStackItem = { | ||
key: string; | ||
icon: () => React.ReactNode; | ||
}; | ||
|
||
export type ButtonStackProps = { | ||
/** The list of buttons that should be displayed */ | ||
items: (ButtonStackItem & (Omit<TooltipProps, 'ref'> | Omit<ButtonProps, 'ref'>))[]; | ||
/** Which button key, from the list of provided `items` should be active */ | ||
activeKey?: string; | ||
/** Whether or not it should have a tooltip on hover or not */ | ||
hasTooltip?: boolean; | ||
/** Classname for the container that wrapps all the buttons */ | ||
containerClassname?: string; | ||
}; | ||
|
||
/** | ||
* A list of buttons besides each other | ||
* | ||
* @param props The component props | ||
* @param props.items The list of buttons that should be displayed | ||
* @param props.activeKey Which button key, from the list of provided `items` should be active | ||
* @param props.hasTooltip Whether or not it should have a tooltip on hover or not | ||
* @param props.containerClassname Classname for the container that wrapps all the buttons | ||
*/ | ||
export const ButtonStack: React.FC<ButtonStackProps> = ({ | ||
items, | ||
activeKey, | ||
hasTooltip = true, | ||
containerClassname | ||
}) => ( | ||
<div className={classNames('mb-2 flex items-center', containerClassname)}> | ||
{hasTooltip | ||
? items.map(({ key, icon, ...props }, index) => { | ||
const tooltipProps = props as Omit<TooltipProps, 'ref'>; | ||
|
||
// ! If any change is done to some of these props, remember to also apply to the <Button> version when `hasTooltip` is false | ||
return ( | ||
<Tooltip | ||
key={key} | ||
content={<p className="text-center">{startCase(key)}</p>} | ||
{...tooltipProps} | ||
buttonProps={{ | ||
...tooltipProps.buttonProps, | ||
className: classNames( | ||
'pui-btn-bordered flex items-center justify-center w-9 px-1', | ||
{ | ||
'bg-pui-placeholder-color text-pui-paragraph-0': | ||
!tooltipProps.buttonProps?.disabled && activeKey === key, // If active | ||
'rounded-l-none': index > 0, // All BUT first | ||
'rounded-r-none border-r-0': index < items.length - 1 // All BUT last | ||
}, | ||
tooltipProps.buttonProps?.className | ||
) | ||
}} | ||
> | ||
<>{icon()}</> | ||
</Tooltip> | ||
); | ||
}) | ||
: items.map(({ key, icon, ...props }, index) => { | ||
const buttonProps = props as Omit<ButtonProps, 'ref'>; | ||
|
||
// ! If any change is done to some of these props, remember to also apply to the <Tooltip> version when `hasTooltip` is true | ||
return ( | ||
<Button | ||
key={key} | ||
variant="bordered" | ||
{...buttonProps} | ||
className={classNames('pui-btn-bordered flex items-center justify-center w-9 px-1', { | ||
'bg-pui-placeholder-color text-pui-paragraph-0': !buttonProps?.disabled && activeKey === key, // If active | ||
'rounded-l-none': index > 0, // All BUT first | ||
'rounded-r-none border-r-0': index < items.length - 1 // All BUT last | ||
})} | ||
> | ||
<>{icon()}</> | ||
</Button> | ||
); | ||
})} | ||
</div> | ||
); |
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