Skip to content

Instantly share code, notes, and snippets.

View chengjianhua's full-sized avatar
💦

Jianhua Cheng chengjianhua

💦
View GitHub Profile
@chengjianhua
chengjianhua / Enhance.js
Created June 10, 2016 11:27 — forked from sebmarkbage/Enhance.js
Higher-order Components
import { Component } from "React";
export var Enhance = ComposedComponent => class extends Component {
constructor() {
this.state = { data: null };
}
componentDidMount() {
this.setState({ data: 'Hello' });
}
render() {
@chengjianhua
chengjianhua / simple-fuzzy-search.js
Last active September 18, 2016 08:42
fuzzy search
const data = this.data.slice();
const searchText = this.getSearchText();
const columnKeys = Object.keys(this.getColumns());
const reg = new RegExp(searchText.toLocaleLowerCase().split('').join('.*?'), ['g']);
const result = data.filter((value) => {
return columnKeys.some((key) => {
return reg.exec(String(value[key]).toLocaleLowerCase());
});
});
@chengjianhua
chengjianhua / Store.js
Last active August 18, 2016 03:30
sort according to the specified field in object
function _sort({array, sortField, order}) {
order = order.toLowerCase();
const isAsc = order === Order.ASC;
return array.sort((a, b) => {
let valueA = a[sortField] === null ? '' : a[sortField];
let valueB = b[sortField] === null ? '' : b[sortField];
if(!isAsc) {
const temp = valueA; valueA = valueB; valueB = temp;
@chengjianhua
chengjianhua / DataTable.jsx
Created August 18, 2016 03:31
React.cloneElement usage
React.cloneElement(child, {
onSort: this.handleSort.bind(this, field),
orderStatus: this.state.orderStatus
});
@chengjianhua
chengjianhua / React.Chindren.jsx
Last active September 18, 2016 08:42
React.Children usage
React.Children.map(this.props.children, (child, index) => {
const props = child.props;
return Object.assign({}, child.props, {index});
});
@chengjianhua
chengjianhua / compute-pagination-buttons.js
Last active September 18, 2016 08:41
compute pagination buttons
function range(len, start) {
let end = 0;
const out = [];
if (start === undefined) {
start = 0;
end = len;
} else {
end = start;
@chengjianhua
chengjianhua / disable-select-by-css.css
Last active September 18, 2016 08:40
disable selection
.disable-select {
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
@chengjianhua
chengjianhua / Data.js
Created September 14, 2016 05:50
update list item in immutable list
import {List, Map} from 'immutable';
import {ActionTypes} from '../constants/Constants';
const defaultState = new Map({
dataList: new Map({
currentAccount: '',
currentPath: new List(),
data: new List(),
isLoading: false
@chengjianhua
chengjianhua / DataTableBody.jsx
Created September 18, 2016 08:22
React check clicked target is whether contained in a another node.
handleOutSideClick(event) {
const {onRowUnSelected} = this.props;
const isDescendantOfRoot = ReactDOM.findDOMNode(this.tbody).contains(event.target);
if (!isDescendantOfRoot) {
event.stopPropagation();
this.setState({
selectedRowIndex: -1
}, () => {
@chengjianhua
chengjianhua / UploadFileModal.jsx
Created September 19, 2016 10:09
Read file when select file
const fileReader = new FileReader();
fileReader.readAsText(file);
fileReader.onloadend = (fileEvent) => {
const result = fileEvent.target.result;
console.log(result);
};