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

add-zod(5): Add makeProps #504

Merged
merged 16 commits into from
Sep 24, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/documentation/pages/examples/layouts.vue
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ export default {
},
userMenuData: [
{
title: 'Switch To',
links: [
{
link: '#',
Expand All @@ -132,9 +131,9 @@ export default {
title: 'Service Panel',
},
],
title: 'Switch To',
},
{
title: 'Recent Service',
links: [
{
link: '#',
Expand All @@ -149,6 +148,7 @@ export default {
title: '4YOURMIND Service',
},
],
title: 'Recent Service',
},
{
links: [
Expand All @@ -161,6 +161,7 @@ export default {
title: 'Logout (Close Example)',
},
],
title: null,
},
],
actionbarMenu: [
Expand Down
28 changes: 17 additions & 11 deletions packages/kotti-ui/source/kotti-user-menu/KtUserMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -55,32 +55,38 @@ import { mixin as clickaway } from 'vue-clickaway'

import { KtAvatar } from '../kotti-avatar'
import { IS_NAVBAR_NARROW } from '../kotti-navbar/constants'
import { propValidator } from '../props'
import { Kotti } from '../types'

import { KottiUserMenu } from './types'

const linkIsValid = (link: KottiUserMenu.SectionLink) => Boolean(link.title)
carsoli marked this conversation as resolved.
Show resolved Hide resolved

const sectionIsValid = (section: KottiUserMenu.Section) =>
Array.isArray(section.links) &&
Boolean(section.links.length) &&
section.links.every(linkIsValid)

export default defineComponent<KottiUserMenu.PropsInternal>({
name: 'KtUserMenu',
components: {
KtAvatar,
},
mixins: [clickaway],
props: {
userAvatar: { type: String, default: null },
sections: {
required: true,
type: Array,
validator: propValidator(KottiUserMenu.propsInternalSchema, 'sections'),
},
userAvatar: {
default: null,
type: String,
validator: propValidator(KottiUserMenu.propsInternalSchema, 'userAvatar'),
},
userName: {
default: null,
type: String,
validator: propValidator(KottiUserMenu.propsInternalSchema, 'userName'),
},
userStatus: {
required: true,
validator: (sections) => sections.every(sectionIsValid),
type: String,
validator: propValidator(KottiUserMenu.propsInternalSchema, 'userStatus'),
},
userName: { type: String, default: null },
userStatus: { type: String, required: true },
},
setup() {
const isMenuShow = ref(false)
Expand Down
29 changes: 20 additions & 9 deletions packages/kotti-ui/source/kotti-user-menu/types.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
import { KottiNavbar } from '../kotti-navbar/types'
import { z } from 'zod'

import { SpecifyRequiredProps } from '../types/utilities'

export namespace KottiUserMenu {
export type SectionLink = KottiNavbar.SectionLink
export type Section = KottiNavbar.Section
export const sectionLinkSchema = z.object({
title: z.string(),
FlorianWendelborn marked this conversation as resolved.
Show resolved Hide resolved
})
export type SectionLink = z.infer<typeof sectionLinkSchema>

export const sectionSchema = z.object({
links: z.array(sectionLinkSchema).nonempty(),
title: z.string().nullable(),
FlorianWendelborn marked this conversation as resolved.
Show resolved Hide resolved
})

export type Section = z.infer<typeof sectionSchema>

export type PropsInternal = {
sections: Array<KottiUserMenu.Section>
userAvatar: string | null
userName: string | null
userStatus: string
}
export const propsInternalSchema = z.object({
sections: z.array(sectionSchema),
FlorianWendelborn marked this conversation as resolved.
Show resolved Hide resolved
userAvatar: z.string().nullable(),
userName: z.string().nullable(),
userStatus: z.string(),
})
export type PropsInternal = z.infer<typeof propsInternalSchema>

export type Props = SpecifyRequiredProps<
KottiUserMenu.PropsInternal,
Expand Down