Skip to content
This repository was archived by the owner on Jun 19, 2025. It is now read-only.

Commit b198faf

Browse files
Merge pull request #17 from rokinsky/fix/component-update
fix: add data attributes and outline class when updating a component
2 parents 66b28c6 + 61883ce commit b198faf

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

lib/sb-editable.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ export interface SbEditableContent {
88
interface SbEditableProps {
99
content: SbEditableContent
1010
}
11-
declare class SbEditable extends React.Component<SbEditableProps, {}> {
11+
declare class SbEditable extends React.PureComponent<SbEditableProps, {}> {
1212
constructor(props: SbEditableProps)
1313
componentDidMount(): void
14+
componentDidUpdate(): void
15+
addPropsOnChildren(): void
1416
addClass(el: HTMLElement, className: string): void
1517
render(): React.ReactNode
1618
}

lib/sb-editable.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ function _possibleConstructorReturn(self, call) { if (!self) { throw new Referen
2222

2323
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
2424

25-
var SbEditable = function (_React$Component) {
26-
_inherits(SbEditable, _React$Component);
25+
var SbEditable = function (_React$PureComponent) {
26+
_inherits(SbEditable, _React$PureComponent);
2727

2828
function SbEditable(props) {
2929
_classCallCheck(this, SbEditable);
@@ -34,6 +34,16 @@ var SbEditable = function (_React$Component) {
3434
_createClass(SbEditable, [{
3535
key: 'componentDidMount',
3636
value: function componentDidMount() {
37+
this.addPropsOnChildren();
38+
}
39+
}, {
40+
key: 'componentDidUpdate',
41+
value: function componentDidUpdate() {
42+
this.addPropsOnChildren();
43+
}
44+
}, {
45+
key: 'addPropsOnChildren',
46+
value: function addPropsOnChildren() {
3747
if (typeof this.props.content._editable === 'undefined' || window.location === window.parent.location) {
3848
return;
3949
}
@@ -67,6 +77,6 @@ var SbEditable = function (_React$Component) {
6777
}]);
6878

6979
return SbEditable;
70-
}(_react2.default.Component);
80+
}(_react2.default.PureComponent);
7181

7282
exports.default = SbEditable;

src/sb-editable.jsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
import React from 'react'
22
import ReactDOM from 'react-dom'
33

4-
class SbEditable extends React.Component {
4+
class SbEditable extends React.PureComponent {
55
constructor(props) {
66
super(props)
77
}
88

99
componentDidMount() {
10+
this.addPropsOnChildren()
11+
}
12+
13+
componentDidUpdate() {
14+
this.addPropsOnChildren()
15+
}
16+
17+
addPropsOnChildren() {
1018
if (typeof this.props.content._editable === 'undefined' ||
1119
window.location === window.parent.location) {
1220
return
1321
}
1422

15-
var el = ReactDOM.findDOMNode(this)
16-
var options = JSON.parse(this.props.content._editable.replace(/^<!--#storyblok#/, '').replace(/-->$/, ''))
23+
const el = ReactDOM.findDOMNode(this)
24+
const options = JSON.parse(this.props.content._editable.replace(/^<!--#storyblok#/, '').replace(/-->$/, ''))
1725

1826
if (el instanceof Object && typeof el.setAttribute === 'function') {
1927
el.setAttribute('data-blok-c', JSON.stringify(options))

0 commit comments

Comments
 (0)