@transferwise/components 0.0.0-experimental-c736330 → 0.0.0-experimental-a2f2fc6

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.
@@ -1 +1 @@
1
- {"version":3,"file":"SegmentedControl.d.ts","sourceRoot":"","sources":["../../../src/segmentedControl/SegmentedControl.tsx"],"names":[],"mappings":";AAMA,KAAK,WAAW,GAAG;IAAE,EAAE,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAAC;AAEhE,KAAK,OAAO,GAAG,WAAW,GAAG;IAAE,QAAQ,CAAC,EAAE,KAAK,CAAA;CAAE,CAAC;AAClD,KAAK,mBAAmB,GAAG,WAAW,GAAG;IAAE,QAAQ,EAAE,MAAM,CAAA;CAAE,CAAC;AAE9D,MAAM,MAAM,QAAQ,GAAG,SAAS,OAAO,EAAE,GAAG,SAAS,mBAAmB,EAAE,CAAC;AAE3E,KAAK,yBAAyB,GAAG;IAC/B,IAAI,EAAE,MAAM,CAAC;IACb,YAAY,EAAE,MAAM,CAAC;IACrB,IAAI,EAAE,OAAO,GAAG,MAAM,CAAC;IACvB,QAAQ,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;CACnC,CAAC;AAEF,KAAK,yBAAyB,GAAG;IAC/B,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,SAAS,mBAAmB,EAAE,CAAC;CAC1C,CAAC;AAEF,KAAK,0BAA0B,GAAG;IAChC,IAAI,EAAE,OAAO,CAAC;IACd,QAAQ,EAAE,SAAS,OAAO,EAAE,CAAC;CAC9B,CAAC;AAEF,MAAM,MAAM,qBAAqB,GAAG,yBAAyB,GAC3D,CAAC,yBAAyB,GAAG,0BAA0B,CAAC,CAAC;AAE3D,QAAA,MAAM,gBAAgB,sDAMnB,qBAAqB,gCAsIvB,CAAC;AAEF,eAAe,gBAAgB,CAAC"}
1
+ {"version":3,"file":"SegmentedControl.d.ts","sourceRoot":"","sources":["../../../src/segmentedControl/SegmentedControl.tsx"],"names":[],"mappings":";AAMA,KAAK,WAAW,GAAG;IAAE,EAAE,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAAC;AAEhE,KAAK,OAAO,GAAG,WAAW,GAAG;IAAE,QAAQ,CAAC,EAAE,KAAK,CAAA;CAAE,CAAC;AAClD,KAAK,mBAAmB,GAAG,WAAW,GAAG;IAAE,QAAQ,EAAE,MAAM,CAAA;CAAE,CAAC;AAE9D,MAAM,MAAM,QAAQ,GAAG,SAAS,OAAO,EAAE,GAAG,SAAS,mBAAmB,EAAE,CAAC;AAE3E,KAAK,yBAAyB,GAAG;IAC/B,IAAI,EAAE,MAAM,CAAC;IACb,YAAY,EAAE,MAAM,CAAC;IACrB,IAAI,EAAE,OAAO,GAAG,MAAM,CAAC;IACvB,QAAQ,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;CACnC,CAAC;AAEF,KAAK,yBAAyB,GAAG;IAC/B,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,SAAS,mBAAmB,EAAE,CAAC;CAC1C,CAAC;AAEF,KAAK,0BAA0B,GAAG;IAChC,IAAI,EAAE,OAAO,CAAC;IACd,QAAQ,EAAE,SAAS,OAAO,EAAE,CAAC;CAC9B,CAAC;AAEF,MAAM,MAAM,qBAAqB,GAAG,yBAAyB,GAC3D,CAAC,yBAAyB,GAAG,0BAA0B,CAAC,CAAC;AAE3D,QAAA,MAAM,gBAAgB,sDAMnB,qBAAqB,gCAiIvB,CAAC;AAEF,eAAe,gBAAgB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@transferwise/components",
3
- "version": "0.0.0-experimental-c736330",
3
+ "version": "0.0.0-experimental-a2f2fc6",
4
4
  "description": "Neptune React components",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -107,9 +107,9 @@ function InputAddon({
107
107
  const ref = useRef<HTMLSpanElement>(null);
108
108
  useResizeObserver(ref, (entry) => {
109
109
  // TODO: Remove fallback once most browsers support `borderBoxSize`
110
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
111
- if (entry.borderBoxSize != null) {
112
- setInputPadding(entry.borderBoxSize[0].inlineSize);
110
+ const inlineSize = entry.borderBoxSize?.[0]?.inlineSize;
111
+ if (inlineSize != null) {
112
+ setInputPadding(inlineSize);
113
113
  } else {
114
114
  const targetStyle = getComputedStyle(entry.target);
115
115
  setInputPadding(
@@ -1,5 +1,7 @@
1
1
  import '@testing-library/jest-dom';
2
- import { render, screen, userEvent } from '../test-utils';
2
+ import React from 'react';
3
+
4
+ import { render, screen, userEvent, waitFor } from '../test-utils';
3
5
 
4
6
  import SegmentedControl, { SegmentedControlProps } from './SegmentedControl';
5
7
 
@@ -86,6 +88,52 @@ describe('SegmentedControl', () => {
86
88
  expect(onChange).toHaveBeenCalledWith('reporting');
87
89
  });
88
90
 
91
+ it('does not call onChange on mount', async () => {
92
+ let onChangeCallCount = 0;
93
+
94
+ const ParentComponent = () => {
95
+ const [_, simulateRerender] = React.useState({});
96
+
97
+ // new function is created on every render
98
+ const onChange = () => {
99
+ onChangeCallCount++;
100
+ simulateRerender({});
101
+ };
102
+
103
+ return <SegmentedControl {...defaultProps} onChange={onChange} />;
104
+ };
105
+
106
+ render(<ParentComponent />);
107
+
108
+ await waitFor(() => {
109
+ expect(onChangeCallCount).toBe(0);
110
+ });
111
+ });
112
+
113
+ it('does not repeatedly call the onChange when the provided onChange prop changes on every render of its parent component', async () => {
114
+ let onChangeCallCount = 0;
115
+
116
+ const ParentComponent = () => {
117
+ const [_, simulateRerender] = React.useState({});
118
+
119
+ // a new onChange function is created on every render
120
+ const onChange = () => {
121
+ onChangeCallCount++;
122
+ simulateRerender({});
123
+ };
124
+
125
+ return <SegmentedControl {...defaultProps} onChange={onChange} />;
126
+ };
127
+
128
+ const { rerender } = render(<ParentComponent />);
129
+
130
+ rerender(<ParentComponent />);
131
+
132
+ await waitFor(() => {
133
+ expect(onChangeCallCount).toBe(0);
134
+ });
135
+ });
136
+
89
137
  it('throws error if user tries to add too many segments', () => {
90
138
  expect(() => {
91
139
  renderSegmentedControl({
@@ -85,10 +85,6 @@ const SegmentedControl = ({
85
85
  // eslint-disable-next-line react-hooks/exhaustive-deps
86
86
  }, [segmentsWithRefs, selectedValue]);
87
87
 
88
- useEffect(() => {
89
- onChange(selectedValue);
90
- }, [onChange, selectedValue]);
91
-
92
88
  return (
93
89
  <div
94
90
  ref={segmentsRef}
@@ -103,8 +99,13 @@ const SegmentedControl = ({
103
99
  })}
104
100
  role={mode !== 'input' ? 'tablist' : undefined}
105
101
  >
106
- {segmentsWithRefs.map((segment) =>
107
- mode === 'input' ? (
102
+ {segmentsWithRefs.map((segment) => {
103
+ const onSelect = () => {
104
+ setAnimate(true);
105
+ setSelectedValue(segment.value);
106
+ onChange(segment.value);
107
+ };
108
+ return mode === 'input' ? (
108
109
  <label
109
110
  ref={segment.ref as React.RefObject<HTMLLabelElement>}
110
111
  key={segment.id}
@@ -120,10 +121,7 @@ const SegmentedControl = ({
120
121
  name={name}
121
122
  value={segment.value}
122
123
  checked={selectedValue === segment.value}
123
- onChange={() => {
124
- setAnimate(true);
125
- setSelectedValue(segment.value);
126
- }}
124
+ onChange={onSelect}
127
125
  />
128
126
  <Body
129
127
  className="segmented-control__text"
@@ -149,10 +147,7 @@ const SegmentedControl = ({
149
147
  className={classNames('segmented-control__segment', 'segmented-control__button', {
150
148
  'segmented-control__selected-segment': selectedValue === segment.value,
151
149
  })}
152
- onClick={() => {
153
- setAnimate(true);
154
- setSelectedValue(segment.value);
155
- }}
150
+ onClick={onSelect}
156
151
  >
157
152
  <Body
158
153
  as="span"
@@ -166,8 +161,8 @@ const SegmentedControl = ({
166
161
  {segment.label}
167
162
  </Body>
168
163
  </button>
169
- ),
170
- )}
164
+ );
165
+ })}
171
166
  </div>
172
167
  </div>
173
168
  );