import { TextInput, InputLabel, SelectInput, InputGroup } from '../../../styles/Components';
import { SearchBarLayout, SearchPeriod } from '../../common/SearchBar';
import { Fragment } from 'react';
import { getOptionsArray } from '../../../utils';
import { PageSkeleton } from '../../Skeleton/SearchSkeleton';
const renderSearchField = (field, searchParams, onSearch) => {
const { type, id, label, placeholder, width, optionsRef } = field;
switch (type) {
case 'text':
return (
<>
{label && label !== 'undefined' && {label}}
onSearch({ [id]: e.target.value }, false)}
required={field.required}
onKeyDown={e => {
if (e.key === 'Enter') {
e.preventDefault();
onSearch(searchParams, true);
}
}}
/>
>
);
case 'select':
return (
<>
{label && label !== 'undefined' && {label}}
onSearch({ [id]: e.target.value }, false)}
>
{getOptionsArray(optionsRef).map((data, index) => (
))}
>
);
case 'period':
return (
<>
{label && label !== 'undefined' && {label}}
onSearch({ [field.startDateId]: date }, false)}
endDate={searchParams[field.endDateId]}
handleEndDate={date => onSearch({ [field.endDateId]: date }, false)}
/>
>
);
case 'searchGroup':
// 검색 타입과 입력이 결합된 컴포넌트
return (
onSearch({ [field.selectId]: e.target.value }, false)}
>
{getOptionsArray(field.optionsRef).map((data, index) => (
))}
onSearch({ [field.inputId]: e.target.value }, false)}
onKeyDown={e => {
if (e.key === 'Enter') {
e.preventDefault();
onSearch(searchParams, true);
}
}}
/>
);
default:
return null;
}
};
const CommonSearchBar = ({ config, searchParams, onSearch, onReset, customProps }) => {
if (!config || !config.searchFields) {
return ;
}
const handleSubmit = event => {
event.preventDefault();
onSearch(searchParams, true);
};
// 검색 필드를 컬럼별로 분류
const firstColumnFields = config.searchFields.filter(field => field.col === 1);
const secondColumnFields = config.searchFields.filter(field => field.col === 2);
const filterField = config.searchFields.find(field => field.col === 'filter');
const firstColumnData = firstColumnFields.map((field, index) => (
{renderSearchField(field, searchParams, onSearch)}
));
const secondColumnData = secondColumnFields.length > 0 ?
secondColumnFields.map((field, index) => (
{renderSearchField(field, searchParams, onSearch)}
)) :
undefined;
const filter = filterField ?
renderSearchField(filterField, searchParams, onSearch) :
undefined;
return (
);
};
export default CommonSearchBar;