import * as React from 'react';
import styled from 'styled-components';
const Wrapper = styled.label`
position: relative;
display: inline-block;
width: 40px;
height: 24px;
`;
const Input: any = styled.input`
display: none;
`;
const Slider = styled.div`
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
transition: .2s;
border-radius: 34px;
&:before {
position: absolute;
content: "";
height: 22px;
width: 22px;
left: 1px;
bottom: 1px;
background-color: white;
transition: .2s;
border-radius: 50%;
}
${Input}:checked + & {
background-color: ${props => props.theme.someColor1};
}
${Input}:focus + & {
box-shadow: 0 0 1px ${props => props.theme.someColor2};
}
${Input}:checked + &:before {
transform: translateX(16px);
}
`;
interface Props = {
name?: string
checked?: boolean
onChange?: Function
innerRef?: Function
className?: string
}
const ToggleSwitch = ({ innerRef, checked, onChange, className, name }: Props) => {
return (
<Wrapper className={className}>
<Input
innerRef={innerRef}
checked={checked}
onChange={onChange}
name={name}
type="checkbox"
/>
<Slider />
</Wrapper>
);
}
componentDidMount() {
this.fetchData();
}
async fetchData() {
try {
const orders = await fetch('orders-path', options).then(response => response.json());
const users = {};
for (let i = 0; i <= orders.length; i++) {
const id = orders[i].user_id;
const user = await fetch('/user/' + id, options).then(response => response.json());
users[id] = user;
}
this.setState({ orders, users });
} catch (e) {
// handle error
}
}
export const actionFromA = data => async (dispatch) => {
dispatch(actionFromA());
}
store.dispatch({ type: 'action_name' });
dispatch(actionFromA());
store.dispatch({ /* объект возвращаемый actionFromA */ });
const withSort = WrappedComponent => props => {
return WrappedComponent ? <WrappedComponent {...props} /> : null;
});
export default withSort(Component);
const withSort = (...params) => WrappedComponent => props => {
// params usage
return WrappedComponent ? <WrappedComponent {...props} /> : null;
});
export default withSort(param1, param2)(Component);
{
type: 'ul', props: { 'class': 'list' }, children: [
{ type: 'li', props: {}, children: ['item 1'] },
{ type: 'li', props: {}, children: ['item 2'] }
]
}
const Wrapper = styled.div`
// some styles
`;
const ChildElement = styled.div`
// some styles
`;
class MyComponent extends React.Component {
static ChildElement = ChildElement;
render() {
const { className } = this.props;
return (
<Wrapper className={className}>
<ChildElement>Some text</ChildElement>
</Wrapper>
)
}
}
export default MyComponent;
const SyledMyComponent = styled(MyComponent)`
// some styles
${MyComponent.ChildElement} {
// some styles for ChildElement
}
`;
const AbstractElement = styled.div`
// some styles
`;
const ConcreteElement = AbstractElement.extend`
// some styles
`;