Skip to content

Commit

Permalink
feat: implement <ActiveSensor>
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Feb 13, 2018
1 parent 936f99b commit 48329ad
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/ActiveSensor/index.ts
@@ -0,0 +1,47 @@
import {Component, cloneElement} from 'react';
import {h} from '../util';
import {Value} from '../Value';
import renderProp from '../util/renderProp';
import faccToHoc from '../util/faccToHoc';

export interface IActiveSensorProps {
bond?: boolean | string;
}

export const ActiveSensor = (props: IActiveSensorProps) => {
let {bond} = props;

return Value({
render: ({value, set}) => {
if (bond) {
if (typeof bond === 'boolean') {
bond = 'bond';
}

return renderProp(this.props, {
isActive: value,
[bond]: {
onMouseDown: () => value(false),
onMouseUp: () => value(true),
}
});
} else {
const element = renderProp(this.props, {
isActive: value
});

return cloneElement(element, {
onMouseDown: () => value(false),
onMouseUp: () => value(true),
});
}
}
});
};

const ActiveSensorWithBond = (props) => h(ActiveSensor, {
bond: true,
...props
});

export const withActive = faccToHoc(ActiveSensorWithBond, 'active');

0 comments on commit 48329ad

Please sign in to comment.