mirror of
https://github.com/ArthurDanjou/ui.git
synced 2026-01-20 23:11:43 +01:00
Merge branch 'main' of https://github.com/benjamincanac/nuxt-ui3 into dev
This commit is contained in:
83
src/runtime/utils/form.ts
Normal file
83
src/runtime/utils/form.ts
Normal file
@@ -0,0 +1,83 @@
|
||||
import type { ZodSchema } from 'zod'
|
||||
import type { ValidationError as JoiError, Schema as JoiSchema } from 'joi'
|
||||
import type { ObjectSchema as YupObjectSchema, ValidationError as YupError } from 'yup'
|
||||
import type { ObjectSchemaAsync as ValibotObjectSchema } from 'valibot'
|
||||
import type { FormError } from '#ui/types/form'
|
||||
|
||||
export function isYupSchema (schema: any): schema is YupObjectSchema<any> {
|
||||
return schema.validate && schema.__isYupSchema__
|
||||
}
|
||||
|
||||
export function isYupError (error: any): error is YupError {
|
||||
return error.inner !== undefined
|
||||
}
|
||||
|
||||
export async function getYupErrors (state: any, schema: YupObjectSchema<any>): Promise<FormError[]> {
|
||||
try {
|
||||
await schema.validate(state, { abortEarly: false })
|
||||
return []
|
||||
} catch (error) {
|
||||
if (isYupError(error)) {
|
||||
return error.inner.map((issue) => ({
|
||||
name: issue.path ?? '',
|
||||
message: issue.message
|
||||
}))
|
||||
} else {
|
||||
throw error
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function isZodSchema (schema: any): schema is ZodSchema {
|
||||
return schema.parse !== undefined
|
||||
}
|
||||
|
||||
export async function getZodErrors (state: any, schema: ZodSchema): Promise<FormError[]> {
|
||||
const result = await schema.safeParseAsync(state)
|
||||
if (result.success === false) {
|
||||
return result.error.issues.map((issue) => ({
|
||||
name: issue.path.join('.'),
|
||||
message: issue.message
|
||||
}))
|
||||
}
|
||||
return []
|
||||
}
|
||||
|
||||
export function isJoiSchema (schema: any): schema is JoiSchema {
|
||||
return schema.validateAsync !== undefined && schema.id !== undefined
|
||||
}
|
||||
|
||||
export function isJoiError (error: any): error is JoiError {
|
||||
return error.isJoi === true
|
||||
}
|
||||
|
||||
export async function getJoiErrors (state: any, schema: JoiSchema): Promise<FormError[]> {
|
||||
try {
|
||||
await schema.validateAsync(state, { abortEarly: false })
|
||||
return []
|
||||
} catch (error) {
|
||||
if (isJoiError(error)) {
|
||||
return error.details.map((detail) => ({
|
||||
name: detail.path.join('.'),
|
||||
message: detail.message
|
||||
}))
|
||||
} else {
|
||||
throw error
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function isValibotSchema (schema: any): schema is ValibotObjectSchema<any> {
|
||||
return schema._parse !== undefined
|
||||
}
|
||||
|
||||
export async function getValibotError (state: any, schema: ValibotObjectSchema<any>): Promise<FormError[]> {
|
||||
const result = await schema._parse(state)
|
||||
if (result.issues) {
|
||||
return result.issues.map((issue) => ({
|
||||
name: issue.path?.map((p) => p.key).join('.') || '',
|
||||
message: issue.message
|
||||
}))
|
||||
}
|
||||
return []
|
||||
}
|
||||
@@ -1,86 +1,24 @@
|
||||
import { defu, createDefu } from 'defu'
|
||||
import { extendTailwindMerge } from 'tailwind-merge'
|
||||
import type { Strategy } from '../types'
|
||||
export function pick<Data extends object, Keys extends keyof Data> (data: Data, keys: Keys[]): Pick<Data, Keys> {
|
||||
const result = {} as Pick<Data, Keys>
|
||||
|
||||
const customTwMerge = extendTailwindMerge<string, string>({
|
||||
extend: {
|
||||
classGroups: {
|
||||
icons: [(classPart: string) => /^i-/.test(classPart)]
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const defuTwMerge = createDefu((obj, key, value, namespace) => {
|
||||
if (namespace === 'default' || namespace.startsWith('default.')) {
|
||||
return false
|
||||
}
|
||||
if (namespace === 'popper' || namespace.startsWith('popper.')) {
|
||||
return false
|
||||
}
|
||||
if (namespace.endsWith('avatar') && key === 'size') {
|
||||
return false
|
||||
}
|
||||
if (namespace.endsWith('chip') && key === 'size') {
|
||||
return false
|
||||
}
|
||||
if (namespace.endsWith('badge') && key === 'size' || key === 'color' || key === 'variant') {
|
||||
return false
|
||||
}
|
||||
if (typeof obj[key] === 'string' && typeof value === 'string' && obj[key] && value) {
|
||||
// @ts-ignore
|
||||
obj[key] = customTwMerge(obj[key], value)
|
||||
return true
|
||||
}
|
||||
})
|
||||
|
||||
export function mergeConfig<T> (strategy: Strategy, ...configs): T {
|
||||
if (strategy === 'override') {
|
||||
return defu({}, ...configs) as T
|
||||
for (const key of keys) {
|
||||
result[key] = data[key]
|
||||
}
|
||||
|
||||
return defuTwMerge({}, ...configs) as T
|
||||
}
|
||||
|
||||
export function hexToRgb (hex: string) {
|
||||
// Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF")
|
||||
const shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i
|
||||
hex = hex.replace(shorthandRegex, function (_, r, g, b) {
|
||||
return r + r + g + g + b + b
|
||||
})
|
||||
|
||||
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex)
|
||||
return result
|
||||
? `${parseInt(result[1], 16)} ${parseInt(result[2], 16)} ${parseInt(result[3], 16)}`
|
||||
: null
|
||||
}
|
||||
|
||||
export function getSlotsChildren (slots: any) {
|
||||
let children = slots.default?.()
|
||||
if (children?.length) {
|
||||
children = children.flatMap(c => {
|
||||
if (typeof c.type === 'symbol') {
|
||||
if (typeof c.children === 'string') {
|
||||
// `v-if="false"` or commented node
|
||||
return
|
||||
}
|
||||
return c.children
|
||||
} else if (c.type.name === 'ContentSlot') {
|
||||
return c.ctx.slots.default?.()
|
||||
}
|
||||
return c
|
||||
}).filter(Boolean)
|
||||
export function omit<Data extends object, Keys extends keyof Data> (data: Data, keys: Keys[]): Omit<Data, Keys> {
|
||||
const result = { ...data }
|
||||
|
||||
for (const key of keys) {
|
||||
delete result[key]
|
||||
}
|
||||
return children || []
|
||||
|
||||
return result as Omit<Data, Keys>
|
||||
}
|
||||
|
||||
/**
|
||||
* "123-foo" will be parsed to 123
|
||||
* This is used for the .number modifier in v-model
|
||||
*/
|
||||
export function looseToNumber (val: any): any {
|
||||
const n = parseFloat(val)
|
||||
return isNaN(n) ? val : n
|
||||
}
|
||||
|
||||
export * from './lodash'
|
||||
export * from './link'
|
||||
|
||||
Reference in New Issue
Block a user