Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

4.2 KB
Raw Blame
'use client'

import type { VariantProps } from 'class-variance-authority'
import type * as React from 'react'
import { Input as BaseInput } from '@base-ui/react/input'
import { cva } from 'class-variance-authority'
import { cn } from '../cn'
import { textControlGroupInputClassName } from '../internal/form-control'
import { resolveClassName } from '../internal/resolve-class-name'

const interactiveElementSelector =
  'button,a[href],[role="button"],[role="link"],select,[tabindex]:not([tabindex="-1"]),input:not([type="hidden"]):not([disabled]),[contenteditable]:not([contenteditable="false"]),textarea:not([disabled])'

type InputGroupProps = React.ComponentProps<'div'>

function InputGroup({ className, onMouseDown, ...props }: InputGroupProps) {
  return (
    // oxlint-disable-next-line jsx-a11y/no-static-element-interactions -- The handler extends the native input's pointer target; keyboard users reach the input directly.
    <div
      {...props}
      className={cn(
        [
          'flex min-h-8 w-full min-w-0 items-center rounded-lg border border-transparent bg-components-input-bg-normal',
          'has-[>input:enabled:not(:focus)]:not-has-[>input:is([readonly],[data-invalid])]:hover:border-components-input-border-hover has-[>input:enabled:not(:focus)]:not-has-[>input:is([readonly],[data-invalid])]:hover:bg-components-input-bg-hover',
          'has-[>input:focus]:ring-2 has-[>input:focus]:ring-state-accent-solid has-[>input:focus]:not-has-[>input:is([readonly],[data-invalid])]:bg-components-input-bg-active',
          'has-[>input:enabled[data-invalid]]:border-components-input-border-destructive has-[>input:enabled[data-invalid]]:bg-components-input-bg-destructive',
          'has-[>input:disabled]:cursor-not-allowed has-[>input:disabled]:border-transparent has-[>input:disabled]:bg-components-input-bg-disabled has-[>input:disabled]:text-components-input-text-filled-disabled',
          'has-[>input:disabled]:*:data-align:cursor-not-allowed has-[>input:disabled]:*:data-align:text-components-input-text-filled-disabled',
          'has-[>input:enabled[readonly]]:cursor-default has-[>input:enabled[readonly]]:*:data-align:cursor-default',
          'has-[>[data-align=inline-start]]:[&>input]:ps-0',
          'has-[>[data-align=inline-end]]:[&>input]:pe-0',
        ],
        className,
      )}
      onMouseDown={(event) => {
        onMouseDown?.(event)
        if (event.defaultPrevented || event.button !== 0) return

        const eventPath = event.nativeEvent.composedPath()
        if (!eventPath.includes(event.currentTarget)) return

        const target = eventPath[0]
        const ElementConstructor = event.currentTarget.ownerDocument.defaultView?.Element
        if (!ElementConstructor || !(target instanceof ElementConstructor)) return
        if (target !== event.currentTarget && target.closest(interactiveElementSelector)) return

        const input = event.currentTarget.querySelector<HTMLInputElement>(':scope > input')
        if (!input || input.disabled) return

        event.preventDefault()
        input.focus()
      }}
    />
  )
}

type InputGroupInputProps = Omit<BaseInput.Props, 'render' | 'size'>

function InputGroupInput({ className, ...props }: InputGroupInputProps) {
  return (
    <BaseInput
      {...props}
      className={(state) =>
        cn(textControlGroupInputClassName, 'px-3', resolveClassName(className, state))
      }
    />
  )
}

const inputGroupAddonVariants = cva(
  'flex shrink-0 cursor-text items-center self-stretch system-sm-regular text-text-secondary select-none',
  {
    variants: {
      align: {
        'inline-start': 'order-first ps-2 pe-1',
        'inline-end': 'order-last ps-1 pe-2',
      },
    },
    defaultVariants: {
      align: 'inline-start',
    },
  },
)

type InputGroupAddonProps = React.ComponentProps<'div'> &
  VariantProps<typeof inputGroupAddonVariants>

function InputGroupAddon({ align = 'inline-start', className, ...props }: InputGroupAddonProps) {
  return (
    <div
      {...props}
      data-align={align}
      className={cn(inputGroupAddonVariants({ align }), className)}
    />
  )
}

export { InputGroup, InputGroupAddon, InputGroupInput }
export type { InputGroupAddonProps, InputGroupInputProps, InputGroupProps }