import React, { useMemo } from 'react'; import { useDispatch, useSelector } from 'react-redux'; import { QSMRootState } from '../../store/qsm-store'; import { setFieldValue } from '../../store/qsm-slice'; import SearchInputGroup from '../search-input-group'; import { FieldConfig, DoubleFieldConfig } from '../../types'; const isDouble = (f: FieldConfig): f is DoubleFieldConfig => f.type === 'double'; interface Props { fieldConfig: DoubleFieldConfig; showReverse?: boolean; } const DoubleSearchInputGroup: React.FC = ({ fieldConfig, showReverse = false }) => { const dispatch = useDispatch(); if (!fieldConfig || !isDouble(fieldConfig)) { return null; } const [firstCfg, secondCfg] = fieldConfig.fields; const firstSelector = useMemo(() => (s: QSMRootState) => (s.qsm as any)[firstCfg.fieldKey] ?? '', [firstCfg.fieldKey]); const secondSelector = useMemo(() => (s: QSMRootState) => (s.qsm as any)[secondCfg.fieldKey] ?? '', [secondCfg.fieldKey]); const firstValue = useSelector(firstSelector); const secondValue = useSelector(secondSelector); const reverse = () => { dispatch( setFieldValue({ fieldKey: firstCfg.fieldKey, value: secondValue }) ); dispatch( setFieldValue({ fieldKey: secondCfg.fieldKey, value: firstValue }) ); }; return (
{showReverse ? (
) : (
)}
); }; export default DoubleSearchInputGroup;