-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
145 additions
and
66 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
69 changes: 25 additions & 44 deletions
69
packages/react-jsx-highstock/src/components/Navigator/NavigatorAxis.js
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 |
---|---|---|
@@ -1,57 +1,38 @@ | ||
import React, { | ||
useRef, | ||
useEffect, | ||
Children, | ||
cloneElement, | ||
isValidElement | ||
} from 'react'; | ||
import React, { useRef } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { | ||
useAxis, | ||
useModifiedProps, | ||
getNonEventHandlerProps | ||
} from 'react-jsx-highcharts'; | ||
|
||
const NavigatorAxis = ({ children, axisId, ...restProps }) => { | ||
const axis = useAxis(axisId); | ||
const renderedRef = useRef(false); | ||
|
||
useEffect(() => { | ||
if (!axis) return; | ||
|
||
updateNavigatorAxis(getNonEventHandlerProps(restProps), axis); | ||
}, [axis]); | ||
import { useChart, useModifiedProps } from 'react-jsx-highcharts'; | ||
import AxisContext from 'react-jsx-highcharts/dist/es/components/AxisContext'; | ||
import createProvidedAxis from 'react-jsx-highcharts/dist/es/components/Axis/createProvidedAxis'; | ||
|
||
const NavigatorAxis = ({ children, axisType, axisId, ...restProps }) => { | ||
const chart = useChart(); | ||
const modifiedProps = useModifiedProps(restProps); | ||
const providedAxisRef = useRef(null); | ||
|
||
useEffect(() => { | ||
if (!renderedRef.current) { | ||
// don't update on first render | ||
renderedRef.current = true; | ||
return; | ||
} | ||
|
||
if (!axis) return; | ||
|
||
if (modifiedProps !== false) { | ||
updateNavigatorAxis(modifiedProps, axis); | ||
} | ||
}); | ||
if (modifiedProps !== false) { | ||
const config = { | ||
navigator: {} | ||
}; | ||
config.navigator[axisType] = modifiedProps; | ||
chart.update(config); | ||
} | ||
|
||
if (!children) return null; | ||
|
||
const axisChildren = Children.map(children, child => { | ||
if (isValidElement(child) === false) return child; | ||
return cloneElement(child, { axisId }); | ||
}); | ||
const axis = chart.get(axisId); | ||
if (!providedAxisRef.current || axis !== providedAxisRef.current.object) { | ||
providedAxisRef.current = createProvidedAxis(axis); | ||
} | ||
|
||
return <>{axisChildren}</>; | ||
}; | ||
const updateNavigatorAxis = (config, axis) => { | ||
axis.update(config); | ||
return ( | ||
<AxisContext.Provider value={providedAxisRef.current}> | ||
{children} | ||
</AxisContext.Provider> | ||
); | ||
}; | ||
|
||
NavigatorAxis.propTypes = { | ||
axisId: PropTypes.string.isRequired | ||
axisId: PropTypes.string.isRequired, | ||
axisType: PropTypes.string.isRequired | ||
}; | ||
export default NavigatorAxis; |
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
101 changes: 101 additions & 0 deletions
101
packages/react-jsx-highstock/test/components/Navigator/NavigatorXAxis.integration.spec.js
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,101 @@ | ||
import React from 'react'; | ||
import Highstock from 'highcharts/highstock'; | ||
import { | ||
Chart, | ||
Debug, | ||
XAxis, | ||
YAxis, | ||
LineSeries, | ||
withHighcharts | ||
} from 'react-jsx-highcharts'; | ||
import { HighchartsStockChart, Navigator } from '../../../src'; | ||
|
||
describe('<NavigatorXAxis /> integration', () => { | ||
describe('when mounted', () => { | ||
it('passes additional props to navigators xAxis', () => { | ||
const data = [1, 2, 3, 4, 5]; | ||
const labels = { x: 0, y: 12 }; | ||
const Component = props => { | ||
return ( | ||
<HighchartsStockChart> | ||
<Debug /> | ||
<Chart /> | ||
<XAxis /> | ||
<YAxis> | ||
<LineSeries data={data} /> | ||
</YAxis> | ||
<Navigator> | ||
<Navigator.XAxis labels={labels} /> | ||
</Navigator> | ||
</HighchartsStockChart> | ||
); | ||
}; | ||
const WithComponent = withHighcharts(Component, Highstock); | ||
mount(<WithComponent />); | ||
const chart = window.chart; | ||
expect(chart.options.navigator.xAxis.labels).toEqual( | ||
expect.objectContaining(labels) | ||
); | ||
}); | ||
}); | ||
|
||
describe('when updated', () => { | ||
it('changes the navigator labels', () => { | ||
const data = [1, 2, 3, 4, 5]; | ||
const labels = { x: 0, y: 12 }; | ||
const Component = props => { | ||
return ( | ||
<HighchartsStockChart> | ||
<Debug /> | ||
<Chart /> | ||
<XAxis /> | ||
<YAxis> | ||
<LineSeries data={data} /> | ||
</YAxis> | ||
<Navigator> | ||
<Navigator.XAxis {...props} /> | ||
</Navigator> | ||
</HighchartsStockChart> | ||
); | ||
}; | ||
|
||
const WithComponent = withHighcharts(Component, Highstock); | ||
const wrapper = mount(<WithComponent />); | ||
wrapper.setProps({ labels }); | ||
const chart = window.chart; | ||
expect(chart.options.navigator.xAxis.labels).toEqual( | ||
expect.objectContaining(labels) | ||
); | ||
}); | ||
}); | ||
|
||
describe('when parent navigator updated', () => { | ||
it('keeps the axis labels', () => { | ||
const data = [1, 2, 3, 4, 5]; | ||
const labels = { x: 0, y: 12 }; | ||
const Component = props => { | ||
return ( | ||
<HighchartsStockChart> | ||
<Debug /> | ||
<Chart /> | ||
<XAxis /> | ||
<YAxis> | ||
<LineSeries data={data} /> | ||
</YAxis> | ||
<Navigator {...props}> | ||
<Navigator.XAxis labels={labels} /> | ||
</Navigator> | ||
</HighchartsStockChart> | ||
); | ||
}; | ||
|
||
const WithComponent = withHighcharts(Component, Highstock); | ||
const wrapper = mount(<WithComponent />); | ||
wrapper.setProps({ height: 100 }); | ||
const chart = window.chart; | ||
expect(chart.options.navigator.xAxis.labels).toEqual( | ||
expect.objectContaining(labels) | ||
); | ||
}); | ||
}); | ||
}); |