Skip to content

Commit

Permalink
update 170325 22:01
Browse files Browse the repository at this point in the history
  • Loading branch information
SPxiaomin committed Mar 25, 2017
1 parent 9afe92a commit a50bbc7
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 46 deletions.
1 change: 0 additions & 1 deletion react-tab-selector/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ class App extends Component {
this.state = {
selected: this.props.selected
};

this.handleOnClick = this.handleOnClick.bind(this);
}

Expand Down
9 changes: 9 additions & 0 deletions react-tab-selector/src/data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const Data = [
{name: 'Red', value: 'red'},
{name: 'Blue', value: 'blue'},
{name: 'Yellow', value: 'yellow'},
{name: 'Green', value: 'green'},
{name: 'White', value: 'white'}
];

export { Data };
11 changes: 2 additions & 9 deletions react-tab-selector/src/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

var data = [
{name: 'Red', value: 'red'},
{name: 'Blue', value: 'blue'},
{name: 'Yellow', value: 'yellow'},
{name: 'Green', value: 'green'},
{name: 'White', value: 'white'}
];
import { Data } from './data';

ReactDOM.render(
<App label="Color" data={data} selected={null} />,
<App label="Color" data={Data} selected={null} />,
document.getElementById('root')
);
44 changes: 24 additions & 20 deletions react-tab-selector/tab-selector-jquery/index-1.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<body>
<div id="container"></div>
<script>
var data = [
let data = [
{name: 'Red', value: 'red'},
{name: 'Blue', value: 'blue'},
{name: 'Yellow', value: 'yellow'},
Expand All @@ -20,25 +20,29 @@
];

function render(options) {
var arr = [];
arr.push('<div class="tab-selector">');
arr.push('<label>', options.label, '</label>');
arr.push('<ul>');
options.data.forEach(function (item){
if (item.value === options.selected) {
arr.push('<li class="selected" data-value="', item.value, '">');
arr.push(item.name);
arr.push('</li>');
} else {
arr.push('<li data-value="', item.value, '">');
arr.push(item.name);
arr.push('</li>');
}
});
arr.push('</ul>');
arr.push('</div>');
let html = `
<div class="tab-selector">
<label>${options.label}</label>
<ul>
${
options.data.map((item) => {
let selected = item.value ===
options.selected ?
'selected' : '';
return `
<li class="${selected}"
data-value="${item.value}">
${item.name}
</li>
`;
}).join('')
}
</ul>
</div>
`;

this.html(arr.join(''));
this.html(html);
}

$.fn.TabSelector = function(options) {
Expand All @@ -48,7 +52,7 @@
// 交互
let that = this;
this.on('click', 'li', function() {
var $this = $(this);
let $this = $(this);

options.selected = $this.data('value');
render.call(that, options);
Expand Down
36 changes: 20 additions & 16 deletions react-tab-selector/tab-selector-jquery/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,39 @@
<body>
<div id="container"></div>
<script>
var data = [
let data = [
{name: 'Red', value: 'red'},
{name: 'Blue', value: 'blue'},
{name: 'Yellow', value: 'yellow'},
{name: 'Green', value: 'green'},
{name: 'White', value: 'white'}
];

// TODO: 使用 es6 改写
$.fn.TabSelector = function(options) {
// DOM 构建
var arr = [];
arr.push('<div class="tab-selector">');
arr.push('<label>', options.label, '</label>');
arr.push('<ul>');
options.data.forEach(function(item) {
arr.push('<li data-value="', item.value, '">');
arr.push(item.name);
arr.push('</li>')
});
arr.push('</ul>');
arr.push('</div>');
let html = `
<div class="tab-selector">
<label>${options.label}</label>
<ul>
${
options.data.map((item) => {
return `
<li data-value="${item.value}">
${item.name}
</li>
`;
}).join('')
}
</ul>
</div>
`;

this.html(arr.join(''));
this.html(html);

// 交互
var lastSelected = null;
let lastSelected = options.selected;
this.on('click', 'li', function() {
var $this = $(this);
let $this = $(this);

if (lastSelected) {
lastSelected.removeClass('selected');
Expand Down

0 comments on commit a50bbc7

Please sign in to comment.