-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexperimentalPlugins.js
More file actions
77 lines (68 loc) · 1.66 KB
/
experimentalPlugins.js
File metadata and controls
77 lines (68 loc) · 1.66 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import React from 'react'
import Plugin from './Plugin'
import propTypes from './propTypes'
import util from './util'
const ucfirst = (s) => {
if (s && s.length) {
return s.charAt(0).toUpperCase() + s.substr(1)
}
return s
}
const lcfirst = (s) => {
if (s && s.length) {
return s.charAt(0).toLowerCase() + s.substr(1)
}
}
// FIXME: naive implementation
const ucword = (s) => {
if (s) {
return s.split(' ').map((word) => {
return ucfirst(word)
}).join(' ')
}
return s
}
export default [
// Text
new Plugin(
'textTransform',
({context, prop, props, state, options}) => {
// Inherited from the parent context
if (!prop && context) {
prop = context.textTransform
}
const transformer = (prop, s) => {
switch(prop) {
case 'uppercase':
s = s.toUpperCase()
break;
case 'lowercase':
s = s.toLowerCase()
break;
case 'capitalize':
s = ucword(s)
break;
}
return s
}
const it = (children) => {
if (util.isString(children)) {
children = transformer(prop, children)
}
return children
}
if (prop && options.supportsText) {
let {children} = props
children = it(children)
children = React.Children.map(children, (child) => {
return it(child)
})
// NOTE: we push children on to the state
// NOTE: the HOC will prefer children in state
// NOTE: to the original children in props
state.children = children
}
},
{propType: propTypes.textTransform}
),
]