@charcoal-ui/react 4.0.0-beta.2 → 4.0.0-beta.3

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,6 +1,6 @@
1
1
  {
2
2
  "name": "@charcoal-ui/react",
3
- "version": "4.0.0-beta.2",
3
+ "version": "4.0.0-beta.3",
4
4
  "license": "Apache-2.0",
5
5
  "main": "./dist/index.cjs.js",
6
6
  "module": "./dist/index.esm.js",
@@ -26,7 +26,7 @@
26
26
  "clean": "rimraf dist .tsbuildinfo"
27
27
  },
28
28
  "devDependencies": {
29
- "@charcoal-ui/esbuild-plugin-styled-components": "^4.0.0-beta.2",
29
+ "@charcoal-ui/esbuild-plugin-styled-components": "^4.0.0-beta.3",
30
30
  "@react-types/switch": "^3.1.2",
31
31
  "@storybook/addon-actions": "^7.4.1",
32
32
  "@storybook/addon-knobs": "^7.0.2",
@@ -60,10 +60,10 @@
60
60
  "typescript": "^4.9.5"
61
61
  },
62
62
  "dependencies": {
63
- "@charcoal-ui/icons": "^4.0.0-beta.2",
64
- "@charcoal-ui/styled": "^4.0.0-beta.2",
65
- "@charcoal-ui/theme": "^4.0.0-beta.2",
66
- "@charcoal-ui/utils": "^4.0.0-beta.2",
63
+ "@charcoal-ui/icons": "^4.0.0-beta.3",
64
+ "@charcoal-ui/styled": "^4.0.0-beta.3",
65
+ "@charcoal-ui/theme": "^4.0.0-beta.3",
66
+ "@charcoal-ui/utils": "^4.0.0-beta.3",
67
67
  "@react-aria/button": "^3.9.1",
68
68
  "@react-aria/checkbox": "^3.13.0",
69
69
  "@react-aria/dialog": "^3.5.10",
@@ -96,5 +96,5 @@
96
96
  "url": "https://github.com/pixiv/charcoal.git",
97
97
  "directory": "packages/react"
98
98
  },
99
- "gitHead": "21a8c3ad738b69cfc849a3a7ec75dbae71eb9e97"
99
+ "gitHead": "9b982f2d1e64be2c9b665bdeb50397dfa679d0fd"
100
100
  }
@@ -1,18 +1,5 @@
1
1
  // Jest Snapshot v1, https://goo.gl/fbAQLP
2
2
 
3
- exports[`Storybook Tests Button Active 1`] = `
4
- <div
5
- data-dark={false}
6
- >
7
- <button
8
- className="charcoal-button"
9
- data-active={true}
10
- >
11
- Button
12
- </button>
13
- </div>
14
- `;
15
-
16
3
  exports[`Storybook Tests Button Danger 1`] = `
17
4
  <div
18
5
  data-dark={false}
@@ -21,7 +8,7 @@ exports[`Storybook Tests Button Danger 1`] = `
21
8
  className="charcoal-button"
22
9
  data-variant="Danger"
23
10
  >
24
- Button
11
+ Danger
25
12
  </button>
26
13
  </div>
27
14
  `;
@@ -46,12 +33,12 @@ exports[`Storybook Tests Button Disabled 1`] = `
46
33
  className="charcoal-button"
47
34
  disabled={true}
48
35
  >
49
- Button
36
+ Disabled
50
37
  </button>
51
38
  </div>
52
39
  `;
53
40
 
54
- exports[`Storybook Tests Button Fixed 1`] = `
41
+ exports[`Storybook Tests Button FullWidth 1`] = `
55
42
  <div
56
43
  data-dark={false}
57
44
  >
@@ -59,33 +46,20 @@ exports[`Storybook Tests Button Fixed 1`] = `
59
46
  className="charcoal-button"
60
47
  data-full-width={true}
61
48
  >
62
- Button
49
+ Full width
63
50
  </button>
64
51
  </div>
65
52
  `;
66
53
 
67
- exports[`Storybook Tests Button Focus 1`] = `
54
+ exports[`Storybook Tests Button IsActive 1`] = `
68
55
  <div
69
56
  data-dark={false}
70
57
  >
71
58
  <button
72
59
  className="charcoal-button"
73
- onMouseOver={[Function]}
74
- >
75
- Mouse over to focus
76
- </button>
77
- </div>
78
- `;
79
-
80
- exports[`Storybook Tests Button Link 1`] = `
81
- <div
82
- data-dark={false}
83
- >
84
- <button
85
- className="charcoal-button"
86
- to="#"
60
+ data-active={true}
87
61
  >
88
- Button
62
+ Active
89
63
  </button>
90
64
  </div>
91
65
  `;
@@ -98,19 +72,7 @@ exports[`Storybook Tests Button Navigation 1`] = `
98
72
  className="charcoal-button"
99
73
  data-variant="Navigation"
100
74
  >
101
- Button
102
- </button>
103
- </div>
104
- `;
105
-
106
- exports[`Storybook Tests Button Nihongo 1`] = `
107
- <div
108
- data-dark={false}
109
- >
110
- <button
111
- className="charcoal-button"
112
- >
113
- 日本語だよ
75
+ Navigation
114
76
  </button>
115
77
  </div>
116
78
  `;
@@ -123,7 +85,7 @@ exports[`Storybook Tests Button Overlay 1`] = `
123
85
  className="charcoal-button"
124
86
  data-variant="Overlay"
125
87
  >
126
- Button
88
+ Overlay
127
89
  </button>
128
90
  </div>
129
91
  `;
@@ -136,7 +98,7 @@ exports[`Storybook Tests Button Primary 1`] = `
136
98
  className="charcoal-button"
137
99
  data-variant="Primary"
138
100
  >
139
- Button
101
+ Primary
140
102
  </button>
141
103
  </div>
142
104
  `;
@@ -149,7 +111,7 @@ exports[`Storybook Tests Button Small 1`] = `
149
111
  className="charcoal-button"
150
112
  data-size="S"
151
113
  >
152
- Button
114
+ Small
153
115
  </button>
154
116
  </div>
155
117
  `;
@@ -0,0 +1,11 @@
1
+ // Jest Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+ exports[`<Button /> componentAs props are passed to the data-as attribute 1`] = `
4
+ <a
5
+ className="charcoal-button"
6
+ data-as="componentAs"
7
+ data-full-width={true}
8
+ href="/"
9
+ target="_blank"
10
+ />
11
+ `;
@@ -1,96 +1,43 @@
1
- import { useRef } from 'react'
2
- import { ClickableElement } from '../Clickable'
3
1
  import Button from '.'
4
2
  import { Meta, StoryObj } from '@storybook/react'
5
3
 
6
4
  export default {
7
5
  title: 'Button',
8
6
  component: Button,
9
- argTypes: {
10
- variant: {
11
- control: {
12
- type: 'inline-radio',
13
- options: ['Primary', 'Default', 'Overlay', 'Danger', 'Navigation'],
14
- },
15
- },
16
- size: {
17
- control: {
18
- type: 'inline-radio',
19
- options: ['S', 'M'],
20
- },
21
- },
22
- },
23
- render: (args) => <Button {...args}>Button</Button>,
24
7
  } as Meta<typeof Button>
25
8
 
26
- export const Default: StoryObj<typeof Button> = {}
9
+ export const Default: StoryObj<typeof Button> = {
10
+ render: (args) => <Button {...args}>Button</Button>,
11
+ }
27
12
 
28
13
  export const Primary: StoryObj<typeof Button> = {
29
- args: {
30
- variant: 'Primary',
31
- },
14
+ render: () => <Button variant="Primary">Primary</Button>,
32
15
  }
33
16
 
34
17
  export const Navigation: StoryObj<typeof Button> = {
35
- args: {
36
- variant: 'Navigation',
37
- },
18
+ render: () => <Button variant="Navigation">Navigation</Button>,
38
19
  }
39
20
 
40
21
  export const Overlay: StoryObj<typeof Button> = {
41
- args: {
42
- variant: 'Overlay',
43
- },
22
+ render: () => <Button variant="Overlay">Overlay</Button>,
44
23
  }
45
24
 
46
25
  export const Danger: StoryObj<typeof Button> = {
47
- args: {
48
- variant: 'Danger',
49
- },
26
+ render: () => <Button variant="Danger">Danger</Button>,
50
27
  }
51
28
 
52
29
  export const Small: StoryObj<typeof Button> = {
53
- args: {
54
- size: 'S',
55
- },
30
+ render: () => <Button size="S">Small</Button>,
56
31
  }
57
32
 
58
- export const Fixed: StoryObj<typeof Button> = {
59
- args: {
60
- fullWidth: true,
61
- },
33
+ export const FullWidth: StoryObj<typeof Button> = {
34
+ render: () => <Button fullWidth>Full width</Button>,
62
35
  }
63
36
 
64
37
  export const Disabled: StoryObj<typeof Button> = {
65
- args: {
66
- disabled: true,
67
- },
68
- }
69
-
70
- export const Active: StoryObj<typeof Button> = {
71
- args: {
72
- isActive: true,
73
- },
74
- }
75
-
76
- export const Link: StoryObj<typeof Button> = {
77
- args: {
78
- to: '#',
79
- },
80
- }
81
-
82
- export const Nihongo: StoryObj<typeof Button> = {
83
- render: (args) => <Button {...args}>日本語だよ</Button>,
38
+ render: () => <Button disabled>Disabled</Button>,
84
39
  }
85
40
 
86
- export const Focus: StoryObj<typeof Button> = {
87
- render: function Render(args) {
88
- const ref = useRef<ClickableElement>(null)
89
- const focus = () => ref.current?.focus()
90
- return (
91
- <Button {...args} onMouseOver={focus} ref={ref}>
92
- Mouse over to focus
93
- </Button>
94
- )
95
- },
41
+ export const IsActive: StoryObj<typeof Button> = {
42
+ render: () => <Button isActive>Active</Button>,
96
43
  }
@@ -0,0 +1,32 @@
1
+ import 'jest-styled-components'
2
+
3
+ import renderer from 'react-test-renderer'
4
+ import Button from '.'
5
+
6
+ function Link({
7
+ as,
8
+ children,
9
+ ...props
10
+ }: { as: string; children: React.ReactNode } & React.ComponentProps<'a'>) {
11
+ return (
12
+ <a {...props} data-as={as}>
13
+ {children}
14
+ </a>
15
+ )
16
+ }
17
+
18
+ describe('<Button />', () => {
19
+ test('componentAs props are passed to the data-as attribute', () => {
20
+ expect(
21
+ renderer.create(
22
+ <Button
23
+ href="/"
24
+ target="_blank"
25
+ fullWidth
26
+ as={Link}
27
+ componentAs="componentAs"
28
+ />
29
+ )
30
+ ).toMatchSnapshot()
31
+ })
32
+ })
@@ -14,8 +14,16 @@ export type ButtonProps<T extends React.ElementType = 'button'> = {
14
14
  size?: Size
15
15
  fullWidth?: boolean
16
16
  isActive?: boolean
17
+ /**
18
+ * The component used for root element.
19
+ * @type T extends React.ElementType = 'button'
20
+ */
17
21
  as?: T
18
- } & Omit<React.ComponentPropsWithRef<T>, 'children'>
22
+ /**
23
+ * The as property of the component specified by the Button component's as attribute.
24
+ */
25
+ componentAs?: React.ComponentPropsWithRef<T>['as']
26
+ } & Omit<React.ComponentPropsWithRef<T>, 'children' | 'as'>
19
27
 
20
28
  const Button = forwardRef(function Button<T extends React.ElementType>(
21
29
  {
@@ -25,6 +33,7 @@ const Button = forwardRef(function Button<T extends React.ElementType>(
25
33
  className,
26
34
  as,
27
35
  isActive,
36
+ componentAs,
28
37
  ...props
29
38
  }: ButtonProps<T>,
30
39
  ref: ForwardedRef<HTMLButtonElement>
@@ -34,6 +43,7 @@ const Button = forwardRef(function Button<T extends React.ElementType>(
34
43
  return (
35
44
  <Component
36
45
  {...props}
46
+ as={componentAs}
37
47
  className={classNames}
38
48
  data-variant={variant}
39
49
  data-size={size}
@@ -140,6 +140,7 @@ exports[`Storybook Tests DropdownSelector AssistiveText 1`] = `
140
140
  }
141
141
  >
142
142
  <select
143
+ onChange={[Function]}
143
144
  tabIndex={-1}
144
145
  value="1"
145
146
  >
@@ -301,6 +302,7 @@ exports[`Storybook Tests DropdownSelector CustomChildren 1`] = `
301
302
  }
302
303
  >
303
304
  <select
305
+ onChange={[Function]}
304
306
  tabIndex={-1}
305
307
  value="bold"
306
308
  >
@@ -465,6 +467,7 @@ exports[`Storybook Tests DropdownSelector Default 1`] = `
465
467
  }
466
468
  >
467
469
  <select
470
+ onChange={[Function]}
468
471
  tabIndex={-1}
469
472
  value=""
470
473
  >
@@ -622,6 +625,7 @@ exports[`Storybook Tests DropdownSelector Disabled 1`] = `
622
625
  }
623
626
  >
624
627
  <select
628
+ onChange={[Function]}
625
629
  tabIndex={-1}
626
630
  value="1"
627
631
  >
@@ -779,6 +783,7 @@ exports[`Storybook Tests DropdownSelector DisabledItem 1`] = `
779
783
  }
780
784
  >
781
785
  <select
786
+ onChange={[Function]}
782
787
  tabIndex={-1}
783
788
  value="2"
784
789
  >
@@ -949,6 +954,7 @@ exports[`Storybook Tests DropdownSelector InFormTag 1`] = `
949
954
  >
950
955
  <select
951
956
  name="exampleOption"
957
+ onChange={[Function]}
952
958
  tabIndex={-1}
953
959
  value="1"
954
960
  >
@@ -1708,6 +1714,7 @@ exports[`Storybook Tests DropdownSelector InModal 1`] = `
1708
1714
  }
1709
1715
  >
1710
1716
  <select
1717
+ onChange={[Function]}
1711
1718
  tabIndex={-1}
1712
1719
  value="1"
1713
1720
  >
@@ -1884,6 +1891,7 @@ exports[`Storybook Tests DropdownSelector InModal 1`] = `
1884
1891
  }
1885
1892
  >
1886
1893
  <select
1894
+ onChange={[Function]}
1887
1895
  tabIndex={-1}
1888
1896
  value="2"
1889
1897
  >
@@ -2062,6 +2070,7 @@ exports[`Storybook Tests DropdownSelector Invalid 1`] = `
2062
2070
  }
2063
2071
  >
2064
2072
  <select
2073
+ onChange={[Function]}
2065
2074
  tabIndex={-1}
2066
2075
  value="1"
2067
2076
  >
@@ -2248,6 +2257,7 @@ exports[`Storybook Tests DropdownSelector InvalidAssistiveText 1`] = `
2248
2257
  }
2249
2258
  >
2250
2259
  <select
2260
+ onChange={[Function]}
2251
2261
  tabIndex={-1}
2252
2262
  value="1"
2253
2263
  >
@@ -2512,6 +2522,7 @@ exports[`Storybook Tests DropdownSelector Label 1`] = `
2512
2522
  }
2513
2523
  >
2514
2524
  <select
2525
+ onChange={[Function]}
2515
2526
  tabIndex={-1}
2516
2527
  value="1"
2517
2528
  >
@@ -2668,6 +2679,7 @@ exports[`Storybook Tests DropdownSelector LongNames 1`] = `
2668
2679
  }
2669
2680
  >
2670
2681
  <select
2682
+ onChange={[Function]}
2671
2683
  tabIndex={-1}
2672
2684
  value="1"
2673
2685
  >
@@ -2964,6 +2976,7 @@ exports[`Storybook Tests DropdownSelector RequiredText 1`] = `
2964
2976
  }
2965
2977
  >
2966
2978
  <select
2979
+ onChange={[Function]}
2967
2980
  tabIndex={-1}
2968
2981
  value="1"
2969
2982
  >
@@ -3120,6 +3133,7 @@ exports[`Storybook Tests DropdownSelector Section 1`] = `
3120
3133
  }
3121
3134
  >
3122
3135
  <select
3136
+ onChange={[Function]}
3123
3137
  tabIndex={-1}
3124
3138
  value="banana"
3125
3139
  >
@@ -3401,6 +3415,7 @@ exports[`Storybook Tests DropdownSelector SubLabel 1`] = `
3401
3415
  }
3402
3416
  >
3403
3417
  <select
3418
+ onChange={[Function]}
3404
3419
  tabIndex={-1}
3405
3420
  value="1"
3406
3421
  >
@@ -1,4 +1,4 @@
1
- import { ReactNode, useState, useRef, useMemo } from 'react'
1
+ import React, { ReactNode, useState, useRef, useMemo, useCallback } from 'react'
2
2
  import styled, { css } from 'styled-components'
3
3
  import { disabledSelector } from '@charcoal-ui/utils'
4
4
  import Icon from '../Icon'
@@ -31,7 +31,10 @@ export type DropdownSelectorProps = {
31
31
 
32
32
  const defaultRequiredText = '*必須'
33
33
 
34
- export default function DropdownSelector(props: DropdownSelectorProps) {
34
+ export default function DropdownSelector({
35
+ onChange,
36
+ ...props
37
+ }: DropdownSelectorProps) {
35
38
  const triggerRef = useRef<HTMLButtonElement>(null)
36
39
  const [isOpen, setIsOpen] = useState(false)
37
40
  const preview = findPreviewRecursive(props.children, props.value)
@@ -45,6 +48,13 @@ export default function DropdownSelector(props: DropdownSelectorProps) {
45
48
 
46
49
  const { visuallyHiddenProps } = useVisuallyHidden()
47
50
 
51
+ const handleChange = useCallback(
52
+ (e: React.ChangeEvent<HTMLSelectElement>) => {
53
+ onChange(e.target.value)
54
+ },
55
+ [onChange]
56
+ )
57
+
48
58
  return (
49
59
  <DropdownSelectorRoot aria-disabled={props.disabled}>
50
60
  {props.showLabel === true && (
@@ -56,7 +66,12 @@ export default function DropdownSelector(props: DropdownSelectorProps) {
56
66
  />
57
67
  )}
58
68
  <div {...visuallyHiddenProps} aria-hidden="true">
59
- <select name={props.name} value={props.value} tabIndex={-1}>
69
+ <select
70
+ name={props.name}
71
+ value={props.value}
72
+ onChange={handleChange}
73
+ tabIndex={-1}
74
+ >
60
75
  {propsArray.map((itemProps) => {
61
76
  return (
62
77
  <option
@@ -96,7 +111,7 @@ export default function DropdownSelector(props: DropdownSelectorProps) {
96
111
  <MenuList
97
112
  value={props.value}
98
113
  onChange={(v) => {
99
- props.onChange(v)
114
+ onChange(v)
100
115
  setIsOpen(false)
101
116
  }}
102
117
  >
@@ -685,6 +685,7 @@ exports[`Storybook Tests Modal BackgroundScroll 1`] = `
685
685
  }
686
686
  >
687
687
  <select
688
+ onChange={[Function]}
688
689
  tabIndex={-1}
689
690
  value="10"
690
691
  >
@@ -1784,6 +1785,7 @@ exports[`Storybook Tests Modal Default 1`] = `
1784
1785
  }
1785
1786
  >
1786
1787
  <select
1788
+ onChange={[Function]}
1787
1789
  tabIndex={-1}
1788
1790
  value="10"
1789
1791
  >