Skip to content

Commit e718994

Browse files
committed
feat: enhance renderPath props.
1 parent 4e278a7 commit e718994

File tree

3 files changed

+62
-53
lines changed

3 files changed

+62
-53
lines changed

core/README.md

Lines changed: 55 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ export default function App() {
103103
## Render Path
104104
105105
```jsx mdx:preview
106-
import React, { useRef, useEffect } from "react";
106+
import React, { useRef } from "react";
107107
import Signature from '@uiw/react-signature';
108108

109109
const points = {
@@ -112,26 +112,32 @@ const points = {
112112
}
113113

114114
export default function App() {
115+
const $svg = useRef(null);
116+
const handle = (evn) => $svg.current?.clear();
115117
return (
116-
<Signature
117-
defaultPoints={points}
118-
renderPath={(path, keyName) => {
119-
if (keyName === 'path-1') {
120-
return <path d={path} fill="red" />
121-
}
122-
if (keyName === 'path-2') {
123-
return <path d={path} fill="blue" />
124-
}
125-
}}
126-
/>
118+
<>
119+
<Signature
120+
ref={$svg}
121+
defaultPoints={points}
122+
renderPath={(path, keyName, data, index) => {
123+
if (keyName === 'path-1' || index === 0) {
124+
return <path d={path} fill="red" />
125+
}
126+
if (keyName === 'path-2' || index === 1) {
127+
return <path d={path} fill="blue" />
128+
}
129+
}}
130+
/>
131+
<button onClick={handle}>Clear</button>
132+
</>
127133
);
128134
}
129135
```
130136
131137
## Stroke
132138
133139
```jsx mdx:preview
134-
import React, { useRef, useEffect } from "react";
140+
import React, { useRef } from "react";
135141
import Signature from '@uiw/react-signature';
136142

137143
const points = {
@@ -140,37 +146,43 @@ const points = {
140146
}
141147

142148
export default function App() {
149+
const $svg = useRef(null);
150+
const handle = (evn) => $svg.current?.clear();
143151
return (
144-
<Signature
145-
defaultPoints={points}
146-
options={{
147-
size: 6,
148-
}}
149-
renderPath={(path, keyName) => {
150-
return (
151-
<>
152-
<path
153-
d={path}
154-
strokeWidth="3"
155-
stroke="blue"
156-
fill="transparent"
157-
strokeLinejoin="round"
158-
strokeLinecap="round"
159-
pointerEvents="all"
160-
/>
161-
<path
162-
d={path}
163-
strokeWidth="0"
164-
stroke="transparent"
165-
fill="#dadada"
166-
strokeLinejoin="round"
167-
strokeLinecap="round"
168-
pointerEvents="all"
169-
/>
170-
</>
171-
)
172-
}}
173-
/>
152+
<>
153+
<Signature
154+
ref={$svg}
155+
defaultPoints={points}
156+
options={{
157+
size: 6,
158+
}}
159+
renderPath={(path, keyName) => {
160+
return (
161+
<>
162+
<path
163+
d={path}
164+
strokeWidth="3"
165+
stroke="blue"
166+
fill="transparent"
167+
strokeLinejoin="round"
168+
strokeLinecap="round"
169+
pointerEvents="all"
170+
/>
171+
<path
172+
d={path}
173+
strokeWidth="0"
174+
stroke="transparent"
175+
fill="#dadada"
176+
strokeLinejoin="round"
177+
strokeLinecap="round"
178+
pointerEvents="all"
179+
/>
180+
</>
181+
)
182+
}}
183+
/>
184+
<button onClick={handle}>Clear</button>
185+
</>
174186
);
175187
}
176188
```

core/src/Paths.tsx

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ export const Paths = () => {
88
const { options, ...data } = useStore();
99
return (
1010
<Fragment>
11-
{Object.keys(data).map((key) => (
12-
<CreatePath key={key} keyName={key} data={data[key]} />
11+
{Object.keys(data).map((key, index) => (
12+
<CreatePath key={key} keyName={key} index={index} data={data[key]} />
1313
))}
1414
</Fragment>
1515
);
@@ -18,17 +18,14 @@ export const Paths = () => {
1818
type CreatePathProps = {
1919
data: number[][];
2020
keyName: string;
21+
index: number;
2122
};
2223

23-
const CreatePath = ({ data = [], keyName }: CreatePathProps) => {
24+
const CreatePath = ({ data = [], index, keyName }: CreatePathProps) => {
2425
const { renderPath, ...options } = useOptionStore();
2526
const stroke = getStroke(data, options);
2627
const pathData = getSvgPathFromStroke(stroke);
27-
const dom = renderPath ? renderPath(pathData, keyName, data) : null;
28+
const dom = renderPath ? renderPath(pathData, keyName, data, index) : null;
2829
if (dom) return dom;
29-
return (
30-
<Fragment>
31-
<path d={pathData} />
32-
</Fragment>
33-
);
30+
return <path d={pathData} />;
3431
};

core/src/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export interface SignatureProps extends React.SVGProps<SVGSVGElement> {
2121
options?: StrokeOptions;
2222
readonly?: boolean;
2323
defaultPoints?: Record<string, number[][]>;
24-
renderPath?: (d: string, keyName: string, point: number[][]) => JSX.Element;
24+
renderPath?: (d: string, keyName: string, point: number[][], index: number) => JSX.Element;
2525
onPointer?: (points: number[][]) => void;
2626
}
2727

0 commit comments

Comments
 (0)