Skip to content

Commit 8309e79

Browse files
scottpchow23JeremyRH
authored andcommitted
fix: Keep input tag's value attribute updated with input's value
1 parent 1d7a984 commit 8309e79

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

src/components/Input/MonthInput.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ export default class MonthInput extends React.Component {
110110
value,
111111
});
112112
this.parseInput(value);
113+
this.inputEl.setAttribute('value', value);
113114
};
114115

115116
onSelect = (newDate) => {
@@ -224,6 +225,7 @@ export default class MonthInput extends React.Component {
224225

225226
if (!isSame) {
226227
this.inputEl.value = currentValue;
228+
this.inputEl.setAttribute('value', currentValue);
227229
}
228230
};
229231

@@ -232,7 +234,9 @@ export default class MonthInput extends React.Component {
232234

233235
const parsedDate = this.props.parse(this.inputEl.value, this.props.dateFormat);
234236
if (parsedDate) {
235-
this.inputEl.value = format(parsedDate, this.props.dateFormat);
237+
const value = format(parsedDate, this.props.dateFormat);
238+
this.inputEl.value = value;
239+
this.inputEl.setAttribute('value', value);
236240
}
237241
};
238242

@@ -260,7 +264,7 @@ export default class MonthInput extends React.Component {
260264
} = this.props;
261265
const { open } = this.state;
262266
const date = this.getCurrentDate();
263-
const dropdownProps = open ? { positionFixed } : {};
267+
const dropdownProps = open && positionFixed ? { strategy: 'fixed' } : {};
264268

265269
// <DropdownToggle tag="div" disabled> is to wrap the input in a container for positioning dropdown/up, without breaking showOnFocus
266270
// TODO extract a DropdownInput component that can encapsulate the defaultValue/value controlled/uncontrolled behavior.

src/components/Input/MonthInput.spec.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import assert from 'assert';
2+
import { render, fireEvent } from '@testing-library/react';
3+
import userEvent from '@testing-library/user-event';
24
import addMonths from 'date-fns/add_months';
35
import addYears from 'date-fns/add_years';
46
import isSameDay from 'date-fns/is_same_day';
@@ -324,4 +326,35 @@ describe('<MonthInput />', () => {
324326
assert.strictEqual('visually-hidden', prevMonthLabel.prop('className'));
325327
});
326328
});
329+
330+
describe('RTL compatibility', () => {
331+
it('initial value should appear in the input value attribute', async () => {
332+
const screen = render(<MonthInput defaultValue="Dec 2025" />);
333+
assert.strictEqual(
334+
screen.getByTestId('react-gears-monthinput-dropdowntoggle-input').getAttribute('value'),
335+
'Dec 2025'
336+
);
337+
});
338+
it('valid user typed value should appear in the input value attribute', async () => {
339+
const screen = render(<MonthInput />);
340+
341+
const input = screen.getByTestId('react-gears-monthinput-dropdowntoggle-input');
342+
await userEvent.type(input, 'Dec 2025');
343+
assert.strictEqual(input.getAttribute('value'), 'Dec 2025');
344+
});
345+
it('invalid user typed value should appear in the input value attribute', async () => {
346+
const screen = render(<MonthInput />);
347+
348+
const input = screen.getByTestId('react-gears-monthinput-dropdowntoggle-input');
349+
await userEvent.type(input, 'Foo bar');
350+
assert.strictEqual(input.getAttribute('value'), 'Foo bar');
351+
});
352+
it('blurring the input should update the input value to the formatted value', async () => {
353+
const screen = render(<MonthInput parse={() => 'Dec 2025'} />);
354+
const input = screen.getByTestId('react-gears-monthinput-dropdowntoggle-input');
355+
await userEvent.type(input, '12/25/2025');
356+
fireEvent.blur(input);
357+
assert.strictEqual(input.getAttribute('value'), 'Dec 2025');
358+
});
359+
});
327360
});

0 commit comments

Comments
 (0)