Skip to content

Commit

Permalink
fix: fall back to first option for secondary projection fields
Browse files Browse the repository at this point in the history
  • Loading branch information
friedjoff committed Mar 10, 2020
1 parent 491da16 commit 06ec27e
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/components/ProjectionForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Dropdown from './Dropdown';
import { setFormLocation } from '../store/actions';
import styles from './ProjectionForm.module.css';

const capitalize = text => text[0].toUpperCase() + text.slice(1);
const getButtonOptions = (type, lng) => key => ({
key,
label: info(type, key)[lng],
Expand Down Expand Up @@ -40,11 +41,15 @@ function ProjectionForm() {
const activateField = field => setFieldActive(field);
const deactivateField = () => setFieldActive('');

const getValue = (field, transition) => {
const name = transition
? `transition${field[0].toUpperCase() + field.slice(1)}`
: field;
return options[field].includes(location[name]) ? location[name] : '';
const getValue = (field, { first, transition } = {}) => {
const name = transition ? `transition${capitalize(field)}` : field;
let value = '';
if (first && options[field] && !location[name]) {
[value] = options[field];
} else if (options[field].includes(location[name])) {
value = location[name];
}
return value;
};

const isDifferent = field => mapLocation[field] !== formLocation[field];
Expand Down Expand Up @@ -149,7 +154,7 @@ function ProjectionForm() {
onFocus={() => activateField('transitionForestType')}
placeholder={t('dropdown.placeholder')}
search
value={getValue('forestType', true)}
value={getValue('forestType', { transition: true })}
/>
<Dropdown
clearable
Expand All @@ -163,7 +168,7 @@ function ProjectionForm() {
}}
onBlur={deactivateField}
onFocus={() => activateField('transitionAltitudinalZone')}
value={getValue('altitudinalZone', true)}
value={getValue('altitudinalZone', { transition: true })}
/>
</Segment>
)}
Expand All @@ -172,7 +177,7 @@ function ProjectionForm() {
label={t('slope.label')}
options={options.slope.map(getButtonOptions('slope', i18n.language))}
onChange={(e, { value }) => setLocation('slope', value)}
value={getValue('slope')}
value={getValue('slope', { first: true })}
/>
)}
{options.additional && options.additional.length > 1 && (
Expand Down

0 comments on commit 06ec27e

Please sign in to comment.