-
Notifications
You must be signed in to change notification settings - Fork 635
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f90eedc
commit bb81208
Showing
24 changed files
with
1,121 additions
and
333 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
packages/oauth/oauth-provider/src/assets/app/components/form-card-async.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { FormEventHandler, ReactNode, useEffect } from 'react' | ||
import { useAsyncAction } from '../hooks/use-async-action' | ||
import { Override } from '../lib/util' | ||
import { Button } from './button' | ||
import { FormCard, FormCardProps } from './form-card' | ||
|
||
export type FormCardAsyncProps = Override< | ||
Omit<FormCardProps, 'cancel' | 'actions' | 'error'>, | ||
{ | ||
onSubmit: FormEventHandler<HTMLFormElement> | ||
submitLabel?: ReactNode | ||
submitAria?: string | ||
|
||
onCancel?: () => void | ||
cancelLabel?: ReactNode | ||
cancelAria?: string | ||
|
||
onLoading?: (loading: boolean) => void | ||
onError?: (error: Error | undefined) => void | ||
|
||
errorMessageFallback?: ReactNode | ||
errorSlot?: (error: Error) => null | undefined | ReactNode | ||
} | ||
> | ||
|
||
export default function FormCardAsync({ | ||
onSubmit, | ||
submitAria = 'Submit', | ||
submitLabel = submitAria, | ||
|
||
onCancel = undefined, | ||
cancelAria = 'Cancel', | ||
cancelLabel = cancelAria, | ||
|
||
errorMessageFallback = 'An unknown error occurred', | ||
errorSlot, | ||
onLoading, | ||
onError, | ||
|
||
children, | ||
|
||
...props | ||
}: FormCardAsyncProps) { | ||
const { run, loading, error } = useAsyncAction(onSubmit) | ||
|
||
useEffect(() => { | ||
onLoading?.(loading) | ||
}, [onLoading, loading]) | ||
|
||
useEffect(() => { | ||
onError?.(error) | ||
}, [onError, error]) | ||
|
||
return ( | ||
<FormCard | ||
{...props} | ||
onSubmit={run} | ||
error={error ? errorSlot?.(error) || errorMessageFallback : undefined} | ||
cancel={ | ||
onCancel && ( | ||
<Button aria-label={cancelAria} onClick={onCancel}> | ||
{cancelLabel} | ||
</Button> | ||
) | ||
} | ||
actions={ | ||
<Button | ||
color="brand" | ||
type="submit" | ||
aria-label={submitAria} | ||
loading={loading} | ||
> | ||
{submitLabel} | ||
</Button> | ||
} | ||
> | ||
{children} | ||
</FormCard> | ||
) | ||
} |
86 changes: 86 additions & 0 deletions
86
packages/oauth/oauth-provider/src/assets/app/components/input-otp.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { ChangeEvent, forwardRef } from 'react' | ||
import { TokenIcon } from './icons/token-icon' | ||
import { InputText, InputTextProps } from './input-text' | ||
|
||
export type InputOtpProps = Omit< | ||
InputTextProps, | ||
| 'type' | ||
| 'pattern' | ||
| 'autoCapitalize' | ||
| 'autoCorrect' | ||
| 'autoComplete' | ||
| 'spellCheck' | ||
| 'minLength' | ||
| 'maxLength' | ||
| 'dir' | ||
> & { | ||
example?: string | ||
onOtp?: (code: string | null) => void | ||
} | ||
|
||
export const OTP_CODE_EXAMPLE = 'XXXXX-XXXXX' | ||
|
||
export const InputOtp = forwardRef<HTMLInputElement, InputOtpProps>( | ||
( | ||
{ | ||
icon = <TokenIcon className="w-5" />, | ||
example = OTP_CODE_EXAMPLE, | ||
title = example, | ||
placeholder = `Looks like ${example}`, | ||
onChange, | ||
onOtp, | ||
...props | ||
}, | ||
ref, | ||
) => { | ||
const handleChange = (event: ChangeEvent<HTMLInputElement>) => { | ||
const { value, selectionEnd, selectionStart } = event.currentTarget | ||
|
||
const fixedValue = fix(value) | ||
|
||
event.currentTarget.value = fixedValue | ||
|
||
// Move the cursor back where it was relative to the original value | ||
const pos = selectionEnd ?? selectionStart | ||
if (pos != null) { | ||
const fixedSlicedValue = fix(value.slice(0, pos)) | ||
event.currentTarget.selectionStart = event.currentTarget.selectionEnd = | ||
fixedSlicedValue.length | ||
} | ||
|
||
onChange?.(event) | ||
|
||
if (!event.isDefaultPrevented()) { | ||
onOtp?.(fixedValue.length === 11 ? fixedValue : null) | ||
} | ||
} | ||
|
||
return ( | ||
<InputText | ||
{...props} | ||
type="text" | ||
autoCapitalize="none" | ||
autoCorrect="off" | ||
autoComplete="off" | ||
spellCheck="false" | ||
minLength={11} | ||
maxLength={11} | ||
dir="auto" | ||
ref={ref} | ||
icon={icon} | ||
pattern="^[A-Z2-7]{5}-[A-Z2-7]{5}$" | ||
placeholder={placeholder} | ||
title={title} | ||
onChange={handleChange} | ||
/> | ||
) | ||
}, | ||
) | ||
|
||
function fix(value: string) { | ||
const normalized = value.toUpperCase().replaceAll(/[^A-Z2-7]/g, '') | ||
|
||
if (normalized.length <= 5) return normalized | ||
|
||
return `${normalized.slice(0, 5)}-${normalized.slice(5, 10)}` | ||
} |
Oops, something went wrong.