Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: memoized otp input and added example for using onOTPFilled #2497

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .changeset/empty-comics-breathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@razorpay/blade': major
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

major :o

---

fix(blade): memoized otp input
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,40 @@ const OTPInputControlledTemplate: StoryFn<typeof OTPInputComponent> = () => {
};
export const OTPInputControlled = OTPInputControlledTemplate.bind({});

export const onOtpFilled = (): React.ReactElement => {
return (
<Sandbox padding="spacing.0" editorHeight="100vh">
{`
import React, { memo, useCallback, useState } from 'react';
import { OTPInput } from '@razorpay/blade/components';

function App() {
const [state, setState] = useState('');

// consider wrapping this function in useCallback.
// since this case we are updating state which will cause App to re-render and hence the function reference will change.
// which will cause the OTPInput to re-render.
const sendOtpToServer = useCallback(({ value }) => {
setState(Math.random().toString());
}, []); // Dependency array ensures the function reference remains stable.

console.log(state);


return (
<>
<OTPInput label="Enter OTP" onOTPFilled={sendOtpToServer} />
</>
);
}

export default App;

`}
</Sandbox>
);
};

export const OTPInputRef: StoryFn<typeof OTPInputComponent> = () => {
// eslint-disable-next-line react-hooks/rules-of-hooks
const [focusOn, setFocusOn] = React.useState(0);
Expand Down
4 changes: 2 additions & 2 deletions packages/blade/src/components/Input/OTPInput/OTPInput.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useImperativeHandle, useState } from 'react';
import React, { memo, useEffect, useImperativeHandle, useState } from 'react';
import type { BaseInputProps } from '../BaseInput';
import { BaseInput } from '../BaseInput';
import { getHintType } from '../BaseInput/BaseInput';
Expand Down Expand Up @@ -419,7 +419,7 @@ const _OTPInput: React.ForwardRefRenderFunction<HTMLInputElement[], OTPInputProp
);
};

const OTPInput = React.forwardRef<HTMLInputElement[], OTPInputProps>(_OTPInput);
const OTPInput = memo(React.forwardRef<HTMLInputElement[], OTPInputProps>(_OTPInput));

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the cause of re-renders though? Shouldn't it not re-render independent of whether we have memo or not?

export type { OTPInputProps };
export { OTPInput };
Loading