Skip to content
This repository was archived by the owner on Mar 4, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Add static `className` prop to result type of `createComponent` @kuzhelov ([#1563](https://github.com/stardust-ui/react/pull/1563))
- Add `hand` icon to Teams theme @t-proko ([#1567](https://github.com/stardust-ui/react/pull/1567))
- Add accessibility attributes and keyboard handlers for `Tooltip` @sophieH29 ([#1575](https://github.com/stardust-ui/react/pull/1575))
- Add `labelPosition` prop to `Checkbox` component @layershifter ([#1578](https://github.com/stardust-ui/react/pull/1578))

### Documentation
- Ensure docs content doesn't overlap with sidebar @kuzhelov ([#1568](https://github.com/stardust-ui/react/pull/1568))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Checkbox, Flex } from '@stardust-ui/react'
import * as React from 'react'

const CheckboxExampleLabel = () => (
<Flex>
<Checkbox label="At start" labelPosition="start" />
<Checkbox label="At end" />
</Flex>
)

export default CheckboxExampleLabel
16 changes: 16 additions & 0 deletions docs/src/examples/components/Checkbox/Slots/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as React from 'react'

import ComponentExample from 'docs/src/components/ComponentDoc/ComponentExample'
import ExampleSection from 'docs/src/components/ComponentDoc/ExampleSection'

const Slots = () => (
<ExampleSection title="Slots">
<ComponentExample
title="Label"
description="A checkbox can have a label."
examplePath="components/Checkbox/Slots/CheckboxExampleLabel"
/>
</ExampleSection>
)

export default Slots
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { useBooleanKnob } from '@stardust-ui/docs-components'
import { Checkbox } from '@stardust-ui/react'
import * as React from 'react'

const CheckboxExampleChecked = () => {
const [checked] = useBooleanKnob({ name: 'checked', initialValue: true })

return (
<>
<Checkbox checked={checked} label="Checked" />
<Checkbox checked={checked} label="Checked toggle" toggle />
</>
)
}

export default CheckboxExampleChecked
21 changes: 21 additions & 0 deletions docs/src/examples/components/Checkbox/States/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import * as React from 'react'

import ComponentExample from 'docs/src/components/ComponentDoc/ComponentExample'
import ExampleSection from 'docs/src/components/ComponentDoc/ExampleSection'

const States = () => (
<ExampleSection title="States">
<ComponentExample
title="Checked"
description="A checkbox can be checked."
examplePath="components/Checkbox/States/CheckboxExampleChecked"
/>
<ComponentExample
title="Disabled"
description="A checkbox can be read-only and unable to change states."
examplePath="components/Checkbox/States/CheckboxExampleDisabled"
/>
</ExampleSection>
)

export default States
5 changes: 0 additions & 5 deletions docs/src/examples/components/Checkbox/Types/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ const Types = () => (
description="A standard checkbox."
examplePath="components/Checkbox/Types/CheckboxExample"
/>
<ComponentExample
title="Disabled"
description="A checkbox can be read-only and unable to change states."
examplePath="components/Checkbox/Types/CheckboxExampleDisabled"
/>
<ComponentExample
title="Toggle"
description="A checkbox can be formatted to show an on or off choice."
Expand Down
5 changes: 5 additions & 0 deletions docs/src/examples/components/Checkbox/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import * as React from 'react'

import Slots from './Slots'
import States from './States'
import Types from './Types'

const CheckboxExamples = () => (
<>
<Types />
<States />
<Slots />
</>
)

Expand Down
16 changes: 14 additions & 2 deletions packages/react/src/components/Checkbox/Checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ export interface CheckboxProps extends UIComponentProps, ChildrenComponentProps
/** The label of the item. */
label?: ShorthandValue

/** A label in the loader can have different positions. */
labelPosition?: 'start' | 'end'

/**
* Called after item checked state is changed.
* @param {SyntheticEvent} event - React's original SyntheticEvent.
Expand Down Expand Up @@ -79,6 +82,7 @@ class Checkbox extends AutoControlledComponent<WithAsProp<CheckboxProps>, Checkb
disabled: PropTypes.bool,
icon: customPropTypes.itemShorthand,
label: customPropTypes.itemShorthand,
labelPosition: PropTypes.oneOf(['start', 'end']),
onChange: PropTypes.func,
onClick: PropTypes.func,
toggle: PropTypes.bool,
Expand All @@ -87,6 +91,7 @@ class Checkbox extends AutoControlledComponent<WithAsProp<CheckboxProps>, Checkb
static defaultProps = {
accessibility: checkboxBehavior,
icon: {},
labelPosition: 'end',
}

static autoControlledProps = ['checked']
Expand Down Expand Up @@ -133,7 +138,13 @@ class Checkbox extends AutoControlledComponent<WithAsProp<CheckboxProps>, Checkb
}

renderComponent({ ElementType, classes, unhandledProps, styles, accessibility }) {
const { label, icon, toggle } = this.props
const { label, labelPosition, icon, toggle } = this.props

const labelElement = Text.create(label, {
defaultProps: {
styles: styles.label,
},
})

return (
<ElementType
Expand All @@ -145,13 +156,14 @@ class Checkbox extends AutoControlledComponent<WithAsProp<CheckboxProps>, Checkb
{...unhandledProps}
{...applyAccessibilityKeyHandlers(accessibility.keyHandlers.root, unhandledProps)}
>
{labelPosition === 'start' && labelElement}
{Icon.create(icon, {
defaultProps: {
name: toggle ? 'stardust-circle' : 'stardust-checkmark',
styles: toggle ? styles.toggle : styles.checkbox,
},
})}
{Text.create(label)}
{labelPosition === 'end' && labelElement}
</ElementType>
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ const checkboxStyles: ComponentSlotStylesInput<CheckboxProps & CheckboxState, Ch
}),
}),

label: ({ props: p, variables: v }): ICSSInJSStyle => ({
[p.labelPosition === 'start' ? ':after' : ':before']: {
content: '" "',
display: 'inline-block',
width: v.checkboxGap,
},
}),

toggle: ({ props: p, variables: v }): ICSSInJSStyle => ({
background: v.toggleBackground,
borderColor: v.toggleBorderColor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type CheckboxVariables = {
checkboxBorderRadius: string
checkboxBorderWidth: string
checkboxColor: string
checkboxGap: string
checkboxMargin: string
checkboxPadding: string

Expand Down Expand Up @@ -45,7 +46,8 @@ export default (siteVars: any): CheckboxVariables => ({
checkboxBorderRadius: pxToRem(4),
checkboxBorderWidth: pxToRem(1),
checkboxColor: 'transparent',
checkboxMargin: `0 ${pxToRem(12)} 0 0`,
checkboxGap: pxToRem(12),
checkboxMargin: '0',
checkboxPadding: '0',

toggleBackground: 'transparent',
Expand All @@ -54,7 +56,7 @@ export default (siteVars: any): CheckboxVariables => ({
toggleBorderRadius: pxToRem(999),
toggleBorderWidth: pxToRem(1),
toggleColor: 'inherit',
toggleMargin: `0 ${pxToRem(12)} 0 0`,
toggleMargin: '0',
togglePadding: `0 ${pxToRem(20)} 0 0`,

checkedCheckboxBackground: 'transparent',
Expand Down