forked from gaearon/react-devtools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReactNativeStyle.js
More file actions
74 lines (65 loc) · 1.83 KB
/
ReactNativeStyle.js
File metadata and controls
74 lines (65 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
*/
'use strict';
var React = require('react');
var StyleEdit = require('./StyleEdit');
function shallowClone(obj) {
var nobj = {};
for (var n in obj) {
nobj[n] = obj[n];
}
return nobj;
}
class NativeStyler extends React.Component {
constructor(props: Object) {
super(props);
this.state = {style: null};
}
componentWillMount() {
this.props.bridge.call('rn-style:get', this.props.id, style => {
this.setState({style});
});
}
componentWillReceiveProps(nextProps: Object) {
if (nextProps.id === this.props.id) {
return;
}
this.setState({style: null});
this.props.bridge.call('rn-style:get', nextProps.id, style => {
this.setState({style});
});
}
_handleStyleChange(attr: string, val: string | number) {
this.state.style[attr] = val;
this.props.bridge.send('rn-style:set', {id: this.props.id, attr, val});
this.setState({style: this.state.style});
}
_handleStyleRename(oldName: string, newName: string, val: string | number) {
var style = shallowClone(this.state.style);
delete style[oldName];
style[newName] = val;
this.props.bridge.send('rn-style:rename', {id: this.props.id, oldName, newName, val});
this.setState({style});
}
render() {
if (!this.state.style) {
return <em>loading</em>;
}
return (
<StyleEdit
style={this.state.style}
onRename={this._handleStyleRename.bind(this)}
onChange={this._handleStyleChange.bind(this)}
/>
);
}
}
module.exports = NativeStyler;