Skip to content

Commit

Permalink
fix: close ecogram popup on outside click and improve text rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
friedjoff committed May 1, 2020
1 parent 6c2571a commit 70875a6
Showing 1 changed file with 33 additions and 14 deletions.
47 changes: 33 additions & 14 deletions src/components/EcogramPopup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import PropTypes from 'prop-types';
import React from 'react';
import React, { useEffect, useRef } from 'react';
import { useTranslation } from 'react-i18next';
import { List, Popup } from 'semantic-ui-react';
// eslint-disable-next-line import/no-unresolved
Expand All @@ -9,23 +9,42 @@ import Button from './Button';
import ForestTypeButton from './ForestTypeButton';

function EcogramPopup({ target, forestTypes, onClose, selectForestType }) {
const container = useRef();
const { t, i18n } = useTranslation();

const handleClickOutside = (e) => {
if (container.current && container.current.contains(e.target) === false) {
onClose();
}
};

useEffect(() => {
document.addEventListener('mousedown', handleClickOutside);
return () => document.removeEventListener('mousedown', handleClickOutside);
});

return (
<Popup context={target} position="top center" open={forestTypes.length > 0}>
<List>
{forestTypes.map((f) => (
<List.Item style={{ whiteSpace: 'nowrap' }}>
<ForestTypeButton code={f} />
<Button active onClick={() => selectForestType(f)}>
{f} - {info('forestType', f)[i18n.language]}
</Button>
<Popup
context={target}
flowing
position="top center"
open={forestTypes.length > 0}
>
<div ref={container}>
<List>
{forestTypes.map((f) => (
<List.Item style={{ display: 'flex' }}>
<ForestTypeButton code={f} />
<Button active onClick={() => selectForestType(f)}>
{f} - {info('forestType', f)[i18n.language]}
</Button>
</List.Item>
))}
<List.Item>
<Button onClick={() => onClose()}>{t('forestType.cancel')}</Button>
</List.Item>
))}
<List.Item>
<Button onClick={() => onClose()}>{t('forestType.cancel')}</Button>
</List.Item>
</List>
</List>
</div>
</Popup>
);
}
Expand Down

0 comments on commit 70875a6

Please sign in to comment.