Skip to content

feat(joint-react): add new set hooks and better ts support #2947

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions packages/joint-react/.storybook/decorators/with-simple-data.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ const initialElements = createElements([
data: { label: 'Node 1', color: PRIMARY },
x: 100,
y: 20,
width: 100,
width: 150,
height: 50,
},
{
id: '2',
data: { label: 'Node 2', color: PRIMARY },
x: 200,
y: 250,
width: 100,
width: 150,
height: 50,
},
]);
Expand All @@ -46,7 +46,7 @@ const defaultLinks = createLinks([
},
]);

function SimpleGraphProviderDecorator({ children }: Readonly<PropsWithChildren>) {
export function SimpleGraphProviderDecorator({ children }: Readonly<PropsWithChildren>) {
return (
<GraphProvider defaultElements={initialElements} defaultLinks={defaultLinks}>
{children}
Expand All @@ -68,14 +68,13 @@ export function RenderItemDecorator(
}>
) {
return (
<div style={{ width: '100%', height: 350 }}>
<div style={{ width: '100%', height: 450 }}>
<SimpleGraphProviderDecorator>
<Paper
width={'100%'}
height={350}
height={450}
renderElement={properties.renderElement}
linkPinning={false}
// snapLinks={{ radius: 20 }}
/>
</SimpleGraphProviderDecorator>
</div>
Expand Down
4 changes: 4 additions & 0 deletions packages/joint-react/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@

## Hooks

- [useAddElement](functions/useAddElement.md)
- [useAddLink](functions/useAddLink.md)
- [useElements](functions/useElements.md)
- [useGraph](functions/useGraph.md)
- [useLinks](functions/useLinks.md)
- [useRemoveCell](functions/useRemoveCell.md)
- [useSetElement](functions/useSetElement.md)

## Models
Expand Down Expand Up @@ -75,6 +78,7 @@ const paper = usePaper();
- [PaperProps](interfaces/PaperProps.md)
- [PortGroupProps](interfaces/PortGroupProps.md)
- [PortProps](interfaces/PortProps.md)
- [ReactElementAttributes](interfaces/ReactElementAttributes.md)
- [StrokeHighlighterProps](interfaces/StrokeHighlighterProps.md)
- [TextNodeProps](interfaces/TextNodeProps.md)

Expand Down
76 changes: 38 additions & 38 deletions packages/joint-react/docs/classes/ReactElement.md

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions packages/joint-react/docs/functions/createElements.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

# Function: createElements()

> **createElements**\<`Data`, `Type`, `Element`\>(`data`): `Element` & `object`[]
> **createElements**\<`Data`, `Type`, `Element`\>(`data`): `Element` & `RequiredElementProps`[]

Defined in: [joint-react/src/utils/create.ts:30](https://github.com/samuelgja/joint/blob/main/packages/joint-react/src/utils/create.ts#L30)
Defined in: [joint-react/src/utils/create.ts:36](https://github.com/samuelgja/joint/blob/main/packages/joint-react/src/utils/create.ts#L36)

Create elements helper function.

Expand All @@ -20,7 +20,7 @@ Create elements helper function.

### Type

`Type` *extends* `string` = `string`
`Type` *extends* `string` = `"react"`

### Element

Expand All @@ -36,7 +36,7 @@ Array of elements to create.

## Returns

`Element` & `object`[]
`Element` & `RequiredElementProps`[]

Array of elements. (Nodes)

Expand Down
4 changes: 2 additions & 2 deletions packages/joint-react/docs/functions/createLinks.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

> **createLinks**\<`Link`, `Type`\>(`data`): `Link` & [`GraphLink`](../interfaces/GraphLink.md)[]

Defined in: [joint-react/src/utils/create.ts:70](https://github.com/samuelgja/joint/blob/main/packages/joint-react/src/utils/create.ts#L70)
Defined in: [joint-react/src/utils/create.ts:74](https://github.com/samuelgja/joint/blob/main/packages/joint-react/src/utils/create.ts#L74)

Create links helper function.

Expand All @@ -20,7 +20,7 @@ Create links helper function.

### Type

`Type` *extends* `string` = `string`
`Type` *extends* `string` = `"standard.Link"`

## Parameters

Expand Down
42 changes: 42 additions & 0 deletions packages/joint-react/docs/functions/useAddElement.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[**@joint/react**](../README.md)

***

[@joint/react](../README.md) / useAddElement

# Function: useAddElement()

> **useAddElement**\<`T`\>(): (`element`) => `void`

Defined in: [joint-react/src/hooks/use-add-element.ts:22](https://github.com/samuelgja/joint/blob/main/packages/joint-react/src/hooks/use-add-element.ts#L22)

A custom hook that adds an element to the graph.

## Type Parameters

### T

`T` *extends* `Element`\<`Attributes`, `ModelSetOptions`\> \| [`GraphElementBase`](../interfaces/GraphElementBase.md)\<`string`\>

## Returns

`Function`

A function that adds the element to the graph.

### Parameters

#### element

`SetElement`\<`T`\>

### Returns

`void`

## Example

```ts
const addElement = useAddElement();
addElement({ id: '1', data: { label: 'Node 1' } });
```
42 changes: 42 additions & 0 deletions packages/joint-react/docs/functions/useAddLink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[**@joint/react**](../README.md)

***

[@joint/react](../README.md) / useAddLink

# Function: useAddLink()

> **useAddLink**\<`T`\>(): (`link`) => `void`

Defined in: [joint-react/src/hooks/use-add-link.ts:17](https://github.com/samuelgja/joint/blob/main/packages/joint-react/src/hooks/use-add-link.ts#L17)

A custom hook that adds a link to the graph.

## Type Parameters

### T

`T` *extends* `Link`\<`Attributes`, `ModelSetOptions`\> \| [`GraphLinkBase`](../interfaces/GraphLinkBase.md)\<`string`\>

## Returns

`Function`

A function that adds the link to the graph.

### Parameters

#### link

`T`

### Returns

`void`

## Example

```ts
const addLink = useAddLink();
addLink({ id: '1', source: { id: '2' }, target: { id: '3' } });
```
2 changes: 1 addition & 1 deletion packages/joint-react/docs/functions/useElement.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ How it works:

### Element

`Element` = [`GraphElement`](../interfaces/GraphElement.md)\<`unknown`, `string`\>
`Element` = [`GraphElement`](../interfaces/GraphElement.md)\<`unknown`, `"react"`\>

### ReturnedElements

Expand Down
2 changes: 1 addition & 1 deletion packages/joint-react/docs/functions/useElements.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ checks if the selected value really changed.

### Elements

`Elements` *extends* [`GraphElementBase`](../interfaces/GraphElementBase.md)\<`string`\> = [`GraphElement`](../interfaces/GraphElement.md)\<`unknown`, `string`\>
`Elements` *extends* [`GraphElementBase`](../interfaces/GraphElementBase.md)\<`string`\> = [`GraphElement`](../interfaces/GraphElement.md)\<`unknown`, `"react"`\>

### SelectorReturnType

Expand Down
36 changes: 36 additions & 0 deletions packages/joint-react/docs/functions/useRemoveCell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
[**@joint/react**](../README.md)

***

[@joint/react](../README.md) / useRemoveCell

# Function: useRemoveCell()

> **useRemoveCell**(): (`id`) => `void`

Defined in: [joint-react/src/hooks/use-remove-cell.ts:15](https://github.com/samuelgja/joint/blob/main/packages/joint-react/src/hooks/use-remove-cell.ts#L15)

A custom hook that removes an node or link from the graph by its ID.

## Returns

`Function`

A function that removes the element from the graph.

### Parameters

#### id

`ID`

### Returns

`void`

## Example

```ts
const removeCell = useRemoveCell();
removeCell('1');
```
6 changes: 3 additions & 3 deletions packages/joint-react/docs/functions/useSetElement.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Defined in: [joint-react/src/hooks/use-set-element.ts:81](https://github.com/sam

**`Experimental`**

Use this hook to set element attributes.
Set the element attribute in the graph.
It returns a function to set the element attribute.

It must be used inside the GraphProvider.
Expand Down Expand Up @@ -89,7 +89,7 @@ Defined in: [joint-react/src/hooks/use-set-element.ts:89](https://github.com/sam

**`Experimental`**

Use this hook to set element attributes.
Set the element attribute in the graph.
It returns a function to set the element attribute.

It must be used inside the GraphProvider.
Expand Down Expand Up @@ -164,7 +164,7 @@ Defined in: [joint-react/src/hooks/use-set-element.ts:93](https://github.com/sam

**`Experimental`**

Use this hook to set element attributes.
Set the element attribute in the graph.
It returns a function to set the element attribute.

It must be used inside the GraphProvider.
Expand Down
Loading