-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from stoplightio/SL-640/state
Sl 640/stateful-components
- Loading branch information
Showing
9 changed files
with
226 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,63 @@ | ||
import noop = require('lodash/noop'); | ||
import * as React from 'react'; | ||
|
||
import { Box } from './Box'; | ||
import { Flex } from './Flex'; | ||
import { Icon } from './Icon'; | ||
import { Input } from './Input'; | ||
import { styled } from './utils'; | ||
|
||
export interface ICheckboxProps { | ||
checked?: boolean; | ||
disabled?: boolean; | ||
id?: string; | ||
className?: string; | ||
checked?: boolean; | ||
disabled?: boolean; | ||
width?: string; | ||
height?: string; | ||
onChange?: React.ChangeEventHandler<HTMLInputElement>; | ||
onChange?: (checked: boolean) => void; | ||
} | ||
|
||
export const Checkbox = styled<ICheckboxProps>((props: ICheckboxProps) => { | ||
const { id, className, checked, width, height, disabled, onChange } = props; | ||
export const BasicCheckbox = (props: ICheckboxProps) => { | ||
const { id, className, width, height, disabled, onChange = noop } = props; | ||
|
||
const [checked, setValue] = React.useState(props.checked || false); | ||
const isChecked = props.hasOwnProperty('checked') ? props.checked : checked; | ||
|
||
const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
setValue(event.target.checked); | ||
onChange(event.target.checked); | ||
}; | ||
|
||
return ( | ||
<span className={className}> | ||
<Box display="inline-block"> | ||
<Input | ||
type="checkbox" | ||
id={id} | ||
checked={checked} | ||
disabled={disabled} | ||
onChange={onChange} | ||
position="absolute" | ||
css={{ clip: 'rect(1px, 1px, 1px, 1px)' }} | ||
/> | ||
<Flex | ||
as="span" | ||
display="block" | ||
m="none" | ||
p="none" | ||
radius="md" | ||
items="center" | ||
justify="center" | ||
bg={checked ? 'toggle.checked.bg' : 'toggle.bg'} | ||
cursor={disabled ? 'not-allowed' : 'pointer'} | ||
width={width || '20px'} | ||
height={height || '20px'} | ||
opacity={disabled ? 0.6 : 1} | ||
css={{ | ||
fontSize: '14px', | ||
transition: 'background-color .15s ease-in-out', | ||
}} | ||
> | ||
{checked && <Icon icon="check" fg="toggle.checked.fg" />} | ||
</Flex> | ||
</Box> | ||
</span> | ||
<Box as="label" display="inline-block" id={id} className={className}> | ||
<input | ||
type="checkbox" | ||
checked={isChecked || false} | ||
disabled={disabled} | ||
onChange={handleChange} | ||
style={{ position: 'absolute', clip: 'rect(1px, 1px, 1px, 1px)' }} | ||
/> | ||
<Flex | ||
as="span" | ||
display="block" | ||
m="none" | ||
p="none" | ||
radius="md" | ||
items="center" | ||
justify="center" | ||
bg={isChecked ? 'toggle.checked.bg' : 'toggle.bg'} | ||
cursor={disabled ? 'not-allowed' : 'pointer'} | ||
width={width || '20px'} | ||
height={height || '20px'} | ||
opacity={disabled ? 0.6 : 1} | ||
css={{ | ||
fontSize: '14px', | ||
transition: 'background-color .15s ease-in-out', | ||
}} | ||
> | ||
{isChecked && <Icon icon="check" fg="toggle.checked.fg" />} | ||
</Flex> | ||
</Box> | ||
); | ||
})``; | ||
}; | ||
|
||
export const Checkbox = styled<ICheckboxProps>(BasicCheckbox as any)``; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,70 @@ | ||
import noop = require('lodash/noop'); | ||
import * as React from 'react'; | ||
|
||
import { Box } from './Box'; | ||
import { Flex } from './Flex'; | ||
import { Icon } from './Icon'; | ||
import { Input } from './Input'; | ||
import { styled } from './utils'; | ||
|
||
export interface IToggleProps { | ||
checked: boolean; | ||
disabled?: boolean; | ||
id?: string; | ||
className?: string; | ||
checked?: boolean; | ||
disabled?: boolean; | ||
width?: string; | ||
height?: string; | ||
onChange?: React.ChangeEventHandler<HTMLInputElement>; | ||
onChange?: (checked: boolean) => void; | ||
} | ||
|
||
export const Toggle = styled<IToggleProps>((props: IToggleProps) => { | ||
const { id, className, checked, width, height, disabled, onChange } = props; | ||
export const BasicToggle = (props: IToggleProps) => { | ||
const { id, className, width, height, disabled, onChange = noop } = props; | ||
|
||
const [checked, setValue] = React.useState(props.checked || false); | ||
const isChecked = props.hasOwnProperty('checked') ? props.checked : checked; | ||
|
||
const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
setValue(event.target.checked); | ||
onChange(event.target.checked); | ||
}; | ||
|
||
return ( | ||
<span className={className}> | ||
<Box display="inline-block"> | ||
<Input | ||
type="checkbox" | ||
id={id} | ||
checked={checked} | ||
disabled={disabled} | ||
onChange={onChange} | ||
position="absolute" | ||
css={{ clip: 'rect(1px, 1px, 1px, 1px)' }} | ||
/> | ||
<Flex | ||
as="span" | ||
display="block" | ||
m="none" | ||
p="none" | ||
radius="100px" | ||
bg={checked ? 'toggle.checked.bg' : 'toggle.bg'} | ||
cursor={disabled ? 'not-allowed' : 'pointer'} | ||
width={width || '40px'} | ||
height={height || '20px'} | ||
opacity={disabled ? 0.6 : 1} | ||
items="center" | ||
<Box as="label" display="inline-block" id={id} className={className}> | ||
<input | ||
type="checkbox" | ||
id={id} | ||
checked={isChecked || false} | ||
disabled={disabled} | ||
onChange={handleChange} | ||
style={{ position: 'absolute', clip: 'rect(1px, 1px, 1px, 1px)' }} | ||
/> | ||
<Flex | ||
as="span" | ||
display="block" | ||
m="none" | ||
p="none" | ||
radius="100px" | ||
bg={isChecked ? 'toggle.checked.bg' : 'toggle.bg'} | ||
cursor={disabled ? 'not-allowed' : 'pointer'} | ||
width={width || '40px'} | ||
height={height || '20px'} | ||
opacity={disabled ? 0.6 : 1} | ||
items="center" | ||
css={{ | ||
fontSize: '14px', | ||
transition: 'background-color .15s ease-in-out', | ||
}} | ||
> | ||
<Icon | ||
icon="circle" | ||
fg={isChecked ? 'toggle.checked.fg' : 'toggle.fg'} | ||
css={{ | ||
fontSize: '14px', | ||
transition: 'background-color .15s ease-in-out', | ||
paddingLeft: isChecked ? '22px' : '4px', | ||
transition: 'padding .15s ease-in-out, color .25s ease-in-out', | ||
}} | ||
> | ||
<Icon | ||
icon="circle" | ||
fg={checked ? 'toggle.checked.fg' : 'toggle.fg'} | ||
css={{ | ||
paddingLeft: checked ? '22px' : '4px', | ||
transition: 'padding .15s ease-in-out, color .25s ease-in-out', | ||
}} | ||
/> | ||
</Flex> | ||
</Box> | ||
</span> | ||
/> | ||
</Flex> | ||
</Box> | ||
); | ||
})``; | ||
}; | ||
|
||
export const Toggle = styled<IToggleProps>(BasicToggle as any)``; |
Oops, something went wrong.