Skip to content

Commit

Permalink
[#256] Support prefixLabel prop
Browse files Browse the repository at this point in the history
  • Loading branch information
darkwebdev committed Oct 12, 2023
1 parent 72e9a7a commit 7c32be8
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/ebay-listbox-button/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ yarn add @ebay/ui-core-react
| `borderless` | boolean | No | To make the listbox borderless |
| `maxHeight` | string | No | example: 100px, 200px, 10rem |
| `prefixId` | string | No | The id of an external element to use as the a11y prefix label for the listbox button. |
| `prefixLabel` | string | No | The label to add before selected option on the button. Cannot be used with `prefixId` |
| `floatingLabel` | string | No | Indicates that the listbox is a floating label type and renders it with a label |
| `unselectedText` | string | No | The text to be shown when no options are selected. Default is '-'. Cannot be used with `floating-label` |

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,6 @@ exports[`Storyshots ebay-listbox-button Floating label 1`] = `
onClick={[Function]}
onKeyUp={[Function]}
onMouseDown={[Function]}
onSelect={[Function]}
type="button"
>
<span
Expand Down Expand Up @@ -422,6 +421,62 @@ exports[`Storyshots ebay-listbox-button Invalid State 1`] = `
</span>
`;

exports[`Storyshots ebay-listbox-button Prefix label 1`] = `
<span
className="listbox-button"
>
<button
aria-expanded={false}
aria-haspopup="listbox"
className="btn btn--form"
onClick={[Function]}
onKeyUp={[Function]}
onMouseDown={[Function]}
type="button"
>
<span
className="btn__cell"
>
<span
className="btn__label"
>
Selected:
</span>
<span
className="btn__text"
>
-
</span>
<svg
aria-hidden={true}
className="icon icon--chevron-down-16"
focusable={false}
xmlns="http://www.w3.org/2000/svg"
>
<use
xlinkHref="#icon-chevron-down-16"
/>
</svg>
</span>
</button>
<select
className="listbox-button__native"
hidden={true}
value=""
>
<option
value="AA"
/>
<option
value="BB"
/>
<option
value="CC"
/>
</select>
</span>
`;

exports[`Storyshots ebay-listbox-button Preselected index 1`] = `
<span
className="listbox-button"
Expand Down
22 changes: 20 additions & 2 deletions src/ebay-listbox-button/__tests__/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ describe("<EbayListboxButton>", () => {
});
it('should display custom button label', async () => {
const component = await render(
<EbayListboxButton unselectedText="Selected:">
<EbayListboxButton unselectedText="Select">
<EbayListboxButtonOption value="AA">Option 1</EbayListboxButtonOption>
<EbayListboxButtonOption value="BB">Option 2</EbayListboxButtonOption>
</EbayListboxButton>
);
expect(component.getByRole('button')).toHaveTextContent("Selected:");
expect(component.getByRole('button')).toHaveTextContent("Select");
});
it('should display button label with selected option', async () => {
const component = await render(
Expand All @@ -70,6 +70,24 @@ describe("<EbayListboxButton>", () => {
);
expect(component.getByRole('button')).toHaveTextContent("Option 2");
});
it('should display button label with prefix', async () => {
const component = await render(
<EbayListboxButton prefixLabel="Selected:">
<EbayListboxButtonOption value="AA">Option 1</EbayListboxButtonOption>
<EbayListboxButtonOption value="BB">Option 2</EbayListboxButtonOption>
</EbayListboxButton>
);
expect(component.getByRole('button')).toHaveTextContent("Selected:-");
});
it('should display button label with prefix and selected option', async () => {
const component = await render(
<EbayListboxButton prefixLabel="Selected:" selected={1}>
<EbayListboxButtonOption value="AA">Option 1</EbayListboxButtonOption>
<EbayListboxButtonOption value="BB">Option 2</EbayListboxButtonOption>
</EbayListboxButton>
);
expect(component.getByRole('button')).toHaveTextContent("Selected:Option 2");
});
it('should preselect option by index', async () => {
const component = await render(
<EbayListboxButton selected={1}>
Expand Down
16 changes: 10 additions & 6 deletions src/ebay-listbox-button/__tests__/index.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ storiesOf(`ebay-listbox-button`, module)
<EbayListboxButtonOption value="BB">Option 2</EbayListboxButtonOption>
<EbayListboxButtonOption value="CC">Option 3</EbayListboxButtonOption>
</EbayListboxButton>
</>)) .add(`Preselected index`, () => (<>
</>))
.add(`Preselected index`, () => (<>
<EbayListboxButton selected={1}>
<EbayListboxButtonOption value="AA">Option 1</EbayListboxButtonOption>
<EbayListboxButtonOption value="BB">Option 2</EbayListboxButtonOption>
Expand Down Expand Up @@ -127,12 +128,15 @@ storiesOf(`ebay-listbox-button`, module)
<EbayListboxButtonOption value="102">Option 39</EbayListboxButtonOption>
</EbayListboxButton>
</>))
.add(`Prefix label`, () => (<>
<EbayListboxButton prefixLabel="Selected:">
<EbayListboxButtonOption value="AA">Option 1</EbayListboxButtonOption>
<EbayListboxButtonOption value="BB">Option 2</EbayListboxButtonOption>
<EbayListboxButtonOption value="CC">Option 3</EbayListboxButtonOption>
</EbayListboxButton>
</>))
.add(`Floating label`, () => (<>
<EbayListboxButton
value=""
floatingLabel="Select"
onSelect={action(`onSelect triggered`)}
>
<EbayListboxButton floatingLabel="Select">
<EbayListboxButtonOption value="AA">Option 1</EbayListboxButtonOption>
<EbayListboxButtonOption value="BB">Option 2</EbayListboxButtonOption>
<EbayListboxButtonOption value="CC">Option 3</EbayListboxButtonOption>
Expand Down
11 changes: 8 additions & 3 deletions src/ebay-listbox-button/listbox-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export type EbayListboxButtonProps = Omit<ComponentProps<'input'>, 'onChange'> &
fluid?: boolean;
maxHeight?: string;
prefixId?: string;
prefixLabel?: string;
floatingLabel?: string;
unselectedText?: string;
onChange?: EbayChangeEventHandler<HTMLButtonElement, ChangeEventProps>;
Expand All @@ -37,6 +38,7 @@ const ListboxButton: FC<EbayListboxButtonProps> = ({
className,
maxHeight,
prefixId,
prefixLabel,
floatingLabel,
unselectedText = '-',
onChange = () => {},
Expand Down Expand Up @@ -246,9 +248,12 @@ const ListboxButton: FC<EbayListboxButtonProps> = ({
{floatingLabel}
</span>
) : (
<span className="btn__text" id={expandBtnTextId}>
{selectedOption?.props.children || unselectedText}
</span>
<>
{prefixLabel && <span className="btn__label">{prefixLabel}</span>}
<span className="btn__text" id={expandBtnTextId}>
{selectedOption?.props.children || unselectedText}
</span>
</>
)

return (
Expand Down

0 comments on commit 7c32be8

Please sign in to comment.