Skip to content

Commit

Permalink
chore(rich-text-editor)!: Rename value to defaultValue
Browse files Browse the repository at this point in the history
To conform to React naming convention for form control props.
Uncontrolled form fields take a `defaultValue` to indicate that
changes to this prop's value will not update the mounted component.
Rather, the component manages its current value as internal state,
and notifies subscribers via onChange when it changes.

Consumers will need to update their code to the new prop name. E.g. before:

```jsx
<RichTextEditor value={…}>
```

and after:

```jsx
<RichTextEditor defaultValue={…}>
```
  • Loading branch information
sentience committed Sep 12, 2023
1 parent ad3640f commit 38891d6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
23 changes: 23 additions & 0 deletions .changeset/fluffy-camels-applaud.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
"@kaizen/rich-text-editor": major
---

Rename `value` prop to reflect uncontrolled form field

To conform to React naming convention for form control props.
Uncontrolled form fields take a `defaultValue` to indicate that
changes to this prop's value will not update the mounted component.
Rather, the component manages its current value as internal state,
and notifies subscribers via onChange when it changes.

Consumers will need to update their code to the new prop name. E.g. before:

```jsx
<RichTextEditor value={…}>
```

and after:

```jsx
<RichTextEditor defaultValue={…}>
```
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const TestBase: Story = {
return (
<RichTextEditor
labelText="Label"
value={rteData}
defaultValue={rteData}
onChange={handleOnChange}
rows={3}
controls={[
Expand Down
11 changes: 7 additions & 4 deletions packages/rich-text-editor/src/RichTextEditor/RichTextEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ import { createSchemaFromControls } from "./schema"
import { Toolbar, ToolbarSection, ToggleIconButton } from "./"
import styles from "./RichTextEditor.module.scss"

type X = HTMLAttributes<HTMLDivElement>["defaultValue"]

Check failure on line 24 in packages/rich-text-editor/src/RichTextEditor/RichTextEditor.tsx

View workflow job for this annotation

GitHub Actions / eslint

'X' is defined but never used. Allowed unused vars must match /^_/u

export interface BaseRichTextEditorProps
extends OverrideClassName<
Omit<HTMLAttributes<HTMLDivElement>, "onChange" | "content">
Omit<HTMLAttributes<HTMLDivElement>, "onChange" | "defaultValue">
> {
onChange: (content: ProseMirrorState.EditorState) => void
value: EditorContentArray
defaultValue: EditorContentArray
controls?: ToolbarItems[]
/**
* Sets a default min-height for the editable area in units of body paragraph line height, similar to the 'rows' attribute on <textarea>.
Expand Down Expand Up @@ -64,7 +66,7 @@ export type RichTextEditorProps = RTEWithLabelText | RTEWithLabelledBy
export const RichTextEditor = (props: RichTextEditorProps): JSX.Element => {
const {
onChange,
value,
defaultValue,
labelText,
"aria-labelledby": labelledBy,
classNameOverride,
Expand Down Expand Up @@ -93,7 +95,8 @@ export const RichTextEditor = (props: RichTextEditorProps): JSX.Element => {
type: "doc",
// we're converting empty arrays to the ProseMirror default "empty" state because when
// given an empty array ProseMirror returns undefined, breaking the type
content: value?.length > 0 ? value : [{ type: "paragraph" }],
content:
defaultValue?.length > 0 ? defaultValue : [{ type: "paragraph" }],
}),
schema,
plugins: getPlugins(controls, schema),
Expand Down

0 comments on commit 38891d6

Please sign in to comment.