mirror of https://github.com/langgenius/dify.git
feat: child chunk component for dataset
This commit is contained in:
parent
d1c3c26dc7
commit
11679dc68a
|
|
@ -0,0 +1,86 @@
|
|||
import { useState } from 'react'
|
||||
import type { FC, ReactNode } from 'react'
|
||||
import { FloatingFocusManager, autoUpdate, flip, shift, useDismiss, useFloating, useHover, useInteractions, useRole } from '@floating-ui/react'
|
||||
import { RiDeleteBinLine } from '@remixicon/react'
|
||||
import type { SliceProps } from './type'
|
||||
import { SliceContainer, SliceContent, SliceDivider, SliceLabel } from './shared'
|
||||
import classNames from '@/utils/classnames'
|
||||
import ActionButton, { ActionButtonState } from '@/app/components/base/action-button'
|
||||
|
||||
type EditSliceProps = SliceProps<{
|
||||
label: ReactNode
|
||||
onDelete: () => void
|
||||
}>
|
||||
|
||||
export const EditSlice: FC<EditSliceProps> = (props) => {
|
||||
const { label, className, text, onDelete, ...rest } = props
|
||||
const [delBtnShow, setDelBtnShow] = useState(false)
|
||||
const [isDelBtnHover, setDelBtnHover] = useState(false)
|
||||
|
||||
const { refs, floatingStyles, context } = useFloating({
|
||||
open: delBtnShow,
|
||||
onOpenChange: setDelBtnShow,
|
||||
placement: 'right',
|
||||
whileElementsMounted: autoUpdate,
|
||||
middleware: [
|
||||
flip(),
|
||||
shift(),
|
||||
],
|
||||
})
|
||||
const hover = useHover(context, {})
|
||||
const dismiss = useDismiss(context)
|
||||
const role = useRole(context)
|
||||
const { getReferenceProps, getFloatingProps } = useInteractions([hover, dismiss, role])
|
||||
|
||||
const isDestructive = delBtnShow && isDelBtnHover
|
||||
|
||||
return (
|
||||
<div>
|
||||
<SliceContainer {...rest}
|
||||
className={className}
|
||||
ref={refs.setReference}
|
||||
{...getReferenceProps()}
|
||||
>
|
||||
<SliceLabel
|
||||
className={classNames(
|
||||
isDestructive && '!bg-red-500 !text-text-primary-on-surface',
|
||||
)}
|
||||
>
|
||||
{label}
|
||||
</SliceLabel>
|
||||
<SliceContent
|
||||
className={classNames(isDestructive && '!bg-state-destructive-hover-alt')}
|
||||
>
|
||||
{text}
|
||||
</SliceContent>
|
||||
<SliceDivider
|
||||
className={classNames(
|
||||
isDestructive && '!bg-state-destructive-hover-alt',
|
||||
)}
|
||||
/>
|
||||
{delBtnShow && <FloatingFocusManager
|
||||
context={context}
|
||||
>
|
||||
<div
|
||||
ref={refs.setFloating}
|
||||
style={floatingStyles}
|
||||
{...getFloatingProps()}
|
||||
className='p-1 rounded-lg bg-components-actionbar-bg shadow flex items-center justify-center'
|
||||
onMouseEnter={() => setDelBtnHover(true)}
|
||||
onMouseLeave={() => setDelBtnHover(false)}
|
||||
>
|
||||
<ActionButton
|
||||
onClick={() => {
|
||||
onDelete()
|
||||
setDelBtnShow(false)
|
||||
}}
|
||||
state={ActionButtonState.Destructive}
|
||||
>
|
||||
<RiDeleteBinLine className='w-4 h-4' />
|
||||
</ActionButton>
|
||||
</div>
|
||||
</FloatingFocusManager>}
|
||||
</SliceContainer>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
import type { FC, PropsWithChildren } from 'react'
|
||||
|
||||
export type FormattedTextProps = PropsWithChildren
|
||||
|
||||
export const FormattedText: FC<FormattedTextProps> = (props) => {
|
||||
return <p className='leading-7'>{props.children}</p>
|
||||
}
|
||||
|
|
@ -2,16 +2,14 @@ import { useState } from 'react'
|
|||
import type { FC, ReactNode } from 'react'
|
||||
import { autoUpdate, flip, inline, shift, useDismiss, useFloating, useHover, useInteractions, useRole } from '@floating-ui/react'
|
||||
import type { SliceProps } from './type'
|
||||
import classNames from '@/utils/classnames'
|
||||
import { SliceContainer, SliceContent, SliceDivider, SliceLabel } from './shared'
|
||||
|
||||
type NormalSliceProps = SliceProps<{
|
||||
type PreviewSliceProps = SliceProps<{
|
||||
label: ReactNode
|
||||
tooltip: ReactNode
|
||||
}>
|
||||
|
||||
const baseStyle = 'py-[3px]'
|
||||
|
||||
export const NormalSlice: FC<NormalSliceProps> = (props) => {
|
||||
export const PreviewSlice: FC<PreviewSliceProps> = (props) => {
|
||||
const { label, className, text, tooltip, ...rest } = props
|
||||
const [tooltipOpen, setTooltipOpen] = useState(false)
|
||||
const { refs, floatingStyles, context } = useFloating({
|
||||
|
|
@ -34,37 +32,23 @@ export const NormalSlice: FC<NormalSliceProps> = (props) => {
|
|||
const { getReferenceProps, getFloatingProps } = useInteractions([hover, dismiss, role])
|
||||
return (
|
||||
<>
|
||||
<span {...rest} className={classNames(
|
||||
'group align-bottom mr-1 select-none text-sm',
|
||||
className,
|
||||
)} ref={refs.setReference}
|
||||
{...getReferenceProps()}
|
||||
<SliceContainer {...rest}
|
||||
className={className}
|
||||
ref={refs.setReference}
|
||||
{...getReferenceProps()}
|
||||
>
|
||||
<span className={classNames(
|
||||
baseStyle,
|
||||
'px-1 bg-state-base-hover-alt text-text-tertiary group-hover:bg-state-accent-solid group-hover:text-white',
|
||||
)}>
|
||||
{label}
|
||||
</span>
|
||||
<span className={classNames(
|
||||
baseStyle,
|
||||
'px-1 text-text-secondary bg-state-base-hover group-hover:bg-state-accent-hover-alt group-hover:text-text-primary',
|
||||
)}>
|
||||
{text}
|
||||
</span>
|
||||
<span
|
||||
className='py-[3px] bg-state-base-active group-hover:bg-state-accent-solid text-sm px-[1px]'
|
||||
>
|
||||
{/* use a zero-width space to make the hover area bigger */}
|
||||
​
|
||||
</span>
|
||||
</span>
|
||||
{tooltipOpen && <div ref={refs.setFloating} style={floatingStyles}
|
||||
<SliceLabel>{label}</SliceLabel>
|
||||
<SliceContent>{text}</SliceContent>
|
||||
<SliceDivider />
|
||||
</SliceContainer>
|
||||
{tooltipOpen && <span
|
||||
ref={refs.setFloating}
|
||||
style={floatingStyles}
|
||||
{...getFloatingProps()}
|
||||
className='p-2 rounded-md bg-components-tooltip-bg shadow shadow-shadow-shadow-5 backdrop-blur-[5px] text-text-secondary leading-4 border-[0.5px] border-components-panel-border text-xs'
|
||||
>
|
||||
{tooltip}
|
||||
</div>}
|
||||
</span>}
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
|
@ -0,0 +1,57 @@
|
|||
import { type ComponentProps, type FC, forwardRef } from 'react'
|
||||
import classNames from '@/utils/classnames'
|
||||
|
||||
const baseStyle = 'py-[3px]'
|
||||
|
||||
export type SliceContainerProps = ComponentProps<'span'>
|
||||
|
||||
export const SliceContainer: FC<SliceContainerProps> = forwardRef((props, ref) => {
|
||||
const { className, ...rest } = props
|
||||
return <span {...rest} ref={ref} className={classNames(
|
||||
'group align-bottom mr-1 select-none text-sm',
|
||||
className,
|
||||
)} />
|
||||
})
|
||||
SliceContainer.displayName = 'SliceContainer'
|
||||
|
||||
export type SliceLabelProps = ComponentProps<'span'>
|
||||
|
||||
export const SliceLabel: FC<SliceLabelProps> = forwardRef((props, ref) => {
|
||||
const { className, children, ...rest } = props
|
||||
return <span {...rest} ref={ref} className={classNames(
|
||||
baseStyle,
|
||||
'px-1 bg-state-base-hover-alt group-hover:bg-state-accent-solid group-hover:text-text-primary-on-surface uppercase text-text-tertiary',
|
||||
className,
|
||||
)}>
|
||||
{children}
|
||||
</span>
|
||||
})
|
||||
SliceLabel.displayName = 'SliceLabel'
|
||||
|
||||
export type SliceContentProps = ComponentProps<'span'>
|
||||
|
||||
export const SliceContent: FC<SliceContentProps> = forwardRef((props, ref) => {
|
||||
const { className, children, ...rest } = props
|
||||
return <span {...rest} ref={ref} className={classNames(
|
||||
baseStyle,
|
||||
'px-1 bg-state-base-hover group-hover:bg-state-accent-hover-alt group-hover:text-text-primary',
|
||||
className,
|
||||
)}>
|
||||
{children}
|
||||
</span>
|
||||
})
|
||||
SliceContent.displayName = 'SliceContent'
|
||||
|
||||
export type SliceDividerProps = ComponentProps<'span'>
|
||||
|
||||
export const SliceDivider: FC<SliceDividerProps> = forwardRef((props, ref) => {
|
||||
const { className, ...rest } = props
|
||||
return <span {...rest} ref={ref} className={classNames(
|
||||
'py-[3px] bg-state-base-active group-hover:bg-state-accent-solid text-sm px-[1px]',
|
||||
className,
|
||||
)}>
|
||||
{/* use a zero-width space to make the hover area bigger */}
|
||||
​
|
||||
</span>
|
||||
})
|
||||
SliceDivider.displayName = 'SliceDivider'
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
import type { ComponentProps, FC } from 'react'
|
||||
import classNames from '@/utils/classnames'
|
||||
|
||||
export type FormattedTextProps = ComponentProps<'p'>
|
||||
|
||||
export const FormattedText: FC<FormattedTextProps> = (props) => {
|
||||
const { className, ...rest } = props
|
||||
return <p
|
||||
{...rest}
|
||||
className={classNames('leading-7', className)}
|
||||
>{props.children}</p>
|
||||
}
|
||||
|
|
@ -1,16 +1,35 @@
|
|||
'use client'
|
||||
|
||||
import { FormattedText } from '../components/datasets/formatted-text/flavours/formatted'
|
||||
import { NormalSlice } from '../components/datasets/formatted-text/flavours/normal'
|
||||
import { FormattedText } from '../components/datasets/formatted-text/formatted'
|
||||
import { PreviewSlice } from '../components/datasets/formatted-text/flavours/preview-slice'
|
||||
import { EditSlice } from '../components/datasets/formatted-text/flavours/edit-slice'
|
||||
|
||||
export default function Page() {
|
||||
return <div className='p-4'>
|
||||
<FormattedText>
|
||||
<NormalSlice label='C-1' text='lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.' tooltip={'Child-chunk-2 · 268 Characters'} />
|
||||
<NormalSlice label='C-1' text='lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.' tooltip={'Child-chunk-2 · 268 Characters'} />
|
||||
<NormalSlice label='C-1' text='lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.' tooltip={'Child-chunk-2 · 268 Characters'} />
|
||||
<NormalSlice label='C-1' text='lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.' tooltip={'Child-chunk-2 · 268 Characters'} />
|
||||
<NormalSlice label='C-1' text='lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.' tooltip={'Child-chunk-2 · 268 Characters'} />
|
||||
<PreviewSlice label='C-1' text='lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.' tooltip={'Child-chunk-2 · 268 Characters'} />
|
||||
<PreviewSlice label='C-1' text='lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.' tooltip={'Child-chunk-2 · 268 Characters'} />
|
||||
<PreviewSlice label='C-1' text='lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.' tooltip={'Child-chunk-2 · 268 Characters'} />
|
||||
<PreviewSlice label='C-1' text='lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.' tooltip={'Child-chunk-2 · 268 Characters'} />
|
||||
<PreviewSlice label='C-1' text='lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.' tooltip={'Child-chunk-2 · 268 Characters'} />
|
||||
</FormattedText>
|
||||
|
||||
<div className='mt-12 flex flex-col gap-2'>
|
||||
<EditSlice label='C-1' text='lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.' onDelete={function (): void {
|
||||
console.log('onDelete')
|
||||
} } />
|
||||
<EditSlice label='C-1' text='lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.' onDelete={function (): void {
|
||||
console.log('onDelete')
|
||||
} } />
|
||||
<EditSlice label='C-1' text='lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.' onDelete={function (): void {
|
||||
console.log('onDelete')
|
||||
} } />
|
||||
<EditSlice label='C-1' text='lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.' onDelete={function (): void {
|
||||
console.log('onDelete')
|
||||
} } />
|
||||
<EditSlice label='C-1' text='lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.' onDelete={function (): void {
|
||||
console.log('onDelete')
|
||||
} } />
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue