@transferwise/components 0.0.0-experimental-4b7aba1 → 0.0.0-experimental-16e2eec
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 +115 -368
- package/build/index.esm.js.map +1 -1
- package/build/index.js +114 -368
- package/build/index.js.map +1 -1
- package/build/main.css +0 -98
- package/build/styles/main.css +0 -98
- package/build/types/common/locale/index.d.ts +26 -43
- package/build/types/common/locale/index.d.ts.map +1 -1
- package/build/types/index.d.ts +0 -1
- package/build/types/index.d.ts.map +1 -1
- package/build/types/phoneNumberInput/PhoneNumberInput.d.ts +22 -27
- package/build/types/phoneNumberInput/PhoneNumberInput.d.ts.map +1 -1
- package/build/types/phoneNumberInput/data/countries.d.ts +5 -10
- package/build/types/phoneNumberInput/data/countries.d.ts.map +1 -1
- package/build/types/phoneNumberInput/index.d.ts +1 -1
- package/build/types/phoneNumberInput/index.d.ts.map +1 -1
- package/build/types/phoneNumberInput/utils/cleanNumber/cleanNumber.d.ts +1 -1
- package/build/types/phoneNumberInput/utils/cleanNumber/cleanNumber.d.ts.map +1 -1
- package/build/types/phoneNumberInput/utils/cleanNumber/index.d.ts +1 -1
- package/build/types/phoneNumberInput/utils/cleanNumber/index.d.ts.map +1 -1
- package/build/types/phoneNumberInput/utils/excludeCountries/excludeCountries.d.ts +8 -1
- package/build/types/phoneNumberInput/utils/excludeCountries/excludeCountries.d.ts.map +1 -1
- package/build/types/phoneNumberInput/utils/excludeCountries/index.d.ts +1 -1
- package/build/types/phoneNumberInput/utils/excludeCountries/index.d.ts.map +1 -1
- package/build/types/phoneNumberInput/utils/explodeNumberModel/index.d.ts +8 -4
- package/build/types/phoneNumberInput/utils/explodeNumberModel/index.d.ts.map +1 -1
- package/build/types/phoneNumberInput/utils/findCountryByCode/index.d.ts +1 -1
- package/build/types/phoneNumberInput/utils/findCountryByCode/index.d.ts.map +1 -1
- package/build/types/phoneNumberInput/utils/findCountryByPrefix/index.d.ts +1 -1
- package/build/types/phoneNumberInput/utils/findCountryByPrefix/index.d.ts.map +1 -1
- package/build/types/phoneNumberInput/utils/groupCountriesByPrefix/groupCountriesByPrefix.d.ts +2 -1
- package/build/types/phoneNumberInput/utils/groupCountriesByPrefix/groupCountriesByPrefix.d.ts.map +1 -1
- package/build/types/phoneNumberInput/utils/groupCountriesByPrefix/index.d.ts +1 -1
- package/build/types/phoneNumberInput/utils/groupCountriesByPrefix/index.d.ts.map +1 -1
- package/build/types/phoneNumberInput/utils/index.d.ts +11 -13
- package/build/types/phoneNumberInput/utils/index.d.ts.map +1 -1
- package/build/types/phoneNumberInput/utils/isStringNumeric/index.d.ts +1 -1
- package/build/types/phoneNumberInput/utils/isStringNumeric/index.d.ts.map +1 -1
- package/build/types/phoneNumberInput/utils/isStringNumeric/isStringNumeric.d.ts +1 -1
- package/build/types/phoneNumberInput/utils/isStringNumeric/isStringNumeric.d.ts.map +1 -1
- package/build/types/phoneNumberInput/utils/isValidPhoneNumber/index.d.ts +1 -1
- package/build/types/phoneNumberInput/utils/isValidPhoneNumber/index.d.ts.map +1 -1
- package/build/types/phoneNumberInput/utils/isValidPhoneNumber/isValidPhoneNumber.d.ts +6 -1
- package/build/types/phoneNumberInput/utils/isValidPhoneNumber/isValidPhoneNumber.d.ts.map +1 -1
- package/build/types/phoneNumberInput/utils/longestMatchingPrefix/index.d.ts +2 -1
- package/build/types/phoneNumberInput/utils/longestMatchingPrefix/index.d.ts.map +1 -1
- package/build/types/phoneNumberInput/utils/setDefaultPrefix/index.d.ts +7 -1
- package/build/types/phoneNumberInput/utils/setDefaultPrefix/index.d.ts.map +1 -1
- package/build/types/phoneNumberInput/utils/sortArrayByProperty/index.d.ts +1 -1
- package/build/types/phoneNumberInput/utils/sortArrayByProperty/index.d.ts.map +1 -1
- package/build/types/phoneNumberInput/utils/sortArrayByProperty/sortArrayByProperty.d.ts +1 -1
- package/build/types/phoneNumberInput/utils/sortArrayByProperty/sortArrayByProperty.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/common/locale/{index.spec.js → index.spec.ts} +4 -4
- package/src/common/locale/index.ts +96 -0
- package/src/index.ts +0 -1
- package/src/main.css +0 -98
- package/src/main.less +0 -1
- package/src/phoneNumberInput/PhoneNumberInput.spec.js +18 -22
- package/src/phoneNumberInput/PhoneNumberInput.tsx +193 -0
- package/src/phoneNumberInput/data/{countries.js → countries.ts} +9 -1
- package/src/phoneNumberInput/utils/cleanNumber/cleanNumber.ts +3 -0
- package/src/phoneNumberInput/utils/excludeCountries/{excludeCountries.spec.js → excludeCountries.spec.ts} +1 -1
- package/src/phoneNumberInput/utils/excludeCountries/{excludeCountries.js → excludeCountries.ts} +6 -5
- package/src/phoneNumberInput/utils/explodeNumberModel/{explodeNumberModel.spec.js → explodeNumberModel.spec.ts} +1 -1
- package/src/phoneNumberInput/utils/explodeNumberModel/index.ts +24 -0
- package/src/phoneNumberInput/utils/findCountryByCode/{findCountryByCode.spec.js → findCountryByCode.spec.ts} +0 -1
- package/src/phoneNumberInput/utils/findCountryByCode/index.ts +12 -0
- package/src/phoneNumberInput/utils/findCountryByPrefix/index.ts +12 -0
- package/src/phoneNumberInput/utils/groupCountriesByPrefix/groupCountriesByPrefix.spec.ts +102 -0
- package/src/phoneNumberInput/utils/groupCountriesByPrefix/groupCountriesByPrefix.ts +12 -0
- package/src/phoneNumberInput/utils/{index.js → index.ts} +0 -2
- package/src/phoneNumberInput/utils/isStringNumeric/{isStringNumeric.spec.js → isStringNumeric.spec.ts} +0 -1
- package/src/phoneNumberInput/utils/isStringNumeric/isStringNumeric.ts +1 -0
- package/src/phoneNumberInput/utils/isValidPhoneNumber/{isValidPhoneNumber.spec.js → isValidPhoneNumber.spec.ts} +1 -1
- package/src/phoneNumberInput/utils/isValidPhoneNumber/isValidPhoneNumber.ts +7 -0
- package/src/phoneNumberInput/utils/longestMatchingPrefix/index.ts +4 -0
- package/src/phoneNumberInput/utils/setDefaultPrefix/index.ts +20 -0
- package/src/phoneNumberInput/utils/sortArrayByProperty/sortArrayByProperty.ts +6 -0
- package/src/ssr.spec.js +0 -17
- package/build/styles/segmentedControl/SegmentedControl.css +0 -98
- package/build/types/phoneNumberInput/utils/filterOptionsForQuery/index.d.ts +0 -2
- package/build/types/phoneNumberInput/utils/filterOptionsForQuery/index.d.ts.map +0 -1
- package/build/types/phoneNumberInput/utils/isOptionAndFitsQuery/index.d.ts +0 -2
- package/build/types/phoneNumberInput/utils/isOptionAndFitsQuery/index.d.ts.map +0 -1
- package/build/types/phoneNumberInput/utils/isOptionAndFitsQuery/isOptionAndFitsQuery.d.ts +0 -3
- package/build/types/phoneNumberInput/utils/isOptionAndFitsQuery/isOptionAndFitsQuery.d.ts.map +0 -1
- package/build/types/segmentedControl/SegmentedControl.d.ts +0 -31
- package/build/types/segmentedControl/SegmentedControl.d.ts.map +0 -1
- package/build/types/segmentedControl/index.d.ts +0 -3
- package/build/types/segmentedControl/index.d.ts.map +0 -1
- package/src/common/locale/index.js +0 -139
- package/src/phoneNumberInput/PhoneNumberInput.js +0 -210
- package/src/phoneNumberInput/data/countries.spec.js +0 -12
- package/src/phoneNumberInput/utils/cleanNumber/cleanNumber.js +0 -4
- package/src/phoneNumberInput/utils/explodeNumberModel/index.js +0 -27
- package/src/phoneNumberInput/utils/filterOptionsForQuery/filterOptionsForQuery.spec.js +0 -36
- package/src/phoneNumberInput/utils/filterOptionsForQuery/index.js +0 -11
- package/src/phoneNumberInput/utils/findCountryByCode/index.js +0 -10
- package/src/phoneNumberInput/utils/findCountryByPrefix/index.js +0 -11
- package/src/phoneNumberInput/utils/groupCountriesByPrefix/groupCountriesByPrefix.js +0 -26
- package/src/phoneNumberInput/utils/groupCountriesByPrefix/groupCountriesByPrefix.spec.js +0 -67
- package/src/phoneNumberInput/utils/isOptionAndFitsQuery/index.js +0 -1
- package/src/phoneNumberInput/utils/isOptionAndFitsQuery/isOptionAndFitsQuery.js +0 -25
- package/src/phoneNumberInput/utils/isOptionAndFitsQuery/isOptionAndFitsQuery.spec.js +0 -66
- package/src/phoneNumberInput/utils/isStringNumeric/isStringNumeric.js +0 -1
- package/src/phoneNumberInput/utils/isValidPhoneNumber/isValidPhoneNumber.js +0 -10
- package/src/phoneNumberInput/utils/longestMatchingPrefix/index.js +0 -2
- package/src/phoneNumberInput/utils/setDefaultPrefix/index.js +0 -25
- package/src/phoneNumberInput/utils/sortArrayByProperty/sortArrayByProperty.js +0 -3
- package/src/segmentedControl/SegmentedControl.css +0 -98
- package/src/segmentedControl/SegmentedControl.less +0 -99
- package/src/segmentedControl/SegmentedControl.spec.tsx +0 -107
- package/src/segmentedControl/SegmentedControl.story.tsx +0 -55
- package/src/segmentedControl/SegmentedControl.tsx +0 -150
- package/src/segmentedControl/__snapshots__/SegmentedControl.spec.tsx.snap +0 -113
- package/src/segmentedControl/index.ts +0 -2
- /package/src/phoneNumberInput/{PhoneNumberInput.story.js → PhoneNumberInput.story.tsx} +0 -0
- /package/src/phoneNumberInput/{index.js → index.ts} +0 -0
- /package/src/phoneNumberInput/utils/cleanNumber/{cleanNumber.spec.js → cleanNumber.spec.ts} +0 -0
- /package/src/phoneNumberInput/utils/cleanNumber/{index.js → index.ts} +0 -0
- /package/src/phoneNumberInput/utils/excludeCountries/{index.js → index.ts} +0 -0
- /package/src/phoneNumberInput/utils/findCountryByPrefix/{findCountryByPrefix.spec.js → findCountryByPrefix.spec.ts} +0 -0
- /package/src/phoneNumberInput/utils/groupCountriesByPrefix/{index.js → index.ts} +0 -0
- /package/src/phoneNumberInput/utils/isStringNumeric/{index.js → index.ts} +0 -0
- /package/src/phoneNumberInput/utils/isValidPhoneNumber/{index.js → index.ts} +0 -0
- /package/src/phoneNumberInput/utils/longestMatchingPrefix/{longestMatchingPrefix.spec.js → longestMatchingPrefix.spec.ts} +0 -0
- /package/src/phoneNumberInput/utils/setDefaultPrefix/{setDefaultPrefix.spec.js → setDefaultPrefix.spec.ts} +0 -0
- /package/src/phoneNumberInput/utils/sortArrayByProperty/{index.js → index.ts} +0 -0
- /package/src/phoneNumberInput/utils/sortArrayByProperty/{sortArrayByProperty.spec.js → sortArrayByProperty.spec.ts} +0 -0
|
@@ -1,99 +0,0 @@
|
|
|
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
|
-
justify-content: center;
|
|
11
|
-
align-items: center;
|
|
12
|
-
background: var(--color-background-neutral);
|
|
13
|
-
padding: var(--size-4);
|
|
14
|
-
border-radius: var(--size-24);
|
|
15
|
-
width: 100%;
|
|
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
|
-
background: var(--color-background-screen);
|
|
27
|
-
width: var(--segment-highlight-width);
|
|
28
|
-
position: absolute;
|
|
29
|
-
top: var(--size-4);
|
|
30
|
-
bottom: var(--size-4);
|
|
31
|
-
left: var(--segment-highlight-x);
|
|
32
|
-
z-index: 0;
|
|
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
|
-
|
|
46
|
-
min-width: 0;
|
|
47
|
-
align-items: center;
|
|
48
|
-
text-align: center;
|
|
49
|
-
vertical-align: middle;
|
|
50
|
-
padding: var(--size-8) var(--size-16);
|
|
51
|
-
margin: 0;
|
|
52
|
-
border-radius: var(--size-24);
|
|
53
|
-
z-index: 1;
|
|
54
|
-
cursor: pointer;
|
|
55
|
-
transition: font-weight 300ms, background 300ms;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
.segmented-control__segment:hover {
|
|
59
|
-
background: var(--color-background-overlay);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
.segmented-control__radio-input {
|
|
63
|
-
position: fixed;
|
|
64
|
-
opacity: 0;
|
|
65
|
-
pointer-events: none;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
.segmented-control__button {
|
|
69
|
-
background: none;
|
|
70
|
-
appearance: none;
|
|
71
|
-
border: none;
|
|
72
|
-
outline: none;
|
|
73
|
-
width: 100%;
|
|
74
|
-
height: 100%;
|
|
75
|
-
font: inherit;
|
|
76
|
-
padding: none;
|
|
77
|
-
outline: 2px solid transparent;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
.segmented-control__button:focus {
|
|
81
|
-
outline-offset: 0px;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
.segmented-control__button:focus-visible {
|
|
85
|
-
outline-color: var(--color-interactive-primary);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
.segmented-control__selected-segment:hover {
|
|
89
|
-
background: transparent;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
.segmented-control__selected-segment {
|
|
93
|
-
font-weight: var(--font-weight-bold);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
.segmented-control__label-text {
|
|
97
|
-
word-wrap: break-word;
|
|
98
|
-
color: var(--color-interactive-primary);
|
|
99
|
-
}
|
|
@@ -1,107 +0,0 @@
|
|
|
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(`renders as expected when input`, () => {
|
|
45
|
-
expect(renderSegmentedControl().container).toMatchSnapshot();
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
it('renders as expected when set as a view', () => {
|
|
49
|
-
expect(
|
|
50
|
-
renderSegmentedControl({ mode: 'view', segments: defaultSegmentsWithControls }).container,
|
|
51
|
-
).toMatchSnapshot();
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
it('dynamically adds inline style props for animated segment overlay', () => {
|
|
55
|
-
renderSegmentedControl();
|
|
56
|
-
|
|
57
|
-
const segmentedControls = screen.getByTestId('segmented-control');
|
|
58
|
-
|
|
59
|
-
expect(segmentedControls).toHaveStyle(
|
|
60
|
-
'--segment-highlight-width: 0px; --segment-highlight-x: 0px;',
|
|
61
|
-
);
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
it('lets the user pick through the segments when it is set to input', () => {
|
|
65
|
-
renderSegmentedControl();
|
|
66
|
-
|
|
67
|
-
const payroll = screen.getByRole('radio', { name: 'Payroll' });
|
|
68
|
-
userEvent.click(payroll);
|
|
69
|
-
|
|
70
|
-
expect(onChange).toHaveBeenCalledWith('payroll');
|
|
71
|
-
|
|
72
|
-
const reporting = screen.getByRole('radio', { name: 'Reporting' });
|
|
73
|
-
userEvent.click(reporting);
|
|
74
|
-
|
|
75
|
-
expect(onChange).toHaveBeenCalledWith('reporting');
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
it('lets the user pick through the segments when it is set to view', () => {
|
|
79
|
-
renderSegmentedControl({ mode: 'view', segments: defaultSegmentsWithControls });
|
|
80
|
-
|
|
81
|
-
const payroll = screen.getByRole('tab', { name: 'Payroll' });
|
|
82
|
-
userEvent.click(payroll);
|
|
83
|
-
|
|
84
|
-
expect(onChange).toHaveBeenCalledWith('payroll');
|
|
85
|
-
|
|
86
|
-
const reporting = screen.getByRole('tab', { name: 'Reporting' });
|
|
87
|
-
userEvent.click(reporting);
|
|
88
|
-
|
|
89
|
-
expect(onChange).toHaveBeenCalledWith('reporting');
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
it('throws error if user tries to add too many segments', () => {
|
|
93
|
-
expect(() => {
|
|
94
|
-
renderSegmentedControl({
|
|
95
|
-
mode: 'input',
|
|
96
|
-
segments: [
|
|
97
|
-
...defaultSegments,
|
|
98
|
-
{
|
|
99
|
-
id: '4',
|
|
100
|
-
value: 'anotherOne',
|
|
101
|
-
label: 'Another One',
|
|
102
|
-
},
|
|
103
|
-
],
|
|
104
|
-
});
|
|
105
|
-
}).toThrow('SegmentedControl only supports up to 3 segments');
|
|
106
|
-
});
|
|
107
|
-
});
|
|
@@ -1,55 +0,0 @@
|
|
|
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
|
-
};
|
|
@@ -1,150 +0,0 @@
|
|
|
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('SegmentedControl only supports up to 3 segments');
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
const segmentsWithRefs = segments.map((segment) => ({
|
|
48
|
-
...segment,
|
|
49
|
-
ref: createRef<HTMLLabelElement | HTMLButtonElement>(),
|
|
50
|
-
}));
|
|
51
|
-
|
|
52
|
-
const updateSegmentPosition = () => {
|
|
53
|
-
const selectedSegmentRef = segmentsWithRefs.find(
|
|
54
|
-
(segment) => segment.value === selectedValue,
|
|
55
|
-
)?.ref;
|
|
56
|
-
|
|
57
|
-
// We grab the active segments style object from the ref
|
|
58
|
-
// and set the css variables to the selected segments width and x position.
|
|
59
|
-
// This is so we can animate the highlight to the selected segment
|
|
60
|
-
if (selectedSegmentRef?.current && segmentsRef.current) {
|
|
61
|
-
const { style } = segmentsRef.current;
|
|
62
|
-
style.setProperty('--segment-highlight-width', `${selectedSegmentRef.current.offsetWidth}px`);
|
|
63
|
-
style.setProperty('--segment-highlight-x', `${selectedSegmentRef.current.offsetLeft}px`);
|
|
64
|
-
}
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
useEffect(() => {
|
|
68
|
-
updateSegmentPosition();
|
|
69
|
-
|
|
70
|
-
const handleWindowSizeChange = () => {
|
|
71
|
-
setAnimate(false);
|
|
72
|
-
updateSegmentPosition();
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
window.addEventListener('resize', handleWindowSizeChange);
|
|
76
|
-
return () => {
|
|
77
|
-
window.removeEventListener('resize', handleWindowSizeChange);
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
81
|
-
}, [segmentsWithRefs, selectedValue]);
|
|
82
|
-
|
|
83
|
-
useEffect(() => {
|
|
84
|
-
onChange(selectedValue);
|
|
85
|
-
}, [onChange, selectedValue]);
|
|
86
|
-
|
|
87
|
-
return (
|
|
88
|
-
<div
|
|
89
|
-
ref={segmentsRef}
|
|
90
|
-
data-testid="segmented-control"
|
|
91
|
-
className={classNames('segmented-control', {
|
|
92
|
-
'segmented-control--input': mode === 'input',
|
|
93
|
-
})}
|
|
94
|
-
>
|
|
95
|
-
<div
|
|
96
|
-
className={classNames('segmented-control__segments', {
|
|
97
|
-
'segmented-control__segments--no-animate': !animate,
|
|
98
|
-
})}
|
|
99
|
-
>
|
|
100
|
-
{segmentsWithRefs.map((segment) =>
|
|
101
|
-
mode === 'input' ? (
|
|
102
|
-
<label
|
|
103
|
-
ref={segment.ref as React.RefObject<HTMLLabelElement>}
|
|
104
|
-
key={segment.id}
|
|
105
|
-
htmlFor={segment.id}
|
|
106
|
-
className={classNames('segmented-control__segment', {
|
|
107
|
-
'segmented-control__selected-segment': selectedValue === segment.value,
|
|
108
|
-
})}
|
|
109
|
-
>
|
|
110
|
-
<input
|
|
111
|
-
type="radio"
|
|
112
|
-
className="segmented-control__radio-input"
|
|
113
|
-
id={segment.id}
|
|
114
|
-
name={name}
|
|
115
|
-
value={segment.value}
|
|
116
|
-
checked={selectedValue === segment.value}
|
|
117
|
-
onChange={() => {
|
|
118
|
-
setAnimate(true);
|
|
119
|
-
setSelectedValue(segment.value);
|
|
120
|
-
}}
|
|
121
|
-
/>
|
|
122
|
-
<div className="segmented-control__label-text">{segment.label}</div>
|
|
123
|
-
</label>
|
|
124
|
-
) : (
|
|
125
|
-
<button
|
|
126
|
-
ref={segment.ref as React.RefObject<HTMLButtonElement>}
|
|
127
|
-
key={segment.id}
|
|
128
|
-
type="button"
|
|
129
|
-
role="tab"
|
|
130
|
-
id={segment.id}
|
|
131
|
-
aria-controls={segment.controls}
|
|
132
|
-
aria-selected={selectedValue === segment.value}
|
|
133
|
-
className={classNames('segmented-control__segment', 'segmented-control__button', {
|
|
134
|
-
'segmented-control__selected-segment': selectedValue === segment.value,
|
|
135
|
-
})}
|
|
136
|
-
onClick={() => {
|
|
137
|
-
setAnimate(true);
|
|
138
|
-
setSelectedValue(segment.value);
|
|
139
|
-
}}
|
|
140
|
-
>
|
|
141
|
-
{segment.label}
|
|
142
|
-
</button>
|
|
143
|
-
),
|
|
144
|
-
)}
|
|
145
|
-
</div>
|
|
146
|
-
</div>
|
|
147
|
-
);
|
|
148
|
-
};
|
|
149
|
-
|
|
150
|
-
export default SegmentedControl;
|
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
|
2
|
-
|
|
3
|
-
exports[`SegmentedControl renders as expected when input 1`] = `
|
|
4
|
-
<div>
|
|
5
|
-
<div
|
|
6
|
-
class="segmented-control segmented-control--input"
|
|
7
|
-
data-testid="segmented-control"
|
|
8
|
-
style="--segment-highlight-width: 0px; --segment-highlight-x: 0px;"
|
|
9
|
-
>
|
|
10
|
-
<div
|
|
11
|
-
class="segmented-control__segments segmented-control__segments--no-animate"
|
|
12
|
-
>
|
|
13
|
-
<label
|
|
14
|
-
class="segmented-control__segment segmented-control__selected-segment"
|
|
15
|
-
for="1"
|
|
16
|
-
>
|
|
17
|
-
<input
|
|
18
|
-
checked=""
|
|
19
|
-
class="segmented-control__radio-input"
|
|
20
|
-
id="1"
|
|
21
|
-
name="segmentedControl"
|
|
22
|
-
type="radio"
|
|
23
|
-
value="accounting"
|
|
24
|
-
/>
|
|
25
|
-
<div
|
|
26
|
-
class="segmented-control__label-text"
|
|
27
|
-
>
|
|
28
|
-
Accounting
|
|
29
|
-
</div>
|
|
30
|
-
</label>
|
|
31
|
-
<label
|
|
32
|
-
class="segmented-control__segment"
|
|
33
|
-
for="2"
|
|
34
|
-
>
|
|
35
|
-
<input
|
|
36
|
-
class="segmented-control__radio-input"
|
|
37
|
-
id="2"
|
|
38
|
-
name="segmentedControl"
|
|
39
|
-
type="radio"
|
|
40
|
-
value="payroll"
|
|
41
|
-
/>
|
|
42
|
-
<div
|
|
43
|
-
class="segmented-control__label-text"
|
|
44
|
-
>
|
|
45
|
-
Payroll
|
|
46
|
-
</div>
|
|
47
|
-
</label>
|
|
48
|
-
<label
|
|
49
|
-
class="segmented-control__segment"
|
|
50
|
-
for="3"
|
|
51
|
-
>
|
|
52
|
-
<input
|
|
53
|
-
class="segmented-control__radio-input"
|
|
54
|
-
id="3"
|
|
55
|
-
name="segmentedControl"
|
|
56
|
-
type="radio"
|
|
57
|
-
value="reporting"
|
|
58
|
-
/>
|
|
59
|
-
<div
|
|
60
|
-
class="segmented-control__label-text"
|
|
61
|
-
>
|
|
62
|
-
Reporting
|
|
63
|
-
</div>
|
|
64
|
-
</label>
|
|
65
|
-
</div>
|
|
66
|
-
</div>
|
|
67
|
-
</div>
|
|
68
|
-
`;
|
|
69
|
-
|
|
70
|
-
exports[`SegmentedControl renders as expected when set as a view 1`] = `
|
|
71
|
-
<div>
|
|
72
|
-
<div
|
|
73
|
-
class="segmented-control"
|
|
74
|
-
data-testid="segmented-control"
|
|
75
|
-
style="--segment-highlight-width: 0px; --segment-highlight-x: 0px;"
|
|
76
|
-
>
|
|
77
|
-
<div
|
|
78
|
-
class="segmented-control__segments segmented-control__segments--no-animate"
|
|
79
|
-
>
|
|
80
|
-
<button
|
|
81
|
-
aria-controls="aControlId0"
|
|
82
|
-
aria-selected="true"
|
|
83
|
-
class="segmented-control__segment segmented-control__button segmented-control__selected-segment"
|
|
84
|
-
id="1"
|
|
85
|
-
role="tab"
|
|
86
|
-
type="button"
|
|
87
|
-
>
|
|
88
|
-
Accounting
|
|
89
|
-
</button>
|
|
90
|
-
<button
|
|
91
|
-
aria-controls="aControlId1"
|
|
92
|
-
aria-selected="false"
|
|
93
|
-
class="segmented-control__segment segmented-control__button"
|
|
94
|
-
id="2"
|
|
95
|
-
role="tab"
|
|
96
|
-
type="button"
|
|
97
|
-
>
|
|
98
|
-
Payroll
|
|
99
|
-
</button>
|
|
100
|
-
<button
|
|
101
|
-
aria-controls="aControlId2"
|
|
102
|
-
aria-selected="false"
|
|
103
|
-
class="segmented-control__segment segmented-control__button"
|
|
104
|
-
id="3"
|
|
105
|
-
role="tab"
|
|
106
|
-
type="button"
|
|
107
|
-
>
|
|
108
|
-
Reporting
|
|
109
|
-
</button>
|
|
110
|
-
</div>
|
|
111
|
-
</div>
|
|
112
|
-
</div>
|
|
113
|
-
`;
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|