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

Commit 625a7bf

Browse files
author
Dom Harrington
committed
Add very basic implementation of Variable component
1 parent e2743fa commit 625a7bf

File tree

3 files changed

+102
-1
lines changed

3 files changed

+102
-1
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
const React = require('react');
2+
const { shallow } = require('enzyme');
3+
4+
const Variable = require('../src/Variable');
5+
6+
test('should render variable', () => {
7+
const variable = shallow(<Variable value="123456" />);
8+
9+
expect(variable.text()).toBe('123456');
10+
});
11+
12+
test('should render the first of multiple values', () => {
13+
const variable = shallow(
14+
<Variable
15+
k="apiKey"
16+
value={[{ name: 'project1', apiKey: '123' }, { name: 'project2', apiKey: '456' }]}
17+
/>,
18+
);
19+
20+
expect(variable.text()).toBe('123');
21+
});
22+
23+
test('should render whatever the selected name is', () => {
24+
const variable = shallow(
25+
<Variable
26+
k="apiKey"
27+
value={[{ name: 'project1', apiKey: '123' }, { name: 'project2', apiKey: '456' }]}
28+
selected="project2"
29+
/>,
30+
);
31+
32+
expect(variable.text()).toBe('456');
33+
});
34+
35+
test('should show dropdown when clicked', () => {
36+
const variable = shallow(
37+
<Variable
38+
k="apiKey"
39+
value={[{ name: 'project1', apiKey: '123' }, { name: 'project2', apiKey: '456' }]}
40+
selected="project2"
41+
/>,
42+
);
43+
44+
variable.simulate('click');
45+
46+
expect(variable.find('ul li').map(el => el.text())).toEqual(['project1', 'project2']);
47+
});
48+
49+
test('should select value when clicked', () => {
50+
const variable = shallow(
51+
<Variable
52+
k="apiKey"
53+
value={[{ name: 'project1', apiKey: '123' }, { name: 'project2', apiKey: '456' }, { name: 'project3'}]}
54+
selected="project1"
55+
/>,
56+
);
57+
58+
variable.simulate('click');
59+
variable.find('ul li').at(1).simulate('click', { target: { innerText: variable.find('ul li').at(1).text() } });
60+
61+
expect(variable.text()).toBe('456');
62+
});
63+
64+
test('should render auth dropdown if default');
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const React = require('react');
2+
3+
class Variable extends React.Component {
4+
constructor(props) {
5+
super(props);
6+
this.state = { showDropdown: false, selected: props.selected };
7+
8+
this.showDropdown = this.showDropdown.bind(this);
9+
this.selectValue = this.selectValue.bind(this);
10+
}
11+
showDropdown() {
12+
this.setState({ showDropdown: true });
13+
}
14+
selectValue(event) {
15+
this.setState({ selected: event.target.innerText, showDropdown: false });
16+
}
17+
render() {
18+
const { k, value } = this.props;
19+
20+
const { selected } = this.state;
21+
22+
if (this.state.showDropdown) {
23+
return (
24+
<ul>{value.map(key => <li onClick={this.selectValue} key={key.name}>{key.name}</li>)}</ul>
25+
);
26+
}
27+
28+
if (Array.isArray(value)) {
29+
const selectedValue = selected ? value.find(key => key.name === selected) : value[0];
30+
return <span onClick={this.showDropdown}>{selectedValue[k]}</span>;
31+
}
32+
return <span>{value}</span>;
33+
}
34+
}
35+
36+
module.exports = Variable;

packages/api-explorer/src/block-types/TextArea.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const React = require('react');
22
const PropTypes = require('prop-types');
33
const markdown = require('../lib/markdown');
44
const sanitizeSchema = require('hast-util-sanitize/lib/github.json');
5+
const Variable = require('../Variable');
56
const variableParser = require('../lib/markdown/variable-parser.js');
67

78
sanitizeSchema.tagNames.push('readme-variable');
@@ -17,7 +18,7 @@ function renderMarkdown(text) {
1718
sanitize: sanitizeSchema,
1819
remarkReactComponents: {
1920
'readme-variable': function ({ variable }) {
20-
return <span>{variable}</span>
21+
return <Variable k={variable} value={[{ name: 'project1', apiKey: '123' }, { name: 'project2', apiKey: '456' }]} />
2122
},
2223
},
2324
})

0 commit comments

Comments
 (0)