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

Feature/ag 13856 pin input #992

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
10 changes: 10 additions & 0 deletions packages/overdrive/lib/components/PinInput/PinInput.css.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { styleVariants } from '@vanilla-extract/css';

export const inputDigit = styleVariants({
small: {
width: '24px',
},
medium: {
width: '40px',
},
});
23 changes: 23 additions & 0 deletions packages/overdrive/lib/components/PinInput/PinInput.spec.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { render } from '@testing-library/react';
import * as React from 'react';

import { PinInput } from './PinInput';

describe('<PinInput />', () => {
it('should not throw', () =>
expect(() => render(<PinInput />)).not.toThrow());

it('should match snapshot', () => {
expect(
render(
<PinInput
size="medium"
value='6'
digits={6}
/>,
).container.firstChild,
).toMatchSnapshot();

expect(render(<PinInput />).container.firstChild).toMatchSnapshot();
});
});
62 changes: 62 additions & 0 deletions packages/overdrive/lib/components/PinInput/PinInput.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import * as React from 'react';
import { FunctionComponent, useRef } from 'react';
import { Box } from '../Box';
import { Inline } from '../Inline';
import { TextInput } from '../TextInput';

import * as styles from './PinInput.css';
import { Tokens } from '../../themes/tokens';

export enum EPinInputSize {
Small = 'small',
Medium = 'medium',
}

export interface Props {
className?: string; // TODO: Remove this in the future
digits: number;
size?: EPinInputSize;
value?: string;
}

const labelSizeMap: Map<EPinInputSize, keyof Tokens['typography']['size']> =
new Map([
[EPinInputSize.Small, '3'],
[EPinInputSize.Medium, '4'],
]);

export const PinInput: FunctionComponent<Props> = ({
className = '',
digits,
value = digits,
size = EPinInputSize.Medium,
}) => {
const handleOnPaste = (event) => {
const stringPasted = event.clipboardData.getData('Text');
//const stringPastedLength = event.clipboardData.getData('Text').length;

[...stringPasted].forEach((digit, index) => {
document.querySelector(`#input-pin-${index}`).value = digit;
const nextfield = document.querySelector(`#input-pin-${index + 1}`);
if (nextfield !== null) {
nextfield.focus();
}
event.preventDefault();
})
};

return (
<Box className={className}>
<Inline space={labelSizeMap.get(size)} alignY="center">
{Array.from({ length: digits })
.fill(0)
.map((_, index) => (
<input id={`input-pin-${index}`} key={index} onPaste={handleOnPaste} className={styles.inputDigit[size]} maxLength={3} name="digit" placeholder='' />
// <TextInput key={index} onPaste={handleOnPaste} className={styles.inputDigit[size]} size={size} maxLength={1} name="digit" placeholder='' />
))}
</Inline>
</Box>
);
};

export default PinInput;
Loading
Loading