mirror of
https://github.com/logseq/logseq.git
synced 2026-05-24 04:34:14 +00:00
* enhance(plugin): call apis with the sdk ns * enhance(plugin): types * enhance(api): get value from the computed style * enhance(api): types * enhance(plugin): types * enhance(plugin): types * fix: lint * fix(apis): incorrect shortcut command registion for block editing mode #10392 * fix(api): types * enhance(apis): support register shortcuts with multi binding vals * fix(plugins): normalize command key to make the internal keyword legal * chore(plugin): build libs core * chore(plugin): bump version * enhance(apis): normalize apis cljs data * chore(plugin): update libs user sdk * chore(plugin): CHANGELOG.md * fix: typo * feat(ui): add package * Update .gitignore * feat(ui): set up shui infrastructure * feat(ui): add storybook macro * enhance(ui): storybook themes * feat(ui): adapt ui button to classic * enhance(ui): shui story * feat(ui): shui toaster * enhance(ui): shui toaster * feat(ui): imperative API for shui toaster * enhance(shui): update API for shui toaster * enhance(shui): update hooks for shui toaster * enhance(shui): remove debug * feat(ui): story for the shui toaster * feat(ui): story * feat(ui): story docs * feat(ui): more variants for the shui toaster * feat(ui): story * fix(ux): support querying plugins with right space chars * feat(ui): add shui `Alert` component * enhance(ui): shui demo * feat(ui): add logseq UI readme * enhance(ui): default shui theme * feat(ui): add shui `Badge` component & demo * fix(ui): outline theme for shui button * feat(ui): custom icon for the toaster item * feat(ui): add shui dropdown & demo * feat(ui): WIP shui form related components * feat(ui): WIP shui form-related components * feat(ui): WIP shui form * feat(ui): WIP shui form state for validation * fix(ui): missing rounded for ui button * feat(ui): add yup for shui form as default validation resolver * enhance(ui): simplify validation schema input for the shui form * fix(ui): accent ring color for input * feat(ui): add shui switch * feat(ui): add shui checkbox & switch * feat(ui): add shui radio group * fix(ui): missing file * feat(ui): add Textarea component * feat(ui): add shui card & skeleton * feat(ui): add shui context menu component & demo * fix(ui): accent color for the context menu item * feat(ui): add shui select component & demo * enhance(ui): ui css priority * feat(ui): add shui calendar & ui details * feat(ui): add shui popover * feat(ui): add date picker & demo * feat(ui): add shui dialog * feat(ui): WIP add shui dialog * feat(ui): WIP shui dialog as modal * feat(ui): WIP imperative APIs for the shui modal * feat(ui): imperative APIs for the shui modal/alert * feat(ui): support imperative API alert!/confirm! return promise * feat(ui): simplify shui components resources * feat(ui): response layout for the demo ui page * feat(ui): simplify colors * feat(ui): simplify colors * feat(ui): simplify colors * refactor(ui): WIP Adapt to the new button component * refactor(ui): polish new button & colors * fix(ui): the new theme color for the plugin settings nav item link * fix(ui): blockquote colors * enhance(ui): more custom colors for shui button * feat(ui): WIP make logseq green as a theme color * enhance(ui): polish logseq classical theme color * fix(ui): theme details of all pages * enhance(ui): polish logseq theme color for dark mode * fix(ui): missing table style * refactor(ui): simplify the all shui buttons & shortcuts for the cmdk component * fix(ui): missing file * refactor(ui): clear up stuff * fix(ui): theme color related issues * enhance(ui): polish button style * enhance(ui): polish the keymap setting pane * fix(ui): hint button from the cmdk pane footer * fix(ui): logseq colors for the storybook * enhance(ui): stories for the shui components * fix(ui): active color for the old toggle component * enhance(ui): keep the constant size of the settings pane * fix(ui): polish search input for the plugins pane * enhance(ui): polish number list bullet colors * feat(ui): add shui tooltip component * chore: build ui * chore(ui): clean up resources * fix: lint * fix: lint * fix: lint * fix(ui): alignment of the keymap title from the settings pane * fix: tests * fix(ui): close button for the classic notification tip * fix(ui): polish toaster viewport * enhance(ui): polish the ghost button colors * enhance(ui): demos for tips * fix(ui): accent colors for the rc-datepicker * fix(ui): accent color for the menu item * refactor(ui): remove unless code for the accent colors * enhance(ui): polish pdf viewer background color for the accent color mode * fix: lint * fix: lint * fix: lint * enhance(ui): support button with the custom href link * enhance(ui): polish aside setting items * enhance(ui): polish accent color for buttons * enhance(ui): polish all pages --------- Co-authored-by: Gabriel Horner <97210743+logseq-cldwalker@users.noreply.github.com>
183 lines
4.1 KiB
TypeScript
183 lines
4.1 KiB
TypeScript
import * as React from 'react'
|
|
import * as LabelPrimitive from '@radix-ui/react-label'
|
|
import { Slot } from '@radix-ui/react-slot'
|
|
import {
|
|
Controller,
|
|
ControllerProps,
|
|
FieldPath,
|
|
FieldValues,
|
|
FormProvider,
|
|
useFormContext,
|
|
useFormState,
|
|
useForm,
|
|
} from 'react-hook-form'
|
|
|
|
// @ts-ignore
|
|
import { cn, useId } from '@/lib/utils'
|
|
import { Label } from './label'
|
|
|
|
const Form = FormProvider
|
|
|
|
type FormFieldContextValue<
|
|
TFieldValues extends FieldValues = FieldValues,
|
|
TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>
|
|
> = {
|
|
name: TName
|
|
}
|
|
|
|
const FormFieldContext = React.createContext<FormFieldContextValue>(
|
|
{} as FormFieldContextValue
|
|
)
|
|
|
|
const FormField = <
|
|
TFieldValues extends FieldValues = FieldValues,
|
|
TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>
|
|
>({
|
|
...props
|
|
}: ControllerProps<TFieldValues, TName>) => {
|
|
return (
|
|
<FormFieldContext.Provider value={{ name: props.name }}>
|
|
<Controller {...props} />
|
|
</FormFieldContext.Provider>
|
|
)
|
|
}
|
|
|
|
const useFormField = () => {
|
|
const fieldContext = React.useContext(FormFieldContext)
|
|
const itemContext = React.useContext(FormItemContext)
|
|
const { getFieldState, formState } = useFormContext()
|
|
|
|
const fieldState = getFieldState(fieldContext.name, formState)
|
|
|
|
if (!fieldContext) {
|
|
throw new Error('useFormField should be used within <FormField>')
|
|
}
|
|
|
|
const { id } = itemContext
|
|
|
|
return {
|
|
id,
|
|
name: fieldContext.name,
|
|
formItemId: `${id}-form-item`,
|
|
formDescriptionId: `${id}-form-item-description`,
|
|
formMessageId: `${id}-form-item-message`,
|
|
...fieldState,
|
|
}
|
|
}
|
|
|
|
type FormItemContextValue = {
|
|
id: string
|
|
}
|
|
|
|
const FormItemContext = React.createContext<FormItemContextValue>(
|
|
{} as FormItemContextValue
|
|
)
|
|
|
|
const FormItem = React.forwardRef<
|
|
HTMLDivElement,
|
|
React.HTMLAttributes<HTMLDivElement>
|
|
>(({ className, ...props }, ref) => {
|
|
const id = useId()
|
|
|
|
return (
|
|
<FormItemContext.Provider value={{ id }}>
|
|
<div ref={ref} className={cn('ui__form-item space-y-2', className)} {...props} />
|
|
</FormItemContext.Provider>
|
|
)
|
|
})
|
|
FormItem.displayName = 'FormItem'
|
|
|
|
const FormLabel = React.forwardRef<
|
|
React.ElementRef<typeof LabelPrimitive.Root>,
|
|
React.ComponentPropsWithoutRef<typeof LabelPrimitive.Root>
|
|
>(({ className, ...props }, ref) => {
|
|
const { error, formItemId } = useFormField()
|
|
|
|
return (
|
|
<Label
|
|
ref={ref}
|
|
className={cn('ui__form-label', error && 'text-destructive', className)}
|
|
htmlFor={formItemId}
|
|
{...props}
|
|
/>
|
|
)
|
|
})
|
|
FormLabel.displayName = 'FormLabel'
|
|
|
|
const FormControl = React.forwardRef<
|
|
React.ElementRef<typeof Slot>,
|
|
React.ComponentPropsWithoutRef<typeof Slot>
|
|
>(({ ...props }, ref) => {
|
|
const { error, formItemId, formDescriptionId, formMessageId } = useFormField()
|
|
|
|
return (
|
|
<Slot
|
|
ref={ref}
|
|
id={formItemId}
|
|
aria-describedby={
|
|
!error
|
|
? `${formDescriptionId}`
|
|
: `${formDescriptionId} ${formMessageId}`
|
|
}
|
|
aria-invalid={!!error}
|
|
{...props}
|
|
/>
|
|
)
|
|
})
|
|
FormControl.displayName = 'FormControl'
|
|
|
|
const FormDescription = React.forwardRef<
|
|
HTMLParagraphElement,
|
|
React.HTMLAttributes<HTMLParagraphElement>
|
|
>(({ className, ...props }, ref) => {
|
|
const { formDescriptionId } = useFormField()
|
|
|
|
return (
|
|
<p
|
|
ref={ref}
|
|
id={formDescriptionId}
|
|
className={cn('ui__form-description text-sm text-muted-foreground', className)}
|
|
{...props}
|
|
/>
|
|
)
|
|
})
|
|
FormDescription.displayName = 'FormDescription'
|
|
|
|
const FormMessage = React.forwardRef<
|
|
HTMLParagraphElement,
|
|
React.HTMLAttributes<HTMLParagraphElement>
|
|
>(({ className, children, ...props }, ref) => {
|
|
const { error, formMessageId } = useFormField()
|
|
const body = error ? String(error?.message) : children
|
|
|
|
if (!body) {
|
|
return null
|
|
}
|
|
|
|
return (
|
|
<p
|
|
ref={ref}
|
|
id={formMessageId}
|
|
className={cn('ui__form-message text-sm font-medium text-destructive', className)}
|
|
{...props}
|
|
>
|
|
{body}
|
|
</p>
|
|
)
|
|
})
|
|
FormMessage.displayName = 'FormMessage'
|
|
|
|
export {
|
|
useForm,
|
|
useFormContext,
|
|
useFormState,
|
|
useFormField,
|
|
Form,
|
|
FormItem,
|
|
FormLabel,
|
|
FormControl,
|
|
FormDescription,
|
|
FormMessage,
|
|
FormField,
|
|
}
|