2023-04-15 19:54:23 +01:00
|
|
|
import { InputAlertProps } from "@types";
|
2023-12-19 15:21:07 +08:00
|
|
|
import { findByProps } from "@metro/filters";
|
|
|
|
|
import { Forms, Alert } from "@ui/components";
|
2023-02-26 16:26:01 -05:00
|
|
|
|
2023-12-19 15:21:07 +08:00
|
|
|
const { FormInput } = Forms;
|
2023-02-26 16:26:01 -05:00
|
|
|
const Alerts = findByProps("openLazy", "close");
|
|
|
|
|
|
2023-12-19 15:21:07 +08:00
|
|
|
export default function InputAlert({ title, confirmText, confirmColor, onConfirm, cancelText, placeholder, initialValue = "", secureTextEntry }: InputAlertProps) {
|
|
|
|
|
const [value, setValue] = React.useState(initialValue);
|
|
|
|
|
const [error, setError] = React.useState("");
|
2023-02-26 16:26:01 -05:00
|
|
|
|
2023-12-19 15:21:07 +08:00
|
|
|
function onConfirmWrapper() {
|
|
|
|
|
const asyncOnConfirm = Promise.resolve(onConfirm(value))
|
2023-02-26 16:26:01 -05:00
|
|
|
|
|
|
|
|
asyncOnConfirm.then(() => {
|
|
|
|
|
Alerts.close();
|
|
|
|
|
}).catch((e: Error) => {
|
2023-12-19 15:21:07 +08:00
|
|
|
setError(e.message);
|
2023-02-26 16:26:01 -05:00
|
|
|
});
|
2023-12-17 03:39:57 +00:00
|
|
|
};
|
2023-12-19 15:21:07 +08:00
|
|
|
|
|
|
|
|
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}
|
2023-12-21 03:30:21 +08:00
|
|
|
style={{ alignSelf: "stretch" }}
|
2023-12-19 15:21:07 +08:00
|
|
|
/>
|
|
|
|
|
</Alert>
|
|
|
|
|
);
|
|
|
|
|
};
|