From 0e9b9888de225029957979456bbc15f2ce3a6643 Mon Sep 17 00:00:00 2001 From: zlatan Date: Tue, 29 Jan 2019 13:56:52 +0700 Subject: [PATCH] histogram: allow passing custom drag button via prop --- .../HistogramSlider/HistogramSlider.tsx | 6 ++ .../RangeSlider/RangeSlider.tsx | 97 ++++++++++++++----- 2 files changed, 78 insertions(+), 25 deletions(-) diff --git a/src/components/HistogramSlider/HistogramSlider.tsx b/src/components/HistogramSlider/HistogramSlider.tsx index f16c68683..f95b01847 100644 --- a/src/components/HistogramSlider/HistogramSlider.tsx +++ b/src/components/HistogramSlider/HistogramSlider.tsx @@ -26,6 +26,10 @@ interface HistogramSliderProps { | (( props: { value: [number, number]; min: number; max: number } ) => JSX.Element); + /** Custom component for render drag button UI, pass `null` if you don't want to show the part */ + ButtonRenderComponent?: + | null + | ((props: { focused?: boolean }) => JSX.Element); /** Showing an `apply` & `reset` button if a function was passed to the prop*/ onApply?: (value: [number, number]) => void; /** Callback function while the range changed */ @@ -150,6 +154,7 @@ export class HistogramSlider extends Component< data, widthPx, InfoRenderComponent, + ButtonRenderComponent, ...rangeSliderProps } = this.props; @@ -176,6 +181,7 @@ export class HistogramSlider extends Component< colors={this.props.colors!} value={this.state.value} onChange={this.handleSliderChange} + ButtonRenderComponent={ButtonRenderComponent} />
diff --git a/src/components/HistogramSlider/RangeSlider/RangeSlider.tsx b/src/components/HistogramSlider/RangeSlider/RangeSlider.tsx index b39cd2680..7661128fe 100644 --- a/src/components/HistogramSlider/RangeSlider/RangeSlider.tsx +++ b/src/components/HistogramSlider/RangeSlider/RangeSlider.tsx @@ -1,4 +1,4 @@ -import React, { Component } from "react"; +import React, { Component, PureComponent } from "react"; import { css } from "emotion"; interface RangeSliderProps { @@ -8,6 +8,9 @@ interface RangeSliderProps { value: [number, number]; distance: number; onChange: (value: [number, number]) => void; + ButtonRenderComponent?: + | null + | ((props: { focused?: boolean }) => JSX.Element); colors: { in: string; out: string; @@ -264,16 +267,23 @@ export class RangeSlider extends Component { this.clearDocumentEvents(); } + preventDefaultClick = (e: React.MouseEvent) => { + e.preventDefault(); + }; + render() { const range = this.getRange(); const { min, max, colors, - value: [minState, maxState] + value: [minState, maxState], + ButtonRenderComponent } = this.props; const right = 100 - ((maxState - min) * 100) / range; const left = ((minState - min) * 100) / range; + const ButtonInnerComponent = + ButtonRenderComponent || DefaultRenderButtonComponent; return (
{
@@ -359,8 +367,20 @@ export class RangeSlider extends Component { } } -const Button = (props: React.HTMLAttributes) => ( - + ); + +class Button extends React.PureComponent< + { + ButtonRenderComponent: ((props: { focused?: boolean }) => JSX.Element); + } & React.HTMLAttributes +> { + state = { + focused: false + }; + + handleFocus = () => { + this.setState({ focused: true }); + }; + + handleBlur = () => { + this.setState({ focused: false }); + }; + + render() { + const { ButtonRenderComponent, ...rest } = this.props; + return ( + + ); + } +}