Skip to content
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

PersistenceTransforms for date in datePickerRange and datePickerSingle #1376

Merged
merged 40 commits into from
Sep 4, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
166 changes: 166 additions & 0 deletions @plotly/dash-test-components/src/components/MyPersistedComponent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
import React, {PureComponent} from 'react';
import PropTypes from 'prop-types';

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

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

/**
* Adapted dcc input component for persistence tests.
*
* Note that 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;
this.setInputValue(
value,
nextProps.value
);
this.setState({value: nextProps.value});
}

componentDidMount() {
const {value} = this.input.current;
this.setInputValue(
value,
this.props.value
);
}

UNSAFE_componentWillMount() {
this.setState({value: this.props.value})
}

render() {
const valprops = {value: this.state.value}
return (
<input
ref={this.input}
onChange={this.onChange}
onKeyPress={this.onKeyPress}
{...valprops}
{...omit(
[
'value',
'setProps',
],
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) {
base = NaN;

if (!isEquivalent(base, value)) {
this.input.current.value = value
}
}

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

onEvent() {
const {value} = this.input.current;
this.props.setProps({value})
}

onKeyPress(e) {
return this.onEvent();
}

onChange() {
this.onEvent()
}
}

MyPersistedComponent.defaultProps = {
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 name of the control, which is submitted with the form data.
*/
name: PropTypes.string,

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


/**
* 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,

},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
import React, {PureComponent} from 'react';
import PropTypes from 'prop-types';

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

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

/**
* Adapted dcc input component for persistence tests.
*
* Note that unnecessary props have been removed.
*/
export default class MyPersistedComponentNested 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;
this.setInputValue(
value,
nextProps.value
);
this.setState({value: nextProps.value});
}

componentDidMount() {
const {value} = this.input.current;
this.setInputValue(
value,
this.props.value
);
}

UNSAFE_componentWillMount() {
this.setState({value: this.props.value})
}

render() {
const valprops = {value: this.state.value}
return (
<input
ref={this.input}
onChange={this.onChange}
onKeyPress={this.onKeyPress}
{...valprops}
{...omit(
[
'value',
'setProps',
],
this.props
)}
/>
);
}

setInputValue(base, value) {
base = NaN;

if (!isEquivalent(base, value)) {
this.input.current.value = value
}
}

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

onEvent() {
const {value} = this.input.current;
this.props.setProps({value})
}

onKeyPress(e) {
return this.onEvent();
}

onChange() {
this.onEvent()
}
}

MyPersistedComponentNested.defaultProps = {
persisted_props: ['value.nested_value'],
persistence_type: 'local',
};

MyPersistedComponentNested.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 name of the control, which is submitted with the form data.
*/
name: PropTypes.string,

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


/**
* 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.nested_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']),
};

MyPersistedComponentNested.persistenceTransforms = {
value: {

nested_value: {

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

}
},
};
5 changes: 4 additions & 1 deletion @plotly/dash-test-components/src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import StyledComponent from './components/StyledComponent';
import MyPersistedComponent from './components/MyPersistedComponent';
import MyPersistedComponentNested from './components/MyPersistedComponentNested';


export {
StyledComponent,
StyledComponent, MyPersistedComponent, MyPersistedComponentNested
};
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
All notable changes to `dash` will be documented in this file.
This project adheres to [Semantic Versioning](https://semver.org/).

## [UNRELEASED]
### Changed
- [#1376](https://github.com/plotly/dash/pull/1376) Extends the `getTransform` logic in the renderer to handle `persistenceTransforms` for both nested and non-nested persisted props. This was used to to fix [dcc#700](https://github.com/plotly/dash-core-components/issues/700) in conjunction with [dcc#854](https://github.com/plotly/dash-core-components/pull/854) by using persistenceTransforms to strip the time part of the datetime so that datepickers can persist when defined in callbacks.

## [1.16.0] - 2020-09-03
### Added
- [#1371](https://github.com/plotly/dash/pull/1371) You can now get [CSP `script-src` hashes](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/script-src) of all added inline scripts by calling `app.csp_hashes()` (both Dash internal inline scripts, and those added with `app.clientside_callback`) .
Expand Down
16 changes: 12 additions & 4 deletions dash-renderer/src/persistence.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,18 @@ const noopTransform = {
apply: (storedValue, _propValue) => storedValue
};

const getTransform = (element, propName, propPart) =>
propPart
? element.persistenceTransforms[propName][propPart]
: noopTransform;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Old logic checks for just propPart for persistenceTransforms, but here we add logic to check if either propName or propPart is the persisted prop.

const getTransform = (element, propName, propPart) => {
if (
element.persistenceTransforms &&
element.persistenceTransforms[propName]
) {
if (propPart) {
return element.persistenceTransforms[propName][propPart];
}
return element.persistenceTransforms[propName];
}
return noopTransform;
};

const getValsKey = (id, persistedProp, persistence) =>
`${stringifyId(id)}.${persistedProp}.${JSON.stringify(persistence)}`;
Expand Down
Loading