Skip to content

Commit

Permalink
feat: add first <ClassList> implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Mar 3, 2018
1 parent cf0f9a4 commit c5ef077
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "libreact",
"version": "0.13.0",
"version": "0.14.0",
"description": "React standard library",
"main": "lib/index.js",
"repository": {
Expand Down
67 changes: 67 additions & 0 deletions src/ClassNames/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import {Component} from 'react';
import {render} from 'react-universal-interface';
import {Inverted} from '../invert';
import {h} from '../util';

const equalSets = (a: any[], b: any[]) => {
if (a.length !== b.length) {
return false;
}

const A = [...a].sort();
const B = [...b].sort();

for (let i = 0; i < A.length; i++) {
if (A[i] !== B[i]) {
return false;
}
}

return true;
};

export interface IClassNamesProps {
el?: HTMLElement;
list: string[],
persist?: boolean;
}

export class ClassNames extends Component<IClassNamesProps, {}> {
componentDidMount () {
this.putList(this.props.list);
}

shouldComponentUpdate (props) {
return !equalSets(props.list, this.props.list);
}

componentDidUpdate (props) {
if (!props.persist) {
this.removeList(props.list, props.el);
}

this.putList(this.props.list);
}

componentWillUnmount () {
if (!this.props.persist) {
this.removeList(this.props.list);
}
}

putList (list: string[], el: HTMLElement = this.props.el) {
for (const className of list) {
el.classList.add(className);
}
}

removeList (list: string[], el: HTMLElement = this.props.el) {
for (const className of list) {
el.classList.remove(className);
}
}

render () {
return render(this.props, null);
}
}

0 comments on commit c5ef077

Please sign in to comment.