Skip to content

Commit

Permalink
Fix and/or simplify prepend/append usages
Browse files Browse the repository at this point in the history
  • Loading branch information
cchaos committed Sep 19, 2019
1 parent 6c9b3c0 commit d8a5547
Show file tree
Hide file tree
Showing 13 changed files with 216 additions and 404 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ export const SavedQueryManagementComponent: FunctionComponent<Props> = ({

const savedQueryPopoverButton = (
<EuiButtonEmpty
className="euiFormControlLayout__prepend"
iconType="arrowDown"
iconSide="right"
onClick={() => {
Expand Down Expand Up @@ -164,7 +163,6 @@ export const SavedQueryManagementComponent: FunctionComponent<Props> = ({
<Fragment>
<EuiPopover
id="savedQueryPopover"
anchorClassName="euiFormControlLayout__prepend"
button={savedQueryPopoverButton}
isOpen={isOpen}
closePopover={() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import React, { useEffect, ChangeEvent } from 'react';

import { EuiFieldNumber, EuiFlexGroup, EuiFlexItem, EuiFormRow, EuiText } from '@elastic/eui';
import { EuiFieldNumber, EuiFlexGroup, EuiFlexItem, EuiFormRow } from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { isUndefined } from 'lodash';
import { AggParamEditorProps } from '..';
Expand Down Expand Up @@ -95,11 +95,7 @@ function ExtendedBoundsParamEditor({
fullWidth={true}
isInvalid={showValidation ? !isValid : false}
aria-label={minLabel}
prepend={
<EuiText size="xs">
<strong>{minLabel}</strong>
</EuiText>
}
prepend={minLabel}
compressed
/>
</EuiFlexItem>
Expand All @@ -111,11 +107,7 @@ function ExtendedBoundsParamEditor({
fullWidth={true}
isInvalid={showValidation ? !isValid : false}
aria-label={maxLabel}
prepend={
<EuiText size="xs">
<strong>{maxLabel}</strong>
</EuiText>
}
prepend={maxLabel}
compressed
/>
</EuiFlexItem>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,14 @@ import PropTypes from 'prop-types';
import { FormattedMessage } from '@kbn/i18n/react';
import { i18n } from '@kbn/i18n';

import {
EuiFlexGroup,
EuiFlexItem,
EuiFormRow,
EuiSelect,
EuiText,
} from '@elastic/eui';
import { EuiFlexGroup, EuiFlexItem, EuiFormRow, EuiSelect } from '@elastic/eui';

export const CronDaily = ({
minute,
minuteOptions,
hour,
hourOptions,
onChange,
}) => (
export const CronDaily = ({ minute, minuteOptions, hour, hourOptions, onChange }) => (
<Fragment>
<EuiFormRow
label={(
<FormattedMessage
id="esUi.cronEditor.cronDaily.fieldTimeLabel"
defaultMessage="Time"
/>
)}
label={
<FormattedMessage id="esUi.cronEditor.cronDaily.fieldTimeLabel" defaultMessage="Time" />
}
fullWidth
data-test-subj="cronFrequencyConfiguration"
>
Expand All @@ -54,20 +39,13 @@ export const CronDaily = ({
options={hourOptions}
value={hour}
aria-label={i18n.translate('esUi.cronEditor.cronDaily.hourSelectLabel', {
defaultMessage: 'Hour'
defaultMessage: 'Hour',
})}
onChange={e => onChange({ hour: e.target.value })}
fullWidth
prepend={(
<EuiText size="xs">
<strong>
<FormattedMessage
id="esUi.cronEditor.cronDaily.fieldHour.textAtLabel"
defaultMessage="At"
/>
</strong>
</EuiText>
)}
prepend={i18n.translate('esUi.cronEditor.cronDaily.fieldHour.textAtLabel', {
defaultMessage: 'At',
})}
data-test-subj="cronFrequencyDailyHourSelect"
/>
</EuiFlexItem>
Expand All @@ -77,17 +55,11 @@ export const CronDaily = ({
options={minuteOptions}
value={minute}
aria-label={i18n.translate('esUi.cronEditor.cronDaily.minuteSelectLabel', {
defaultMessage: 'Minute'
defaultMessage: 'Minute',
})}
onChange={e => onChange({ minute: e.target.value })}
fullWidth
prepend={(
<EuiText size="xs">
<strong>
:
</strong>
</EuiText>
)}
prepend=":"
data-test-subj="cronFrequencyDailyMinuteSelect"
/>
</EuiFlexItem>
Expand Down
126 changes: 60 additions & 66 deletions src/plugins/es_ui_shared/public/components/cron_editor/cron_editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,9 @@ import React, { Component, Fragment } from 'react';
import PropTypes from 'prop-types';
import { padLeft } from 'lodash';
import { FormattedMessage } from '@kbn/i18n/react';
import { i18n } from '@kbn/i18n';

import {
EuiSelect,
EuiText,
EuiFormRow,
} from '@elastic/eui';
import { EuiSelect, EuiFormRow } from '@elastic/eui';

import {
getOrdinalValue,
Expand Down Expand Up @@ -81,25 +78,32 @@ const MONTH_OPTIONS = makeSequence(1, 12).map(value => ({
text: getMonthName(value - 1),
}));

const UNITS = [{
value: MINUTE,
text: 'minute',
}, {
value: HOUR,
text: 'hour',
}, {
value: DAY,
text: 'day',
}, {
value: WEEK,
text: 'week',
}, {
value: MONTH,
text: 'month',
}, {
value: YEAR,
text: 'year',
}];
const UNITS = [
{
value: MINUTE,
text: 'minute',
},
{
value: HOUR,
text: 'hour',
},
{
value: DAY,
text: 'day',
},
{
value: WEEK,
text: 'week',
},
{
value: MONTH,
text: 'month',
},
{
value: YEAR,
text: 'year',
},
];

const frequencyToFieldsMap = {
[MINUTE]: {},
Expand Down Expand Up @@ -185,7 +189,7 @@ export class CronEditor extends Component {
frequency: PropTypes.string.isRequired,
cronExpression: PropTypes.string.isRequired,
onChange: PropTypes.func.isRequired,
}
};

static getDerivedStateFromProps(props) {
const { cronExpression } = props;
Expand All @@ -209,12 +213,15 @@ export class CronEditor extends Component {

// Update fields which aren't editable with acceptable baseline values.
const editableFields = Object.keys(frequencyToFieldsMap[frequency]);
const inheritedFields = editableFields.reduce((baselineFields, field) => {
if (fieldToPreferredValueMap[field] != null) {
baselineFields[field] = fieldToPreferredValueMap[field];
}
return baselineFields;
}, { ...frequencyToBaselineFieldsMap[frequency] });
const inheritedFields = editableFields.reduce(
(baselineFields, field) => {
if (fieldToPreferredValueMap[field] != null) {
baselineFields[field] = fieldToPreferredValueMap[field];
}
return baselineFields;
},
{ ...frequencyToBaselineFieldsMap[frequency] }
);

const newCronExpression = cronPartsToExpression(inheritedFields);

Expand All @@ -231,17 +238,20 @@ export class CronEditor extends Component {
const editableFields = Object.keys(frequencyToFieldsMap[frequency]);
const newFieldToPreferredValueMap = {};

const editedFields = editableFields.reduce((accumFields, field) => {
if (fields[field] !== undefined) {
accumFields[field] = fields[field];
// Once the user touches a field, we want to persist its value as the user changes
// the cron frequency.
newFieldToPreferredValueMap[field] = fields[field];
} else {
accumFields[field] = this.state[field];
}
return accumFields;
}, { ...frequencyToBaselineFieldsMap[frequency] });
const editedFields = editableFields.reduce(
(accumFields, field) => {
if (fields[field] !== undefined) {
accumFields[field] = fields[field];
// Once the user touches a field, we want to persist its value as the user changes
// the cron frequency.
newFieldToPreferredValueMap[field] = fields[field];
} else {
accumFields[field] = this.state[field];
}
return accumFields;
},
{ ...frequencyToBaselineFieldsMap[frequency] }
);

const newCronExpression = cronPartsToExpression(editedFields);

Expand All @@ -251,20 +261,14 @@ export class CronEditor extends Component {
fieldToPreferredValueMap: {
...fieldToPreferredValueMap,
...newFieldToPreferredValueMap,
}
},
});
};

renderForm() {
const { frequency } = this.props;

const {
minute,
hour,
day,
date,
month,
} = this.state;
const { minute, hour, day, date, month } = this.state;

switch (frequency) {
case MINUTE:
Expand Down Expand Up @@ -342,29 +346,19 @@ export class CronEditor extends Component {
return (
<Fragment>
<EuiFormRow
label={(
<FormattedMessage
id="esUi.cronEditor.fieldFrequencyLabel"
defaultMessage="Frequency"
/>
)}
label={
<FormattedMessage id="esUi.cronEditor.fieldFrequencyLabel" defaultMessage="Frequency" />
}
fullWidth
>
<EuiSelect
options={UNITS}
value={frequency}
onChange={e => this.onChangeFrequency(e.target.value)}
fullWidth
prepend={(
<EuiText size="xs">
<strong>
<FormattedMessage
id="esUi.cronEditor.textEveryLabel"
defaultMessage="Every"
/>
</strong>
</EuiText>
)}
prepend={i18n.translate('esUi.cronEditor.textEveryLabel', {
defaultMessage: 'Every',
})}
data-test-subj="cronFrequencySelect"
/>
</EuiFormRow>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,16 @@
import React, { Fragment } from 'react';
import PropTypes from 'prop-types';
import { FormattedMessage } from '@kbn/i18n/react';
import { i18n } from '@kbn/i18n';

import {
EuiFormRow,
EuiSelect,
EuiText,
} from '@elastic/eui';
import { EuiFormRow, EuiSelect } from '@elastic/eui';

export const CronHourly = ({
minute,
minuteOptions,
onChange,
}) => (
export const CronHourly = ({ minute, minuteOptions, onChange }) => (
<Fragment>
<EuiFormRow
label={(
<FormattedMessage
id="esUi.cronEditor.cronHourly.fieldTimeLabel"
defaultMessage="Minute"
/>
)}
label={
<FormattedMessage id="esUi.cronEditor.cronHourly.fieldTimeLabel" defaultMessage="Minute" />
}
fullWidth
data-test-subj="cronFrequencyConfiguration"
>
Expand All @@ -48,16 +38,9 @@ export const CronHourly = ({
value={minute}
onChange={e => onChange({ minute: e.target.value })}
fullWidth
prepend={(
<EuiText size="xs">
<strong>
<FormattedMessage
id="esUi.cronEditor.cronHourly.fieldMinute.textAtLabel"
defaultMessage="At"
/>
</strong>
</EuiText>
)}
prepend={i18n.translate('esUi.cronEditor.cronHourly.fieldMinute.textAtLabel', {
defaultMessage: 'At',
})}
data-test-subj="cronFrequencyHourlyMinuteSelect"
/>
</EuiFormRow>
Expand Down
Loading

0 comments on commit d8a5547

Please sign in to comment.