Skip to content

Commit

Permalink
Add "onControlFinished" prop (resolves #2)
Browse files Browse the repository at this point in the history
  • Loading branch information
mnkhouri committed Jan 19, 2020
1 parent ddd944f commit 87e1d7c
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 8 deletions.
8 changes: 8 additions & 0 deletions docs/api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ export const ApiDocs: React.FunctionComponent = () => {
},
description: ""
},
{
name: "onControlFinished",
required: false,
default: "",
type: "( ) => void",
description:
"Called when the user is done controlling (i.e. mouseUp / mouseLeave)"
},
{
name: "handle1",
required: false,
Expand Down
10 changes: 5 additions & 5 deletions docs/build/index.js

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions docs/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { prism as syntaxStyle } from "react-syntax-highlighter/dist/esm/styles/p
import { CircularSlider, CircularSliderWithChildren } from "../src";

import { ApiDocs } from "./api";
import { OnControlFinishedExample } from "./onControlFinished";

class Main extends React.Component {
render() {
Expand All @@ -27,6 +28,12 @@ class Main extends React.Component {
opts={{ startAngle: 120, endAngle: 300 }}
/>
</div>
<h3>Take action when user is done:</h3>
You can use the "onControlFinished" prop to pass a function that will be
called when the user finishes their control (i.e. onMouseUp or
onMouseLeave). You could use this callback, for example, to write the
final value of the control to a database.
<OnControlFinishedExample />
</div>
);
}
Expand Down Expand Up @@ -76,6 +83,8 @@ const MyApp = () => {
onChange: v => setValue2(v)
}}
arcColor="#690"
startAngle={40}
endAngle={320}
/>
);
};
Expand Down
62 changes: 62 additions & 0 deletions docs/onControlFinished.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { default as React, useState } from "react";
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
import { prism as syntaxStyle } from "react-syntax-highlighter/dist/esm/styles/prism";

import { CircularSlider, CircularSliderWithChildren } from "../src";

import { ApiDocs } from "./api";

export const OnControlFinishedExample: React.FunctionComponent = () => {
const MyApp: React.FunctionComponent = () => {
const [value1, setValue1] = useState(20);
const [endingValue, setEndingValue] = useState();

return (
<div>
<CircularSlider
handle1={{
value: value1,
onChange: v => setValue1(v)
}}
onControlFinished={() => setEndingValue(value1)}
arcColor="#690"
/>
Current value: {value1}
<br />
Result of last control: {endingValue}
</div>
);
};
return (
<div style={{ display: "flex", flexWrap: "wrap" }}>
<div style={{ width: 200, flexShrink: 0 }}>
<MyApp />
</div>
<SyntaxHighlighter language="tsx" style={syntaxStyle}>
{` import CircularSlider from 'react-circular-slider-svg';
const MyApp: React.FunctionComponent = () => {
const [value1, setValue1] = useState(20);
const [endingValue, setEndingValue] = useState();
return (
<div>
<CircularSlider
handle1={{
value: value1,
onChange: v => setValue1(v)
}}
onControlFinished={() => setEndingValue(value1)}
arcColor="#690"
/>
Current value: {value1}
<br />
Result of last control: {endingValue}
</div>
);
};
`}
</SyntaxHighlighter>
</div>
);
};
4 changes: 4 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Props = {
value: number;
onChange: (value: number) => void;
};
onControlFinished?: () => void;
disabled?: boolean;
arcColor: string;
arcBackgroundColor: string;
Expand Down Expand Up @@ -81,6 +82,9 @@ export class CircularSlider extends React.Component<Props> {
svgRef.removeEventListener("mouseleave", this.removeMouseListeners);
svgRef.removeEventListener("mouseup", this.removeMouseListeners);
}
if (this.props.onControlFinished) {
this.props.onControlFinished();
}
};

processSelection = (ev: React.MouseEvent<SVGSVGElement> | MouseEvent) => {
Expand Down
10 changes: 7 additions & 3 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@ module.exports = {
rules: [
{
test: /\.tsx?$/,
loader: "ts-loader"
loader: "ts-loader",
options: {
compilerOptions: {
declaration: false
}
}
}
]
},
resolve: {
extensions: [".ts", ".tsx", ".js"]
},
devServer: {
}
devServer: {}
};

0 comments on commit 87e1d7c

Please sign in to comment.