Prevent tab-event closing the chart range settings
This commit is contained in:
@@ -1,43 +1,38 @@
|
|||||||
import * as React from 'react'
|
import React, { useCallback, useState, ChangeEvent, MouseEvent } from 'react'
|
||||||
import { ChartParameters } from '../../../reducers/Charts'
|
import { ChartParameters } from '../../../reducers/Charts'
|
||||||
import { Menu, MenuItem, TextField, Typography } from '@material-ui/core'
|
import { Menu, TextField, Typography } from '@material-ui/core'
|
||||||
import { connect } from 'react-redux'
|
import { connect } from 'react-redux'
|
||||||
import { bindActionCreators } from 'redux'
|
import { bindActionCreators } from 'redux'
|
||||||
import { chartActions } from '../../../actions'
|
import { chartActions } from '../../../actions'
|
||||||
|
import { KeyCodes } from '../../../utils/KeyCodes'
|
||||||
|
|
||||||
function RangeSettings(props: {
|
interface Props {
|
||||||
actions: { chart: typeof chartActions }
|
actions: { chart: typeof chartActions }
|
||||||
chart: ChartParameters
|
chart: ChartParameters
|
||||||
anchorEl?: Element
|
anchorEl?: Element
|
||||||
open: boolean
|
open: boolean
|
||||||
onClose: () => void
|
onClose: () => void
|
||||||
}) {
|
}
|
||||||
const dismissClick = React.useCallback((e: React.MouseEvent) => e.stopPropagation(), [])
|
|
||||||
const [rangeFrom, setRangeFrom] = React.useState<string | number | undefined>(
|
|
||||||
props.chart.range && props.chart.range.from
|
|
||||||
)
|
|
||||||
const [rangeTo, setRangeTo] = React.useState<string | number | undefined>(props.chart.range && props.chart.range.to)
|
|
||||||
|
|
||||||
React.useEffect(() => {
|
function RangeSettings(props: Props) {
|
||||||
const from = parseFloat(rangeFrom as any)
|
const dismissClick = useCallback((e: MouseEvent) => e.stopPropagation(), [])
|
||||||
const to = parseFloat(rangeTo as any)
|
const [rangeFrom, setRangeFrom] = useState<string | number | undefined>(props.chart.range && props.chart.range.from)
|
||||||
props.actions.chart.updateChart({
|
const [rangeTo, setRangeTo] = useState<string | number | undefined>(props.chart.range && props.chart.range.to)
|
||||||
topic: props.chart.topic,
|
useRangeStateToFireUpdateAction(rangeFrom, rangeTo, props)
|
||||||
dotPath: props.chart.dotPath,
|
const dismissTabKey = (e: React.KeyboardEvent<any>) => {
|
||||||
range: {
|
if (e.keyCode === KeyCodes.tab) {
|
||||||
from: isNaN(from) ? undefined : from,
|
e.stopPropagation()
|
||||||
to: isNaN(to) ? undefined : to,
|
}
|
||||||
},
|
}
|
||||||
})
|
|
||||||
}, [rangeFrom, rangeTo])
|
|
||||||
|
|
||||||
const setFromHandler = React.useCallback((e: React.ChangeEvent<HTMLInputElement>) => setRangeFrom(e.target.value), [])
|
const setFromHandler = useCallback((e: ChangeEvent<HTMLInputElement>) => setRangeFrom(e.target.value), [])
|
||||||
const setToHandler = React.useCallback((e: React.ChangeEvent<HTMLInputElement>) => setRangeTo(e.target.value), [])
|
const setToHandler = useCallback((e: ChangeEvent<HTMLInputElement>) => setRangeTo(e.target.value), [])
|
||||||
return (
|
return (
|
||||||
<Menu style={{ textAlign: 'center' }} anchorEl={props.anchorEl} open={props.open} onClose={props.onClose}>
|
<Menu style={{ textAlign: 'center' }} anchorEl={props.anchorEl} open={props.open} onClose={props.onClose}>
|
||||||
<Typography>Define custom ranges for the Y-Axis</Typography>
|
<Typography>Define custom ranges for the Y-Axis</Typography>
|
||||||
<div style={{ padding: '0 16px' }}>
|
<div style={{ padding: '0 16px' }}>
|
||||||
<TextField
|
<TextField
|
||||||
|
onKeyDownCapture={dismissTabKey}
|
||||||
style={{ marginTop: '0' }}
|
style={{ marginTop: '0' }}
|
||||||
onClick={dismissClick}
|
onClick={dismissClick}
|
||||||
label="from"
|
label="from"
|
||||||
@@ -70,3 +65,22 @@ export default connect(
|
|||||||
undefined,
|
undefined,
|
||||||
mapDispatchToProps
|
mapDispatchToProps
|
||||||
)(RangeSettings)
|
)(RangeSettings)
|
||||||
|
|
||||||
|
function useRangeStateToFireUpdateAction(
|
||||||
|
rangeFrom: string | number | undefined,
|
||||||
|
rangeTo: string | number | undefined,
|
||||||
|
props: Props
|
||||||
|
) {
|
||||||
|
React.useEffect(() => {
|
||||||
|
const from = parseFloat(rangeFrom as any)
|
||||||
|
const to = parseFloat(rangeTo as any)
|
||||||
|
props.actions.chart.updateChart({
|
||||||
|
topic: props.chart.topic,
|
||||||
|
dotPath: props.chart.dotPath,
|
||||||
|
range: {
|
||||||
|
from: isNaN(from) ? undefined : from,
|
||||||
|
to: isNaN(to) ? undefined : to,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}, [rangeFrom, rangeTo])
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,31 +0,0 @@
|
|||||||
import { KeyCodes } from '../utils/KeyCodes'
|
|
||||||
import { useCallback } from 'react'
|
|
||||||
|
|
||||||
export function useKeyEventHandler(key: KeyCodes, callback: () => void, dependencies: Array<any> = []) {
|
|
||||||
return useKeyEventHandlers([{ key, callback }], dependencies)
|
|
||||||
}
|
|
||||||
|
|
||||||
export function useKeyEventHandlers(
|
|
||||||
actions: Array<{
|
|
||||||
key: KeyCodes
|
|
||||||
callback: (event: KeyboardEvent) => void
|
|
||||||
preventDefault?: boolean
|
|
||||||
stopPropagation?: boolean
|
|
||||||
}>,
|
|
||||||
dependencies: Array<any> = []
|
|
||||||
) {
|
|
||||||
return useCallback(() => {
|
|
||||||
return function handleKeyEvent(event: KeyboardEvent) {
|
|
||||||
const action = actions.find(a => a.key === event.keyCode)
|
|
||||||
if (action) {
|
|
||||||
action.callback(event)
|
|
||||||
if (action.preventDefault !== false) {
|
|
||||||
event.preventDefault()
|
|
||||||
}
|
|
||||||
if (action.stopPropagation !== false) {
|
|
||||||
event.stopPropagation()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}, dependencies)
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user