Skip to content

Commit

Permalink
Merge pull request #3287 from TyMick/caption-state-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
felixfeng33 authored Jun 15, 2024
2 parents cd97544 + f805c83 commit 9d1b552
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 92 deletions.
5 changes: 5 additions & 0 deletions .changeset/fuzzy-dolls-speak.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@udecode/plate-caption": patch
---

Update caption UI when element `caption` property changes
5 changes: 5 additions & 0 deletions apps/www/content/docs/components/changelog.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ Use the [CLI](https://platejs.org/docs/components/cli) to install the latest ver

## June 2024 #11

### Pending #11.2

- fix `caption`: update UI when element `caption` property changes

### June 6 #11.1

- replace `combobox` with `inline-combobox`
Expand Down Expand Up @@ -111,6 +115,7 @@ export const TableElement = withHOC(
- new dependency: `@udecode/cn`
- remove `@/lib/utils.ts` in favor of `cn` from `@udecode/cn`. Replace all imports from `@/lib/utils` with `@udecode/cn`
- import `withProps` from `@udecode/cn` instead of `@udecode/plate-common
`
- all components using `forwardRef` are now using `withRef`. `withProps`, `withCn` and `withVariants` are also used to reduce boilerplate.
- add `withCn` to ESLint `settings.tailwindcss.callees` and `classAttributes` in your IDE settings
Expand Down
33 changes: 17 additions & 16 deletions packages/caption/src/components/CaptionTextarea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,27 @@ export const useCaptionTextareaFocus = (

export const useCaptionTextareaState = () => {
const element = useElement<TCaptionElement>();
const editor = useEditorRef();

const {
caption: nodeCaption = [{ children: [{ text: '' }] }] as [TElement],
} = element;

const [captionValue, setCaptionValue] = React.useState<
TextareaAutosizeProps['value']
>(getNodeString(nodeCaption[0]));
const captionValue: TextareaAutosizeProps['value'] = getNodeString(
nodeCaption[0]
);

function setCaptionValue(newValue: TextareaAutosizeProps['value']) {
const path = findNodePath(editor, element);

if (!path) return;

setNodes<TCaptionElement>(
editor,
{ caption: [{ text: newValue }] },
{ at: path }
);
}

const readOnly = useReadOnly();

Expand Down Expand Up @@ -84,21 +97,9 @@ export const useCaptionTextarea = ({
(e: React.ChangeEvent<HTMLTextAreaElement>) => {
const newValue = e.target.value;

// local state
setCaptionValue(newValue);

const path = findNodePath(editor, element);

if (!path) return;

// saved state
setNodes<TCaptionElement>(
editor,
{ caption: [{ text: newValue }] },
{ at: path }
);
},
[editor, element, setCaptionValue]
[setCaptionValue]
);

const onKeyDown: TextareaAutosizeProps['onKeyDown'] = (e) => {
Expand Down
Loading

0 comments on commit 9d1b552

Please sign in to comment.