@@ -8,7 +8,7 @@ A babel plugin that provides some directives for react(any JSX), similar to dire
88[ ![ npm] ( https://img.shields.io/npm/v/babel-plugin-react-directives.svg )] ( https://www.npmjs.com/package/babel-plugin-react-directives )
99[ ![ GitHub] ( https://img.shields.io/github/license/mashape/apistatus.svg )] ( https://github.com/peakchen90/babel-plugin-react-directives/blob/master/LICENSE )
1010
11- > 🇨🇳 [ ** 中文文档** ] ( ./README.ZH-CN.md )
11+ > [ ** 中文文档** ] ( ./README.ZH-CN.md )
1212
1313## Table of Contents
1414- [ Usage] ( #toc-usage )
@@ -22,6 +22,7 @@ A babel plugin that provides some directives for react(any JSX), similar to dire
2222 - [ x-for] ( #toc-directives-x-for )
2323 - [ x-model] ( #toc-directives-x-model )
2424 - [ x-model-hook] ( #toc-directives-x-model-hook )
25+ - [ x-class] ( #toc-directives-x-class )
2526- [ Related Packages] ( #toc-related-packages )
2627- [ Known Issues] ( #toc-known-issues )
2728- [ CHANGELOG] ( #toc-changeloog )
@@ -137,7 +138,7 @@ const foo = (
137138)
138139```
139140
140- Of course, it will also merge other ` style ` by calling the [ runtime function ] ( ./lib/ runtime.js ) , for example:
141+ Of course, it will also merge other ` style ` props by calling the [ mergeProps method ] ( ./runtime/merge-props .js ) , for example:
141142``` jsx harmony
142143const foo = (
143144 < div
@@ -155,7 +156,7 @@ const foo = (
155156 < div
156157 {... extraProps}
157158 style= {{
158- ... require ( " babel-plugin-react-directives/lib/runtime " ). mergeProps .call (this , " style" , [
159+ ... mergeProps .call (this , " style" , [
159160 { style: { color: ' red' } },
160161 extraProps
161162 ]),
@@ -228,7 +229,7 @@ const foo = (
228229
229230### <span id =" toc-directives-x-model " >x-model</span >
230231The ` x-model ` is a syntax sugar similar to vue ` v-model ` , which binds a state to the ` value ` prop of the ** form element** and automatically updates the state when the element is updated.
231- It resolves the updated value by calling the [ runtime function ] ( ./lib/ runtime.js ) (If the first argument ` arg ` is non-empty, and ` arg.target ` is an object, return ` arg.target.value ` , otherwise return ` arg ` ).
232+ It resolves the updated value by calling the [ resolveValue method ] ( ./runtime/resolve-value .js ) (If the first argument ` arg ` is non-empty, and ` arg.target ` is an object, return ` arg.target.value ` , otherwise return ` arg ` ).
232233
233234** Example:**
234235``` jsx harmony
@@ -255,7 +256,7 @@ class Foo extends React.Component {
255256 render () {
256257 return (
257258 < input value= {this .state .data } onChange= {(... _args ) => {
258- let _value = require ( " babel-plugin-react-directives/lib/runtime " ). resolveValue (_args);
259+ let _value = resolveValue (_args);
259260
260261 this .setState (_prevState => {
261262 return { data: _value };
@@ -266,7 +267,7 @@ class Foo extends React.Component {
266267}
267268```
268269
269- When there are other ` onChange ` props, merge them by calling the [ runtime function ] ( ./lib/ runtime.js ) :
270+ When there are other ` onChange ` props, merge them by calling the [ invokeOnchange method ] ( ./runtime/invoke-onchange .js ) :
270271``` jsx harmony
271272class Foo extends React .Component {
272273 constructor (props ) {
@@ -308,13 +309,13 @@ class Foo extends React.Component {
308309 {... this .props }
309310 value= {this .state .data }
310311 onChange= {(... _args ) => {
311- let _value = require ( " babel-plugin-react-directives/lib/runtime " ). resolveValue (_args);
312+ let _value = resolveValue (_args);
312313
313314 this .setState (_prevState => {
314315 return { data: _value };
315316 });
316317
317- require ( " babel-plugin-react-directives/lib/runtime " ). invokeExtraOnChange .call (this , _args, [
318+ invokeOnchange .call (this , _args, [
318319 { onChange: this .onChange .bind (this ) },
319320 this .props
320321 ]);
@@ -359,7 +360,7 @@ class Foo extends React.Component {
359360 < input
360361 value= {data .text }
361362 onChange= {(... _args ) => {
362- let _value = require ( " babel-plugin-react-directives/lib/runtime " ). resolveValue (_args);
363+ let _value = resolveValue (_args);
363364
364365 this .setState (_prevState => {
365366 let _val = {
@@ -394,7 +395,7 @@ function Foo() {
394395 < input
395396 value= {data}
396397 onChange= {(... _args ) => {
397- let _value = require ( " babel-plugin-react-directives/lib/runtime " ). resolveValue (_args);
398+ let _value = resolveValue (_args);
398399
399400 setData (_value);
400401 }}
@@ -406,6 +407,51 @@ function Foo() {
406407** Note** : If you use [ ** ESLint** ] ( https://eslint.org ) , you may receive an error that ` setData ` is defined but never used.
407408Please install [ ** eslint-plugin-react-directives** ] ( https://github.com/peakchen90/eslint-plugin-react-directives ) plugin to solve it.
408409
410+ ### <span id =" toc-directives-x-class " >x-class</span >
411+
412+ > * New in 1.1.0*
413+
414+ The ` x-class ` for conditionally joining classNames together by [ classnames] ( https://github.com/JedWatson/classnames ) , and it is useful for dynamically generating className.
415+ Usage is the same as [ classnames] ( https://github.com/JedWatson/classnames ) , the binding value will be passed as a parameter to the [ ` classNames ` ] ( https://github.com/JedWatson/classnames#usage ) method.
416+
417+ ** Example:**
418+ ``` jsx harmony
419+ const foo = < div x- class = {{ abc: true , def: false }}>
420+ ```
421+
422+ ** Convert to:**
423+ ``` jsx harmony
424+ const foo = < div className= {classNames ({ abc: true , def: false })}>
425+ // className="abc"
426+ ```
427+ ** Note** : ` classNames ` method references [ runtime/classnames.js] ( ./runtime/classnames.js ) .
428+
429+ Of course, it will also merge other ` className ` props, for example:
430+ ** Example:**
431+ ``` jsx harmony
432+ const foo = < div x- class = {{ abc: true , def: false }} className= " xyz" >
433+ ```
434+
435+ will be converted to:
436+ ``` jsx harmony
437+ const foo = < div className= {classNames ([" xyz" , { abc: true , def: false }])}>
438+ // className="xyz abc"
439+ ```
440+
441+ The ` x-class ` can also be used with [ css-modules] ( https://github.com/css-modules/css-modules ) , for example:
442+ ``` jsx harmony
443+ import styles from ' ./style.css' ;
444+
445+ const foo = (
446+ < div
447+ className= {styles .foo }
448+ x- class = {{
449+ [styles .bar ]: true ,
450+ [styles .qux ]: false
451+ }}
452+ / >
453+ )
454+ ```
409455
410456## <span id =" toc-related-packages " >Related Packages</span >
411457- [ eslint-plugin-react-directives] ( https://github.com/peakchen90/eslint-plugin-react-directives )
0 commit comments