mirror of
https://github.com/home-assistant/frontend.git
synced 2025-12-22 11:48:46 +00:00
Add automation editor (#275)
* Add automation editor * Build JS before running tests * Add browser warning * Re-order from/to in state
This commit is contained in:
56
preact-src/json_textarea.js
Normal file
56
preact-src/json_textarea.js
Normal file
@@ -0,0 +1,56 @@
|
||||
import { h, Component } from 'preact';
|
||||
|
||||
|
||||
export default class JSONTextArea extends Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.state.isValid = true;
|
||||
this.state.value = JSON.stringify(props.value || {}, null, 2);
|
||||
this.onChange = this.onChange.bind(this);
|
||||
}
|
||||
|
||||
onChange(ev) {
|
||||
const value = ev.target.value;
|
||||
let parsed;
|
||||
let isValid;
|
||||
|
||||
try {
|
||||
parsed = JSON.parse(value);
|
||||
isValid = true;
|
||||
} catch (err) {
|
||||
// Invalid JSON
|
||||
isValid = false;
|
||||
}
|
||||
|
||||
this.setState({
|
||||
value,
|
||||
isValid,
|
||||
});
|
||||
if (isValid) {
|
||||
this.props.onChange(parsed);
|
||||
}
|
||||
}
|
||||
|
||||
componentWillReceiveProps({ value }) {
|
||||
this.setState({
|
||||
value: JSON.stringify(value, null, 2),
|
||||
isValid: true,
|
||||
});
|
||||
}
|
||||
|
||||
render(props, { value, isValid }) {
|
||||
const style = {
|
||||
minWidth: 300,
|
||||
};
|
||||
if (!isValid) {
|
||||
style.border = '1px solid red';
|
||||
}
|
||||
return (
|
||||
<textarea
|
||||
value={value}
|
||||
style={style}
|
||||
onChange={this.onChange}
|
||||
/>
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user