Skip to content

Commit

Permalink
feat(HybridSelect): add eventFrom argument onChange function (#713)
Browse files Browse the repository at this point in the history
* feat(HybridSelect): add eventFrom argument onChange function

* feat(HybridSelect): fix comment
  • Loading branch information
chiragg928 authored Mar 17, 2021
1 parent b9a3228 commit 3ef4d66
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion catalog/pages/inputs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -1012,7 +1012,7 @@ rows:
- Prop: onChange
Type: function
Default: null
Notes: Invoked with an array of updatedSelection when an option is selected by the user
Notes: Invoked with an array of updatedSelection and eventFrom ["select" or "dropdown"] when an option is selected by the user
- Prop: placeholder
Type: string
Default: ""
Expand Down
6 changes: 3 additions & 3 deletions src/components/Input/HybridDropDown/HybridSelect.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import Select from "../Select/Select";

class HybridSelect extends Component {
// onChange function called when value is changed
onChange = value => {
onChange = (value, eventFrom) => {
const { onChange } = this.props;
if (onChange) {
onChange(value);
onChange(value, eventFrom || "dropdown");
}
};

Expand All @@ -35,7 +35,7 @@ class HybridSelect extends Component {
selectRef = React.createRef();

// update function called when value of native select is changed
updateValue = e => this.onChange([e.target.value]); // passing value in array to match dropdown's format
updateValue = e => this.onChange([e.target.value], "select"); // passing value in array to match dropdown's format

// To add optionFor prop to the children
renderChildren = (extraProps, children) => {
Expand Down
4 changes: 2 additions & 2 deletions src/components/Input/__tests__/HybridDropDown.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ describe("HybridDropDown", () => {
fireEvent.click(getByTestId("test-hybridoption"));

expect(onChange).toHaveBeenCalledTimes(1);
expect(onChange).toHaveBeenCalledWith(["2"]);
expect(onChange).toHaveBeenCalledWith(["2"], "dropdown");
});

it("should call onChange when updateValue is called", () => {
const instance = renderer.create(testComponentHTML()).getInstance();
const spyOnChange = jest.spyOn(instance, "onChange");
instance.updateValue({ target: { value: "1" } });
expect(spyOnChange).toHaveBeenCalledTimes(1);
expect(spyOnChange).toHaveBeenCalledWith(["1"]);
expect(spyOnChange).toHaveBeenCalledWith(["1"], "select");
spyOnChange.mockRestore();
});
});

0 comments on commit 3ef4d66

Please sign in to comment.