Skip to content

Commit d4742d0

Browse files
authored
Merge pull request #157 from aeltanawy/add-function-component
Add function components
2 parents 994fb0a + 7369210 commit d4742d0

File tree

8 files changed

+96
-78
lines changed

8 files changed

+96
-78
lines changed

cookiecutter.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"github_org": "",
1010
"description": "Project Description",
1111
"use_async": ["False", "True"],
12+
"component_type": ["Function Component", "Class Component"],
1213
"license": [
1314
"MIT License",
1415
"BSD License",

hooks/post_gen_project.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ def _execute_command(cmd):
5050
os.remove(os.path.join(os.getcwd(), 'src', 'lib', 'LazyLoader.js'))
5151

5252

53-
5453
if install_deps != 'True':
5554
print('`install_dependencies` is false!!', file=sys.stderr)
5655
print('Please create a venv in your project root'

{{cookiecutter.project_shortname}}/cookiecutter_templates/AsyncComponent.react.js renamed to {{cookiecutter.project_shortname}}/cookiecutter_templates/AsyncFunctionComponent.react.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, {Component} from 'react';
1+
import React from 'react';
22
import PropTypes from 'prop-types';
33
import { {{cookiecutter.component_name}} as RealComponent } from '../LazyLoader';
44

@@ -9,15 +9,13 @@ import { {{cookiecutter.component_name}} as RealComponent } from '../LazyLoader'
99
* It renders an input with the property `value`
1010
* which is editable by the user.
1111
*/
12-
export default class {{cookiecutter.component_name}} extends Component {
13-
render() {
14-
return (
15-
<React.Suspense fallback={null}>
16-
<RealComponent {...this.props}/>
17-
</React.Suspense>
18-
);
19-
}
20-
}
12+
const {{cookiecutter.component_name}} = (props) => {
13+
return (
14+
<React.Suspense fallback={null}>
15+
<RealComponent {...props}/>
16+
</React.Suspense>
17+
);
18+
};
2119

2220
{{cookiecutter.component_name}}.defaultProps = {};
2321

@@ -44,6 +42,7 @@ export default class {{cookiecutter.component_name}} extends Component {
4442
setProps: PropTypes.func
4543
};
4644

45+
export default {{cookiecutter.component_name}};
4746

4847
export const defaultProps = {{cookiecutter.component_name}}.defaultProps;
49-
export const propTypes = {{cookiecutter.component_name}}.propTypes;
48+
export const propTypes = {{ cookiecutter.component_name }}.propTypes;

{{cookiecutter.project_shortname}}/cookiecutter_templates/Component.react.js renamed to {{cookiecutter.project_shortname}}/cookiecutter_templates/ClassComponent.react.js

File renamed without changes.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import React, { useState } from 'react';
2+
import PropTypes from 'prop-types';
3+
4+
/**
5+
* ExampleComponent is an example component.
6+
* It takes a property, `label`, and
7+
* displays it.
8+
* It renders an input with the property `value`
9+
* which is editable by the user.
10+
*/
11+
const {{cookiecutter.component_name}} = (props) => {
12+
const {id, label, setProps, value} = props;
13+
14+
return (
15+
<div id={id}>
16+
ExampleComponent: {label}&nbsp;
17+
<input
18+
value={value}
19+
onChange={
20+
/*
21+
* Send the new value to the parent component.
22+
* setProps is a prop that is automatically supplied
23+
* by dash's front-end ("dash-renderer").
24+
* In a Dash app, this will update the component's
25+
* props and send the data back to the Python Dash
26+
* app server if a callback uses the modified prop as
27+
* Input or State.
28+
*/
29+
e => setProps({ value: e.target.value })
30+
}
31+
/>
32+
</div>
33+
);
34+
}
35+
36+
{{cookiecutter.component_name}}.defaultProps = {};
37+
38+
{{cookiecutter.component_name}}.propTypes = {
39+
/**
40+
* The ID used to identify this component in Dash callbacks.
41+
*/
42+
id: PropTypes.string,
43+
44+
/**
45+
* A label that will be printed when this component is rendered.
46+
*/
47+
label: PropTypes.string.isRequired,
48+
49+
/**
50+
* The value displayed in the input.
51+
*/
52+
value: PropTypes.string,
53+
54+
/**
55+
* Dash-assigned callback that should be called to report property changes
56+
* to Dash, to make them available for callbacks.
57+
*/
58+
setProps: PropTypes.func
59+
};
60+
61+
export default {{cookiecutter.component_name}};
Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,24 @@
11
/* eslint no-magic-numbers: 0 */
2-
import React, {Component} from 'react';
2+
import React, { useState } from 'react';
33

44
import { {{cookiecutter.component_name}} } from '../lib';
55

6-
class App extends Component {
6+
const App = () => {
77

8-
constructor(props) {
9-
super(props);
10-
this.state = {
11-
value: ''
8+
const [state, setState] = useState({value:'', label:'Type Here'});
9+
const setProps = (newProps) => {
10+
setState(newProps);
1211
};
13-
this.setProps = this.setProps.bind(this);
14-
}
1512

16-
setProps(newProps) {
17-
this.setState(newProps);
18-
}
13+
return (
14+
<div>
15+
<{{cookiecutter.component_name}}
16+
setProps={setProps}
17+
{...state}
18+
/>
19+
</div>
20+
)
21+
};
1922

20-
render() {
21-
return (
22-
<div>
23-
<{{cookiecutter.component_name}}
24-
setProps={this.setProps}
25-
{...this.state}
26-
/>
27-
</div>
28-
)
29-
}
30-
}
3123

3224
export default App;
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
{%- if cookiecutter.use_async == "True" -%}
2-
{%- include 'cookiecutter_templates/AsyncComponent.react.js' -%}
2+
{%- include 'cookiecutter_templates/AsyncFunctionComponent.react.js' -%}
3+
{%- elif cookiecutter.component_type == "Function Component" -%}
4+
{%- include 'cookiecutter_templates/FunctionComponent.react.js' -%}
35
{%- else -%}
4-
{%- include 'cookiecutter_templates/Component.react.js' -%}
5-
{%- endif -%}
6+
{%- include 'cookiecutter_templates/ClassComponent.react.js' -%}
7+
{%- endif -%}
Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,5 @@
1-
import React, {Component} from 'react';
2-
import PropTypes from 'prop-types';
3-
import {defaultProps, propTypes} from '../components/{{cookiecutter.component_name}}.react';
4-
5-
/**
6-
* ExampleComponent is an example component.
7-
* It takes a property, `label`, and
8-
* displays it.
9-
* It renders an input with the property `value`
10-
* which is editable by the user.
11-
*/
12-
export default class {{cookiecutter.component_name}} extends Component {
13-
render() {
14-
const {id, label, setProps, value} = this.props;
15-
16-
return (
17-
<div id={id}>
18-
ExampleComponent: {label}&nbsp;
19-
<input
20-
value={value}
21-
onChange={
22-
/*
23-
* Send the new value to the parent component.
24-
* setProps is a prop that is automatically supplied
25-
* by dash's front-end ("dash-renderer").
26-
* In a Dash app, this will update the component's
27-
* props and send the data back to the Python Dash
28-
* app server if a callback uses the modified prop as
29-
* Input or State.
30-
*/
31-
e => setProps({ value: e.target.value })
32-
}
33-
/>
34-
</div>
35-
);
36-
}
37-
}
38-
39-
40-
{{cookiecutter.component_name}}.defaultProps = defaultProps;
41-
{{cookiecutter.component_name}}.propTypes = propTypes;
1+
{% if cookiecutter.component_type == "Function Component" -%}
2+
{%- include 'cookiecutter_templates/FunctionComponent.react.js' -%}
3+
{%- else -%}
4+
{%- include 'cookiecutter_templates/ClassComponent.react.js' -%}
5+
{%- endif -%}

0 commit comments

Comments
 (0)