Commit 66092b9e authored by Oleksii Savran's avatar Oleksii Savran

SmartCollapse for BooleanArrFilter

parent 3a66ab39
......@@ -48,11 +48,12 @@ const AccessionFilters = ({handleSubmit, initialValues, initialize, filterCode,
}
/>
</div>
<CollapsibleComponentSearch sectionIndex={ sectionIndex++ } title={ t('accessions.public.f.historic') } collapsed>
<CollapsibleComponentSearch sectionIndex={ sectionIndex++ } title={ t('accessions.public.f.historic') } smartCollapse>
<BooleanArrFilter
name="historic"
terms={ terms && terms.get('historic') }
notNull
defaultValue={ false }
/>
</CollapsibleComponentSearch>
<CollapsibleComponentSearch sectionIndex={ sectionIndex++ } title={ t('accessions.public.f.holdingInstitute') } smartCollapse>
......
......@@ -6,12 +6,17 @@ import Number from 'ui/common/Number';
import BooleanArrCheckbox from './BooleanCheckbox';
import TextField from '@material-ui/core/TextField';
import { cleanFilters } from 'utilities';
import { fieldWithSmartCollapse as withSmartCollapse } from 'ui/common/filter/withSmartCollapse';
const InternalWithCollapse = withSmartCollapse(BooleanArrCheckbox);
interface IBooleanArrFilter extends React.ClassAttributes<any>, WithTranslation {
name: string;
label?: string;
terms?: Map<string, any>;
notNull?: boolean;
defaultValue?: boolean;
expandWrapper?: () => void;
isNull?: boolean;
isNotNull?: boolean;
......@@ -50,13 +55,20 @@ class BooleanArrFilter extends React.Component<IBooleanArrFilter, any> {
public componentDidUpdate(prevProps: IBooleanArrFilter, prevState) {
const { isNull: prevIsNull, isNotNull: prevIsNotNull } = prevProps;
const { isNull, isNotNull } = this.props;
const { isNull, isNotNull, expandWrapper } = this.props;
if (!isNull && prevIsNull && this.state.nullChecked && prevState.nullChecked) {
this.setState({nullChecked: false});
}
if (!isNotNull && prevIsNotNull && this.state.notNullChecked && prevState.notNullChecked) {
this.setState({notNullChecked: false});
}
if (expandWrapper) {
// for smart collapse
if ((isNull && isNull !== prevIsNull) || (isNotNull && isNotNull !== prevIsNotNull)) {
expandWrapper();
}
}
}
private getLabel = (val) => val === 'true' ? this.props.t(`common:label.yes`) : val === 'false' ? this.props.t(`common:label.no`) : this.props.t(`common:f.NULL`);
......@@ -135,16 +147,17 @@ class BooleanArrFilter extends React.Component<IBooleanArrFilter, any> {
}
public render() {
const { t, terms, name, label, isNull, isNotNull, notNull = false } = this.props;
const { t, terms, name, label, isNull, isNotNull, notNull = false, expandWrapper, defaultValue } = this.props;
const { nullChecked, notNullChecked } = this.state;
const options = notNull ? [true, false] : [true, false, 'NULL'];
const defaultValueSmartCollapse = defaultValue !== undefined ? defaultValue : notNull ? cleanFilters.DEFINED_NULL : null;
return (
<div>
<Field
name={ name }
singleColumn
component={ BooleanArrCheckbox }
component={ InternalWithCollapse }
formLabel={ t(label) }
options={ options }
classes={ {label: 'full-width'} }
......@@ -152,6 +165,8 @@ class BooleanArrFilter extends React.Component<IBooleanArrFilter, any> {
notNull={ notNull }
isNull={ isNull }
isNotNull={ isNotNull }
expandWrapper={ expandWrapper }
defaultValue={ defaultValueSmartCollapse }
renderOptionLabel={ (stat) =>
<span
className="full-width"
......
......@@ -43,13 +43,19 @@ const fieldsWithSmartCollapse = (Filter) => (props) => {
}
function FieldHOC({Filter, ...props}): React.ReactElement<any> {
const { expandWrapper, input } = props;
const prevInputValue = usePrevious(input && input.value);
const { expandWrapper, input, defaultValue } = props;
const prevInputValue = usePrevious(input && input.value !== undefined ? input.value : null);
React.useEffect(() => {
if (expandWrapper && input && input.value) {
if (!_.isEqual(prevInputValue, input.value)) {
// console.log('expand wrapper', prevInputValue, input.value);
if (expandWrapper) {
if (defaultValue !== undefined) {
if (input && input.value !== defaultValue && input.value !== prevInputValue && input.value !== '') {
expandWrapper();
}
return;
}
if (input && input.value && !_.isEqual(prevInputValue, input.value)) {
expandWrapper();
}
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment