Files
kycnotme/web/src/components/InputFile.astro

70 lines
1.7 KiB
Plaintext
Raw Normal View History

2025-05-19 10:23:36 +00:00
---
import { cn } from '../lib/cn'
import { baseInputClassNames } from '../lib/formInputs'
import InputWrapper from './InputWrapper.astro'
import type { ComponentProps } from 'astro/types'
type Props = Omit<ComponentProps<typeof InputWrapper>, 'children' | 'inputId'> & {
accept?: string
disabled?: boolean
multiple?: boolean
2025-05-19 11:22:11 +00:00
removeCheckbox?: {
name: string
label: string
}
2025-05-19 10:23:36 +00:00
}
2025-05-25 12:28:30 +00:00
const { accept, disabled, multiple, removeCheckbox, classNames, ...wrapperProps } = Astro.props
2025-05-19 10:23:36 +00:00
const inputId = Astro.locals.makeId(`input-${wrapperProps.name}`)
const hasError = !!wrapperProps.error && wrapperProps.error.length > 0
---
2025-05-25 12:28:30 +00:00
<InputWrapper
inputId={inputId}
classNames={{
...classNames,
description: cn(classNames?.description, '[&:is(:has([data-remove-checkbox]:checked)_~_*)]:hidden'),
}}
{...wrapperProps}
>
2025-05-19 11:22:11 +00:00
{
!!removeCheckbox && (
2025-05-21 07:03:39 +00:00
<label
class={cn(
'flex cursor-pointer items-center gap-2 py-1 pl-1 text-sm leading-none',
disabled && 'cursor-not-allowed opacity-50'
)}
>
<input
transition:persist
type="checkbox"
name={removeCheckbox.name}
data-remove-checkbox
disabled={disabled}
/>
2025-05-19 11:22:11 +00:00
{removeCheckbox.label || 'Remove'}
</label>
)
}
2025-05-19 10:23:36 +00:00
<input
transition:persist
type="file"
id={inputId}
class={cn(
baseInputClassNames.input,
baseInputClassNames.file,
hasError && baseInputClassNames.error,
2025-05-19 11:22:11 +00:00
disabled && baseInputClassNames.disabled,
'[&:is(:has([data-remove-checkbox]:checked)_~_*)]:hidden'
2025-05-19 10:23:36 +00:00
)}
required={wrapperProps.required}
disabled={disabled}
name={wrapperProps.name}
accept={accept}
multiple={multiple}
/>
</InputWrapper>