-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding object param-type selector
- Loading branch information
Showing
7 changed files
with
126 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
client/Components/ComponentParams/ParamSelector/ParamSelectorObject/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import React from 'react'; | ||
import _ from 'underscore'; | ||
|
||
import TextField from '../../../UI/TextField'; | ||
import ErrorIndicator from '../../../UI/ErrorIndicator'; | ||
|
||
const DEBOUNCE_AMOUNT = 300; | ||
|
||
/** | ||
* @description | ||
* open input for object values. will try to eval value and fallback to undefined. | ||
* | ||
* @param {function} onChange | ||
*/ | ||
export default class ParamSelectorObject extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = {error: null}; | ||
this.onChange = this.onChange.bind(this); | ||
this.reportChangeBounced = _.debounce(val => this.reportChange(val), DEBOUNCE_AMOUNT); | ||
} | ||
|
||
/** | ||
* A bit of a hack - it puts initial Object to the TextField but free it right after | ||
* (by setting it immediately after to undefined) | ||
*/ | ||
componentDidMount() { | ||
let value = JSON.stringify(this.props.selectedValue); | ||
if (value) { | ||
this.setState( | ||
state => _.extend({}, state, {value}), | ||
() => this.setState(state => _.extend({}, state, {value: undefined})) | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Will update the new value from the event. | ||
* sadly the event cannot be sent to the parent onChange because we debounce the onChange | ||
* @param {event} e | ||
*/ | ||
onChange(e) { | ||
const newValue = e.target.value; | ||
this.reportChangeBounced(newValue); | ||
} | ||
|
||
/** | ||
* report to parent onChange, this function is debounced to be reportChangeBounced | ||
* @param {string} val | ||
*/ | ||
reportChange(val) { | ||
const {name, compiler = _.noop, onChange = _.noop} = this.props; | ||
let obj, error = null; | ||
try { | ||
obj = compiler(`Object(${val})`); | ||
} catch (e) { | ||
error = e; | ||
} | ||
this.setState(state => _.extend({}, state, {error})); | ||
onChange(null, obj); | ||
} | ||
|
||
render() { | ||
return ( | ||
<ErrorIndicator error={this.state.error}> | ||
<div className="library-_-object-selector-wrapper"> | ||
<TextField | ||
value={this.state.value} | ||
onChange={this.onChange} | ||
placeholder="{ object: ... }" /> | ||
</div> | ||
</ErrorIndicator> | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import React from 'react'; | ||
|
||
import Tooltip from '../Tooltip'; | ||
import './index.scss'; | ||
|
||
/** | ||
* @name | ||
* ErrorIndicator | ||
* | ||
* @module | ||
* Content | ||
* | ||
* @description | ||
* Show an error tooltip on a container | ||
* | ||
* @example | ||
* <ErrorIndicator error={new Error('message')}> | ||
* My error-full content | ||
* </ErrorIndicator> | ||
* | ||
* @param {node} children | ||
* @param {String|Error} [error] | ||
*/ | ||
export default function ErrorIndicator ({children, error}) { | ||
return ( | ||
<div className="library-_-tooltip-error-indicator-wrapper"> | ||
{error ? (<div className="library-_-error-indicator-wrapper"> | ||
<Tooltip tooltip={<pre>{error.toString && error.toString()}</pre>}> | ||
<div className="library-_-error-indicator" /> | ||
</Tooltip> | ||
</div>) : null} | ||
{children} | ||
</div> | ||
); | ||
} |
2 changes: 1 addition & 1 deletion
2
...ParamSelector/ParamSelectorJSX/index.scss → ...t/Components/UI/ErrorIndicator/index.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.