-
Notifications
You must be signed in to change notification settings - Fork 10
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
#396 ButtonGroup rename SegmentedControl + component improvements #422
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
75ea64b
segmented control
MichalPaszowski 1a2cf9d
lading status fixes
MichalPaszowski fbafc13
simpler
MichalPaszowski 29025b1
component rebuilt
MichalPaszowski ff64fd6
taking state from button props
MichalPaszowski 0b0f975
picking props and choosing array for state
MichalPaszowski e9cafc3
story remade to fit new convention
MichalPaszowski f50d3d8
away with state props
MichalPaszowski 2ba5b54
selective size options
MichalPaszowski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you follow the story convention from eg.
Avatar
?We're trying to avoid the renaming of the component using the
Component
suffix. This happens thanks to naming the first storyDefault
.