Skip to content

PersistenceTransforms for date in datePickerRange and datePickerSingle #1376

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 40 commits into from
Sep 4, 2020
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
56f0a0e
add logic for checking for persisted prop in propName and propPart
harryturr Aug 18, 2020
4f07812
add persistence checking defining date picker single inside of callback
harryturr Aug 20, 2020
4479682
add persistence test checking defining date picker range inside of ca…
harryturr Aug 20, 2020
6937f1c
lint
harryturr Aug 21, 2020
65938ac
remove comments / sleep
harryturr Aug 21, 2020
94b91c7
rename functions
harryturr Aug 21, 2020
951a767
update circleci config to point to dcc branch
harryturr Aug 21, 2020
b180d00
Merge branch 'dev' into persistence-hg
harryturr Aug 21, 2020
a5e0874
Merge branch 'dev' into persistence-hg
harryturr Aug 25, 2020
54f6229
Merge branch 'dev' into persistence-hg
harryturr Aug 25, 2020
9b60d4a
update circleci to new dcc branch
harryturr Aug 25, 2020
59bbf30
Update .circleci/config.yml
harryturr Aug 26, 2020
900af6f
Update .circleci/config.yml
harryturr Aug 26, 2020
109ea46
typo
harryturr Aug 26, 2020
fe58b2a
functions for interaction with date pickers, rm print statements
harryturr Aug 26, 2020
91c5324
simplify check for PropName
harryturr Aug 26, 2020
5c11477
add dash-generator-test-component-persisted
harryturr Aug 27, 2020
1f04bc5
remove date picker tests
harryturr Aug 27, 2020
424b6dd
rm obsolete imports
harryturr Aug 28, 2020
0cb4d02
update test persisted component
harryturr Aug 31, 2020
9c5e33d
add test components for persisted props and nested persisted props
harryturr Aug 31, 2020
963031b
add persistenceTransforms test for prop and nested prop
harryturr Sep 1, 2020
4c47006
add build for test compenent in package.json
harryturr Sep 1, 2020
95af752
Merge branch 'dev' into persistence-hg
harryturr Sep 1, 2020
738a9ec
simplify test component props and dependencies
harryturr Sep 1, 2020
0b97608
Merge branch 'dev' into persistence-hg
harryturr Sep 1, 2020
e541e62
add build for MyPersistedComponentNested
harryturr Sep 1, 2020
3c3efbf
update name
harryturr Sep 2, 2020
1586634
Merge branch 'dev' into persistence-hg
harryturr Sep 2, 2020
b063d60
add test persistence components to @plotly/dash-test-components
harryturr Sep 2, 2020
e2cab41
update package.json
harryturr Sep 2, 2020
06d77d3
rm old persisted test components
harryturr Sep 2, 2020
2b240e2
update imports for test_persistence
harryturr Sep 2, 2020
e4026e6
remove old r builds
harryturr Sep 2, 2020
5601ec3
update component comment description
harryturr Sep 2, 2020
1596d43
Merge branch 'dev' into persistence-hg
harryturr Sep 3, 2020
37d454f
remove unnecessary props from test components
harryturr Sep 4, 2020
f7c04fd
remove dcc branch from ci
harryturr Sep 4, 2020
6f2ce30
edit code style with conditional chaining
harryturr Sep 4, 2020
b5f78b3
update CHANGELOG.md
harryturr Sep 4, 2020
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
6 changes: 3 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ jobs:
command: |
. venv/bin/activate && pip install --no-cache-dir --upgrade -e . --progress-bar off && mkdir packages
cd dash-renderer && renderer build && python setup.py sdist && mv dist/* ../packages/ && cd ..
git clone --depth 1 https://github.com/plotly/dash-core-components.git
git clone -b hg-700-persistence --depth 1 https://github.com/plotly/dash-core-components.git
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should be unnecessary now that we test against a custom-made component

cd dash-core-components && npm ci && npm run build && python setup.py sdist && mv dist/* ../packages/ && cd ..
ls -la packages
- persist_to_workspace:
Expand Down Expand Up @@ -205,7 +205,7 @@ jobs:
command: |
source venv/Scripts/activate && pip install --no-cache-dir --upgrade -e . --progress-bar off && mkdir packages
cd dash-renderer && renderer build && python setup.py sdist && mv dist/* ../packages/ && cd ..
git clone --depth 1 https://github.com/plotly/dash-core-components.git
git clone -b hg-700-persistence --depth 1 https://github.com/plotly/dash-core-components.git
cd dash-core-components && npm ci && npm run build && python setup.py sdist && mv dist/* ../packages/ && cd ..
ls -la packages
- persist_to_workspace:
Expand Down Expand Up @@ -234,7 +234,7 @@ jobs:
pip install --no-cache-dir --upgrade -e .[dev,testing] --progress-bar off
git clone --depth 1 https://github.com/plotly/dashR.git -b dev dashR
git clone --depth 1 https://github.com/plotly/dash-html-components.git
git clone --depth 1 https://github.com/plotly/dash-core-components.git
git clone -b hg-700-persistence --depth 1 https://github.com/plotly/dash-core-components.git
git clone --depth 1 https://github.com/plotly/dash-table.git
shopt -s extglob
cd dash-html-components; npm ci && npm run build; rm -rf !(.|..|DESCRIPTION|LICENSE.txt|LICENSE|NAMESPACE|.Rbuildignore|R|man|inst|vignettes|build)
Expand Down
293 changes: 293 additions & 0 deletions @plotly/dash-test-components/src/components/MyPersistedComponent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,293 @@
import React, {PureComponent} from 'react';
import PropTypes from 'prop-types';

// simple function to substitute is-numeric for our use case
const isNumber = (n) => {
return !isNaN(parseFloat(n)) && isFinite(n);
}

// eslint-disable-next-line no-implicit-coercion
const convert = val => (isNumber(val) ? +val : NaN);

const isEquivalent = (v1, v2) => v1 === v2 || (isNaN(v1) && isNaN(v2));

// using these inline functions instead of ramda
const isNil = val => val == null

const omit = (key, obj) => {
const { [key]: omitted, ...rest } = obj;
return rest;
}

/**
* Adapted dcc input component for persistence tests.
*
* Note that some unnecessary props have been removed.
*/
export default class MyPersistedComponent extends PureComponent {
constructor(props) {
super(props);

this.input = React.createRef();

this.onChange = this.onChange.bind(this);
this.onEvent = this.onEvent.bind(this);
this.onKeyPress = this.onKeyPress.bind(this);
this.setInputValue = this.setInputValue.bind(this);
this.setPropValue = this.setPropValue.bind(this);
}

UNSAFE_componentWillReceiveProps(nextProps) {
const {value} = this.input.current;
const valueAsNumber = convert(value);
this.setInputValue(
isNil(valueAsNumber) ? value : valueAsNumber,
nextProps.value
);
if (this.props.type !== 'number') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stuff like number support can be 🔪 from the test component

this.setState({value: nextProps.value});
}
}

componentDidMount() {
const {value} = this.input.current;
const valueAsNumber = convert(value);
this.setInputValue(
isNil(valueAsNumber) ? value : valueAsNumber,
this.props.value
);
}

UNSAFE_componentWillMount() {
if (this.props.type !== 'number') {
this.setState({value: this.props.value});
}
}

render() {
const valprops =
this.props.type === 'number' ? {} : {value: this.state.value};
const {loading_state} = this.props;
return (
<input
data-dash-is-loading={
(loading_state && loading_state.is_loading) || undefined
}
ref={this.input}
onChange={this.onChange}
onKeyPress={this.onKeyPress}
{...valprops}
{...omit(
[
'debounce',
'value',
'n_submit',
'n_submit_timestamp',
'selectionDirection',
'selectionEnd',
'selectionStart',
'setProps',
'loading_state',
],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we had discussed, this is copied over from dcc.Input. A lot of these props are not really useful for the test either. Try and keep the surface area for this component to the minimum you need to do the tests you need, nothing more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this.props
)}
/>
);
}

setInputValue(base, value) {
const __value = value;
base = this.input.current.checkValidity() ? convert(base) : NaN;
value = convert(value);

if (!isEquivalent(base, value)) {
this.input.current.value = isNumber(value) ? value : __value;
}
}

setPropValue(base, value) {
base = convert(base);
value = this.input.current.checkValidity() ? convert(value) : NaN;

if (!isEquivalent(base, value)) {
this.props.setProps({value});
}
}

onEvent() {
const {value} = this.input.current;
const valueAsNumber = convert(value);
if (this.props.type === 'number') {
this.setPropValue(
this.props.value,
isNil(valueAsNumber) ? value : valueAsNumber
);
} else {
this.props.setProps({value});
}
}

onKeyPress(e) {
if (e.key === 'Enter') {
this.props.setProps({
n_submit: this.props.n_submit + 1,
n_submit_timestamp: Date.now(),
});
this.input.current.checkValidity();
}
return this.props.debounce && e.key === 'Enter' && this.onEvent();
}

onChange() {
if (!this.props.debounce) {
this.onEvent();
} else if (this.props.type !== 'number') {
this.setState({value: this.input.current.value});
}
}
}

MyPersistedComponent.defaultProps = {
type: 'text',
n_submit: 0,
n_submit_timestamp: -1,
debounce: false,
persisted_props: ['value'],
persistence_type: 'local',
};

MyPersistedComponent.propTypes = {
/**
* The ID of this component, used to identify dash components
* in callbacks. The ID needs to be unique across all of the
* components in an app.
*/
id: PropTypes.string,

/**
* The value of the input
*/
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),

/**
* The input's inline styles
*/
style: PropTypes.object,

/**
* The class of the input element
*/
className: PropTypes.string,

/**
* If true, changes to input will be sent back to the Dash server only on enter or when losing focus.
* If it's false, it will sent the value back on every change.
*/
debounce: PropTypes.bool,

/**
* The type of control to render.
*/
type: PropTypes.oneOf([
// Only allowing the input types with wide browser compatibility
'text',
'number',
'password',
'email',
'range',
'search',
'tel',
'url',
'hidden',
]),

/**
* The name of the control, which is submitted with the form data.
*/
name: PropTypes.string,

/**
* 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.
*/
pattern: PropTypes.string,

/**
* 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.
*/
placeholder: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),

/**
* Number of times the `Enter` key was pressed while the input had focus.
*/
n_submit: PropTypes.number,
/**
* Last time that `Enter` was pressed.
*/
n_submit_timestamp: PropTypes.number,

/**
* Dash-assigned callback that gets fired when the value changes.
*/
setProps: PropTypes.func,

/**
* Object that holds the loading state object coming from dash-renderer
*/
loading_state: PropTypes.shape({
/**
* Determines if the component is loading or not
*/
is_loading: PropTypes.bool,
/**
* Holds which property is loading
*/
prop_name: PropTypes.string,
/**
* Holds the name of the component that is loading
*/
component_name: PropTypes.string,
}),

/**
* Used to allow user interactions in this component to be persisted when
* the component - or the page - is refreshed. If `persisted` is truthy and
* hasn't changed from its previous value, a `value` that the user has
* changed while using the app will keep that change, as long as
* the new `value` also matches what was given originally.
* Used in conjunction with `persistence_type`.
*/
persistence: PropTypes.oneOfType([
PropTypes.bool,
PropTypes.string,
PropTypes.number,
]),

/**
* Properties whose user interactions will persist after refreshing the
* component or the page. Since only `value` is allowed this prop can
* normally be ignored.
*/
persisted_props: PropTypes.arrayOf(PropTypes.oneOf(['value'])),

/**
* Where persisted user changes will be stored:
* memory: only kept in memory, reset on page refresh.
* local: window.localStorage, data is kept after the browser quit.
* session: window.sessionStorage, data is cleared once the browser quit.
*/
persistence_type: PropTypes.oneOf(['local', 'session', 'memory']),
};

MyPersistedComponent.persistenceTransforms = {
value: {

extract: propValue => {
if (!(propValue === null || propValue === undefined)) {
return propValue.toUpperCase();
}
return propValue;
},
apply: storedValue => storedValue,

},
};
Loading