@bitrise/bitkit 10.24.2-alpha-chakra.1 → 10.24.2

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.24.2-alpha-chakra.1",
4
+ "version": "10.24.2",
5
5
  "repository": "git@github.com:bitrise-io/bitkit.git",
6
6
  "main": "src/index.ts",
7
7
  "license": "UNLICENSED",
@@ -1,5 +1,6 @@
1
1
  import { useState } from 'react';
2
2
  import { ComponentStory, ComponentMeta } from '@storybook/react';
3
+ import { useForm } from 'react-hook-form';
3
4
  import { sortObjectByKey } from '../../utils/storyUtils';
4
5
  import Select from './Select';
5
6
 
@@ -54,6 +55,22 @@ export const Controlled = () => {
54
55
  );
55
56
  };
56
57
 
58
+ export const ControlledWithReactHookForm = () => {
59
+ const form = useForm({
60
+ defaultValues: {
61
+ field: undefined,
62
+ },
63
+ });
64
+
65
+ const props = form.register('field');
66
+
67
+ return (
68
+ <Select w="300px" placeholder="Test holder" {...props} isRequired>
69
+ {children}
70
+ </Select>
71
+ );
72
+ };
73
+
57
74
  export const WithLoading = () => (
58
75
  <>
59
76
  <Select w="300px" isDisabled placeholder="Test holder">
@@ -0,0 +1,96 @@
1
+ import { render, screen } from '@testing-library/react';
2
+ import userEvent from '@testing-library/user-event';
3
+ import { useForm } from 'react-hook-form';
4
+ import Select from './Select';
5
+
6
+ const WithReactHookForm = ({ placeholder }: { placeholder?: string }) => {
7
+ const form = useForm({ defaultValues: { rating: undefined } });
8
+ const props = form.register('rating');
9
+
10
+ return (
11
+ <Select placeholder={placeholder} {...props}>
12
+ <option value="bad">Bad</option>
13
+ <option value="good">Good</option>
14
+ </Select>
15
+ );
16
+ };
17
+
18
+ const placeholder = 'What was your impression?';
19
+
20
+ describe('Select', () => {
21
+ it('renders placeholder option', async () => {
22
+ render(
23
+ <Select placeholder={placeholder}>
24
+ <option value="bad">Bad</option>
25
+ <option value="good">Good</option>
26
+ </Select>,
27
+ );
28
+
29
+ const placeholderOption = screen.queryByRole('option', { name: placeholder });
30
+ expect(placeholderOption).toBeInTheDocument();
31
+ });
32
+
33
+ it('renders placeholder option as disabled', async () => {
34
+ render(
35
+ <Select placeholder={placeholder}>
36
+ <option value="bad">Bad</option>
37
+ <option value="good">Good</option>
38
+ </Select>,
39
+ );
40
+
41
+ const placeholderOption = screen.queryByRole('option', { name: placeholder });
42
+ expect(placeholderOption).toBeDisabled();
43
+ });
44
+
45
+ it('placeholder option is selected if no value is set', async () => {
46
+ render(
47
+ <Select placeholder={placeholder}>
48
+ <option value="bad">Bad</option>
49
+ <option value="good">Good</option>
50
+ </Select>,
51
+ );
52
+
53
+ const select = await screen.findByRole<HTMLInputElement>('combobox');
54
+ expect(select).toHaveValue('');
55
+ });
56
+
57
+ it('updates selected option without placeholder', async () => {
58
+ render(<WithReactHookForm />);
59
+
60
+ const select = await screen.findByRole<HTMLInputElement>('combobox');
61
+ await userEvent.selectOptions(select, 'good');
62
+ expect(select).toHaveValue('good');
63
+ });
64
+
65
+ it('updates selected option with placeholder', async () => {
66
+ render(<WithReactHookForm placeholder="What was your impression?" />);
67
+
68
+ const select = await screen.findByRole<HTMLInputElement>('combobox');
69
+ await userEvent.selectOptions(select, 'good');
70
+ expect(select).toHaveValue('good');
71
+ });
72
+
73
+ it('handles defaultValue correctly without placeholder', async () => {
74
+ render(
75
+ <Select defaultValue="good">
76
+ <option value="bad">Bad</option>
77
+ <option value="good">Good</option>
78
+ </Select>,
79
+ );
80
+
81
+ const select = await screen.findByRole<HTMLInputElement>('combobox');
82
+ expect(select).toHaveValue('good');
83
+ });
84
+
85
+ it('handles defaultValue correctly with placeholder', async () => {
86
+ render(
87
+ <Select placeholder="What was your impression?" defaultValue="good">
88
+ <option value="bad">Bad</option>
89
+ <option value="good">Good</option>
90
+ </Select>,
91
+ );
92
+
93
+ const select = await screen.findByRole<HTMLInputElement>('combobox');
94
+ expect(select).toHaveValue('good');
95
+ });
96
+ });
@@ -41,6 +41,7 @@ const Select = forwardRef<SelectProps, 'div'>((props, ref) => {
41
41
  placeholder,
42
42
  size,
43
43
  value,
44
+ defaultValue,
44
45
  ...rest
45
46
  } = props;
46
47
  const iconSize = size === 'medium' ? '24' : '16';
@@ -48,7 +49,6 @@ const Select = forwardRef<SelectProps, 'div'>((props, ref) => {
48
49
  isDisabled: isDisabled || isLoading,
49
50
  isInvalid: isInvalid || !!errorText,
50
51
  ...rest,
51
- ref,
52
52
  };
53
53
  const selectProperties: ChakraSelectProps = {
54
54
  icon: isLoading ? (
@@ -61,21 +61,23 @@ const Select = forwardRef<SelectProps, 'div'>((props, ref) => {
61
61
  onChange,
62
62
  size,
63
63
  value,
64
+ defaultValue,
64
65
  variant: 'select',
65
66
  };
66
67
  if (placeholder) {
67
- if ('value' in selectProperties) {
68
- selectProperties.value = selectProperties.value || '';
68
+ if ('value' in props) {
69
+ selectProperties.value = selectProperties.value ?? '';
69
70
  } else {
70
- selectProperties.defaultValue = '';
71
+ selectProperties.defaultValue = props.defaultValue ?? '';
71
72
  }
72
73
  }
74
+
73
75
  return (
74
76
  <FormControl {...formControlProps}>
75
77
  {label && <FormLabel>{label}</FormLabel>}
76
78
  <ChakraSelect data-testid={dataTestid} ref={ref} {...selectProperties}>
77
79
  {placeholder && (
78
- <option hidden disabled value="">
80
+ <option disabled value="">
79
81
  {placeholder}
80
82
  </option>
81
83
  )}
@@ -1,4 +1,4 @@
1
- import { Text as ChakraText, TextProps as ChakraTextProps, forwardRef } from '@chakra-ui/react';
1
+ import { Text as ChakraText, TextProps as ChakraTextProps, forwardRef, ResponsiveValue } from '@chakra-ui/react';
2
2
  import { TextSizes } from '../../Foundations/Typography/Typography';
3
3
 
4
4
  type TextTags =
@@ -37,21 +37,20 @@ type TextTags =
37
37
  | 'var';
38
38
 
39
39
  export interface TextProps extends ChakraTextProps {
40
- align?: 'center' | 'justify' | 'left' | 'right';
40
+ align?: ResponsiveValue<'center' | 'justify' | 'left' | 'right'>;
41
41
  /**
42
42
  * Any valid HTML text tag
43
43
  */
44
44
  as?: TextTags;
45
- className?: string;
46
45
  /**
47
46
  * Font weight
48
47
  */
49
- fontWeight?: 'bold' | 'normal' | string[];
48
+ fontWeight?: ResponsiveValue<'bold' | 'normal'>;
50
49
  /**
51
50
  * Size config (https://www.figma.com/file/grik9mTaJ5DfhydhWhXdP5/Bitkit-Foundations?node-id=211%3A12)
52
51
  */
53
- size?: TextSizes;
54
- textTransform?: 'capitalize' | 'lowercase' | 'none' | 'uppercase';
52
+ size?: ResponsiveValue<TextSizes>;
53
+ textTransform?: ResponsiveValue<'capitalize' | 'lowercase' | 'none' | 'uppercase'>;
55
54
  hasEllipsis?: boolean;
56
55
  }
57
56