-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* segmented control * lading status fixes * simpler * component rebuilt * taking state from button props * picking props and choosing array for state * story remade to fit new convention * away with state props * selective size options
- Loading branch information
1 parent
f51ed56
commit 98184c8
Showing
9 changed files
with
166 additions
and
158 deletions.
There are no files selected for viewing
53 changes: 0 additions & 53 deletions
53
packages/react-components/src/components/ButtonGroup/ButtonGroup.stories.tsx
This file was deleted.
Oops, something went wrong.
75 changes: 0 additions & 75 deletions
75
packages/react-components/src/components/ButtonGroup/ButtonGroup.tsx
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
52 changes: 28 additions & 24 deletions
52
...mponents/ButtonGroup/ButtonGroup.spec.tsx → ...egmentedControl/SegmentedControl.spec.tsx
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
50 changes: 50 additions & 0 deletions
50
packages/react-components/src/components/SegmentedControl/SegmentedControl.stories.tsx
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,50 @@ | ||
import * as React from 'react'; | ||
import { ComponentMeta, Story } from '@storybook/react'; | ||
|
||
import { SegmentedControl, SegmentedControlProps } from './SegmentedControl'; | ||
|
||
const buttonSizes = ['compact', 'medium', 'large']; | ||
|
||
export default { | ||
title: 'Components/Segmented Control', | ||
component: SegmentedControl, | ||
argTypes: { | ||
size: { | ||
options: buttonSizes, | ||
control: { | ||
type: 'select', | ||
labels: buttonSizes, | ||
}, | ||
}, | ||
}, | ||
} as ComponentMeta<typeof SegmentedControl>; | ||
|
||
export const Default: Story<SegmentedControlProps> = ( | ||
args: SegmentedControlProps | ||
) => <SegmentedControl {...args} />; | ||
|
||
Default.storyName = 'Controlled'; | ||
Default.args = { | ||
buttons: [ | ||
{ id: 'one', label: 'one', loading: true, disabled: true }, | ||
{ id: 'two', label: 'two', disabled: true }, | ||
{ id: 'three', label: 'three' }, | ||
{ id: 'fourth', label: 'fourth' }, | ||
], | ||
initialId: 'fourth', | ||
}; | ||
|
||
export const Uncontrolled: Story<SegmentedControlProps> = ( | ||
args: SegmentedControlProps | ||
) => <SegmentedControl {...args} />; | ||
|
||
Uncontrolled.storyName = 'Uncontrolled With Initial Selection'; | ||
Uncontrolled.args = { | ||
buttons: [ | ||
{ id: 'one', label: 'one' }, | ||
{ id: 'two', label: 'two', disabled: true }, | ||
{ id: 'three', label: 'three' }, | ||
{ id: 'fourth', label: 'fourth' }, | ||
], | ||
currentId: 'one', | ||
}; |
81 changes: 81 additions & 0 deletions
81
packages/react-components/src/components/SegmentedControl/SegmentedControl.tsx
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,81 @@ | ||
import * as React from 'react'; | ||
import cx from 'clsx'; | ||
|
||
import { Button, ButtonProps } from '../Button'; | ||
|
||
import styles from './SegmentedControl.module.scss'; | ||
|
||
import noop from '../../utils/noop'; | ||
|
||
const baseClass = 'segmented-control'; | ||
|
||
export type ButtonSize = 'compact' | 'medium' | 'large'; | ||
|
||
type ButtonElement = { | ||
id: string; | ||
label: string; | ||
} & Pick<ButtonProps, 'disabled' | 'loading'>; | ||
|
||
export interface SegmentedControlProps | ||
extends React.HTMLAttributes<HTMLDivElement> { | ||
className?: string; | ||
buttons: ButtonElement[]; | ||
fullWidth?: boolean; | ||
size?: ButtonSize; | ||
initialId?: string; | ||
currentId?: string; | ||
onButtonClick?: (id: string, event: React.MouseEvent<HTMLElement>) => void; | ||
} | ||
|
||
export const SegmentedControl: React.FC<SegmentedControlProps> = ({ | ||
size = 'medium', | ||
buttons, | ||
className, | ||
initialId, | ||
currentId, | ||
fullWidth = false, | ||
onButtonClick = noop, | ||
}) => { | ||
const mergedClassName = cx(styles[baseClass], className); | ||
const [currentStateId, setCurrentStateId] = React.useState(() => initialId); | ||
|
||
const isControlled = typeof currentId === 'string'; | ||
|
||
React.useEffect(() => { | ||
isControlled && setCurrentStateId(currentId); | ||
}, [currentId]); | ||
|
||
const handleClick = (id: string, event: any) => { | ||
if (!isControlled) { | ||
setCurrentStateId(id); | ||
} | ||
|
||
onButtonClick(id, event); | ||
}; | ||
const buttonSet = buttons.map(({ id, label, loading, disabled }) => { | ||
const activityStyles = id === currentStateId ? styles['btn--active'] : ''; | ||
const loadingStatus = id === currentStateId ? false : loading; | ||
|
||
return ( | ||
<Button | ||
key={id} | ||
fullWidth={fullWidth} | ||
loading={loadingStatus} | ||
disabled={disabled} | ||
kind="secondary" | ||
className={cx(styles['btn'], styles[`btn--${size}`], activityStyles)} | ||
onClick={(event: React.MouseEvent<HTMLElement>) => { | ||
handleClick(id, event); | ||
}} | ||
> | ||
{label} | ||
</Button> | ||
); | ||
}); | ||
|
||
return ( | ||
<div role="group" className={mergedClassName}> | ||
{buttonSet} | ||
</div> | ||
); | ||
}; |
1 change: 1 addition & 0 deletions
1
packages/react-components/src/components/SegmentedControl/index.ts
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 @@ | ||
export { SegmentedControl } from './SegmentedControl'; |
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