Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/components/Input/MonthInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export default class MonthInput extends React.Component {
value,
});
this.parseInput(value);
this.inputEl.setAttribute('value', value);
};

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

if (!isSame) {
this.inputEl.value = currentValue;
this.inputEl.setAttribute('value', currentValue);
}
};

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

const parsedDate = this.props.parse(this.inputEl.value, this.props.dateFormat);
if (parsedDate) {
this.inputEl.value = format(parsedDate, this.props.dateFormat);
const value = format(parsedDate, this.props.dateFormat);
this.inputEl.value = value;
this.inputEl.setAttribute('value', value);
}
};

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

// <DropdownToggle tag="div" disabled> is to wrap the input in a container for positioning dropdown/up, without breaking showOnFocus
// TODO extract a DropdownInput component that can encapsulate the defaultValue/value controlled/uncontrolled behavior.
Expand Down
33 changes: 33 additions & 0 deletions src/components/Input/MonthInput.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import assert from 'assert';
import { render, fireEvent } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import addMonths from 'date-fns/add_months';
import addYears from 'date-fns/add_years';
import isSameDay from 'date-fns/is_same_day';
Expand Down Expand Up @@ -324,4 +326,35 @@ describe('<MonthInput />', () => {
assert.strictEqual('visually-hidden', prevMonthLabel.prop('className'));
});
});

describe('RTL compatibility', () => {
it('initial value should appear in the input value attribute', async () => {
const screen = render(<MonthInput defaultValue="Dec 2025" />);
assert.strictEqual(
screen.getByTestId('react-gears-monthinput-dropdowntoggle-input').getAttribute('value'),
'Dec 2025'
);
});
it('valid user typed value should appear in the input value attribute', async () => {
const screen = render(<MonthInput />);

const input = screen.getByTestId('react-gears-monthinput-dropdowntoggle-input');
await userEvent.type(input, 'Dec 2025');
assert.strictEqual(input.getAttribute('value'), 'Dec 2025');
});
it('invalid user typed value should appear in the input value attribute', async () => {
const screen = render(<MonthInput />);

const input = screen.getByTestId('react-gears-monthinput-dropdowntoggle-input');
await userEvent.type(input, 'Foo bar');
assert.strictEqual(input.getAttribute('value'), 'Foo bar');
});
it('blurring the input should update the input value to the formatted value', async () => {
const screen = render(<MonthInput parse={() => 'Dec 2025'} />);
const input = screen.getByTestId('react-gears-monthinput-dropdowntoggle-input');
await userEvent.type(input, '12/25/2025');
fireEvent.blur(input);
assert.strictEqual(input.getAttribute('value'), 'Dec 2025');
});
});
});
Loading