@ohos-ports/instructure-ui-toggle-details 11.7.5-beta.1

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.
Files changed (91) hide show
  1. package/CHANGELOG.md +1323 -0
  2. package/LICENSE.md +28 -0
  3. package/README.md +42 -0
  4. package/es/ToggleDetails/v1/index.js +177 -0
  5. package/es/ToggleDetails/v1/props.js +26 -0
  6. package/es/ToggleDetails/v1/styles.js +196 -0
  7. package/es/ToggleDetails/v1/theme.js +75 -0
  8. package/es/ToggleDetails/v2/index.js +181 -0
  9. package/es/ToggleDetails/v2/props.js +26 -0
  10. package/es/ToggleDetails/v2/styles.js +188 -0
  11. package/es/ToggleGroup/v1/index.js +172 -0
  12. package/es/ToggleGroup/v1/props.js +26 -0
  13. package/es/ToggleGroup/v1/styles.js +40 -0
  14. package/es/ToggleGroup/v1/theme.js +41 -0
  15. package/es/ToggleGroup/v2/index.js +178 -0
  16. package/es/ToggleGroup/v2/props.js +26 -0
  17. package/es/ToggleGroup/v2/styles.js +40 -0
  18. package/es/exports/a.js +26 -0
  19. package/es/exports/b.js +26 -0
  20. package/lib/ToggleDetails/v1/index.js +184 -0
  21. package/lib/ToggleDetails/v1/props.js +31 -0
  22. package/lib/ToggleDetails/v1/styles.js +202 -0
  23. package/lib/ToggleDetails/v1/theme.js +81 -0
  24. package/lib/ToggleDetails/v2/index.js +187 -0
  25. package/lib/ToggleDetails/v2/props.js +31 -0
  26. package/lib/ToggleDetails/v2/styles.js +194 -0
  27. package/lib/ToggleGroup/v1/index.js +180 -0
  28. package/lib/ToggleGroup/v1/props.js +31 -0
  29. package/lib/ToggleGroup/v1/styles.js +46 -0
  30. package/lib/ToggleGroup/v1/theme.js +47 -0
  31. package/lib/ToggleGroup/v2/index.js +185 -0
  32. package/lib/ToggleGroup/v2/props.js +31 -0
  33. package/lib/ToggleGroup/v2/styles.js +46 -0
  34. package/lib/exports/a.js +19 -0
  35. package/lib/exports/b.js +19 -0
  36. package/lib/package.json +1 -0
  37. package/package.json +94 -0
  38. package/src/ToggleDetails/v1/README.md +233 -0
  39. package/src/ToggleDetails/v1/index.tsx +204 -0
  40. package/src/ToggleDetails/v1/props.ts +114 -0
  41. package/src/ToggleDetails/v1/styles.ts +190 -0
  42. package/src/ToggleDetails/v1/theme.ts +80 -0
  43. package/src/ToggleDetails/v2/README.md +233 -0
  44. package/src/ToggleDetails/v2/index.tsx +206 -0
  45. package/src/ToggleDetails/v2/props.ts +115 -0
  46. package/src/ToggleDetails/v2/styles.ts +188 -0
  47. package/src/ToggleGroup/v1/README.md +93 -0
  48. package/src/ToggleGroup/v1/index.tsx +206 -0
  49. package/src/ToggleGroup/v1/props.ts +115 -0
  50. package/src/ToggleGroup/v1/styles.ts +47 -0
  51. package/src/ToggleGroup/v1/theme.ts +43 -0
  52. package/src/ToggleGroup/v2/README.md +93 -0
  53. package/src/ToggleGroup/v2/index.tsx +214 -0
  54. package/src/ToggleGroup/v2/props.ts +115 -0
  55. package/src/ToggleGroup/v2/styles.ts +47 -0
  56. package/src/exports/a.ts +29 -0
  57. package/src/exports/b.ts +29 -0
  58. package/tsconfig.build.json +26 -0
  59. package/tsconfig.json +4 -0
  60. package/types/ToggleDetails/v1/index.d.ts +56 -0
  61. package/types/ToggleDetails/v1/index.d.ts.map +1 -0
  62. package/types/ToggleDetails/v1/props.d.ts +55 -0
  63. package/types/ToggleDetails/v1/props.d.ts.map +1 -0
  64. package/types/ToggleDetails/v1/styles.d.ts +15 -0
  65. package/types/ToggleDetails/v1/styles.d.ts.map +1 -0
  66. package/types/ToggleDetails/v1/theme.d.ts +10 -0
  67. package/types/ToggleDetails/v1/theme.d.ts.map +1 -0
  68. package/types/ToggleDetails/v2/index.d.ts +55 -0
  69. package/types/ToggleDetails/v2/index.d.ts.map +1 -0
  70. package/types/ToggleDetails/v2/props.d.ts +56 -0
  71. package/types/ToggleDetails/v2/props.d.ts.map +1 -0
  72. package/types/ToggleDetails/v2/styles.d.ts +16 -0
  73. package/types/ToggleDetails/v2/styles.d.ts.map +1 -0
  74. package/types/ToggleGroup/v1/index.d.ts +55 -0
  75. package/types/ToggleGroup/v1/index.d.ts.map +1 -0
  76. package/types/ToggleGroup/v1/props.d.ts +65 -0
  77. package/types/ToggleGroup/v1/props.d.ts.map +1 -0
  78. package/types/ToggleGroup/v1/styles.d.ts +15 -0
  79. package/types/ToggleGroup/v1/styles.d.ts.map +1 -0
  80. package/types/ToggleGroup/v1/theme.d.ts +10 -0
  81. package/types/ToggleGroup/v1/theme.d.ts.map +1 -0
  82. package/types/ToggleGroup/v2/index.d.ts +54 -0
  83. package/types/ToggleGroup/v2/index.d.ts.map +1 -0
  84. package/types/ToggleGroup/v2/props.d.ts +66 -0
  85. package/types/ToggleGroup/v2/props.d.ts.map +1 -0
  86. package/types/ToggleGroup/v2/styles.d.ts +15 -0
  87. package/types/ToggleGroup/v2/styles.d.ts.map +1 -0
  88. package/types/exports/a.d.ts +5 -0
  89. package/types/exports/a.d.ts.map +1 -0
  90. package/types/exports/b.d.ts +5 -0
  91. package/types/exports/b.d.ts.map +1 -0
@@ -0,0 +1,115 @@
1
+ /*
2
+ * The MIT License (MIT)
3
+ *
4
+ * Copyright (c) 2015 - present Instructure, Inc.
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ import React from 'react'
26
+
27
+ import type { OtherHTMLAttributes } from '@instructure/shared-types'
28
+ import type { WithStyleProps, ComponentStyle } from '@instructure/emotion'
29
+ import type { NewComponentTypes } from '@instructure/ui-themes'
30
+ import type { ViewProps } from '@instructure/ui-view/latest'
31
+
32
+ type ToggleDetailsOwnProps = {
33
+ variant?: 'default' | 'filled'
34
+ /**
35
+ * The summary that displays and can be interacted with
36
+ */
37
+ summary: React.ReactNode
38
+ /**
39
+ * Whether the content is expanded or hidden
40
+ */
41
+ expanded?: boolean // TODO: controllable(PropTypes.bool, 'onToggle', 'defaultExpanded')
42
+ /**
43
+ * Whether the content is initially expanded or hidden (uncontrolled)
44
+ */
45
+ defaultExpanded?: boolean
46
+ onToggle?: (
47
+ event: React.KeyboardEvent<ViewProps> | React.MouseEvent<ViewProps>,
48
+ expanded: boolean
49
+ ) => void
50
+ /**
51
+ * The icon to display next to the summary text when content is hidden
52
+ */
53
+ icon?: (...args: any[]) => React.ReactElement
54
+ /**
55
+ * The icon to display when content is expanded
56
+ */
57
+ iconExpanded?: (...args: any[]) => React.ReactElement
58
+ /**
59
+ * Icon position at the start or end of the summary text
60
+ */
61
+ iconPosition?: 'start' | 'end'
62
+ /**
63
+ * should the summary fill the width of its container
64
+ */
65
+ fluidWidth?: boolean
66
+ /**
67
+ * Choose a size for the expand/collapse icon
68
+ */
69
+ size?: 'small' | 'medium' | 'large'
70
+ /**
71
+ * The toggleable content passed inside the ToggleDetails component
72
+ */
73
+ children?: React.ReactNode
74
+ }
75
+
76
+ type PropKeys = keyof ToggleDetailsOwnProps
77
+
78
+ type AllowedPropKeys = Readonly<Array<PropKeys>>
79
+
80
+ type ToggleDetailsProps = ToggleDetailsOwnProps &
81
+ WithStyleProps<
82
+ ReturnType<NewComponentTypes['ToggleDetails']>,
83
+ ToggleDetailsStyle
84
+ > &
85
+ OtherHTMLAttributes<ToggleDetailsOwnProps>
86
+
87
+ type ToggleDetailsStyle = ComponentStyle<
88
+ | 'toggleDetails'
89
+ | 'summary'
90
+ | 'summaryText'
91
+ | 'toggle'
92
+ | 'icon'
93
+ | 'details'
94
+ | 'content'
95
+ >
96
+
97
+ type ToggleDetailsStyleProps = {
98
+ animate: boolean
99
+ }
100
+ const allowedProps: AllowedPropKeys = [
101
+ 'variant',
102
+ 'summary',
103
+ 'expanded',
104
+ 'defaultExpanded',
105
+ 'onToggle',
106
+ 'icon',
107
+ 'iconExpanded',
108
+ 'iconPosition',
109
+ 'fluidWidth',
110
+ 'children',
111
+ 'size'
112
+ ]
113
+
114
+ export type { ToggleDetailsProps, ToggleDetailsStyleProps, ToggleDetailsStyle }
115
+ export { allowedProps }
@@ -0,0 +1,188 @@
1
+ /*
2
+ * The MIT License (MIT)
3
+ *
4
+ * Copyright (c) 2015 - present Instructure, Inc.
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ import { keyframes } from '@instructure/emotion'
26
+
27
+ import type { NewComponentTypes, SharedTokens } from '@instructure/ui-themes'
28
+ import type {
29
+ ToggleDetailsProps,
30
+ ToggleDetailsStyleProps,
31
+ ToggleDetailsStyle
32
+ } from './props'
33
+
34
+ const contentAnimation = keyframes`
35
+ to {
36
+ opacity: 1;
37
+ }`
38
+
39
+ /**
40
+ * ---
41
+ * private: true
42
+ * ---
43
+ * Generates the style object from the theme and provided additional information
44
+ * @param {Object} componentTheme The theme variable object.
45
+ * @param {Object} props the props of the component, the style is applied to
46
+ * @param {Object} sharedTokens the shared tokens of the component
47
+ * @param {Object} state the state of the component, the style is applied to
48
+ * @return {Object} The final style object, which will be used in the component
49
+ */
50
+ const generateStyle = (
51
+ componentTheme: ReturnType<NewComponentTypes['ToggleDetails']>,
52
+ props: ToggleDetailsProps,
53
+ sharedTokens: SharedTokens,
54
+ _state: ToggleDetailsStyleProps
55
+ ): ToggleDetailsStyle => {
56
+ const { fluidWidth, iconPosition, size, variant } = props
57
+
58
+ const positionIconAtEnd =
59
+ iconPosition === 'end' && (variant === 'filled' || fluidWidth)
60
+ const fluidWidthStyles = fluidWidth ? { display: 'block', width: '100%' } : {}
61
+
62
+ const iconPositionStyles = {
63
+ start: {
64
+ marginInlineEnd: componentTheme.iconMargin,
65
+ marginInlineStart: 0
66
+ },
67
+ end: {
68
+ marginInlineStart: componentTheme.iconMargin,
69
+ marginInlineEnd: 0
70
+ }
71
+ }
72
+
73
+ const fontSizeStyles = {
74
+ small: { fontSize: componentTheme.fontSizeSmall },
75
+ medium: { fontSize: componentTheme.fontSizeMedium },
76
+ large: { fontSize: componentTheme.fontSizeLarge }
77
+ }
78
+
79
+ const indentDetailsStyles =
80
+ iconPosition === 'start' && !fluidWidth
81
+ ? {
82
+ small: {
83
+ paddingInlineStart: `calc(${componentTheme.contentPaddingSmall} + ${componentTheme.togglePadding})`,
84
+ paddingInlineEnd: '0'
85
+ },
86
+ medium: {
87
+ paddingInlineStart: `calc(${componentTheme.contentPaddingMedium} + ${componentTheme.togglePadding})`,
88
+ paddingInlineEnd: '0'
89
+ },
90
+ large: {
91
+ paddingInlineStart: `calc(${componentTheme.contentPaddingLarge} + ${componentTheme.togglePadding})`,
92
+ paddingInlineEnd: '0'
93
+ }
94
+ }
95
+ : {
96
+ small: {},
97
+ medium: {},
98
+ large: {}
99
+ }
100
+
101
+ return {
102
+ toggleDetails: {
103
+ label: 'toggleDetails',
104
+ fontFamily: componentTheme.fontFamily,
105
+ fontWeight: componentTheme.fontWeight
106
+ },
107
+ summary: {
108
+ label: 'toggleDetails__summary',
109
+ position: 'relative',
110
+ display: 'flex',
111
+ alignItems: 'center',
112
+ boxSizing: 'border-box',
113
+ textAlign: 'start'
114
+ },
115
+ summaryText: {
116
+ label: 'toggleDetails__summaryText',
117
+ minWidth: '0.0625rem',
118
+ flexGrow: 1,
119
+ ...(positionIconAtEnd ? { flex: 1 } : {})
120
+ },
121
+ toggle: {
122
+ label: 'toggleDetails__toggle',
123
+ fontFamily: componentTheme.fontFamily,
124
+ lineHeight: componentTheme.lineHeight,
125
+ appearance: 'none',
126
+ cursor: 'pointer',
127
+ userSelect: 'none',
128
+ touchAction: 'manipulation',
129
+ position: 'relative',
130
+ overflow: 'visible',
131
+ boxSizing: 'border-box',
132
+ padding: '0',
133
+ margin: '0',
134
+ outline: 'none',
135
+ border: 'none',
136
+ background: 'transparent',
137
+ color: componentTheme.textColor,
138
+ '&::-moz-focus-inner': { padding: '0', margin: '0' },
139
+ '&::before': {
140
+ content: '""',
141
+ position: 'absolute',
142
+ top: '-0.375rem',
143
+ left: '-0.375rem',
144
+ right: '-0.375rem',
145
+ bottom: '-0.375rem',
146
+ border: `${componentTheme.toggleBorderWidth} ${componentTheme.toggleBorderStyle} ${sharedTokens.focusOutline.infoColor}`,
147
+ borderRadius: `calc(${componentTheme.toggleBorderRadius} * 1.5)`,
148
+ opacity: 0,
149
+ pointerEvents: 'none'
150
+ },
151
+ '&:focus': { '&::before': { opacity: 1 } },
152
+ '&:focus, &:hover, &:active': {
153
+ textDecoration: 'none',
154
+ color: componentTheme.textColor
155
+ },
156
+ textDecoration: 'none',
157
+ ...fluidWidthStyles,
158
+ ...fontSizeStyles[size!]
159
+ },
160
+ icon: {
161
+ label: 'toggleDetails__icon',
162
+ display: 'flex',
163
+ alignItems: 'center',
164
+ '& > svg': {
165
+ display: 'block' /* fix vertical alignment of icon */
166
+ },
167
+ ...iconPositionStyles[iconPosition!]
168
+ },
169
+ details: {
170
+ label: 'toggleDetails__details',
171
+ boxSizing: 'border-box',
172
+ paddingTop: componentTheme.togglePadding,
173
+ color: componentTheme.textColor,
174
+ lineHeight: componentTheme.lineHeight,
175
+ ...fontSizeStyles[size!],
176
+ ...indentDetailsStyles[size!]
177
+ },
178
+ content: {
179
+ label: 'toggleDetails__content',
180
+ opacity: 0.01,
181
+ animationName: contentAnimation,
182
+ animationFillMode: 'forwards',
183
+ animationDuration: '.3s'
184
+ }
185
+ }
186
+ }
187
+
188
+ export default generateStyle
@@ -0,0 +1,93 @@
1
+ ---
2
+ describes: ToggleGroup
3
+ ---
4
+
5
+ Performs the same function as [`ToggleDetails`](ToggleDetails), but with the summary separated from the
6
+ toggle button, and built in padding and borders around the summary and main content area.
7
+
8
+ ### Basic example
9
+
10
+ ```javascript
11
+ ---
12
+ type: example
13
+ ---
14
+ <ToggleGroup
15
+ toggleLabel="This is the toggle button label for screenreaders"
16
+ summary="This is the summary"
17
+ background="default"
18
+ >
19
+ <View display="block" padding="small">Here is the expanded content</View>
20
+ </ToggleGroup>
21
+ ```
22
+
23
+ ### More detailed examples
24
+
25
+ #### `defaultExpanded` to make the component `expanded` when it renders
26
+
27
+ ```javascript
28
+ ---
29
+ type: example
30
+ ---
31
+ <ToggleGroup
32
+ toggleLabel="This is the toggle button label for screenreaders"
33
+ summary="This is the summary"
34
+ defaultExpanded
35
+ >
36
+ <View display="block" padding="small">This content is expanded when the component renders</View>
37
+ </ToggleGroup>
38
+ ```
39
+
40
+ #### Passing in your own `icon` and `iconExpanded`
41
+
42
+ ```javascript
43
+ ---
44
+ type: example
45
+ ---
46
+ <ToggleGroup
47
+ toggleLabel="This is the toggle button label for screenreaders"
48
+ summary="This is the summary"
49
+ iconExpanded={IconXSolid}
50
+ icon={IconPlusSolid}
51
+ >
52
+ <View display="block" padding="small">Here is the expanded content</View>
53
+ </ToggleGroup>
54
+ ```
55
+
56
+ #### Disable default transition of details
57
+
58
+ ```javascript
59
+ ---
60
+ type: example
61
+ ---
62
+ <ToggleGroup
63
+ transition={false}
64
+ toggleLabel="This is the toggle button label for screenreaders"
65
+ summary="This is the summary"
66
+ >
67
+ <View display="block" padding="small">This content will not fade in</View>
68
+ </ToggleGroup>
69
+ ```
70
+
71
+ #### Disable default border if you want to nest ToggleGroups
72
+
73
+ ```javascript
74
+ ---
75
+ type: example
76
+ ---
77
+ <ToggleGroup
78
+ defaultExpanded
79
+ toggleLabel="This is the toggle button label for screenreaders"
80
+ summary={<Heading level="h3">Parent ToggleGroup</Heading>}
81
+ >
82
+ <ToggleGroup
83
+ size="small"
84
+ toggleLabel="This is the toggle button label for screenreaders"
85
+ summary="I am nested inside a parent ToggleGroup"
86
+ border={false}
87
+ >
88
+ <View display="block" padding="small">
89
+ This is the details section of the nested ToggleGroup
90
+ </View>
91
+ </ToggleGroup>
92
+ </ToggleGroup>
93
+ ```
@@ -0,0 +1,206 @@
1
+ /*
2
+ * The MIT License (MIT)
3
+ *
4
+ * Copyright (c) 2015 - present Instructure, Inc.
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ import { Component } from 'react'
26
+
27
+ import {
28
+ omitProps,
29
+ pickProps,
30
+ getElementType,
31
+ callRenderProp
32
+ } from '@instructure/ui-react-utils'
33
+ import { IconButton } from '@instructure/ui-buttons/v11_6'
34
+ import { Transition } from '@instructure/ui-motion'
35
+ import { Expandable } from '@instructure/ui-expandable'
36
+ import type { ExpandableToggleProps } from '@instructure/ui-expandable'
37
+ import { isActiveElement } from '@instructure/ui-dom-utils'
38
+ import { Flex } from '@instructure/ui-flex/v11_6'
39
+ import { View } from '@instructure/ui-view/v11_6'
40
+ import {
41
+ IconArrowOpenEndSolid,
42
+ IconArrowOpenDownSolid
43
+ } from '@instructure/ui-icons'
44
+ import type { ToggleGroupProps } from './props'
45
+ import { allowedProps } from './props.js'
46
+
47
+ import { withStyle } from '@instructure/emotion'
48
+
49
+ import generateStyle from './styles.js'
50
+ import generateComponentTheme from './theme.js'
51
+
52
+ /**
53
+ ---
54
+ category: components
55
+ ---
56
+ **/
57
+ @withStyle(generateStyle, generateComponentTheme)
58
+ class ToggleGroup extends Component<ToggleGroupProps> {
59
+ static displayName = 'ToggleGroup'
60
+ static readonly componentId = 'ToggleGroup'
61
+
62
+ static allowedProps = allowedProps
63
+
64
+ static defaultProps = {
65
+ size: 'medium',
66
+ icon: IconArrowOpenEndSolid,
67
+ iconExpanded: IconArrowOpenDownSolid,
68
+ defaultExpanded: false,
69
+ transition: true,
70
+ as: 'span',
71
+ border: true
72
+ } as const
73
+
74
+ ref: Element | null = null
75
+ _button: HTMLElement | null = null
76
+ _shouldTransition = false
77
+
78
+ handleRef = (el: Element | null) => {
79
+ const { elementRef } = this.props
80
+
81
+ this.ref = el
82
+
83
+ if (typeof elementRef === 'function') {
84
+ elementRef(el)
85
+ }
86
+ }
87
+
88
+ handleButtonRef = (el: Element | null) => {
89
+ this._button = el as HTMLElement
90
+ }
91
+
92
+ get focused() {
93
+ return isActiveElement(this._button)
94
+ }
95
+
96
+ focus() {
97
+ this._button?.focus()
98
+ }
99
+
100
+ componentDidMount() {
101
+ this._shouldTransition = true
102
+ }
103
+
104
+ componentDidUpdate() {
105
+ this.props.makeStyles?.(this.state)
106
+ }
107
+
108
+ renderIcon(expanded: boolean) {
109
+ const Icon = expanded ? this.props.iconExpanded : this.props.icon
110
+ return Icon ? callRenderProp(Icon) : null
111
+ }
112
+
113
+ renderToggle(
114
+ toggleProps: ReturnType<ExpandableToggleProps>,
115
+ expanded: boolean
116
+ ) {
117
+ const { toggleLabel, size } = this.props
118
+ let label
119
+ if (typeof toggleLabel === 'function') {
120
+ label = toggleLabel(expanded)
121
+ } else {
122
+ label = toggleLabel
123
+ }
124
+
125
+ const props = { ...toggleProps } as Record<string, any>
126
+ return (
127
+ <IconButton
128
+ {...props}
129
+ withBackground={false}
130
+ withBorder={false}
131
+ size={size === 'large' ? 'medium' : 'small'}
132
+ elementRef={this.handleButtonRef}
133
+ screenReaderLabel={label}
134
+ >
135
+ {this.renderIcon(expanded)}
136
+ </IconButton>
137
+ )
138
+ }
139
+
140
+ renderDetails(detailsProps: { id: string }) {
141
+ const { styles } = this.props
142
+ return (
143
+ <View
144
+ {...detailsProps}
145
+ display="block"
146
+ borderWidth="small none none none"
147
+ borderColor={styles?.borderColor}
148
+ >
149
+ {this.props.transition && this._shouldTransition ? (
150
+ <Transition transitionOnMount in type="fade">
151
+ {this.props.children}
152
+ </Transition>
153
+ ) : (
154
+ this.props.children
155
+ )}
156
+ </View>
157
+ )
158
+ }
159
+
160
+ render() {
161
+ const Element = getElementType(ToggleGroup, this.props)
162
+ const { styles } = this.props
163
+ return (
164
+ <Expandable {...pickProps(this.props, Expandable.allowedProps)}>
165
+ {({ expanded, getToggleProps, getDetailsProps }) => {
166
+ return (
167
+ <View
168
+ {...omitProps(this.props, ToggleGroup.allowedProps)}
169
+ borderWidth={this.props.border ? 'small' : 'none'}
170
+ as={Element}
171
+ elementRef={this.handleRef}
172
+ display="block"
173
+ borderRadius="medium"
174
+ background="primary"
175
+ borderColor={styles?.borderColor}
176
+ data-cid="ToggleGroup"
177
+ >
178
+ <Flex
179
+ padding={
180
+ this.props.size === 'small'
181
+ ? 'x-small'
182
+ : 'small small small x-small'
183
+ }
184
+ >
185
+ <Flex.Item>
186
+ {this.renderToggle(getToggleProps(), expanded)}
187
+ </Flex.Item>
188
+ <Flex.Item shouldGrow shouldShrink padding="0 0 0 x-small">
189
+ {this.props.summary}
190
+ </Flex.Item>
191
+ </Flex>
192
+ {expanded ? (
193
+ this.renderDetails(getDetailsProps())
194
+ ) : (
195
+ <span {...getDetailsProps()} />
196
+ )}
197
+ </View>
198
+ )
199
+ }}
200
+ </Expandable>
201
+ )
202
+ }
203
+ }
204
+
205
+ export default ToggleGroup
206
+ export { ToggleGroup }