Перейти к содержанию

AuthForm

Готовая форма входа, регистрации или сброса пароля: поля, кнопки сервисов и отправка.

Использование

Компонент AuthForm собран на Form: форма входа для внешнего приложения или личного кабинета. Её кладут прямо на страницу или в PageCard.

Вход
Введите данные, чтобы войти в аккаунт.
Запомнить меня
<script setup lang="ts">
import * as z from 'zod'
import type { FormSubmitEvent, AuthFormField } from '@bxshefby/b24ui-nuxt'

const toast = useToast()

const fields: AuthFormField[] = [{
  name: 'email',
  type: 'email',
  label: 'Эл. почта',
  placeholder: 'Введите эл. почту',
  required: true
}, {
  name: 'password',
  label: 'Пароль',
  type: 'password',
  placeholder: 'Введите пароль',
  required: true
}, {
  name: 'remember',
  label: 'Запомнить меня',
  type: 'checkbox'
}]

const providers = [{
  label: 'Битрикс24',
  icon: 'i-bx24-common-service-bitrix24',
  onClick: () => {
    toast.add({ title: 'Битрикс24', description: 'Вход через Битрикс24' })
  }
}, {
  label: 'Яндекс',
  icon: 'i-bx24-common-service-yandex',
  onClick: () => {
    toast.add({ title: 'Яндекс', description: 'Вход через Яндекс ID' })
  }
}]

const schema = z.object({
  email: z.email('Неверный адрес почты'),
  password: z.string('Введите пароль').min(8, 'Не короче 8 символов')
})

type Schema = z.output<typeof schema>

function onSubmit(payload: FormSubmitEvent<Schema>) {
  console.log('Submitted', payload)
}
</script>

<template>
  <div class="flex flex-col items-center justify-center gap-4 p-4">
    <B24PageCard class="w-full max-w-md">
      <B24AuthForm
        :schema="schema"
        title="Вход"
        description="Введите данные, чтобы войти в аккаунт."
        icon="i-bx24-outline-person"
        :fields="fields"
        :providers="providers"
        @submit="onSubmit"
      />
    </B24PageCard>
  </div>
</template>

Поля

Форма строится по пропсу fields, состояние она хранит сама.

Пропс fields — массив объектов со свойствами:

  • name: string
  • type: 'checkbox' | 'select' | 'otp' | 'InputHTMLAttributes['type']'

Свойство type выбирает поле и его пропсы: checkbox — пропсы Checkbox, select — SelectMenu, otp — PinInput, остальные типы — Input.

Каждому полю можно передать и любой пропс FormField.

Запомнить меня

Вход сохранится на 30 дней.

<script setup lang="ts">
import type { AuthFormField } from '@bxshefby/b24ui-nuxt'

const fields = ref<AuthFormField[]>([
  {
    name: 'email',
    type: 'email',
    label: 'Эл. почта',
    placeholder: 'Введите эл. почту',
    required: true
  },
  {
    name: 'password',
    type: 'password',
    label: 'Пароль',
    placeholder: 'Введите пароль',
    required: true
  },
  {
    name: 'city',
    type: 'select',
    label: 'Город',
    placeholder: 'Выберите город',
    items: [
      {
        label: 'Минск',
        value: 'minsk'
      },
      {
        label: 'Москва',
        value: 'moscow'
      },
      {
        label: 'Алматы',
        value: 'almaty'
      },
      {
        label: 'Ташкент',
        value: 'tashkent'
      }
    ]
  },
  {
    name: 'otp',
    type: 'otp',
    label: 'Код из СМС',
    length: 6,
    placeholder: '○'
  },
  {
    name: 'remember',
    type: 'checkbox',
    label: 'Запомнить меня',
    description: 'Вход сохранится на 30 дней.'
  }
])
</script>

<template>
  <B24AuthForm :fields="fields" class="max-w-sm" />
</template>
<script setup lang="ts">
import { ref } from 'vue'
import type { AuthFormField } from '@bxshefby/b24ui-nuxt'

const fields = ref<AuthFormField[]>([
  {
    name: 'email',
    type: 'email',
    label: 'Эл. почта',
    placeholder: 'Введите эл. почту',
    required: true
  },
  {
    name: 'password',
    type: 'password',
    label: 'Пароль',
    placeholder: 'Введите пароль',
    required: true
  },
  {
    name: 'city',
    type: 'select',
    label: 'Город',
    placeholder: 'Выберите город',
    items: [
      {
        label: 'Минск',
        value: 'minsk'
      },
      {
        label: 'Москва',
        value: 'moscow'
      },
      {
        label: 'Алматы',
        value: 'almaty'
      },
      {
        label: 'Ташкент',
        value: 'tashkent'
      }
    ]
  },
  {
    name: 'otp',
    type: 'otp',
    label: 'Код из СМС',
    length: 6,
    placeholder: '○'
  },
  {
    name: 'remember',
    type: 'checkbox',
    label: 'Запомнить меня',
    description: 'Вход сохранится на 30 дней.'
  }
])
</script>

<template>
  <B24AuthForm :fields="fields" class="max-w-sm" />
</template>

Заголовок

Пропс title задаёт заголовок формы.

Вход
<script setup lang="ts">
import type { AuthFormField } from '@bxshefby/b24ui-nuxt'

const fields = ref<AuthFormField[]>([
  {
    name: 'email',
    type: 'text',
    label: 'Эл. почта'
  },
  {
    name: 'password',
    type: 'password',
    label: 'Пароль'
  }
])
</script>

<template>
  <B24AuthForm title="Вход" :fields="fields" class="max-w-md" />
</template>
<script setup lang="ts">
import { ref } from 'vue'
import type { AuthFormField } from '@bxshefby/b24ui-nuxt'

const fields = ref<AuthFormField[]>([
  {
    name: 'email',
    type: 'text',
    label: 'Эл. почта'
  },
  {
    name: 'password',
    type: 'password',
    label: 'Пароль'
  }
])
</script>

<template>
  <B24AuthForm title="Вход" :fields="fields" class="max-w-md" />
</template>
Заголовок по умолчанию отрисовывается как div. Чтобы сделать его заголовком, передайте в пропс as объект, например :as="{ title: 'h3' }". Корневой элемент задаётся так же: :as="{ root: 'section', title: 'h3' }".

Описание

Пропс description задаёт пояснение под заголовком.

Вход
Введите данные, чтобы войти в аккаунт.
<script setup lang="ts">
import type { AuthFormField } from '@bxshefby/b24ui-nuxt'

const fields = ref<AuthFormField[]>([
  {
    name: 'email',
    type: 'text',
    label: 'Эл. почта'
  },
  {
    name: 'password',
    type: 'password',
    label: 'Пароль'
  }
])
</script>

<template>
  <B24AuthForm
    title="Вход"
    description="Введите данные, чтобы войти в аккаунт."
    :fields="fields"
    class="max-w-md"
  />
</template>
<script setup lang="ts">
import { ref } from 'vue'
import type { AuthFormField } from '@bxshefby/b24ui-nuxt'

const fields = ref<AuthFormField[]>([
  {
    name: 'email',
    type: 'text',
    label: 'Эл. почта'
  },
  {
    name: 'password',
    type: 'password',
    label: 'Пароль'
  }
])
</script>

<template>
  <B24AuthForm
    title="Вход"
    description="Введите данные, чтобы войти в аккаунт."
    :fields="fields"
    class="max-w-md"
  />
</template>

Иконка

Пропс icon задаёт иконку над заголовком.

Вход
Введите данные, чтобы войти в аккаунт.
<script setup lang="ts">
import type { AuthFormField } from '@bxshefby/b24ui-nuxt'

const fields = ref<AuthFormField[]>([
  {
    name: 'email',
    type: 'text',
    label: 'Эл. почта'
  },
  {
    name: 'password',
    type: 'password',
    label: 'Пароль'
  }
])
</script>

<template>
  <B24AuthForm
    title="Вход"
    description="Введите данные, чтобы войти в аккаунт."
    icon="i-bx24-outline-person"
    :fields="fields"
    class="max-w-md"
  />
</template>
<script setup lang="ts">
import { ref } from 'vue'
import type { AuthFormField } from '@bxshefby/b24ui-nuxt'

const fields = ref<AuthFormField[]>([
  {
    name: 'email',
    type: 'text',
    label: 'Эл. почта'
  },
  {
    name: 'password',
    type: 'password',
    label: 'Пароль'
  }
])
</script>

<template>
  <B24AuthForm
    title="Вход"
    description="Введите данные, чтобы войти в аккаунт."
    icon="i-bx24-outline-person"
    :fields="fields"
    class="max-w-md"
  />
</template>

Сервисы входа

Пропс providers добавляет кнопки входа через сервисы. Каждой кнопке можно передать любой пропс Button: color, to и другие.

Вход
Введите данные, чтобы войти в аккаунт.
<script setup lang="ts">
import type { ButtonProps, AuthFormField } from '@bxshefby/b24ui-nuxt'

const providers = ref<ButtonProps[]>([
  {
    label: 'Битрикс24',
    icon: 'i-bx24-common-service-bitrix24'
  },
  {
    label: 'Яндекс',
    icon: 'i-bx24-common-service-yandex'
  }
])
const fields = ref<AuthFormField[]>([
  {
    name: 'email',
    type: 'text',
    label: 'Эл. почта'
  },
  {
    name: 'password',
    type: 'password',
    label: 'Пароль'
  }
])
</script>

<template>
  <B24AuthForm
    title="Вход"
    description="Введите данные, чтобы войти в аккаунт."
    icon="i-bx24-outline-person"
    :providers="providers"
    :fields="fields"
    class="max-w-md"
  />
</template>
<script setup lang="ts">
import { ref } from 'vue'
import type { ButtonProps, AuthFormField } from '@bxshefby/b24ui-nuxt'

const providers = ref<ButtonProps[]>([
  {
    label: 'Битрикс24',
    icon: 'i-bx24-common-service-bitrix24'
  },
  {
    label: 'Яндекс',
    icon: 'i-bx24-common-service-yandex'
  }
])
const fields = ref<AuthFormField[]>([
  {
    name: 'email',
    type: 'text',
    label: 'Эл. почта'
  },
  {
    name: 'password',
    type: 'password',
    label: 'Пароль'
  }
])
</script>

<template>
  <B24AuthForm
    title="Вход"
    description="Введите данные, чтобы войти в аккаунт."
    icon="i-bx24-outline-person"
    :providers="providers"
    :fields="fields"
    class="max-w-md"
  />
</template>

Разделитель

Пропс separator меняет Separator между кнопками сервисов и полями. По умолчанию — подпись из локали (или в русской).

Вход
Введите данные, чтобы войти в аккаунт.
<script setup lang="ts">
import type { ButtonProps, AuthFormField } from '@bxshefby/b24ui-nuxt'

const providers = ref<ButtonProps[]>([
  {
    label: 'Битрикс24',
    icon: 'i-bx24-common-service-bitrix24'
  },
  {
    label: 'Яндекс',
    icon: 'i-bx24-common-service-yandex'
  }
])
const fields = ref<AuthFormField[]>([
  {
    name: 'email',
    type: 'text',
    label: 'Эл. почта'
  },
  {
    name: 'password',
    type: 'password',
    label: 'Пароль'
  }
])
</script>

<template>
  <B24AuthForm
    title="Вход"
    description="Введите данные, чтобы войти в аккаунт."
    icon="i-bx24-outline-person"
    :providers="providers"
    :fields="fields"
    class="max-w-md"
    separator="Или по почте"
  />
</template>
<script setup lang="ts">
import { ref } from 'vue'
import type { ButtonProps, AuthFormField } from '@bxshefby/b24ui-nuxt'

const providers = ref<ButtonProps[]>([
  {
    label: 'Битрикс24',
    icon: 'i-bx24-common-service-bitrix24'
  },
  {
    label: 'Яндекс',
    icon: 'i-bx24-common-service-yandex'
  }
])
const fields = ref<AuthFormField[]>([
  {
    name: 'email',
    type: 'text',
    label: 'Эл. почта'
  },
  {
    name: 'password',
    type: 'password',
    label: 'Пароль'
  }
])
</script>

<template>
  <B24AuthForm
    title="Вход"
    description="Введите данные, чтобы войти в аккаунт."
    icon="i-bx24-outline-person"
    :providers="providers"
    :fields="fields"
    class="max-w-md"
    separator="Или по почте"
  />
</template>

Разделителю можно передать любой пропс Separator.

Вход
Введите данные, чтобы войти в аккаунт.
<script setup lang="ts">
import type { ButtonProps, AuthFormField } from '@bxshefby/b24ui-nuxt'

const providers = ref<ButtonProps[]>([
  {
    label: 'Битрикс24',
    icon: 'i-bx24-common-service-bitrix24'
  },
  {
    label: 'Яндекс',
    icon: 'i-bx24-common-service-yandex'
  }
])
const fields = ref<AuthFormField[]>([
  {
    name: 'email',
    type: 'text',
    label: 'Эл. почта'
  },
  {
    name: 'password',
    type: 'password',
    label: 'Пароль'
  }
])
</script>

<template>
  <B24AuthForm
    title="Вход"
    description="Введите данные, чтобы войти в аккаунт."
    icon="i-bx24-outline-person"
    :providers="providers"
    :fields="fields"
    class="max-w-md"
    :separator="{
      icon: 'i-bx24-outline-person'
    }"
  />
</template>
<script setup lang="ts">
import { ref } from 'vue'
import type { ButtonProps, AuthFormField } from '@bxshefby/b24ui-nuxt'

const providers = ref<ButtonProps[]>([
  {
    label: 'Битрикс24',
    icon: 'i-bx24-common-service-bitrix24'
  },
  {
    label: 'Яндекс',
    icon: 'i-bx24-common-service-yandex'
  }
])
const fields = ref<AuthFormField[]>([
  {
    name: 'email',
    type: 'text',
    label: 'Эл. почта'
  },
  {
    name: 'password',
    type: 'password',
    label: 'Пароль'
  }
])
</script>

<template>
  <B24AuthForm
    title="Вход"
    description="Введите данные, чтобы войти в аккаунт."
    icon="i-bx24-outline-person"
    :providers="providers"
    :fields="fields"
    class="max-w-md"
    :separator="{
      icon: 'i-bx24-outline-person'
    }"
  />
</template>

Кнопка отправки

Пропс submit меняет кнопку отправки формы. Ей можно передать любой пропс Button, кроме ссылочных: color, size и другие.

Вход
Введите данные, чтобы войти в аккаунт.
<script setup lang="ts">
import type { AuthFormField } from '@bxshefby/b24ui-nuxt'

const fields = ref<AuthFormField[]>([
  {
    name: 'email',
    type: 'text',
    label: 'Эл. почта'
  },
  {
    name: 'password',
    type: 'password',
    label: 'Пароль'
  }
])
</script>

<template>
  <B24AuthForm
    title="Вход"
    description="Введите данные, чтобы войти в аккаунт."
    icon="i-bx24-outline-person"
    :fields="fields"
    class="max-w-md"
    :submit="{
      label: 'Войти',
      color: 'air-primary-success'
    }"
  />
</template>
<script setup lang="ts">
import { ref } from 'vue'
import type { AuthFormField } from '@bxshefby/b24ui-nuxt'

const fields = ref<AuthFormField[]>([
  {
    name: 'email',
    type: 'text',
    label: 'Эл. почта'
  },
  {
    name: 'password',
    type: 'password',
    label: 'Пароль'
  }
])
</script>

<template>
  <B24AuthForm
    title="Вход"
    description="Введите данные, чтобы войти в аккаунт."
    icon="i-bx24-outline-person"
    :fields="fields"
    class="max-w-md"
    :submit="{
      label: 'Войти',
      color: 'air-primary-success'
    }"
  />
</template>

Примеры

На странице

Оберните AuthForm в PageCard, чтобы показать её, например, на странице login.vue.

С возвращением!
Нет аккаунта? Зарегистрируйтесь.
Запомнить меня
Не удалось войти
Входя, вы принимаете условия использования.
<script setup lang="ts">
import * as z from 'zod'
import type { FormSubmitEvent, AuthFormField } from '@bxshefby/b24ui-nuxt'

const toast = useToast()

const fields: AuthFormField[] = [{
  name: 'email',
  type: 'email',
  label: 'Эл. почта',
  placeholder: 'Введите эл. почту',
  required: true
}, {
  name: 'password',
  label: 'Пароль',
  type: 'password',
  placeholder: 'Введите пароль',
  required: true
}, {
  name: 'remember',
  label: 'Запомнить меня',
  type: 'checkbox'
}]

const providers = [{
  label: 'Битрикс24',
  icon: 'i-bx24-common-service-bitrix24',
  onClick: () => {
    toast.add({ title: 'Битрикс24', description: 'Вход через Битрикс24' })
  }
}, {
  label: 'Яндекс',
  icon: 'i-bx24-common-service-yandex',
  onClick: () => {
    toast.add({ title: 'Яндекс', description: 'Вход через Яндекс ID' })
  }
}]

const schema = z.object({
  email: z.email('Неверный адрес почты'),
  password: z.string('Введите пароль').min(8, 'Не короче 8 символов')
})

type Schema = z.output<typeof schema>

function onSubmit(payload: FormSubmitEvent<Schema>) {
  console.log('Submitted', payload)
}
</script>

<template>
  <div class="flex flex-col items-center justify-center gap-4 p-4">
    <B24PageCard class="w-full max-w-md">
      <B24AuthForm
        :schema="schema"
        :fields="fields"
        :providers="providers"
        title="С возвращением!"
        icon="i-bx24-outline-lock-l"
        @submit="onSubmit"
      >
        <template #description>
          Нет аккаунта? <B24Link to="#" class="text-(--ui-color-accent-main-primary) font-(--ui-font-weight-medium)">Зарегистрируйтесь</B24Link>.
        </template>
        <template #password-hint>
          <B24Link to="#" class="text-(--ui-color-accent-main-primary) font-(--ui-font-weight-medium)" tabindex="-1">Забыли пароль?</B24Link>
        </template>
        <template #validation>
          <B24Alert color="air-primary-alert" icon="i-bx24-outline-info-circle" title="Не удалось войти" />
        </template>
        <template #footer>
          Входя, вы принимаете <B24Link to="#" class="text-(--ui-color-accent-main-primary) font-(--ui-font-weight-medium)">условия использования</B24Link>.
        </template>
      </B24AuthForm>
    </B24PageCard>
  </div>
</template>

API

Пропсы

Пропс По умолчанию Тип
as'div'any

The element or component this component should render as.

icon IconProp

The icon displayed above the title.

title string
description string
fields F[]
providers ButtonProps[]

Display a list of Button under the description. { color: 'air-secondary-no-accent', block: true }

separatort('authForm.separator') string | SeparatorProps

The text displayed in the separator.

submit Omit<ButtonProps, LinkPropsKeys>

Display a submit button at the bottom of the form. { label: 'Continue', color: 'air-primary', block: true }

schema T
validate (state: Partial<InferInput<T>>): FormError<string>[] | Promise<FormError<string>[]>
validateOn FormInputEvents[]
validateOnInputDelay number
disabledboolean
loadingboolean
loadingAutoboolean
name string
autocomplete string
acceptcharset string
action string
enctype string
method string
novalidate false | true | "true" | "false"
target string
b24ui { root?: SlotClass; header?: SlotClass; leading?: SlotClass; leadingIcon?: SlotClass; title?: SlotClass; description?: SlotClass; body?: SlotClass; providers?: SlotClass; checkbox?: SlotClass; select?: SlotClass; password?: SlotClass; otp?: SlotClass; input?: SlotClass; separator?: SlotClass; form?: SlotClass; footer?: SlotClass; }
Компонент принимает и все атрибуты HTML-элемента <form>.

Слоты

Слот Тип
header{}
leading{ b24ui: object; }
title{}
description{}
providers{}
separator{}
validation{}
submit{ loading: boolean; }
footer{}

События

Событие Тип
submit[payload: FormSubmitEvent<InferOutput<T>>]

Доступ через ref

Типизированный экземпляр компонента (с formRef и state) доступен через useTemplateRef. Например, в отдельной форме сброса пароля:

<script setup lang="ts">
const authForm = useTemplateRef('authForm')
</script>

<template>
  <B24AuthForm ref="authForm" />
</template>

Доступны свойства:

ИмяТип
formRefRef<HTMLFormElement | null>
stateReactive<FormStateType>

Тема

Была ли страница полезна?