Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

Commit

Permalink
Add very basic implementation of Variable component
Browse files Browse the repository at this point in the history
  • Loading branch information
Dom Harrington committed Jun 22, 2018
1 parent e2743fa commit 625a7bf
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 1 deletion.
64 changes: 64 additions & 0 deletions packages/api-explorer/__tests__/Variable.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const React = require('react');
const { shallow } = require('enzyme');

const Variable = require('../src/Variable');

test('should render variable', () => {
const variable = shallow(<Variable value="123456" />);

expect(variable.text()).toBe('123456');
});

test('should render the first of multiple values', () => {
const variable = shallow(
<Variable
k="apiKey"
value={[{ name: 'project1', apiKey: '123' }, { name: 'project2', apiKey: '456' }]}
/>,
);

expect(variable.text()).toBe('123');
});

test('should render whatever the selected name is', () => {
const variable = shallow(
<Variable
k="apiKey"
value={[{ name: 'project1', apiKey: '123' }, { name: 'project2', apiKey: '456' }]}
selected="project2"
/>,
);

expect(variable.text()).toBe('456');
});

test('should show dropdown when clicked', () => {
const variable = shallow(
<Variable
k="apiKey"
value={[{ name: 'project1', apiKey: '123' }, { name: 'project2', apiKey: '456' }]}
selected="project2"
/>,
);

variable.simulate('click');

expect(variable.find('ul li').map(el => el.text())).toEqual(['project1', 'project2']);
});

test('should select value when clicked', () => {
const variable = shallow(
<Variable
k="apiKey"
value={[{ name: 'project1', apiKey: '123' }, { name: 'project2', apiKey: '456' }, { name: 'project3'}]}
selected="project1"
/>,
);

variable.simulate('click');
variable.find('ul li').at(1).simulate('click', { target: { innerText: variable.find('ul li').at(1).text() } });

expect(variable.text()).toBe('456');
});

test('should render auth dropdown if default');
36 changes: 36 additions & 0 deletions packages/api-explorer/src/Variable.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const React = require('react');

class Variable extends React.Component {
constructor(props) {
super(props);
this.state = { showDropdown: false, selected: props.selected };

this.showDropdown = this.showDropdown.bind(this);
this.selectValue = this.selectValue.bind(this);
}
showDropdown() {
this.setState({ showDropdown: true });
}
selectValue(event) {
this.setState({ selected: event.target.innerText, showDropdown: false });
}
render() {
const { k, value } = this.props;

const { selected } = this.state;

if (this.state.showDropdown) {
return (
<ul>{value.map(key => <li onClick={this.selectValue} key={key.name}>{key.name}</li>)}</ul>
);
}

if (Array.isArray(value)) {
const selectedValue = selected ? value.find(key => key.name === selected) : value[0];
return <span onClick={this.showDropdown}>{selectedValue[k]}</span>;
}
return <span>{value}</span>;
}
}

module.exports = Variable;
3 changes: 2 additions & 1 deletion packages/api-explorer/src/block-types/TextArea.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const React = require('react');
const PropTypes = require('prop-types');
const markdown = require('../lib/markdown');
const sanitizeSchema = require('hast-util-sanitize/lib/github.json');
const Variable = require('../Variable');
const variableParser = require('../lib/markdown/variable-parser.js');

sanitizeSchema.tagNames.push('readme-variable');
Expand All @@ -17,7 +18,7 @@ function renderMarkdown(text) {
sanitize: sanitizeSchema,
remarkReactComponents: {
'readme-variable': function ({ variable }) {
return <span>{variable}</span>
return <Variable k={variable} value={[{ name: 'project1', apiKey: '123' }, { name: 'project2', apiKey: '456' }]} />
},
},
})
Expand Down

0 comments on commit 625a7bf

Please sign in to comment.