Skip to content

Commit b884fd1

Browse files
committed
fix: add name prop to onChange dropdown props
1 parent 012813e commit b884fd1

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/FormsyDropdown.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class FormsyDropdown extends Component<IFormsyDropdownProps> {
5151
e: React.SyntheticEvent<HTMLElement>,
5252
data: StrictDropdownProps & { value: FormsyDropdownValue }
5353
) => {
54-
const { multiple, value, setValue, onChange } = this.props;
54+
const { multiple, value, setValue, onChange, name } = this.props;
5555

5656
if (
5757
multiple &&
@@ -65,7 +65,7 @@ class FormsyDropdown extends Component<IFormsyDropdownProps> {
6565
setValue(data.value);
6666

6767
if (onChange) {
68-
onChange(e, data);
68+
onChange(e, { ...data, name });
6969
}
7070
};
7171

test/FormsyDropdown.spec.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const options = [
1919
{ text: 'Socks', value: 'socks' },
2020
];
2121

22-
const TestForm = () => (
22+
const TestForm = ({ onChange }: { onChange: any }) => (
2323
<Form>
2424
<FormsyDropdown
2525
label={<span data-testid="label">Clothing</span>}
@@ -30,15 +30,18 @@ const TestForm = () => (
3030
validationErrors={{
3131
isDefaultRequiredValue: validationError,
3232
}}
33+
onChange={onChange}
3334
/>
3435
</Form>
3536
);
3637

3738
describe('<Dropdown/>', () => {
3839
let wrapper: RenderResult;
40+
let onChangeSpy: jest.Mock;
3941

4042
beforeEach(() => {
41-
wrapper = render(<TestForm />);
43+
onChangeSpy = jest.fn();
44+
wrapper = render(<TestForm onChange={onChangeSpy} />);
4245
});
4346

4447
const submitForm = () => {
@@ -61,6 +64,14 @@ describe('<Dropdown/>', () => {
6164
});
6265
});
6366

67+
it('should add `name` prop to onChange dropdown props', () => {
68+
selectOptionAt(1);
69+
expect(onChangeSpy).toHaveBeenCalledWith(
70+
expect.anything(),
71+
expect.objectContaining({ name: 'testInput', value: options[1].value })
72+
);
73+
});
74+
6475
describe('When value is invalid', () => {
6576
it("Doesn't show any errors initially", () => {
6677
expect(wrapper.queryByTestId('error-label')).not.toBeInTheDocument();

0 commit comments

Comments
 (0)