@transferwise/components 0.0.0-experimental-dd495d2 → 0.0.0-experimental-3776330
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/build/index.esm.js +97 -1
- package/build/index.esm.js.map +1 -1
- package/build/index.js +97 -0
- package/build/index.js.map +1 -1
- package/build/main.css +103 -0
- package/build/styles/main.css +103 -0
- package/build/styles/segmentedControl/SegmentedControl.css +103 -0
- package/build/types/index.d.ts +2 -0
- package/build/types/index.d.ts.map +1 -1
- package/build/types/segmentedControl/SegmentedControl.d.ts +31 -0
- package/build/types/segmentedControl/SegmentedControl.d.ts.map +1 -0
- package/build/types/segmentedControl/index.d.ts +3 -0
- package/build/types/segmentedControl/index.d.ts.map +1 -0
- package/package.json +4 -11
- package/src/index.ts +2 -0
- package/src/main.css +103 -0
- package/src/main.less +1 -0
- package/src/segmentedControl/SegmentedControl.css +103 -0
- package/src/segmentedControl/SegmentedControl.less +103 -0
- package/src/segmentedControl/SegmentedControl.spec.tsx +106 -0
- package/src/segmentedControl/SegmentedControl.story.tsx +55 -0
- package/src/segmentedControl/SegmentedControl.tsx +152 -0
- package/src/segmentedControl/index.ts +2 -0
- package/src/ssr.spec.js +17 -0
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
.segmented-control {
|
|
2
|
+
box-sizing: border-box;
|
|
3
|
+
--segment-highlight-width: 0;
|
|
4
|
+
--segment-highlight-x: var(--size-4);
|
|
5
|
+
}
|
|
6
|
+
.segmented-control__segments {
|
|
7
|
+
display: inline-flex;
|
|
8
|
+
position: relative;
|
|
9
|
+
padding: 4px;
|
|
10
|
+
padding: var(--size-4);
|
|
11
|
+
width: 100%;
|
|
12
|
+
justify-content: center;
|
|
13
|
+
align-items: center;
|
|
14
|
+
background: rgba(134,167,189,0.10196);
|
|
15
|
+
background: var(--color-background-neutral);
|
|
16
|
+
border-radius: 24px;
|
|
17
|
+
border-radius: var(--size-24);
|
|
18
|
+
transition: outline 300ms;
|
|
19
|
+
outline: 2px solid transparent;
|
|
20
|
+
}
|
|
21
|
+
.segmented-control--input:has(:focus-visible) > .segmented-control__segments::after {
|
|
22
|
+
outline: 2px solid var(--color-interactive-primary);
|
|
23
|
+
}
|
|
24
|
+
.segmented-control__segments::after {
|
|
25
|
+
content: "";
|
|
26
|
+
position: absolute;
|
|
27
|
+
width: var(--segment-highlight-width);
|
|
28
|
+
top: 4px;
|
|
29
|
+
top: var(--size-4);
|
|
30
|
+
bottom: 4px;
|
|
31
|
+
bottom: var(--size-4);
|
|
32
|
+
left: var(--segment-highlight-x);
|
|
33
|
+
z-index: 0;
|
|
34
|
+
background: #ffffff;
|
|
35
|
+
background: var(--color-background-screen);
|
|
36
|
+
border-radius: 24px;
|
|
37
|
+
border-radius: var(--size-24);
|
|
38
|
+
transition: left 300ms;
|
|
39
|
+
}
|
|
40
|
+
.segmented-control__segments--no-animate::after {
|
|
41
|
+
transition: none !important;
|
|
42
|
+
}
|
|
43
|
+
.segmented-control__segment {
|
|
44
|
+
position: relative;
|
|
45
|
+
flex: 1 1 100%;
|
|
46
|
+
flex-flow: column;
|
|
47
|
+
padding: 8px 16px;
|
|
48
|
+
padding: var(--size-8) var(--size-16);
|
|
49
|
+
margin: 0 0 0 4px;
|
|
50
|
+
margin: 0 0 0 var(--size-4);
|
|
51
|
+
min-width: 0;
|
|
52
|
+
line-height: inherit;
|
|
53
|
+
align-items: center;
|
|
54
|
+
text-align: center;
|
|
55
|
+
vertical-align: middle;
|
|
56
|
+
border-radius: 24px;
|
|
57
|
+
border-radius: var(--size-24);
|
|
58
|
+
z-index: 1;
|
|
59
|
+
cursor: pointer;
|
|
60
|
+
transition: font-weight 300ms, background 300ms;
|
|
61
|
+
color: var(--color-interactive-primary);
|
|
62
|
+
}
|
|
63
|
+
.segmented-control__segment:first-child {
|
|
64
|
+
margin-left: 0;
|
|
65
|
+
}
|
|
66
|
+
.segmented-control__segment:hover {
|
|
67
|
+
background: rgba(0,0,0,0.10196);
|
|
68
|
+
background: var(--color-background-overlay);
|
|
69
|
+
}
|
|
70
|
+
.segmented-control__radio-input {
|
|
71
|
+
position: fixed;
|
|
72
|
+
opacity: 0;
|
|
73
|
+
pointer-events: none;
|
|
74
|
+
}
|
|
75
|
+
.segmented-control__button {
|
|
76
|
+
width: 100%;
|
|
77
|
+
height: 100%;
|
|
78
|
+
background: none;
|
|
79
|
+
-webkit-appearance: none;
|
|
80
|
+
-moz-appearance: none;
|
|
81
|
+
appearance: none;
|
|
82
|
+
border: none;
|
|
83
|
+
outline: none;
|
|
84
|
+
font: inherit;
|
|
85
|
+
outline: 2px solid transparent;
|
|
86
|
+
}
|
|
87
|
+
.segmented-control__button:focus {
|
|
88
|
+
outline-offset: 0px;
|
|
89
|
+
}
|
|
90
|
+
.segmented-control__button:focus-visible {
|
|
91
|
+
outline-color: var(--color-interactive-primary);
|
|
92
|
+
}
|
|
93
|
+
.segmented-control__selected-segment:hover {
|
|
94
|
+
background: transparent;
|
|
95
|
+
}
|
|
96
|
+
.segmented-control__selected-segment {
|
|
97
|
+
font-weight: 700;
|
|
98
|
+
font-weight: var(--font-weight-bold);
|
|
99
|
+
}
|
|
100
|
+
.segmented-control__label-text {
|
|
101
|
+
word-wrap: break-word;
|
|
102
|
+
color: var(--color-interactive-primary);
|
|
103
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
.segmented-control {
|
|
2
|
+
box-sizing: border-box;
|
|
3
|
+
--segment-highlight-width: 0;
|
|
4
|
+
--segment-highlight-x: var(--size-4);
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
.segmented-control__segments {
|
|
8
|
+
display: inline-flex;
|
|
9
|
+
position: relative;
|
|
10
|
+
padding: var(--size-4);
|
|
11
|
+
width: 100%;
|
|
12
|
+
justify-content: center;
|
|
13
|
+
align-items: center;
|
|
14
|
+
background: var(--color-background-neutral);
|
|
15
|
+
border-radius: var(--size-24);
|
|
16
|
+
transition: outline 300ms;
|
|
17
|
+
outline: 2px solid transparent;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
.segmented-control--input:has(:focus-visible) > .segmented-control__segments::after {
|
|
21
|
+
outline: 2px solid var(--color-interactive-primary);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
.segmented-control__segments::after {
|
|
25
|
+
content: "";
|
|
26
|
+
position: absolute;
|
|
27
|
+
width: var(--segment-highlight-width);
|
|
28
|
+
top: var(--size-4);
|
|
29
|
+
bottom: var(--size-4);
|
|
30
|
+
left: var(--segment-highlight-x);
|
|
31
|
+
z-index: 0;
|
|
32
|
+
background: var(--color-background-screen);
|
|
33
|
+
border-radius: var(--size-24);
|
|
34
|
+
transition: left 300ms;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
.segmented-control__segments--no-animate::after {
|
|
38
|
+
transition: none !important;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
.segmented-control__segment {
|
|
42
|
+
position: relative;
|
|
43
|
+
flex: 1 1 100%;
|
|
44
|
+
flex-flow: column;
|
|
45
|
+
padding: var(--size-8) var(--size-16);
|
|
46
|
+
margin: 0 0 0 var(--size-4);
|
|
47
|
+
min-width: 0;
|
|
48
|
+
line-height: inherit;
|
|
49
|
+
align-items: center;
|
|
50
|
+
text-align: center;
|
|
51
|
+
vertical-align: middle;
|
|
52
|
+
border-radius: var(--size-24);
|
|
53
|
+
z-index: 1;
|
|
54
|
+
cursor: pointer;
|
|
55
|
+
transition: font-weight 300ms, background 300ms;
|
|
56
|
+
color: var(--color-interactive-primary);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
.segmented-control__segment:first-child {
|
|
60
|
+
margin-left: 0;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
.segmented-control__segment:hover {
|
|
64
|
+
background: var(--color-background-overlay);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
.segmented-control__radio-input {
|
|
68
|
+
position: fixed;
|
|
69
|
+
opacity: 0;
|
|
70
|
+
pointer-events: none;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
.segmented-control__button {
|
|
74
|
+
width: 100%;
|
|
75
|
+
height: 100%;
|
|
76
|
+
background: none;
|
|
77
|
+
appearance: none;
|
|
78
|
+
border: none;
|
|
79
|
+
outline: none;
|
|
80
|
+
font: inherit;
|
|
81
|
+
outline: 2px solid transparent;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
.segmented-control__button:focus {
|
|
85
|
+
outline-offset: 0px;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
.segmented-control__button:focus-visible {
|
|
89
|
+
outline-color: var(--color-interactive-primary);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
.segmented-control__selected-segment:hover {
|
|
93
|
+
background: transparent;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
.segmented-control__selected-segment {
|
|
97
|
+
font-weight: var(--font-weight-bold);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
.segmented-control__label-text {
|
|
101
|
+
word-wrap: break-word;
|
|
102
|
+
color: var(--color-interactive-primary);
|
|
103
|
+
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import '@testing-library/jest-dom';
|
|
2
|
+
import { render, screen, userEvent } from '../test-utils';
|
|
3
|
+
|
|
4
|
+
import SegmentedControl, { SegmentedControlProps } from './SegmentedControl';
|
|
5
|
+
|
|
6
|
+
const defaultSegments = [
|
|
7
|
+
{
|
|
8
|
+
id: '1',
|
|
9
|
+
value: 'accounting',
|
|
10
|
+
label: 'Accounting',
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
id: '2',
|
|
14
|
+
value: 'payroll',
|
|
15
|
+
label: 'Payroll',
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
id: '3',
|
|
19
|
+
value: 'reporting',
|
|
20
|
+
label: 'Reporting',
|
|
21
|
+
},
|
|
22
|
+
];
|
|
23
|
+
|
|
24
|
+
const defaultSegmentsWithControls = defaultSegments.map((segment, index) => ({
|
|
25
|
+
...segment,
|
|
26
|
+
controls: `aControlId${index}`,
|
|
27
|
+
}));
|
|
28
|
+
|
|
29
|
+
const onChange = jest.fn();
|
|
30
|
+
|
|
31
|
+
const defaultProps: SegmentedControlProps = {
|
|
32
|
+
name: 'segmentedControl',
|
|
33
|
+
defaultValue: defaultSegments[0].value,
|
|
34
|
+
mode: 'input',
|
|
35
|
+
segments: defaultSegments,
|
|
36
|
+
onChange,
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const renderSegmentedControl = (overrides: Partial<SegmentedControlProps> = {}) => {
|
|
40
|
+
return render(<SegmentedControl {...defaultProps} {...(overrides as SegmentedControlProps)} />);
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
describe('SegmentedControl', () => {
|
|
44
|
+
it('dynamically adds inline style props for animated segment overlay', () => {
|
|
45
|
+
renderSegmentedControl();
|
|
46
|
+
|
|
47
|
+
const segmentedControls = screen.getByTestId('segmented-control');
|
|
48
|
+
|
|
49
|
+
expect(segmentedControls).toHaveStyle(
|
|
50
|
+
'--segment-highlight-width: 0px; --segment-highlight-x: 0px;',
|
|
51
|
+
);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('default value is selected', () => {
|
|
55
|
+
renderSegmentedControl({ mode: 'view', segments: defaultSegmentsWithControls });
|
|
56
|
+
|
|
57
|
+
const accountingTab = screen.getByRole('tab', { name: 'Accounting' });
|
|
58
|
+
expect(accountingTab).toHaveAttribute('aria-selected', 'true');
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('lets the user pick through the segments when it is set to input', () => {
|
|
62
|
+
renderSegmentedControl();
|
|
63
|
+
|
|
64
|
+
const payroll = screen.getByRole('radio', { name: 'Payroll' });
|
|
65
|
+
userEvent.click(payroll);
|
|
66
|
+
|
|
67
|
+
expect(onChange).toHaveBeenCalledWith('payroll');
|
|
68
|
+
|
|
69
|
+
const reporting = screen.getByRole('radio', { name: 'Reporting' });
|
|
70
|
+
userEvent.click(reporting);
|
|
71
|
+
|
|
72
|
+
expect(onChange).toHaveBeenCalledWith('reporting');
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('lets the user pick through the segments when it is set to view', () => {
|
|
76
|
+
renderSegmentedControl({ mode: 'view', segments: defaultSegmentsWithControls });
|
|
77
|
+
|
|
78
|
+
const payroll = screen.getByRole('tab', { name: 'Payroll' });
|
|
79
|
+
userEvent.click(payroll);
|
|
80
|
+
|
|
81
|
+
expect(onChange).toHaveBeenCalledWith('payroll');
|
|
82
|
+
|
|
83
|
+
const reporting = screen.getByRole('tab', { name: 'Reporting' });
|
|
84
|
+
userEvent.click(reporting);
|
|
85
|
+
|
|
86
|
+
expect(onChange).toHaveBeenCalledWith('reporting');
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('throws error if user tries to add too many segments', () => {
|
|
90
|
+
expect(() => {
|
|
91
|
+
renderSegmentedControl({
|
|
92
|
+
mode: 'input',
|
|
93
|
+
segments: [
|
|
94
|
+
...defaultSegments,
|
|
95
|
+
{
|
|
96
|
+
id: '4',
|
|
97
|
+
value: 'anotherOne',
|
|
98
|
+
label: 'Another One',
|
|
99
|
+
},
|
|
100
|
+
],
|
|
101
|
+
});
|
|
102
|
+
}).toThrow(
|
|
103
|
+
'SegmentedControl only supports up to 3 segments. Please refer to: https://wise.design/components/segmented-control',
|
|
104
|
+
);
|
|
105
|
+
});
|
|
106
|
+
});
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { StoryFn } from '@storybook/react';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
|
|
4
|
+
import SegmentedControl, { Segments } from './SegmentedControl';
|
|
5
|
+
|
|
6
|
+
export default {
|
|
7
|
+
component: SegmentedControl,
|
|
8
|
+
title: 'Forms/SegmentedControl',
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
const segments: Segments = [
|
|
12
|
+
{ id: 'CUPCAKE', label: 'Cupcakes', value: 'cupcakes' },
|
|
13
|
+
{ id: 'SPONGECAKE', label: 'Sponge cake', value: 'spongecake' },
|
|
14
|
+
{ id: 'CARROT_CAKE', label: 'Carrot cake', value: 'carrotcake' },
|
|
15
|
+
];
|
|
16
|
+
|
|
17
|
+
const segmentsWithControls: Segments = [
|
|
18
|
+
{ id: 'CUPCAKE', label: 'Cupcakes', value: 'cupcakes', controls: 'aControlId' },
|
|
19
|
+
{ id: 'SPONGECAKE', label: 'Sponge cake', value: 'spongecake', controls: 'aControlId' },
|
|
20
|
+
{ id: 'CARROT_CAKE', label: 'Carrot cake', value: 'carrotcake', controls: 'aControlId' },
|
|
21
|
+
];
|
|
22
|
+
|
|
23
|
+
const Template: StoryFn = (args) => {
|
|
24
|
+
const [selectedValue, setSelectedValue] = React.useState(segments[0].value);
|
|
25
|
+
|
|
26
|
+
return (
|
|
27
|
+
<div className="p-a-2">
|
|
28
|
+
<SegmentedControl
|
|
29
|
+
name="aSegmentedControl"
|
|
30
|
+
defaultValue={selectedValue}
|
|
31
|
+
onChange={setSelectedValue}
|
|
32
|
+
{...(args.mode === 'view'
|
|
33
|
+
? { segments: segmentsWithControls, mode: 'view', controls: 'aControlId' }
|
|
34
|
+
: { segments, mode: 'input' })}
|
|
35
|
+
/>
|
|
36
|
+
<div className="m-a-2" id="aControlId">
|
|
37
|
+
<p>Selected value: {selectedValue}</p>
|
|
38
|
+
</div>
|
|
39
|
+
</div>
|
|
40
|
+
);
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export const SegmentedControlDefault = {
|
|
44
|
+
render: Template,
|
|
45
|
+
args: {
|
|
46
|
+
mode: 'input',
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export const SegmentedControlView = {
|
|
51
|
+
render: Template,
|
|
52
|
+
args: {
|
|
53
|
+
mode: 'view',
|
|
54
|
+
},
|
|
55
|
+
};
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import classNames from 'classnames';
|
|
2
|
+
import { createRef, useEffect, useRef, useState } from 'react';
|
|
3
|
+
|
|
4
|
+
type SegmentBase = { id: string; label: string; value: string };
|
|
5
|
+
|
|
6
|
+
type Segment = SegmentBase & { controls?: never };
|
|
7
|
+
type SegmentWithControls = SegmentBase & { controls: string };
|
|
8
|
+
|
|
9
|
+
export type Segments = Segment[] | SegmentWithControls[];
|
|
10
|
+
|
|
11
|
+
type SegmentedControlPropsBase = {
|
|
12
|
+
name: string;
|
|
13
|
+
defaultValue: string;
|
|
14
|
+
mode: 'input' | 'view';
|
|
15
|
+
onChange: (value: string) => void;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
type SegmentedControlViewProps = {
|
|
19
|
+
mode: 'view';
|
|
20
|
+
segments: SegmentWithControls[];
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
type SegmentedControlInputProps = {
|
|
24
|
+
mode: 'input';
|
|
25
|
+
segments: Segment[];
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export type SegmentedControlProps = SegmentedControlPropsBase &
|
|
29
|
+
(SegmentedControlViewProps | SegmentedControlInputProps);
|
|
30
|
+
|
|
31
|
+
const SegmentedControl = ({
|
|
32
|
+
name,
|
|
33
|
+
defaultValue,
|
|
34
|
+
mode = 'input',
|
|
35
|
+
segments,
|
|
36
|
+
onChange,
|
|
37
|
+
}: SegmentedControlProps) => {
|
|
38
|
+
const [selectedValue, setSelectedValue] = useState(defaultValue || segments[0].value);
|
|
39
|
+
const [animate, setAnimate] = useState(false);
|
|
40
|
+
|
|
41
|
+
const segmentsRef = useRef<HTMLDivElement>(null);
|
|
42
|
+
|
|
43
|
+
if (segments.length > 3) {
|
|
44
|
+
throw new Error(
|
|
45
|
+
'SegmentedControl only supports up to 3 segments. Please refer to: https://wise.design/components/segmented-control',
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const segmentsWithRefs = segments.map((segment) => ({
|
|
50
|
+
...segment,
|
|
51
|
+
ref: createRef<HTMLLabelElement | HTMLButtonElement>(),
|
|
52
|
+
}));
|
|
53
|
+
|
|
54
|
+
const updateSegmentPosition = () => {
|
|
55
|
+
const selectedSegmentRef = segmentsWithRefs.find(
|
|
56
|
+
(segment) => segment.value === selectedValue,
|
|
57
|
+
)?.ref;
|
|
58
|
+
|
|
59
|
+
// We grab the active segments style object from the ref
|
|
60
|
+
// and set the css variables to the selected segments width and x position.
|
|
61
|
+
// This is so we can animate the highlight to the selected segment
|
|
62
|
+
if (selectedSegmentRef?.current && segmentsRef.current) {
|
|
63
|
+
const { style } = segmentsRef.current;
|
|
64
|
+
style.setProperty('--segment-highlight-width', `${selectedSegmentRef.current.offsetWidth}px`);
|
|
65
|
+
style.setProperty('--segment-highlight-x', `${selectedSegmentRef.current.offsetLeft}px`);
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
useEffect(() => {
|
|
70
|
+
updateSegmentPosition();
|
|
71
|
+
|
|
72
|
+
const handleWindowSizeChange = () => {
|
|
73
|
+
setAnimate(false);
|
|
74
|
+
updateSegmentPosition();
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
window.addEventListener('resize', handleWindowSizeChange);
|
|
78
|
+
return () => {
|
|
79
|
+
window.removeEventListener('resize', handleWindowSizeChange);
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
83
|
+
}, [segmentsWithRefs, selectedValue]);
|
|
84
|
+
|
|
85
|
+
useEffect(() => {
|
|
86
|
+
onChange(selectedValue);
|
|
87
|
+
}, [onChange, selectedValue]);
|
|
88
|
+
|
|
89
|
+
return (
|
|
90
|
+
<div
|
|
91
|
+
ref={segmentsRef}
|
|
92
|
+
data-testid="segmented-control"
|
|
93
|
+
className={classNames('segmented-control', {
|
|
94
|
+
'segmented-control--input': mode === 'input',
|
|
95
|
+
})}
|
|
96
|
+
>
|
|
97
|
+
<div
|
|
98
|
+
className={classNames('segmented-control__segments', {
|
|
99
|
+
'segmented-control__segments--no-animate': !animate,
|
|
100
|
+
})}
|
|
101
|
+
>
|
|
102
|
+
{segmentsWithRefs.map((segment) =>
|
|
103
|
+
mode === 'input' ? (
|
|
104
|
+
<label
|
|
105
|
+
ref={segment.ref as React.RefObject<HTMLLabelElement>}
|
|
106
|
+
key={segment.id}
|
|
107
|
+
htmlFor={segment.id}
|
|
108
|
+
className={classNames('segmented-control__segment', {
|
|
109
|
+
'segmented-control__selected-segment': selectedValue === segment.value,
|
|
110
|
+
})}
|
|
111
|
+
>
|
|
112
|
+
<input
|
|
113
|
+
type="radio"
|
|
114
|
+
className="segmented-control__radio-input"
|
|
115
|
+
id={segment.id}
|
|
116
|
+
name={name}
|
|
117
|
+
value={segment.value}
|
|
118
|
+
checked={selectedValue === segment.value}
|
|
119
|
+
onChange={() => {
|
|
120
|
+
setAnimate(true);
|
|
121
|
+
setSelectedValue(segment.value);
|
|
122
|
+
}}
|
|
123
|
+
/>
|
|
124
|
+
<div className="segmented-control__label-text">{segment.label}</div>
|
|
125
|
+
</label>
|
|
126
|
+
) : (
|
|
127
|
+
<button
|
|
128
|
+
ref={segment.ref as React.RefObject<HTMLButtonElement>}
|
|
129
|
+
key={segment.id}
|
|
130
|
+
type="button"
|
|
131
|
+
role="tab"
|
|
132
|
+
id={segment.id}
|
|
133
|
+
aria-controls={segment.controls}
|
|
134
|
+
aria-selected={selectedValue === segment.value}
|
|
135
|
+
className={classNames('segmented-control__segment', 'segmented-control__button', {
|
|
136
|
+
'segmented-control__selected-segment': selectedValue === segment.value,
|
|
137
|
+
})}
|
|
138
|
+
onClick={() => {
|
|
139
|
+
setAnimate(true);
|
|
140
|
+
setSelectedValue(segment.value);
|
|
141
|
+
}}
|
|
142
|
+
>
|
|
143
|
+
{segment.label}
|
|
144
|
+
</button>
|
|
145
|
+
),
|
|
146
|
+
)}
|
|
147
|
+
</div>
|
|
148
|
+
</div>
|
|
149
|
+
);
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
export default SegmentedControl;
|
package/src/ssr.spec.js
CHANGED
|
@@ -154,6 +154,23 @@ describe('Server side rendering', () => {
|
|
|
154
154
|
label: 'Two',
|
|
155
155
|
},
|
|
156
156
|
],
|
|
157
|
+
segments: [
|
|
158
|
+
{
|
|
159
|
+
id: '1',
|
|
160
|
+
value: 'accounting',
|
|
161
|
+
label: 'Accounting',
|
|
162
|
+
},
|
|
163
|
+
{
|
|
164
|
+
id: '2',
|
|
165
|
+
value: 'payroll',
|
|
166
|
+
label: 'Payroll',
|
|
167
|
+
},
|
|
168
|
+
{
|
|
169
|
+
id: '3',
|
|
170
|
+
value: 'reporting',
|
|
171
|
+
label: 'Reporting',
|
|
172
|
+
},
|
|
173
|
+
],
|
|
157
174
|
};
|
|
158
175
|
|
|
159
176
|
// Override props in case of name collision.
|