[UI > InputAlert] Revert and fix (#211)
* Revert "[UI > InputAlert] Fix for new UI"
This reverts commit 963ddc50c5
.
* real fix
* unrevert some changes
This commit is contained in:
parent
963ddc50c5
commit
0e48cbcb9e
1 changed files with 38 additions and 31 deletions
|
@ -1,42 +1,49 @@
|
||||||
import { InputAlertProps } from "@types";
|
import { InputAlertProps } from "@types";
|
||||||
import { findByName, findByProps } from "@metro/filters";
|
import { findByProps } from "@metro/filters";
|
||||||
|
import { Forms, Alert } from "@ui/components";
|
||||||
interface InternalInputAlertProps extends InputAlertProps {
|
|
||||||
onClose?: () => void;
|
|
||||||
onSubmit?: (input: string) => void;
|
|
||||||
isLoading?: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
const { FormInput } = Forms;
|
||||||
const Alerts = findByProps("openLazy", "close");
|
const Alerts = findByProps("openLazy", "close");
|
||||||
const UserSettingsInputAlert = findByName("UserSettingsInputAlert") as React.ComponentClass<InternalInputAlertProps>;
|
|
||||||
|
|
||||||
// TODO: Moving to Discord's UserSettingsInputAlert here has some issues:
|
export default function InputAlert({ title, confirmText, confirmColor, onConfirm, cancelText, placeholder, initialValue = "", secureTextEntry }: InputAlertProps) {
|
||||||
//* - Errors are not cleared upon typing
|
const [value, setValue] = React.useState(initialValue);
|
||||||
//* - The confirmText does not apply
|
const [error, setError] = React.useState("");
|
||||||
//* - The confirm button is not disabled when there is an error
|
|
||||||
|
|
||||||
export default class InputAlert extends UserSettingsInputAlert {
|
function onConfirmWrapper() {
|
||||||
constructor(props: InternalInputAlertProps) {
|
const asyncOnConfirm = Promise.resolve(onConfirm(value))
|
||||||
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;
|
|
||||||
|
|
||||||
const asyncOnConfirm = Promise.resolve(this.props.onConfirm(input));
|
|
||||||
|
|
||||||
asyncOnConfirm.then(() => {
|
asyncOnConfirm.then(() => {
|
||||||
Alerts.close();
|
Alerts.close();
|
||||||
}).catch((e: Error) => {
|
}).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>
|
||||||
|
);
|
||||||
|
};
|
Loading…
Reference in a new issue