diff --git a/packages/lsd-react/src/components/CSSBaseline/CSSBaseline.tsx b/packages/lsd-react/src/components/CSSBaseline/CSSBaseline.tsx index f36c9e3..2e3118b 100644 --- a/packages/lsd-react/src/components/CSSBaseline/CSSBaseline.tsx +++ b/packages/lsd-react/src/components/CSSBaseline/CSSBaseline.tsx @@ -7,6 +7,7 @@ import { ButtonStyles } from '../Button/Button.styles' import { CardStyles } from '../Card/Card.styles' import { CardBodyStyles } from '../CardBody/CardBody.styles' import { CardHeaderStyles } from '../CardHeader/CardHeader.styles' +import { CheckboxStyles } from '../Checkbox/Checkbox.styles' import { CollapseStyles } from '../Collapse/Collapse.styles' import { CollapseHeaderStyles } from '../CollapseHeader/CollapseHeader.styles' import { DropdownStyles } from '../Dropdown/Dropdown.styles' @@ -15,6 +16,8 @@ import { IconButtonStyles } from '../IconButton/IconButton.styles' import { LsdIconStyles } from '../Icons/LsdIcon/LsdIcon.styles' import { ListBoxStyles } from '../ListBox/ListBox.styles' import { QuoteStyles } from '../Quote/Quote.styles' +import { RadioButtonStyles } from '../RadioButton/RadioButton.styles' +import { RadioButtonGroupStyles } from '../RadioButtonGroup/RadioButtonGroup.styles' import { TabItemStyles } from '../TabItem/TabItem.styles' import { TabsStyles } from '../Tabs/Tabs.styles' import { TagStyles } from '../Tag/Tag.styles' @@ -40,10 +43,13 @@ const componentStyles: Array | SerializedStyles> = CardBodyStyles, TagStyles, TextFieldStyles, + CheckboxStyles, AutocompleteStyles, QuoteStyles, CollapseStyles, CollapseHeaderStyles, + RadioButtonStyles, + RadioButtonGroupStyles, ] export const CSSBaseline: React.FC<{ theme?: Theme }> = ({ diff --git a/packages/lsd-react/src/components/Checkbox/Checkbox.classes.ts b/packages/lsd-react/src/components/Checkbox/Checkbox.classes.ts new file mode 100644 index 0000000..8d224d5 --- /dev/null +++ b/packages/lsd-react/src/components/Checkbox/Checkbox.classes.ts @@ -0,0 +1,15 @@ +export const checkboxClasses = { + root: `lsd-checkbox`, + + input: `lsd-checkbox__input`, + icon: `lsd-checkbox__icon`, + label: `lsd-checkbox__label`, + + focused: `lsd-checkbox--focused`, + disabled: `lsd-checkbox--disabled`, + indeterminate: 'lsd-checkbox--indeterminate', + + large: `lsd-checkbox--large`, + medium: `lsd-checkbox--medium`, + small: 'lsd-checkbox--small', +} diff --git a/packages/lsd-react/src/components/Checkbox/Checkbox.stories.tsx b/packages/lsd-react/src/components/Checkbox/Checkbox.stories.tsx new file mode 100644 index 0000000..0bed9b3 --- /dev/null +++ b/packages/lsd-react/src/components/Checkbox/Checkbox.stories.tsx @@ -0,0 +1,28 @@ +import { Meta, Story } from '@storybook/react' +import { Checkbox, CheckboxProps } from './Checkbox' + +export default { + title: 'Checkbox', + component: Checkbox, + argTypes: { + size: { + type: { + name: 'enum', + value: ['small', 'medium', 'large'], + }, + defaultValue: 'large', + }, + }, +} as Meta + +export const Root: Story = (args) => ( + Checkbox +) + +Root.args = { + size: 'large', + disabled: false, + indeterminate: false, + checked: undefined, + onChange: undefined, +} diff --git a/packages/lsd-react/src/components/Checkbox/Checkbox.styles.ts b/packages/lsd-react/src/components/Checkbox/Checkbox.styles.ts new file mode 100644 index 0000000..bde6ed5 --- /dev/null +++ b/packages/lsd-react/src/components/Checkbox/Checkbox.styles.ts @@ -0,0 +1,59 @@ +import { css } from '@emotion/react' +import { checkboxClasses } from './Checkbox.classes' + +export const CheckboxStyles = css` + .${checkboxClasses.root} { + position: relative; + display: flex; + flex-direction: row; + align-items: center; + } + + .${checkboxClasses.input} { + opacity: 0; + position: absolute; + left: 0; + top: 0; + padding: 0; + margin: 0; + width: 100%; + height: 100%; + } + + .${checkboxClasses.root}:not(.${checkboxClasses.disabled}) { + &:hover, + &.${checkboxClasses.focused} { + text-decoration: underline; + } + + .${checkboxClasses.input} { + cursor: pointer; + } + } + + .${checkboxClasses.disabled} { + opacity: 0.34; + } + + .${checkboxClasses.label} { + margin-left: 18px; + } + + .${checkboxClasses.large} { + .${checkboxClasses.label} { + margin-left: 18px; + } + } + + .${checkboxClasses.medium} { + .${checkboxClasses.label} { + margin-left: 14px; + } + } + + .${checkboxClasses.small} { + .${checkboxClasses.label} { + margin-left: 12px; + } + } +` diff --git a/packages/lsd-react/src/components/Checkbox/Checkbox.tsx b/packages/lsd-react/src/components/Checkbox/Checkbox.tsx new file mode 100644 index 0000000..43c649c --- /dev/null +++ b/packages/lsd-react/src/components/Checkbox/Checkbox.tsx @@ -0,0 +1,100 @@ +import clsx from 'clsx' +import React, { useEffect, useRef, useState } from 'react' +import { useInput } from '../../utils/useInput' +import { CheckboxFilledIcon, CheckboxIcon } from '../Icons' +import { CheckboxIndeterminateIcon } from '../Icons/CheckboxIndeterminate' +import { Typography } from '../Typography' +import { checkboxClasses } from './Checkbox.classes' + +export type CheckboxProps = Omit< + React.LabelHTMLAttributes, + 'onChange' | 'value' | 'color' +> & + Pick< + React.InputHTMLAttributes, + 'name' | 'onChange' | 'checked' | 'defaultChecked' + > & { + disabled?: boolean + indeterminate?: boolean + size?: 'small' | 'medium' | 'large' + inputProps?: React.InputHTMLAttributes + } + +export const Checkbox: React.FC & { + classes: typeof checkboxClasses +} = ({ + name, + size = 'large', + onChange, + checked, + defaultChecked, + disabled = false, + indeterminate = false, + inputProps = {}, + children, + ...props +}) => { + const ref = useRef(null) + const [focused, setFocused] = useState(false) + const input = useInput({ + value: checked, + defaultValue: defaultChecked ?? false, + onChange, + ref, + }) + + useEffect(() => { + if (!ref.current) return + + const onFocus = () => setFocused(true) + const onBlur = () => setFocused(false) + + ref.current.addEventListener('focus', onFocus) + ref.current.addEventListener('blur', onBlur) + + return () => { + ref.current?.removeEventListener('focus', onFocus) + ref.current?.removeEventListener('blur', onBlur) + } + }, [ref.current]) + + return ( + + + {indeterminate ? ( + + ) : input.value ? ( + + ) : ( + + )} + {children} + + ) +} + +Checkbox.classes = checkboxClasses diff --git a/packages/lsd-react/src/components/Checkbox/index.ts b/packages/lsd-react/src/components/Checkbox/index.ts new file mode 100644 index 0000000..0dab115 --- /dev/null +++ b/packages/lsd-react/src/components/Checkbox/index.ts @@ -0,0 +1 @@ +export * from './Checkbox' diff --git a/packages/lsd-react/src/components/Icons/CheckboxIndeterminate/CheckboxIndeterminateIcon.tsx b/packages/lsd-react/src/components/Icons/CheckboxIndeterminate/CheckboxIndeterminateIcon.tsx new file mode 100644 index 0000000..dad4ef2 --- /dev/null +++ b/packages/lsd-react/src/components/Icons/CheckboxIndeterminate/CheckboxIndeterminateIcon.tsx @@ -0,0 +1,24 @@ +import { LsdIcon } from '../LsdIcon' + +export const CheckboxIndeterminateIcon = LsdIcon( + (props) => ( + + + + ), + { + filled: true, + }, +) diff --git a/packages/lsd-react/src/components/Icons/CheckboxIndeterminate/index.ts b/packages/lsd-react/src/components/Icons/CheckboxIndeterminate/index.ts new file mode 100644 index 0000000..1775ffb --- /dev/null +++ b/packages/lsd-react/src/components/Icons/CheckboxIndeterminate/index.ts @@ -0,0 +1 @@ +export * from './CheckboxIndeterminateIcon' diff --git a/packages/lsd-react/src/components/Icons/RadioButtonFilledIcon/RadioButtonFilledIcon.tsx b/packages/lsd-react/src/components/Icons/RadioButtonFilledIcon/RadioButtonFilledIcon.tsx new file mode 100644 index 0000000..9c78c51 --- /dev/null +++ b/packages/lsd-react/src/components/Icons/RadioButtonFilledIcon/RadioButtonFilledIcon.tsx @@ -0,0 +1,26 @@ +import { LsdIcon } from '../LsdIcon' + +export const RadioButtonFilledIcon = LsdIcon( + (props) => ( + + + + + ), + { + filled: true, + }, +) diff --git a/packages/lsd-react/src/components/Icons/RadioButtonFilledIcon/index.ts b/packages/lsd-react/src/components/Icons/RadioButtonFilledIcon/index.ts new file mode 100644 index 0000000..62143be --- /dev/null +++ b/packages/lsd-react/src/components/Icons/RadioButtonFilledIcon/index.ts @@ -0,0 +1 @@ +export * from './RadioButtonFilledIcon' diff --git a/packages/lsd-react/src/components/Icons/RadioButtonIcon/RadioButtonIcon.tsx b/packages/lsd-react/src/components/Icons/RadioButtonIcon/RadioButtonIcon.tsx new file mode 100644 index 0000000..b349332 --- /dev/null +++ b/packages/lsd-react/src/components/Icons/RadioButtonIcon/RadioButtonIcon.tsx @@ -0,0 +1,22 @@ +import { LsdIcon } from '../LsdIcon' + +export const RadioButtonIcon = LsdIcon( + (props) => ( + + + + ), + { + filled: true, + }, +) diff --git a/packages/lsd-react/src/components/Icons/RadioButtonIcon/index.ts b/packages/lsd-react/src/components/Icons/RadioButtonIcon/index.ts new file mode 100644 index 0000000..03275c3 --- /dev/null +++ b/packages/lsd-react/src/components/Icons/RadioButtonIcon/index.ts @@ -0,0 +1 @@ +export * from './RadioButtonIcon' diff --git a/packages/lsd-react/src/components/Icons/index.ts b/packages/lsd-react/src/components/Icons/index.ts index 40f1f7f..40d3d8f 100644 --- a/packages/lsd-react/src/components/Icons/index.ts +++ b/packages/lsd-react/src/components/Icons/index.ts @@ -17,3 +17,5 @@ export * from './NavigateNextIcon' export * from './NewPageIcon' export * from './SearchIcon' export * from './PickIcon' +export * from './RadioButtonIcon' +export * from './RadioButtonFilledIcon' diff --git a/packages/lsd-react/src/components/RadioButton/RadioButton.classes.ts b/packages/lsd-react/src/components/RadioButton/RadioButton.classes.ts new file mode 100644 index 0000000..6c88247 --- /dev/null +++ b/packages/lsd-react/src/components/RadioButton/RadioButton.classes.ts @@ -0,0 +1,12 @@ +export const radioButtonClasses = { + root: `lsd-radio-button`, + + input: `lsd-radio-button__input`, + label: `lsd-radio-button__label`, + + disabled: `lsd-radio-button--disabled`, + + large: `lsd-radio-button--large`, + medium: `lsd-radio-button--medium`, + small: 'lsd-radio-button--small', +} diff --git a/packages/lsd-react/src/components/RadioButton/RadioButton.stories.tsx b/packages/lsd-react/src/components/RadioButton/RadioButton.stories.tsx new file mode 100644 index 0000000..2a9d446 --- /dev/null +++ b/packages/lsd-react/src/components/RadioButton/RadioButton.stories.tsx @@ -0,0 +1,28 @@ +import { Meta, Story } from '@storybook/react' +import { RadioButton, RadioButtonProps } from './RadioButton' + +export default { + title: 'RadioButton', + component: RadioButton, + argTypes: { + size: { + type: { + name: 'enum', + value: ['small', 'medium', 'large'], + }, + defaultValue: 'large', + }, + }, +} as Meta + +export const Root: Story = (args) => ( + RadioButton label +) + +Root.args = { + size: 'large', + disabled: false, + checked: undefined, + onChange: undefined, + value: '1', +} diff --git a/packages/lsd-react/src/components/RadioButton/RadioButton.styles.ts b/packages/lsd-react/src/components/RadioButton/RadioButton.styles.ts new file mode 100644 index 0000000..dc13696 --- /dev/null +++ b/packages/lsd-react/src/components/RadioButton/RadioButton.styles.ts @@ -0,0 +1,58 @@ +import { css } from '@emotion/react' +import { radioButtonClasses } from './RadioButton.classes' + +export const RadioButtonStyles = css` + .${radioButtonClasses.root} { + position: relative; + display: flex; + flex-direction: row; + align-items: center; + } + + .${radioButtonClasses.input} { + opacity: 0; + position: absolute; + left: 0; + top: 0; + padding: 0; + margin: 0; + width: 100%; + height: 100%; + } + + .${radioButtonClasses.root}:not(.${radioButtonClasses.disabled}) { + &:hover { + text-decoration: underline; + } + + .${radioButtonClasses.input} { + cursor: pointer; + } + } + + .${radioButtonClasses.disabled} { + opacity: 0.34; + } + + .${radioButtonClasses.label} { + margin-left: 18px; + } + + .${radioButtonClasses.large} { + .${radioButtonClasses.label} { + margin-left: 18px; + } + } + + .${radioButtonClasses.medium} { + .${radioButtonClasses.label} { + margin-left: 14px; + } + } + + .${radioButtonClasses.small} { + .${radioButtonClasses.label} { + margin-left: 12px; + } + } +` diff --git a/packages/lsd-react/src/components/RadioButton/RadioButton.tsx b/packages/lsd-react/src/components/RadioButton/RadioButton.tsx new file mode 100644 index 0000000..ca87974 --- /dev/null +++ b/packages/lsd-react/src/components/RadioButton/RadioButton.tsx @@ -0,0 +1,95 @@ +import clsx from 'clsx' +import React, { useRef } from 'react' +import { useInput } from '../../utils/useInput' +import { RadioButtonFilledIcon, RadioButtonIcon } from '../Icons' +import { useRadioButtonGroupContext } from '../RadioButtonGroup/RadioButtonGroup.context' +import { Typography } from '../Typography' +import { radioButtonClasses } from './RadioButton.classes' + +export type RadioButtonProps = Omit< + React.LabelHTMLAttributes, + 'onChange' | 'value' | 'color' +> & + Pick< + React.InputHTMLAttributes, + 'onChange' | 'checked' | 'defaultChecked' + > & { + disabled?: boolean + size?: 'small' | 'medium' | 'large' + name?: string + value: string + inputProps?: React.InputHTMLAttributes + } + +export const RadioButton: React.FC & { + classes: typeof radioButtonClasses +} = ({ + size: _size = 'large', + onChange, + checked: _checked, + defaultChecked, + disabled = false, + value, + name: _name, + inputProps = {}, + children, + ...props +}) => { + const ref = useRef(null) + + const radioButtonGroup = useRadioButtonGroupContext() + const size = radioButtonGroup?.size ?? _size + const name = radioButtonGroup?.name ?? _name ?? '' + const selected = radioButtonGroup + ? radioButtonGroup.value === value + : _checked + + const input = useInput({ + value: selected, + defaultValue: defaultChecked ?? false, + onChange, + ref, + }) + + const handleChange = (event: React.ChangeEvent) => { + radioButtonGroup + ? radioButtonGroup.setActiveRadioButton(event.target.value) + : input.onChange(event) + } + + return ( + + + {input.value ? ( + + ) : ( + + )} + {children} + + ) +} + +RadioButton.classes = radioButtonClasses diff --git a/packages/lsd-react/src/components/RadioButton/index.ts b/packages/lsd-react/src/components/RadioButton/index.ts new file mode 100644 index 0000000..37d8240 --- /dev/null +++ b/packages/lsd-react/src/components/RadioButton/index.ts @@ -0,0 +1 @@ +export * from './RadioButton' diff --git a/packages/lsd-react/src/components/RadioButtonGroup/RadioButtonGroup.classes.ts b/packages/lsd-react/src/components/RadioButtonGroup/RadioButtonGroup.classes.ts new file mode 100644 index 0000000..f5f4aac --- /dev/null +++ b/packages/lsd-react/src/components/RadioButtonGroup/RadioButtonGroup.classes.ts @@ -0,0 +1,4 @@ +export const radioButtonGroupClasses = { + root: `lsd-radio-button-group`, + label: `lsd-radio-button-group__label`, +} diff --git a/packages/lsd-react/src/components/RadioButtonGroup/RadioButtonGroup.context.ts b/packages/lsd-react/src/components/RadioButtonGroup/RadioButtonGroup.context.ts new file mode 100644 index 0000000..381b5d0 --- /dev/null +++ b/packages/lsd-react/src/components/RadioButtonGroup/RadioButtonGroup.context.ts @@ -0,0 +1,19 @@ +import React from 'react' +import { + ActiveRadioButtonType, + RadioButtonGroupProps, +} from './RadioButtonGroup' + +export type RadioButtonGroupContextType = { + value?: ActiveRadioButtonType | null + name?: string | null + setActiveRadioButton: (value: ActiveRadioButtonType) => void + + size?: RadioButtonGroupProps['size'] +} + +export const RadioButtonGroupContext = + React.createContext(null as any) + +export const useRadioButtonGroupContext = () => + React.useContext(RadioButtonGroupContext) diff --git a/packages/lsd-react/src/components/RadioButtonGroup/RadioButtonGroup.stories.tsx b/packages/lsd-react/src/components/RadioButtonGroup/RadioButtonGroup.stories.tsx new file mode 100644 index 0000000..4bde163 --- /dev/null +++ b/packages/lsd-react/src/components/RadioButtonGroup/RadioButtonGroup.stories.tsx @@ -0,0 +1,35 @@ +import { Meta, Story } from '@storybook/react' +import { RadioButton } from '../RadioButton' +import { RadioButtonGroup, RadioButtonGroupProps } from './RadioButtonGroup' + +export default { + title: 'RadioButtonGroup', + component: RadioButtonGroup, + argTypes: { + size: { + type: { + name: 'enum', + value: ['small', 'medium', 'large'], + }, + defaultValue: 'large', + }, + }, +} as Meta + +export const Root: Story = (args) => ( + + RadioButton label + RadioButton label + RadioButton label + RadioButton label + RadioButton label + RadioButton label + RadioButton label + RadioButton label + +) + +Root.args = { + size: 'large', + label: 'Label', +} diff --git a/packages/lsd-react/src/components/RadioButtonGroup/RadioButtonGroup.styles.ts b/packages/lsd-react/src/components/RadioButtonGroup/RadioButtonGroup.styles.ts new file mode 100644 index 0000000..b7d9bf1 --- /dev/null +++ b/packages/lsd-react/src/components/RadioButtonGroup/RadioButtonGroup.styles.ts @@ -0,0 +1,15 @@ +import { css } from '@emotion/react' +import { radioButtonGroupClasses } from './RadioButtonGroup.classes' + +export const RadioButtonGroupStyles = css` + .${radioButtonGroupClasses.root} { + display: flex; + flex-direction: column; + gap: 6px; + width: fit-content; + } + + .${radioButtonGroupClasses.label} { + margin-bottom: 6px; + } +` diff --git a/packages/lsd-react/src/components/RadioButtonGroup/RadioButtonGroup.tsx b/packages/lsd-react/src/components/RadioButtonGroup/RadioButtonGroup.tsx new file mode 100644 index 0000000..57913e3 --- /dev/null +++ b/packages/lsd-react/src/components/RadioButtonGroup/RadioButtonGroup.tsx @@ -0,0 +1,52 @@ +import clsx from 'clsx' +import React, { useEffect, useRef, useState } from 'react' +import { RadioButtonGroupContext } from './RadioButtonGroup.context' +import { radioButtonGroupClasses } from './RadioButtonGroup.classes' +import { Typography } from '../Typography' + +export type ActiveRadioButtonType = string | readonly string[] + +export type RadioButtonGroupProps = React.HTMLAttributes & { + value: ActiveRadioButtonType | null + name?: string | null + onChange?: (value: ActiveRadioButtonType) => void + size?: 'small' | 'medium' | 'large' + label?: string +} + +export const RadioButtonGroup: React.FC & { + classes: typeof radioButtonGroupClasses +} = ({ size = 'large', label, value, name, onChange, children, ...props }) => { + const ref = useRef(null) + const [activeValue, setActiveValue] = useState(value) + + const setActiveRadioButton = (value: ActiveRadioButtonType) => { + if (onChange) onChange(value) + else setActiveValue(value) + } + + useEffect(() => setActiveValue(value), [value]) + + return ( + +
+ + {label && label} + + {children} +
+
+ ) +} + +RadioButtonGroup.classes = radioButtonGroupClasses diff --git a/packages/lsd-react/src/components/RadioButtonGroup/index.ts b/packages/lsd-react/src/components/RadioButtonGroup/index.ts new file mode 100644 index 0000000..a9f5a07 --- /dev/null +++ b/packages/lsd-react/src/components/RadioButtonGroup/index.ts @@ -0,0 +1 @@ +export * from './RadioButtonGroup' diff --git a/packages/lsd-react/src/index.ts b/packages/lsd-react/src/index.ts index 94a2dc2..1c4c165 100644 --- a/packages/lsd-react/src/index.ts +++ b/packages/lsd-react/src/index.ts @@ -5,6 +5,7 @@ export * from './components/Button' export * from './components/Card' export * from './components/CardBody' export * from './components/CardHeader' +export * from './components/Checkbox' export * from './components/Collapse' export * from './components/CollapseHeader' export * from './components/Dropdown' @@ -17,3 +18,5 @@ export * from './components/TabItem' export * from './components/Tabs' export * from './components/Tag' export * from './components/Theme' +export * from './components/RadioButton' +export * from './components/RadioButtonGroup' diff --git a/packages/lsd-react/src/utils/useInput.ts b/packages/lsd-react/src/utils/useInput.ts index 9a8925e..0e8f1d0 100644 --- a/packages/lsd-react/src/utils/useInput.ts +++ b/packages/lsd-react/src/utils/useInput.ts @@ -1,22 +1,23 @@ import React, { useEffect, useState } from 'react' export type InputValueType = - React.InputHTMLAttributes['value'] + | React.InputHTMLAttributes['value'] + | boolean export type InputOnChangeType = React.InputHTMLAttributes['onChange'] -export type InputProps = { - value?: InputValueType - defaultValue?: InputValueType +export type InputProps = { + value?: T + defaultValue: T onChange?: InputOnChangeType ref?: React.RefObject } -export const useInput = (props: InputProps) => { - const [value, setValue] = useState( - props.value ?? props.defaultValue ?? '', - ) +export const useInput = ( + props: InputProps, +) => { + const [value, setValue] = useState(props.value ?? props.defaultValue) const uncontrolled = typeof props.value === 'undefined' const filled = @@ -27,7 +28,12 @@ export const useInput = (props: InputProps) => { : value.toString().length > 0 const onChange: InputOnChangeType = (event) => { - if (uncontrolled) return setValue(event.target.value) + const type = event.target.type + const value = + event.target[ + type === 'checkbox' || type === 'radio' ? 'checked' : 'value' + ] + if (uncontrolled) return setValue(value as T) props.onChange && props.onChange(event) } @@ -46,7 +52,7 @@ export const useInput = (props: InputProps) => { } useEffect(() => { - !uncontrolled && setValue(props.value) + !uncontrolled && setValue(props.value as T) }, [uncontrolled, props.value]) return {