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

V2 - remove keepValue in favour of a metaAsParcel()-based helper #265

Open
dxinteractive opened this issue Jan 3, 2020 · 0 comments
Open

Comments

@dxinteractive
Copy link
Collaborator

Modifying values halfway through a parcel chain can be fine (e.g. turning all chars to uppercase), but the idea breaks down when there are more input values than can be represented in the data type in state (e.g. number in state -> string for editing), because invalid values must be remembered somewhere so that the input doesn't immediately eliminate the user's changes. The keepValue prop on ParcelBoundary tried to solve this, but it has 2 massive downsides:

  • the state is stored in React state, so 2 fields editing the same data cannot share invalid states
  • its quite complex and adds a lot of code to the react-dataparcels package

Instead its possible to store the user-input value as meta, and apply it to the value when its valid. It's very clear to understand, and all state is stored in the parcel. Something like:

const numberToString = (parcel) => parcel
    .modifyDown(asNode(node => {
        if('_asStringFrom' in node.meta && Object.is(node.meta._asStringFrom, node.value)) {
            return node;
        }
        let asString = node.value === undefined ? undefined : `${node.value}`;
        return node.setMeta({
            _asStringFrom: node.value,
            asString
        });
    }))
    .modifyUp(asNode(node => {
        let newValue = Number(node.meta.asString);
        return isNaN(newValue) ? node : node.update(() => newValue);
    }))
    .metaAsParcel('asString');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant