Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions __tests__/index.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ describe('single variable', () => {
expect(container).toHaveTextContent('APIKEY');
});

it('should allow the use of "name" as a prop', () => {
const { variable, ...rest } = props;
const { container } = render(<Variable {...rest} name="apiKey" />);

expect(container).toHaveTextContent('123456');
});

it('should render auth dropdown if default and oauth enabled', () => {
const { container } = render(
<Variable {...props} defaults={[{ name: 'apiKey', default: 'default' }]} oauth user={{}} />,
Expand Down
23 changes: 16 additions & 7 deletions index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Variable extends React.Component {
super(props);
this.state = { showDropdown: false };

this.getName = this.getName.bind(this);
this.toggleVarDropdown = this.toggleVarDropdown.bind(this);
this.toggleAuthDropdown = this.toggleAuthDropdown.bind(this);
this.renderVarDropdown = this.renderVarDropdown.bind(this);
Expand All @@ -22,10 +23,18 @@ class Variable extends React.Component {
this.props.changeSelected(event.target.value);
}

getName() {
const name = this.props.name || this.props.variable;
if (!name) throw new TypeError("Missing prop 'name' or 'variable' for Variable component!");

return name;
}

getDefault() {
const def = this.props.defaults.filter(Boolean).find(d => d.name === this.props.variable) || {};
const def = this.props.defaults.filter(Boolean).find(d => d.name === this.getName()) || {};

if (def.default) return def.default;
return this.props.variable.toUpperCase();
return this.getName().toUpperCase();
Comment thread
kellyjosephprice marked this conversation as resolved.
Resolved
}

getSelectedValue(selected) {
Expand All @@ -43,9 +52,9 @@ class Variable extends React.Component {
// Additionally do not show the dropdown if there is only a
// single key, since there's nothing to change it to.
shouldShowVarDropdown(selected) {
const { user, variable } = this.props;
const { user } = this.props;

return !!this.getSelectedValue(selected)[variable] && Array.isArray(user.keys) && user.keys.length > 1;
return !!this.getSelectedValue(selected)[this.getName()] && Array.isArray(user.keys) && user.keys.length > 1;
}

// Return value in this order
Expand All @@ -54,10 +63,9 @@ class Variable extends React.Component {
// - default value
// - uppercase key
getValue(selected) {
const { variable } = this.props;
const selectedValue = this.getSelectedValue(selected);

const value = selectedValue[variable] || this.props.user[variable] || this.getDefault();
const value = selectedValue[this.getName()] || this.props.user[this.getName()] || this.getDefault();

return typeof value === 'object' ? JSON.stringify(value) : value;
}
Expand Down Expand Up @@ -142,12 +150,13 @@ class Variable extends React.Component {
Variable.propTypes = {
changeSelected: PropTypes.func.isRequired,
defaults: PropTypes.arrayOf(PropTypes.shape({ default: PropTypes.string, name: PropTypes.string })).isRequired,
name: PropTypes.string,
oauth: PropTypes.bool,
selected: PropTypes.string.isRequired,
user: PropTypes.shape({
keys: PropTypes.array,
}).isRequired,
variable: PropTypes.string.isRequired,
variable: PropTypes.string,
};

Variable.defaultProps = {
Expand Down