GithubHelp home page GithubHelp logo

ansont / react-freezer-link Goto Github PK

View Code? Open in Web Editor NEW
0.0 3.0 0.0 13 KB

A React mixin for linking form fields to a deep structure of data within the component's prop which provided by Freezer.

License: MIT License

JavaScript 100.00%

react-freezer-link's Introduction

NPM

react-freezer-link

A React mixin for linking form fields to a deep structure of data within the component's prop which provided by Freezer.

Description

This mixin is a substitute for the standard React mixin React.addons.LinkedStateMixin. While the standard mixin only allows you to link a form field to a key directly within the component's state, this mixin allows you to link a form field to a key deeper in the component's prop object which provided by Freezer.

Getting Started

To install from npm, run:

npm install --save react-freezer-link

Then include the mixin in the component that will use it:

import FreezerLinkMixin from 'react-freezer-link';

...

var MyComponent = React.createClass({
	mixins: [FreezerLinkMixin],
	
	...
});

or

import FreezerLinkDecorator from 'react-freezer-link/lib/decorator';

...

@FreezerLinkDecorator
class MyComponent extends React.Component {
	...
}

Usage Examples

Link a text field to this.props.data.user.name:

<input type="text" valueLink={this.linkProp('data.user.name')} />

In React v15 valueLink is deprecated "due to very low popularity", because of this there are other variant of usage:

<input type="text"     {...this.valueLinkToProp('data.user.name')} />
<input type="checkbox" {...this.checkedLinkToProp('data.user.onDiet')} />

Link a text field to this.props.data.user.name, translating an empty string in the text field to null in the state and vice-versa:

<input type="text" valueLink={this.linkProp('data.user.name', {storeEmptyStringAsNull: true})} />

In both cases above you can add callback:

<input type="text" valueLink={this.linkProp('data.user.name', (newVale) => {...})} />
<input type="text" valueLink={this.linkProp('data.user.name', {storeEmptyStringAsNull: true}, (newVale) => {...})} />

Furthermore you can set context:

items.map(user => (
	<input type="text" valueLink={this.linkProp('name', {context: user})} />
))

In options you can set mutator function:

<input type="text" {...this.valueLinkToProp('data.user.name', {mutator: _ => _.toUpperCase()})} />

Also you can define global configs:

MyComponent.freezerLinkConfig = {
	storeEmptyStringAsNull: true
};
// or
class MyComponent extends React.Component {
	
	static freezerLinkConfig = {
		storeEmptyStringAsNull: true
	};

	...
}

react-freezer-link's People

Contributors

dangreen avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.