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

#282 - Add EbaySegmentedButtons component #370

Merged
merged 9 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
42 changes: 42 additions & 0 deletions src/ebay-segmented-buttons/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# EbaySegmentedButtons

## Demo
[Storybook](https://opensource.ebay.com/ebayui-core-react/main/?path=/story/buttons-ebay-segmented-buttons--default)

## Install
```
yarn add @ebay/ui-core-react
```

## Usage
```
import React from 'react'
import EbaySegmentedButtons, EbaySegmentedButtons as Button from './segmented-buttons'
import { EbayIcon } from '../ebay-icon'
import '@ebay/skin/segmented-buttons'

export const Example = () => (
<EbaySegmentedButtons
size="large"
onChange={(e, { i, value }) => console.log('Selected:', i, value)}
>
<Button value="1" selected>Button 1</Button>
<Button value="2">Button 2</Button>
<Button value="3"><EbayIcon name="settings24" /> Button 3</Button>
/>
);
```

## EbaySegmentedButtons Props

Name | Type | Required | Description
--- |----------| --- | ---
`size` | enum | No | Can be `regular` (default) or `large`
`onChange` | Function | No | props: (e: event, { index: number, value: string), triggered on selected button change

## EbaySegmentedButton Props

Name | Type | Required | Description
--- | --- |---------------------------------| ---
`value` | String | No | the value to use with `onChange` callback
`selected` | Boolean | No | Whether or not the button is selected
52 changes: 52 additions & 0 deletions src/ebay-segmented-buttons/__tests__/index.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import React from 'react'
import { Meta, StoryObj } from '@storybook/react'
import { action } from '@storybook/addon-actions'
import { EbaySegmentedButtons, SegmentedButtonsProps, EbaySegmentedButton as Button } from '..'
import { EbayIcon } from '../../ebay-icon'

export default {
title: 'Buttons/ebay-segmented-buttons',
component: EbaySegmentedButtons,
argTypes: {
size: {
options: ['large', 'regular'],
control: {
type: 'select'
},
table: {
defaultValue: {
summary: "regular",
},
},
},
onChange: {
action: 'changed',
table: {
category: 'Events',
defaultValue: {
summary: 'originalEvent, { index, value }'
}
}
}
}
} as Meta<typeof EbaySegmentedButtons>

export const Default: StoryObj<SegmentedButtonsProps> = {
render: args => (
<EbaySegmentedButtons onChange={action('change')} {...args}>
<Button selected value="quarter1">Q1</Button>
<Button value="quarter2">Q2</Button>
<Button value="quarter3">Q3</Button>
<Button value="quarter4">Q4</Button>
</EbaySegmentedButtons>
)
}

export const WithIcons: StoryObj<SegmentedButtonsProps> = {
render: args => (
<EbaySegmentedButtons onChange={action('change')} {...args}>
<Button selected><EbayIcon name="fullView24"/> Desktop</Button>
<Button><EbayIcon name="mobile24"/> Mobile</Button>
</EbaySegmentedButtons>
)
}
38 changes: 38 additions & 0 deletions src/ebay-segmented-buttons/button.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React, { FC } from 'react'
import classNames from 'classnames'
import { excludeComponent, findComponent } from '../common/component-utils'
import { EbayIcon } from '../ebay-icon'
import { SegmentedButtonProps } from './types'

const SegmentedButton: FC<SegmentedButtonProps> = ({
selected,
children,
onClick
}) => {
const icon = findComponent(children, EbayIcon)

const iconWithText = () => {
const text = excludeComponent(children, EbayIcon)

return (
<span className="segmented-buttons__button-cell">
{icon}
<span>{text}</span>
</span>
)
}

return (
<li>
<button
className={classNames('segmented-buttons__button')}
aria-current={selected || undefined}
onClick={onClick}
>
{icon ? iconWithText() : children}
</button>
</li>
)
}

export default SegmentedButton
3 changes: 3 additions & 0 deletions src/ebay-segmented-buttons/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { default as EbaySegmentedButtons } from './segmented-buttons'
export { default as EbaySegmentedButton } from './button'
export { SegmentedButtonsProps, SegmentedButtonProps } from './types'
47 changes: 47 additions & 0 deletions src/ebay-segmented-buttons/segmented-buttons.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import React, { cloneElement, FC, ReactElement, useState } from 'react'
import classNames from 'classnames'
import { SegmentedButtonProps, SegmentedButtonsProps } from './types'
import { filterByType } from '../common/component-utils'
import SegmentedButton from './button'

const EbaySegmentedButtons: FC<SegmentedButtonsProps> = ({
size,
className,
onChange = () => {},
children,
...rest
}) => {
const buttons = filterByType(children, SegmentedButton)
const [selectedIndex, setSelectedIndex] = useState(
buttons.findIndex(button => button.props.selected) || 0
)

const handleClick = (e: React.MouseEvent<HTMLButtonElement>, index: number, value: string) => {
setSelectedIndex(index)
onChange(e, { index, value })
}

return (
<div
className={classNames('segmented-buttons', size && `segmented-buttons--${size}`, className)}
{...rest}
>
<ul>
{buttons.map((button: ReactElement, i) => {
const {
value,
...buttonRest
}: SegmentedButtonProps = button.props

return cloneElement(button, {
...buttonRest,
onClick: e => handleClick(e, i, value),
selected: i === selectedIndex
} as SegmentedButtonProps)
})}
</ul>
</div>
)
}

export default EbaySegmentedButtons
14 changes: 14 additions & 0 deletions src/ebay-segmented-buttons/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { ComponentProps, ReactNode } from 'react'
Fixed Show fixed Hide fixed
import { EbayMouseEventHandler } from '../common/event-utils/types'

export interface SegmentedButtonProps extends Omit<ComponentProps<'button'>, 'onClick'> {
value?: string;
selected?: boolean;
onClick?: EbayMouseEventHandler<HTMLButtonElement>;
}

export interface SegmentedButtonsProps extends Omit<ComponentProps<'div'>, 'onChange'> {
buttons?: SegmentedButtonProps[];
size?: 'large' | 'regular';
onChange?: EbayMouseEventHandler<HTMLButtonElement, { index: number, value?: string }>;
}
Loading