random local classNames

use styled-components className generator for that
This commit is contained in:
Sérgio Ramos 2016-12-09 14:56:34 +00:00
parent cc8a2f28ed
commit 18978abf08
4 changed files with 47 additions and 15 deletions

View File

@ -22,12 +22,12 @@
"lodash.isstring": "^4.0.1", "lodash.isstring": "^4.0.1",
"lodash.isundefined": "^3.0.1", "lodash.isundefined": "^3.0.1",
"param-case": "^2.1.0", "param-case": "^2.1.0",
"random-natural": "^1.0.3",
"react": "^15.4.1", "react": "^15.4.1",
"react-dom": "^15.4.1", "react-dom": "^15.4.1",
"react-icons": "^2.2.1", "react-icons": "^2.2.1",
"reduce-css-calc": "^1.3.0", "reduce-css-calc": "^1.3.0",
"styled-components": "^1.1.3", "styled-components": "^1.1.3"
"uuid": "^3.0.1"
}, },
"devDependencies": { "devDependencies": {
"@kadira/storybook": "^2.34.0", "@kadira/storybook": "^2.34.0",

View File

@ -2,7 +2,6 @@ const constants = require('../../shared/constants');
const fns = require('../../shared/functions'); const fns = require('../../shared/functions');
const React = require('react'); const React = require('react');
const Styled = require('styled-components'); const Styled = require('styled-components');
const uuid = require('uuid').v4;
const { const {
boxes, boxes,
@ -10,13 +9,18 @@ const {
} = constants; } = constants;
const { const {
remcalc remcalc,
rndId
} = fns; } = fns;
const { const {
default: styled default: styled
} = Styled; } = Styled;
const classNames = {
label: rndId()
};
const StyledLabel = styled.label` const StyledLabel = styled.label`
border-radius: ${boxes.borderRadius}; border-radius: ${boxes.borderRadius};
color: #464646; color: #464646;
@ -62,7 +66,7 @@ const StyledInput = styled.input`
display: none; display: none;
&:checked { &:checked {
& + .tgl-label { & + .${classNames.label} {
background: ${colors.confirmation}; background: ${colors.confirmation};
border: ${boxes.border.confirmed}; border: ${boxes.border.confirmed};
color: #FFFFFF; color: #FFFFFF;
@ -87,7 +91,7 @@ const Toggle = ({
checked, checked,
className, className,
defaultChecked, defaultChecked,
id = uuid(), id = rndId(),
style style
}) => { }) => {
return ( return (
@ -101,7 +105,7 @@ const Toggle = ({
id={id} id={id}
type='checkbox' type='checkbox'
/> />
<StyledToggleLabel className='tgl-label' /> <StyledToggleLabel className={classNames.label} />
</StyledLabel> </StyledLabel>
); );
}; };

View File

@ -1,27 +1,36 @@
const React = require('react'); const React = require('react');
const constants = require('../../shared/constants'); const constants = require('../../shared/constants');
const fns = require('../../shared/functions');
const Styled = require('styled-components'); const Styled = require('styled-components');
const { const {
boxes boxes
} = constants; } = constants;
const {
rndId
} = fns;
const { const {
default: styled, default: styled,
} = Styled; } = Styled;
const classNames = {
content: rndId()
};
const StyledInput = styled.input` const StyledInput = styled.input`
display: none; display: none;
visibility: hidden; visibility: hidden;
&:checked { &:checked {
& ~ .content { & ~ .${classNames.content} {
border: ${boxes.border.checked}; border: ${boxes.border.checked};
} }
} }
&:disabled { &:disabled {
& ~ .content { & ~ .${classNames.content} {
cursor: not-allowed; cursor: not-allowed;
filter: grayscale(80%); filter: grayscale(80%);
opacity: 0.4; opacity: 0.4;
@ -45,6 +54,7 @@ const Widget = ({
children, children,
className, className,
disabled = false, disabled = false,
id,
name, name,
selectable = 'single', selectable = 'single',
style, style,
@ -53,7 +63,11 @@ const Widget = ({
const type = selectable === 'single' ? 'radio' : 'checkbox'; const type = selectable === 'single' ? 'radio' : 'checkbox';
return ( return (
<label className={className} htmlFor={value}> <label
className={className}
htmlFor={value}
id={id}
>
<StyledInput <StyledInput
checked={checked} checked={checked}
disabled={disabled} disabled={disabled}
@ -62,7 +76,7 @@ const Widget = ({
type={type} type={type}
value={value} value={value}
/> />
<StyledContent> <StyledContent className={classNames.content}>
{children} {children}
</StyledContent> </StyledContent>
</label> </label>
@ -74,6 +88,7 @@ Widget.propTypes = {
children: React.PropTypes.object, children: React.PropTypes.object,
className: React.PropTypes.string, className: React.PropTypes.string,
disabled: React.PropTypes.bool, disabled: React.PropTypes.bool,
id: React.PropTypes.string,
name: React.PropTypes.string, name: React.PropTypes.string,
selectable: React.PropTypes.string, selectable: React.PropTypes.string,
style: React.PropTypes.object, style: React.PropTypes.object,

View File

@ -1,7 +1,21 @@
const calc = require('reduce-css-calc'); const calc = require('reduce-css-calc');
const randomNatural = require('random-natural');
// from https://github.com/styled-components/styled-components/blob/065001c725744629c7870240e4a955b924ef5337/src/utils/generateAlphabeticName.js
const chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('')
const rndId = (_code) => {
const code = !_code ? randomNatural({
min: 1000000000
}) : _code;
const lastDigit = chars[code % chars.length];
return code > chars.length
? `${rndId(Math.floor(code / chars.length))}${lastDigit}`
: lastDigit;
};
module.exports = { module.exports = {
remcalc: function(values) { remcalc: (values) => {
values = values.toString().replace('px', '').split(' '); values = values.toString().replace('px', '').split(' ');
let outputRems = ''; let outputRems = '';
@ -14,7 +28,6 @@ module.exports = {
return outputRems; return outputRems;
}, },
calc: function(str) { calc: (str) => calc(`calc(${str})`),
return calc(`calc(${str})`); rndId
}
}; };