Skip to content

Commit

Permalink
Adds optional onBlur prop to TextInput2
Browse files Browse the repository at this point in the history
If provided, onBlur function will be executed when focus is removed from the input element.
  • Loading branch information
antoniodangond committed Sep 17, 2021
1 parent 2d68ce6 commit b7f97a4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
6 changes: 6 additions & 0 deletions docs/components/TextInput2View.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,12 @@ export default class TextInput2View extends React.PureComponent {
type: "Function",
description: "Called when value of input changes",
},
{
name: "onBlur",
type: "Function",
description: "Called when focus is removed from the input",
optional: true,
},
{
name: "size",
type: "string",
Expand Down
7 changes: 6 additions & 1 deletion src/TextInput2/TextInput2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface Props {
showErrorOnBlurOnly?: boolean;
value: string;
onChange: React.ChangeEventHandler<HTMLInputElement>;
onBlur?: React.FocusEventHandler<HTMLInputElement>;
size?: Values<typeof FormElementSize>;
}

Expand Down Expand Up @@ -66,6 +67,7 @@ const TextInput2: React.FC<Props> = ({
obscurable,
value,
onChange,
onBlur,
size,
}) => {
const id = name;
Expand Down Expand Up @@ -148,7 +150,10 @@ const TextInput2: React.FC<Props> = ({
onChange(e);
}}
onFocus={() => setIsFocused(true)}
onBlur={() => setIsFocused(false)}
onBlur={(e) => {
setIsFocused(false);
if (onBlur) onBlur(e);
}}
/>
{errorMessage != null && (
<FontAwesome className={cssClass.ERROR_ICON} name="exclamation-circle" />
Expand Down
7 changes: 7 additions & 0 deletions src/TextInput2/TextInput2_test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,13 @@ describe("TextInput2", () => {
});
});

it("executes onBlur prop if provided", () => {
defaultProps.onBlur = jest.fn();
const wrapper = shallow(<TextInput2 {...defaultProps} />);
getInput(wrapper).simulate("blur");
expect(defaultProps.onBlur).toHaveBeenCalled();
});

// TODO: Test any relevant state changes/event handling/prop-driven rendering.
/*
it("propagates event", () => {
Expand Down

0 comments on commit b7f97a4

Please sign in to comment.