Skip to content

Commit 37d454f

Browse files
committed
remove unnecessary props from test components
1 parent 1596d43 commit 37d454f

File tree

2 files changed

+22
-278
lines changed

2 files changed

+22
-278
lines changed

Diff for: @plotly/dash-test-components/src/components/MyPersistedComponent.js

+11-138
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,8 @@
11
import React, {PureComponent} from 'react';
22
import PropTypes from 'prop-types';
33

4-
// simple function to substitute is-numeric for our use case
5-
const isNumber = (n) => {
6-
return !isNaN(parseFloat(n)) && isFinite(n);
7-
}
8-
9-
// eslint-disable-next-line no-implicit-coercion
10-
const convert = val => (isNumber(val) ? +val : NaN);
11-
124
const isEquivalent = (v1, v2) => v1 === v2 || (isNaN(v1) && isNaN(v2));
135

14-
// using these inline functions instead of ramda
15-
const isNil = val => val == null
16-
176
const omit = (key, obj) => {
187
const { [key]: omitted, ...rest } = obj;
198
return rest;
@@ -22,14 +11,12 @@ const omit = (key, obj) => {
2211
/**
2312
* Adapted dcc input component for persistence tests.
2413
*
25-
* Note that some unnecessary props have been removed.
14+
* Note that unnecessary props have been removed.
2615
*/
2716
export default class MyPersistedComponent extends PureComponent {
2817
constructor(props) {
2918
super(props);
30-
3119
this.input = React.createRef();
32-
3320
this.onChange = this.onChange.bind(this);
3421
this.onEvent = this.onEvent.bind(this);
3522
this.onKeyPress = this.onKeyPress.bind(this);
@@ -39,55 +26,37 @@ export default class MyPersistedComponent extends PureComponent {
3926

4027
UNSAFE_componentWillReceiveProps(nextProps) {
4128
const {value} = this.input.current;
42-
const valueAsNumber = convert(value);
4329
this.setInputValue(
44-
isNil(valueAsNumber) ? value : valueAsNumber,
30+
value,
4531
nextProps.value
4632
);
47-
if (this.props.type !== 'number') {
48-
this.setState({value: nextProps.value});
49-
}
33+
this.setState({value: nextProps.value});
5034
}
5135

5236
componentDidMount() {
5337
const {value} = this.input.current;
54-
const valueAsNumber = convert(value);
5538
this.setInputValue(
56-
isNil(valueAsNumber) ? value : valueAsNumber,
39+
value,
5740
this.props.value
5841
);
5942
}
6043

6144
UNSAFE_componentWillMount() {
62-
if (this.props.type !== 'number') {
63-
this.setState({value: this.props.value});
64-
}
45+
this.setState({value: this.props.value})
6546
}
6647

6748
render() {
68-
const valprops =
69-
this.props.type === 'number' ? {} : {value: this.state.value};
70-
const {loading_state} = this.props;
49+
const valprops = {value: this.state.value}
7150
return (
7251
<input
73-
data-dash-is-loading={
74-
(loading_state && loading_state.is_loading) || undefined
75-
}
7652
ref={this.input}
7753
onChange={this.onChange}
7854
onKeyPress={this.onKeyPress}
7955
{...valprops}
8056
{...omit(
8157
[
82-
'debounce',
8358
'value',
84-
'n_submit',
85-
'n_submit_timestamp',
86-
'selectionDirection',
87-
'selectionEnd',
88-
'selectionStart',
8959
'setProps',
90-
'loading_state',
9160
],
9261
this.props
9362
)}
@@ -96,62 +65,34 @@ export default class MyPersistedComponent extends PureComponent {
9665
}
9766

9867
setInputValue(base, value) {
99-
const __value = value;
100-
base = this.input.current.checkValidity() ? convert(base) : NaN;
101-
value = convert(value);
68+
base = NaN;
10269

10370
if (!isEquivalent(base, value)) {
104-
this.input.current.value = isNumber(value) ? value : __value;
71+
this.input.current.value = value
10572
}
10673
}
10774

10875
setPropValue(base, value) {
109-
base = convert(base);
110-
value = this.input.current.checkValidity() ? convert(value) : NaN;
111-
11276
if (!isEquivalent(base, value)) {
11377
this.props.setProps({value});
11478
}
11579
}
11680

11781
onEvent() {
11882
const {value} = this.input.current;
119-
const valueAsNumber = convert(value);
120-
if (this.props.type === 'number') {
121-
this.setPropValue(
122-
this.props.value,
123-
isNil(valueAsNumber) ? value : valueAsNumber
124-
);
125-
} else {
126-
this.props.setProps({value});
127-
}
83+
this.props.setProps({value})
12884
}
12985

13086
onKeyPress(e) {
131-
if (e.key === 'Enter') {
132-
this.props.setProps({
133-
n_submit: this.props.n_submit + 1,
134-
n_submit_timestamp: Date.now(),
135-
});
136-
this.input.current.checkValidity();
137-
}
138-
return this.props.debounce && e.key === 'Enter' && this.onEvent();
87+
return this.onEvent();
13988
}
14089

14190
onChange() {
142-
if (!this.props.debounce) {
143-
this.onEvent();
144-
} else if (this.props.type !== 'number') {
145-
this.setState({value: this.input.current.value});
146-
}
91+
this.onEvent()
14792
}
14893
}
14994

15095
MyPersistedComponent.defaultProps = {
151-
type: 'text',
152-
n_submit: 0,
153-
n_submit_timestamp: -1,
154-
debounce: false,
15596
persisted_props: ['value'],
15697
persistence_type: 'local',
15798
};
@@ -169,84 +110,16 @@ MyPersistedComponent.propTypes = {
169110
*/
170111
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
171112

172-
/**
173-
* The input's inline styles
174-
*/
175-
style: PropTypes.object,
176-
177-
/**
178-
* The class of the input element
179-
*/
180-
className: PropTypes.string,
181-
182-
/**
183-
* If true, changes to input will be sent back to the Dash server only on enter or when losing focus.
184-
* If it's false, it will sent the value back on every change.
185-
*/
186-
debounce: PropTypes.bool,
187-
188-
/**
189-
* The type of control to render.
190-
*/
191-
type: PropTypes.oneOf([
192-
// Only allowing the input types with wide browser compatibility
193-
'text',
194-
'number',
195-
'password',
196-
'email',
197-
'range',
198-
'search',
199-
'tel',
200-
'url',
201-
'hidden',
202-
]),
203-
204113
/**
205114
* The name of the control, which is submitted with the form data.
206115
*/
207116
name: PropTypes.string,
208117

209-
/**
210-
* A regular expression that the control's value is checked against. The pattern must match the entire value, not just some subset. Use the title attribute to describe the pattern to help the user. This attribute applies when the value of the type attribute is text, search, tel, url, email, or password, otherwise it is ignored. The regular expression language is the same as JavaScript RegExp algorithm, with the 'u' parameter that makes it treat the pattern as a sequence of unicode code points. The pattern is not surrounded by forward slashes.
211-
*/
212-
pattern: PropTypes.string,
213-
214-
/**
215-
* A hint to the user of what can be entered in the control . The placeholder text must not contain carriage returns or line-feeds. Note: Do not use the placeholder attribute instead of a <label> element, their purposes are different. The <label> attribute describes the role of the form element (i.e. it indicates what kind of information is expected), and the placeholder attribute is a hint about the format that the content should take. There are cases in which the placeholder attribute is never displayed to the user, so the form must be understandable without it.
216-
*/
217-
placeholder: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
218-
219-
/**
220-
* Number of times the `Enter` key was pressed while the input had focus.
221-
*/
222-
n_submit: PropTypes.number,
223-
/**
224-
* Last time that `Enter` was pressed.
225-
*/
226-
n_submit_timestamp: PropTypes.number,
227-
228118
/**
229119
* Dash-assigned callback that gets fired when the value changes.
230120
*/
231121
setProps: PropTypes.func,
232122

233-
/**
234-
* Object that holds the loading state object coming from dash-renderer
235-
*/
236-
loading_state: PropTypes.shape({
237-
/**
238-
* Determines if the component is loading or not
239-
*/
240-
is_loading: PropTypes.bool,
241-
/**
242-
* Holds which property is loading
243-
*/
244-
prop_name: PropTypes.string,
245-
/**
246-
* Holds the name of the component that is loading
247-
*/
248-
component_name: PropTypes.string,
249-
}),
250123

251124
/**
252125
* Used to allow user interactions in this component to be persisted when

0 commit comments

Comments
 (0)