-
Notifications
You must be signed in to change notification settings - Fork 461
/
placeholderStyle.js
56 lines (46 loc) · 1.78 KB
/
placeholderStyle.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import React, { Component } from 'react';
import { Style } from 'radium';
function generateUniqueId() {
return Math.floor((Math.random() * 10000) + 1) + '-' + +Math.floor((Math.random() * 10000) + 1) + '-' + +Math.floor((Math.random() * 10000) + 1) + '-' + +Math.floor((Math.random() * 10000) + 1) + '-' + +Math.floor((Math.random() * 10000) + 1) + '-' + +Math.floor((Math.random() * 10000) + 1) + '-' + +Math.floor((Math.random() * 10000) + 1) + '-' +
Math.floor((Math.random() * 100000000000000));
}
function mapRules(selector, style) {
let styles = { 0: style };
if (style[':hover']) {
styles = { ...styles, ':hover': style[':hover'] };
delete styles[0][':hover'];
}
if (style[':active']) {
styles = { ...styles, ':active': style[':active'] };
delete styles[0][':active'];
}
if (style[':focus']) {
styles = { ...styles, ':focus': style[':focus'] };
delete styles[0][':focus'];
}
let rules = {};
for (var prop in styles) {
if (styles.hasOwnProperty(prop)) {
rules[`${selector} input${prop !== '0' ? prop : ''}::-webkit-input-placeholder`] = styles[prop];
rules[`${selector} input${prop !== '0' ? prop : ''}::-moz-placeholder`] = styles[prop];
rules[`${selector} input${prop !== '0' ? prop : ''}:-ms-input-placeholder`] = styles[prop];
rules[`${selector} input${prop !== '0' ? prop : ''}:placeholder`] = styles[prop];
}
}
return rules;
}
export default class extends Component {
constructor() {
super();
this._id = generateUniqueId();
}
render() {
const { children, placeholderStyle, ...props } = this.props;
return (
<div data-reactdesktopid={this._id} {...props}>
{children}
<Style rules={mapRules(`[data-reactdesktopid="${this._id}"]`, placeholderStyle)}/>
</div>
);
}
}