Files
react/components/checkbox/checkbox-group.tsx
witt 7facec3849 feat(scaleable): add scaleable props to each component (#531)
* feat(scaleable): add scaleable props to each component

* chore(scaleable): update the exported type

* feat: apply scaleable to components

chore: remove with-default

test: improve testcase for scaleable

chore: resolve test warning

ci: upgrade nodejs to latest lts

docs: fix type error in document site

* docs: update documents to be compatible with scaleable

chore: fix build errors

* chore: remove all size-related attributes

docs: improve guide document

* docs: add scaleable documentation

test: update snapshots

chore: remove unused

* feat: add scaleable to grid components

* docs: improve docs

* test: update snapshots

* fix(grid): fix basic component props
2021-08-13 17:10:57 +08:00

85 lines
2.3 KiB
TypeScript

import React, { useEffect, useMemo, useState } from 'react'
import { CheckboxContext } from './checkbox-context'
import useWarning from '../utils/use-warning'
import useScaleable, { withScaleable } from '../use-scaleable'
interface Props {
value: string[]
disabled?: boolean
onChange?: (values: string[]) => void
className?: string
}
const defaultProps = {
disabled: false,
className: '',
}
type NativeAttrs = Omit<React.HTMLAttributes<any>, keyof Props>
export type CheckboxGroupProps = Props & NativeAttrs
const CheckboxGroupComponent: React.FC<React.PropsWithChildren<CheckboxGroupProps>> = ({
disabled,
onChange,
value,
children,
className,
...props
}: CheckboxGroupProps & typeof defaultProps) => {
const { SCALES } = useScaleable()
const [selfVal, setSelfVal] = useState<string[]>([])
if (!value) {
value = []
useWarning('Props "value" is required.', 'Checkbox Group')
}
const updateState = (val: string, checked: boolean) => {
const removed = selfVal.filter(v => v !== val)
const next = checked ? [...removed, val] : removed
setSelfVal(next)
onChange && onChange(next)
}
const providerValue = useMemo(() => {
return {
updateState,
disabledAll: disabled,
inGroup: true,
values: selfVal,
}
}, [disabled, selfVal])
useEffect(() => {
setSelfVal(value)
}, [value.join(',')])
return (
<CheckboxContext.Provider value={providerValue}>
<div className={`group ${className}`} {...props}>
{children}
<style jsx>{`
.group {
width: ${SCALES.width(1, 'auto')};
height: ${SCALES.height(1, 'auto')};
padding: ${SCALES.pt(0)} ${SCALES.pr(0)} ${SCALES.pb(0)} ${SCALES.pl(0)};
margin: ${SCALES.mt(0)} ${SCALES.mr(0)} ${SCALES.mb(0)} ${SCALES.ml(0)};
}
.group :global(label) {
margin-right: calc(${SCALES.font(1)} * 2);
--checkbox-size: ${SCALES.font(1)};
}
.group :global(label:last-of-type) {
margin-right: 0;
}
`}</style>
</div>
</CheckboxContext.Provider>
)
}
CheckboxGroupComponent.defaultProps = defaultProps
CheckboxGroupComponent.displayName = 'GeistCheckboxGroup'
const CheckboxGroup = withScaleable(CheckboxGroupComponent)
export default CheckboxGroup