Skip to content

Commit 213d280

Browse files
committed
Keep input tag's value attribute updated with input's value
1 parent 490c51f commit 213d280

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-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: positionFixed } : {};
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: 30 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,32 @@ 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(screen.getByTestId('react-gears-monthinput-dropdowntoggle-input').getAttribute('value'), 'Dec 2025');
334+
});
335+
it('valid user typed value should appear in the input value attribute', async () => {
336+
const screen = render(<MonthInput />);
337+
338+
const input = screen.getByTestId('react-gears-monthinput-dropdowntoggle-input');
339+
await userEvent.type(input, 'Dec 2025');
340+
assert.strictEqual(screen.getByTestId('react-gears-monthinput-dropdowntoggle-input').getAttribute('value'), 'Dec 2025');
341+
});
342+
it('invalid user typed value should appear in the input value attribute', async () => {
343+
const screen = render(<MonthInput />);
344+
345+
const input = screen.getByTestId('react-gears-monthinput-dropdowntoggle-input');
346+
await userEvent.type(input, 'Foo bar');
347+
assert.strictEqual(screen.getByTestId('react-gears-monthinput-dropdowntoggle-input').getAttribute('value'), 'Foo bar');
348+
});
349+
it('blurring the input should update the input value to the formatted value', async () => {
350+
const screen = render(<MonthInput parse={() => 'Dec 2025'} />);
351+
const input = screen.getByTestId('react-gears-monthinput-dropdowntoggle-input');
352+
await userEvent.type(input, '12/25/2025');
353+
fireEvent.blur(input);
354+
assert.strictEqual(screen.getByTestId('react-gears-monthinput-dropdowntoggle-input').getAttribute('value'), 'Dec 2025');
355+
});
356+
});
327357
});

0 commit comments

Comments
 (0)