Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: pinInput supports disabled props #4851

Merged
merged 2 commits into from
Nov 10, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@ defineOptions({
inheritAttrs: false,
});

const props = withDefaults(defineProps<PinInputProps>(), {
btnLoading: false,
codeLength: 6,
handleSendCode: async () => {},
maxTime: 60,
});
const {
codeLength = 6,
createText = async () => {},
disabled = false,
handleSendCode = async () => {},
loading = false,
maxTime = 60,
} = defineProps<PinInputProps>();

const emit = defineEmits<{
complete: [];
sendError: [error: any];
}>();

const timer = ref<ReturnType<typeof setTimeout>>();
Expand All @@ -30,11 +33,11 @@ const countdown = ref(0);

const btnText = computed(() => {
const countdownValue = countdown.value;
return props.createText?.(countdownValue);
return createText?.(countdownValue);
});

const btnLoading = computed(() => {
return props.loading || countdown.value > 0;
return loading || countdown.value > 0;
});

watch(
Expand All @@ -50,10 +53,16 @@ function handleComplete(e: string[]) {
}

async function handleSend(e: Event) {
e?.preventDefault();
await props.handleSendCode();
countdown.value = props.maxTime;
startCountdown();
try {
e?.preventDefault();
await handleSendCode();
countdown.value = maxTime;
startCountdown();
} catch (error) {
console.error('Failed to send code:', error);
// Consider emitting an error event or showing a notification
emit('sendError', error);
}
}

function startCountdown() {
Expand All @@ -77,6 +86,7 @@ const id = useId();
<PinInput
:id="id"
v-model="inputValue"
:disabled="disabled"
class="flex w-full justify-between"
otp
placeholder="○"
Expand All @@ -92,6 +102,7 @@ const id = useId();
/>
</PinInputGroup>
<VbenButton
:disabled="disabled"
:loading="btnLoading"
class="flex-grow"
size="lg"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ interface PinInputProps {
* 发送验证码按钮文本
*/
createText?: (countdown: number) => string;
/**
* 是否禁用
*/
disabled?: boolean;
/**
* 自定义验证码发送逻辑
* @returns
Expand Down
Loading