Skip to content
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
17 changes: 11 additions & 6 deletions components/StyledInputMask.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import React from 'react';
import MaskedInput from 'react-text-mask';
import type { MaskitoOptions } from '@maskito/core';
import { MASKITO_DEFAULT_OPTIONS } from '@maskito/core';
import { useMaskito } from '@maskito/react';

import StyledInput from './StyledInput';

interface StyledInputMaskProps extends React.ComponentProps<typeof MaskedInput> {
render?: (ref: React.Ref<any>, props: any) => React.ReactNode;
interface StyledInputMaskProps extends React.ComponentProps<typeof StyledInput> {
render?: (ref: React.Ref<unknown>, props: unknown) => React.ReactNode;
maskito?: MaskitoOptions;
}

const DefaultMaskRenderer = (ref, props) => <StyledInput ref={ref} {...props} />;

const StyledInputMask = ({ render = DefaultMaskRenderer, ...props }: StyledInputMaskProps) => (
<MaskedInput render={render} {...props} />
);
const StyledInputMask = ({
render = DefaultMaskRenderer,
maskito = MASKITO_DEFAULT_OPTIONS,
...props
}: StyledInputMaskProps) => render(useMaskito({ options: maskito }), props);

export default StyledInputMask;
46 changes: 17 additions & 29 deletions components/edit-collective/AssignVirtualCardModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ const AssignVirtualCardModal = ({ collective = undefined, host, onSuccess, onClo
const { toast } = useToast();
const [assignNewVirtualCard, { loading: isBusy }] = useMutation(assignNewVirtualCardMutation);
const [getCollectiveUsers, { loading: isLoadingUsers, data: users }] = useLazyQuery(collectiveMembersQuery);
const maskCard = { mask: Array.from({ length: 22 }, (_, i) => (i % 6 < 4 ? /\d/ : ' ')) };
const maskCvc = { mask: [/\d/, /\d/, /\d/] };
const maskExp = {
mask: [/[01]/, /\d/, '/', '2', '0', /\d/, /\d/],
postprocessors: [
({ value, selection }, initial) =>
value.length === 2 && initial.value.length === 1
? { value: `${value}/20`, selection: [5, 5] }
: { value, selection },
],
};

const formik = useFormik({
initialValues: {
Expand Down Expand Up @@ -329,32 +340,9 @@ const AssignVirtualCardModal = ({ collective = undefined, host, onSuccess, onClo
{...inputProps}
name="cardNumber"
id="cardNumber"
onChange={formik.handleChange}
onInput={formik.handleChange}
value={formik.values.cardNumber}
mask={[
/\d/,
/\d/,
/\d/,
/\d/,
' ',
' ',
/\d/,
/\d/,
/\d/,
/\d/,
' ',
' ',
/\d/,
/\d/,
/\d/,
/\d/,
' ',
' ',
/\d/,
/\d/,
/\d/,
/\d/,
]}
maskito={maskCard}
render={(ref, props) => (
<StyledInputGroup
prepend={<CreditCard height="18px" style={{ marginTop: '-1px' }} />}
Expand All @@ -379,9 +367,9 @@ const AssignVirtualCardModal = ({ collective = undefined, host, onSuccess, onClo
{...inputProps}
name="expiryDate"
id="expiryDate"
onChange={formik.handleChange}
onInput={formik.handleChange}
value={formik.values.expiryDate}
mask={[/[01]/, /\d/, '/', '2', '0', /\d/, /\d/]}
maskito={maskExp}
placeholder="MM/YYYY"
guide={false}
disabled={isBusy}
Expand All @@ -399,9 +387,9 @@ const AssignVirtualCardModal = ({ collective = undefined, host, onSuccess, onClo
{...inputProps}
id="cvv"
name="cvv"
onChange={formik.handleChange}
onInput={formik.handleChange}
value={formik.values.cvv}
mask={[/\d/, /\d/, /\d/]}
maskito={maskCvc}
guide={false}
placeholder="123"
disabled={isBusy}
Expand Down
Loading