This repository has been archived by the owner on Feb 6, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[UI > InputAlert] Revert and fix (#211)
* Revert "[UI > InputAlert] Fix for new UI" This reverts commit 963ddc5. * real fix * unrevert some changes
- Loading branch information
Showing
1 changed file
with
38 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,49 @@ | ||
import { InputAlertProps } from "@types"; | ||
import { findByName, findByProps } from "@metro/filters"; | ||
|
||
interface InternalInputAlertProps extends InputAlertProps { | ||
onClose?: () => void; | ||
onSubmit?: (input: string) => void; | ||
isLoading?: boolean; | ||
} | ||
import { findByProps } from "@metro/filters"; | ||
import { Forms, Alert } from "@ui/components"; | ||
|
||
const { FormInput } = Forms; | ||
const Alerts = findByProps("openLazy", "close"); | ||
const UserSettingsInputAlert = findByName("UserSettingsInputAlert") as React.ComponentClass<InternalInputAlertProps>; | ||
|
||
// TODO: Moving to Discord's UserSettingsInputAlert here has some issues: | ||
//* - Errors are not cleared upon typing | ||
//* - The confirmText does not apply | ||
//* - The confirm button is not disabled when there is an error | ||
|
||
export default class InputAlert extends UserSettingsInputAlert { | ||
constructor(props: InternalInputAlertProps) { | ||
super(props); | ||
props.secureTextEntry = false; | ||
props.onClose = Alerts.close; | ||
props.onSubmit = (i: string) => this.onConfirmWrapper(i); | ||
this.state = { input: props.initialValue }; | ||
} | ||
|
||
onConfirmWrapper(input: string) { | ||
// @ts-expect-error | ||
this.props.isLoading = true; | ||
export default function InputAlert({ title, confirmText, confirmColor, onConfirm, cancelText, placeholder, initialValue = "", secureTextEntry }: InputAlertProps) { | ||
const [value, setValue] = React.useState(initialValue); | ||
const [error, setError] = React.useState(""); | ||
|
||
const asyncOnConfirm = Promise.resolve(this.props.onConfirm(input)); | ||
function onConfirmWrapper() { | ||
const asyncOnConfirm = Promise.resolve(onConfirm(value)) | ||
|
||
asyncOnConfirm.then(() => { | ||
Alerts.close(); | ||
}).catch((e: Error) => { | ||
this.setState({ error: e.message }); | ||
setError(e.message); | ||
}); | ||
|
||
// @ts-expect-error | ||
this.props.isLoading = false; | ||
}; | ||
} | ||
|
||
return ( | ||
<Alert | ||
title={title} | ||
confirmText={confirmText} | ||
confirmColor={confirmColor} | ||
isConfirmButtonDisabled={error.length !== 0} | ||
onConfirm={onConfirmWrapper} | ||
cancelText={cancelText} | ||
onCancel={() => Alerts.close()} | ||
> | ||
<FormInput | ||
placeholder={placeholder} | ||
value={value} | ||
onChange={(v: string | { text: string }) => { | ||
setValue(typeof v === "string" ? v : v.text); | ||
if (error) setError(""); | ||
}} | ||
returnKeyType="done" | ||
onSubmitEditing={onConfirmWrapper} | ||
error={error || undefined} | ||
secureTextEntry={secureTextEntry} | ||
autoFocus={true} | ||
showBorder={true} | ||
style={{ paddingVertical: 5, alignSelf: "stretch", paddingHorizontal: 0 }} | ||
/> | ||
</Alert> | ||
); | ||
}; |