This commit is contained in:
+36
-12
@@ -17,30 +17,54 @@ type Props = {
|
||||
} & SelectHTMLAttributes<HTMLSelectElement>
|
||||
|
||||
const Select: FC<Props> = (props: Props) => {
|
||||
const {
|
||||
className,
|
||||
items,
|
||||
error_text,
|
||||
placeholder,
|
||||
label,
|
||||
isNotRequired,
|
||||
value,
|
||||
...selectProps
|
||||
} = props
|
||||
|
||||
const hasMatchingItem =
|
||||
value !== undefined &&
|
||||
value !== '' &&
|
||||
items?.some((item) => String(item.value) === String(value))
|
||||
|
||||
const selectValue =
|
||||
!value || hasMatchingItem || !items?.length
|
||||
? (value ?? '')
|
||||
: ''
|
||||
|
||||
return (
|
||||
<div className='w-full relative'>
|
||||
{
|
||||
props.label &&
|
||||
label &&
|
||||
<div className='flex items-center gap-1'>
|
||||
<label className='text-sm'>
|
||||
{props.label}
|
||||
{label}
|
||||
</label>
|
||||
{props.isNotRequired && (
|
||||
{isNotRequired && (
|
||||
<span className='text-xs text-description'>(اختیاری)</span>
|
||||
)}
|
||||
</div>
|
||||
}
|
||||
<select {...props} className={clx(
|
||||
<select
|
||||
{...selectProps}
|
||||
value={selectValue}
|
||||
className={clx(
|
||||
'w-full text-black block border appearance-none border-border !bg-white px-2.5 h-10 text-sm rounded-[10px] bg-gray',
|
||||
props.className,
|
||||
props.label && 'mt-1'
|
||||
className,
|
||||
label && 'mt-1'
|
||||
)}>
|
||||
{
|
||||
props.placeholder &&
|
||||
<option value="" disabled selected>{props.placeholder}</option>
|
||||
placeholder &&
|
||||
<option value="" disabled>{placeholder}</option>
|
||||
}
|
||||
{
|
||||
props.items?.map((item) => {
|
||||
items?.map((item) => {
|
||||
return (
|
||||
<option key={item.value} value={item.value}>
|
||||
{item.label}
|
||||
@@ -51,12 +75,12 @@ const Select: FC<Props> = (props: Props) => {
|
||||
</select>
|
||||
<ArrowDown2 size={16} color='black' className={clx(
|
||||
'absolute z-0 top-3 left-2',
|
||||
props.label && 'top-10'
|
||||
label && 'top-10'
|
||||
)} />
|
||||
{
|
||||
props.error_text && props.error_text !== '' ?
|
||||
error_text && error_text !== '' ?
|
||||
<Error
|
||||
errorText={props.error_text}
|
||||
errorText={error_text}
|
||||
/>
|
||||
: null
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user