Skip to content

Commit

Permalink
feat: implement <FocusSensor>
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Feb 13, 2018
1 parent 48329ad commit a6120e9
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/FocusSensor/index.ts
Original file line number Diff line number Diff line change
@@ -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 IFocusSensorProps {
bond?: boolean | string;
}

export const FocusSensor: React.StatelessComponent<IFocusSensorProps> = (props: IFocusSensorProps) => {
let {bond} = props;

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

return renderProp(props, {
isActive: value,
[bond]: {
onFocus: () => set(true),
onBlur: () => set(false),
}
});
} else {
const element = renderProp(props, {
isActive: value
});

return cloneElement(element, {
onFocus: () => set(true),
onBlur: () => set(false),
});
}
}
});
};

const FocusSensorWithBond = (props) => h(FocusSensor, {
bond: true,
...props
});

export const withFocus = faccToHoc(FocusSensorWithBond, 'focus');

0 comments on commit a6120e9

Please sign in to comment.