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

perf: expose the formApi for a login form #4806

Merged
merged 1 commit into from
Nov 4, 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
2 changes: 2 additions & 0 deletions packages/@core/base/shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,12 @@
"dependencies": {
"@ctrl/tinycolor": "catalog:",
"@tanstack/vue-store": "catalog:",
"@types/lodash.get": "catalog:",
"@vue/shared": "catalog:",
"clsx": "catalog:",
"defu": "catalog:",
"lodash.clonedeep": "catalog:",
"lodash.get": "catalog:",
"nprogress": "catalog:",
"tailwind-merge": "catalog:",
"theme-colors": "catalog:"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const emit = defineEmits<{

const router = useRouter();

const [Form, { validate, getValues }] = useVbenForm(
const [Form, formApi] = useVbenForm(
reactive({
commonConfig: {
hideLabel: true,
Expand All @@ -65,8 +65,8 @@ const [Form, { validate, getValues }] = useVbenForm(
);

async function handleSubmit() {
const { valid } = await validate();
const values = await getValues();
const { valid } = await formApi.validate();
const values = await formApi.getValues();
if (valid) {
emit('submit', {
code: values?.code,
Expand All @@ -78,6 +78,10 @@ async function handleSubmit() {
function goToLogin() {
router.push(props.loginPath);
}

defineExpose({
getFormApi: () => formApi,
});
</script>

<template>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const emit = defineEmits<{
submit: [Record<string, any>];
}>();

const [Form, { validate, getValues }] = useVbenForm(
const [Form, formApi] = useVbenForm(
reactive({
commonConfig: {
hideLabel: true,
Expand All @@ -64,8 +64,8 @@ const [Form, { validate, getValues }] = useVbenForm(
const router = useRouter();

async function handleSubmit() {
const { valid } = await validate();
const values = await getValues();
const { valid } = await formApi.validate();
const values = await formApi.getValues();
if (valid) {
emit('submit', values);
}
Expand All @@ -74,6 +74,10 @@ async function handleSubmit() {
function goToLogin() {
router.push(props.loginPath);
}

defineExpose({
getFormApi: () => formApi,
});
</script>

<template>
Expand Down
12 changes: 8 additions & 4 deletions packages/effects/common-ui/src/ui/authentication/login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const emit = defineEmits<{
submit: [Recordable<any>];
}>();

const [Form, { setFieldValue, validate, getValues }] = useVbenForm(
const [Form, formApi] = useVbenForm(
reactive({
commonConfig: {
hideLabel: true,
Expand All @@ -63,8 +63,8 @@ const localUsername = localStorage.getItem(REMEMBER_ME_KEY) || '';
const rememberMe = ref(!!localUsername);

async function handleSubmit() {
const { valid } = await validate();
const values = await getValues();
const { valid } = await formApi.validate();
const values = await formApi.getValues();
if (valid) {
localStorage.setItem(
REMEMBER_ME_KEY,
Expand All @@ -80,9 +80,13 @@ function handleGo(path: string) {

onMounted(() => {
if (localUsername) {
setFieldValue('username', localUsername);
formApi.setFieldValue('username', localUsername);
}
});

defineExpose({
getFormApi: () => formApi,
});
</script>

<template>
Expand Down
10 changes: 7 additions & 3 deletions packages/effects/common-ui/src/ui/authentication/register.vue
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const emit = defineEmits<{
submit: [Recordable<any>];
}>();

const [Form, { validate, getValues }] = useVbenForm(
const [Form, formApi] = useVbenForm(
reactive({
commonConfig: {
hideLabel: true,
Expand All @@ -66,8 +66,8 @@ const [Form, { validate, getValues }] = useVbenForm(
const router = useRouter();

async function handleSubmit() {
const { valid } = await validate();
const values = await getValues();
const { valid } = await formApi.validate();
const values = await formApi.getValues();
if (valid) {
emit('submit', values as { password: string; username: string });
}
Expand All @@ -76,6 +76,10 @@ async function handleSubmit() {
function goToLogin() {
router.push(props.loginPath);
}

defineExpose({
getFormApi: () => formApi,
});
</script>

<template>
Expand Down
23 changes: 11 additions & 12 deletions packages/effects/plugins/src/vxe-table/use-vxe-grid.vue
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ const props = withDefaults(defineProps<Props>(), {});

const FORM_SLOT_PREFIX = 'form-';

const TOOLBAR_ACTIONS = 'toolbar-actions';
const TOOLBAR_TOOLS = 'toolbar-tools';

const gridRef = useTemplateRef<VxeGridInstance>('gridRef');

const state = props.api?.useStore?.();
Expand Down Expand Up @@ -87,15 +90,16 @@ const showTableTitle = computed(() => {

const showToolbar = computed(() => {
return (
!!slots['toolbar-actions']?.() ||
!!slots['toolbar-tools']?.() ||
!!slots[TOOLBAR_ACTIONS]?.() ||
!!slots[TOOLBAR_TOOLS]?.() ||
showTableTitle.value
);
});

const toolbarOptions = computed(() => {
const slotActions = slots['toolbar-actions']?.();
const slotTools = slots['toolbar-tools']?.();
const slotActions = slots[TOOLBAR_ACTIONS]?.();
const slotTools = slots[TOOLBAR_TOOLS]?.();

if (!showToolbar.value) {
return {};
}
Expand All @@ -105,9 +109,9 @@ const toolbarOptions = computed(() => {
toolbarConfig: {
slots: {
...(slotActions || showTableTitle.value
? { buttons: 'toolbar-actions' }
? { buttons: TOOLBAR_ACTIONS }
: {}),
...(slotTools ? { tools: 'toolbar-tools' } : {}),
...(slotTools ? { tools: TOOLBAR_TOOLS } : {}),
},
},
};
Expand All @@ -122,11 +126,6 @@ const options = computed(() => {
toolbarOptions.value,
toRaw(gridOptions.value),
globalGridConfig,
{
// toolbarConfig: {
// tools: [],
// },
} as VxeTableGridProps,
),
);

Expand Down Expand Up @@ -185,7 +184,7 @@ const delegatedSlots = computed(() => {
const resultSlots: string[] = [];

for (const key of Object.keys(slots)) {
if (!['empty', 'form', 'loading', 'toolbar-actions'].includes(key)) {
if (!['empty', 'form', 'loading', TOOLBAR_ACTIONS].includes(key)) {
resultSlots.push(key);
}
}
Expand Down
Loading