@bitrise/bitkit 10.26.1 → 10.27.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@bitrise/bitkit",
3
3
  "description": "Bitrise React component library",
4
- "version": "10.26.1",
4
+ "version": "10.27.0",
5
5
  "repository": "git@github.com:bitrise-io/bitkit.git",
6
6
  "main": "src/index.ts",
7
7
  "license": "UNLICENSED",
@@ -16,7 +16,7 @@ type DropdownContext<T> = {
16
16
  searchOnSubmit: () => void;
17
17
  filter?: string;
18
18
  activeIndex?: number | null;
19
- getItemProps: (x: object) => object;
19
+ getItemProps: (userProps?: React.HTMLProps<HTMLElement> | undefined) => Record<string, unknown>;
20
20
  };
21
21
 
22
22
  const [DropdownContextProvider, useDropdownContext] = createContext<DropdownContext<unknown>>();
@@ -581,4 +581,46 @@ describe('Dropdown', () => {
581
581
  expect(button).toHaveTextContent('Custom label');
582
582
  });
583
583
  });
584
+
585
+ describe('option onClick support', () => {
586
+ it('calls onClick handler on option', async () => {
587
+ const onChangeHandler = jest.fn();
588
+ const onClickHandler = jest.fn();
589
+
590
+ render(
591
+ <Dropdown onChange={onChangeHandler}>
592
+ <DropdownOption value="Test option 1">Test option 1</DropdownOption>
593
+ <DropdownOption value="Test option 2" onClick={onClickHandler}>
594
+ Test option 2
595
+ </DropdownOption>
596
+ </Dropdown>,
597
+ );
598
+
599
+ const button = await screen.findByRole('combobox');
600
+ await userEvent.click(button);
601
+ const option2 = await screen.findByRole('option', { name: 'Test option 2' });
602
+ await userEvent.click(option2);
603
+ expect(onClickHandler).toHaveBeenCalled();
604
+ expect(onChangeHandler).toHaveBeenCalled();
605
+ });
606
+
607
+ it("doesn't call onChange handler if option's onClick handler prevented it", async () => {
608
+ const onChangeHandler = jest.fn();
609
+
610
+ render(
611
+ <Dropdown onChange={onChangeHandler}>
612
+ <DropdownOption value="Test option 1">Test option 1</DropdownOption>
613
+ <DropdownOption value="Test option 2" onClick={(event) => event.preventDefault()}>
614
+ Test option 2
615
+ </DropdownOption>
616
+ </Dropdown>,
617
+ );
618
+
619
+ const button = await screen.findByRole('combobox');
620
+ await userEvent.click(button);
621
+ const option2 = await screen.findByRole('option', { name: 'Test option 2' });
622
+ await userEvent.click(option2);
623
+ expect(onChangeHandler).not.toHaveBeenCalled();
624
+ });
625
+ });
584
626
  });
@@ -1,4 +1,4 @@
1
- import { ReactNode, useId } from 'react';
1
+ import { MouseEventHandler, ReactNode, useId } from 'react';
2
2
  import { chakra } from '@chakra-ui/react';
3
3
  import { cx } from '@chakra-ui/utils';
4
4
  import Text from '../Text/Text';
@@ -9,8 +9,9 @@ export type DropdownOptionProps<T> = {
9
9
  value?: T | null;
10
10
  children?: ReactNode;
11
11
  'aria-label'?: string;
12
+ onClick?: MouseEventHandler<HTMLElement>;
12
13
  };
13
- const DropdownOption = <T = string,>({ value = null, children, ...rest }: DropdownOptionProps<T>) => {
14
+ const DropdownOption = <T = string,>({ value = null, children, onClick, ...rest }: DropdownOptionProps<T>) => {
14
15
  const { item } = useDropdownStyles();
15
16
  const ctx = useDropdownContext<T | null>();
16
17
  const { index } = rest as { index?: number };
@@ -30,8 +31,12 @@ const DropdownOption = <T = string,>({ value = null, children, ...rest }: Dropdo
30
31
  )}
31
32
  __css={item}
32
33
  {...ctx.getItemProps({
33
- onClick() {
34
- ctx.onOptionSelected({ value, index, label: children });
34
+ onClick(event) {
35
+ onClick?.(event);
36
+
37
+ if (!event.defaultPrevented) {
38
+ ctx.onOptionSelected({ value, index, label: children });
39
+ }
35
40
  },
36
41
  })}
37
42
  >