-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Adding legend controls * Adding url support and finishing feature * Moving config to right side; setting min/max to data boundries when deactivating auto * Removing legend from max and min labels * removing uneseccary unshift(0) * Change autobounds behavior
- Loading branch information
1 parent
96197f7
commit db5e282
Showing
11 changed files
with
298 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
170 changes: 170 additions & 0 deletions
170
x-pack/plugins/infra/public/components/waffle/legend_controls.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { | ||
EuiButton, | ||
EuiButtonIcon, | ||
EuiFieldNumber, | ||
EuiFlexGroup, | ||
EuiFlexItem, | ||
EuiForm, | ||
EuiFormRow, | ||
EuiPopover, | ||
EuiPopoverTitle, | ||
EuiSwitch, | ||
EuiText, | ||
} from '@elastic/eui'; | ||
import { FormattedMessage, InjectedIntl, injectI18n } from '@kbn/i18n/react'; | ||
import React, { SyntheticEvent, useState } from 'react'; | ||
import styled from 'styled-components'; | ||
import { InfraWaffleMapBounds } from '../../lib/lib'; | ||
|
||
interface Props { | ||
onChange: (options: { auto: boolean; bounds: InfraWaffleMapBounds }) => void; | ||
bounds: InfraWaffleMapBounds; | ||
dataBounds: InfraWaffleMapBounds; | ||
autoBounds: boolean; | ||
boundsOverride: InfraWaffleMapBounds; | ||
intl: InjectedIntl; | ||
} | ||
|
||
export const LegendControls = injectI18n( | ||
({ intl, autoBounds, boundsOverride, onChange, dataBounds }: Props) => { | ||
const [isPopoverOpen, setPopoverState] = useState(false); | ||
const [draftAuto, setDraftAuto] = useState(autoBounds); | ||
const [draftBounds, setDraftBounds] = useState(autoBounds ? dataBounds : boundsOverride); // should come from bounds prop | ||
const buttonComponent = ( | ||
<EuiButtonIcon | ||
iconType="gear" | ||
color="text" | ||
aria-label={intl.formatMessage({ | ||
id: 'xpack.infra.legendControls.buttonLabel', | ||
defaultMessage: 'configure legend', | ||
})} | ||
onClick={() => setPopoverState(true)} | ||
/> | ||
); | ||
|
||
const handleAutoChange = (e: SyntheticEvent<HTMLInputElement>) => { | ||
setDraftAuto(e.currentTarget.checked); | ||
}; | ||
|
||
const createBoundsHandler = (name: string) => (e: SyntheticEvent<HTMLInputElement>) => { | ||
const value = parseFloat(e.currentTarget.value); | ||
setDraftBounds({ ...draftBounds, [name]: value }); | ||
}; | ||
|
||
const handlePopoverClose = () => { | ||
setPopoverState(false); | ||
}; | ||
|
||
const handleApplyClick = () => { | ||
onChange({ auto: draftAuto, bounds: draftBounds }); | ||
}; | ||
|
||
const commited = | ||
draftAuto === autoBounds && | ||
boundsOverride.min === draftBounds.min && | ||
boundsOverride.max === draftBounds.max; | ||
|
||
const boundsValidRange = draftBounds.min < draftBounds.max; | ||
|
||
return ( | ||
<ControlContainer> | ||
<EuiPopover | ||
isOpen={isPopoverOpen} | ||
closePopover={handlePopoverClose} | ||
id="legendControls" | ||
button={buttonComponent} | ||
withTitle | ||
> | ||
<EuiPopoverTitle>Legend Options</EuiPopoverTitle> | ||
<EuiForm> | ||
<EuiFormRow> | ||
<EuiSwitch | ||
name="bounds" | ||
label={intl.formatMessage({ | ||
id: 'xpack.infra.legendControls.switchLabel', | ||
defaultMessage: 'Auto calculate range', | ||
})} | ||
checked={draftAuto} | ||
onChange={handleAutoChange} | ||
/> | ||
</EuiFormRow> | ||
{(!boundsValidRange && ( | ||
<EuiText color="danger" grow={false} size="s"> | ||
<p> | ||
<FormattedMessage | ||
id="xpack.infra.legendControls.errorMessage" | ||
defaultMessage="Min should be less than max" | ||
/> | ||
</p> | ||
</EuiText> | ||
)) || | ||
null} | ||
<EuiFlexGroup style={{ marginTop: 0 }}> | ||
<EuiFlexItem> | ||
<EuiFormRow | ||
label={intl.formatMessage({ | ||
id: 'xpack.infra.legendControls.minLabel', | ||
defaultMessage: 'Min', | ||
})} | ||
isInvalid={!boundsValidRange} | ||
> | ||
<EuiFieldNumber | ||
disabled={draftAuto} | ||
step={0.1} | ||
value={isNaN(draftBounds.min) ? '' : draftBounds.min} | ||
isInvalid={!boundsValidRange} | ||
name="legendMin" | ||
onChange={createBoundsHandler('min')} | ||
/> | ||
</EuiFormRow> | ||
</EuiFlexItem> | ||
<EuiFlexItem> | ||
<EuiFormRow | ||
label={intl.formatMessage({ | ||
id: 'xpack.infra.legendControls.maxLabel', | ||
defaultMessage: 'Max', | ||
})} | ||
isInvalid={!boundsValidRange} | ||
> | ||
<EuiFieldNumber | ||
disabled={draftAuto} | ||
step={0.1} | ||
isInvalid={!boundsValidRange} | ||
value={isNaN(draftBounds.max) ? '' : draftBounds.max} | ||
name="legendMax" | ||
onChange={createBoundsHandler('max')} | ||
/> | ||
</EuiFormRow> | ||
</EuiFlexItem> | ||
</EuiFlexGroup> | ||
<EuiButton | ||
type="submit" | ||
size="s" | ||
fill | ||
disabled={commited || !boundsValidRange} | ||
onClick={handleApplyClick} | ||
> | ||
<FormattedMessage | ||
id="xpack.infra.legendControls.applyButton" | ||
defaultMessage="Apply" | ||
/> | ||
</EuiButton> | ||
</EuiForm> | ||
</EuiPopover> | ||
</ControlContainer> | ||
); | ||
} | ||
); | ||
|
||
const ControlContainer = styled.div` | ||
position: absolute; | ||
top: -20px; | ||
right: 6px; | ||
bottom: 0; | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.