-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(fields): add MultiCheckbox (#37)
* feat(elements): add Label * feat(elements): add Field * feat(elements): add Fieldset * feat(elements): add Legend * feat(fields): add MultiCheckbox * feat(formiks): add FormikMultiCheckbox
- Loading branch information
1 parent
e6aeaec
commit 405ce01
Showing
18 changed files
with
371 additions
and
39 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import styled from 'styled-components' | ||
|
||
import type { HTMLAttributes } from 'react' | ||
|
||
export type FieldProps = HTMLAttributes<HTMLDivElement> | ||
export const Field = styled.div` | ||
display: flex; | ||
flex-direction: column; | ||
` |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import styled from 'styled-components' | ||
|
||
import { Field } from './Field' | ||
|
||
import type { FieldsetHTMLAttributes } from 'react' | ||
|
||
export type FieldsetProps = FieldsetHTMLAttributes<HTMLFieldSetElement> | ||
export function Fieldset(nativeProps: FieldsetProps) { | ||
return <StyledField as="fieldset" {...nativeProps} /> | ||
} | ||
|
||
const StyledField = styled(Field)` | ||
border: 0; | ||
margin: 0; | ||
padding: 0; | ||
` |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import styled from 'styled-components' | ||
|
||
import type { LabelHTMLAttributes } from 'react' | ||
|
||
export type LabelProps = LabelHTMLAttributes<HTMLLabelElement> | ||
export const Label = styled.label` | ||
color: ${p => p.theme.color.slateGray}; | ||
font-size: 13px; | ||
line-height: 1.4; | ||
margin-bottom: 0.5rem; | ||
` |
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import styled from 'styled-components' | ||
|
||
import { Label } from './Label' | ||
|
||
import type { HTMLAttributes } from 'react' | ||
|
||
export type LegendProps = HTMLAttributes<HTMLLegendElement> & { | ||
isHidden?: boolean | ||
} | ||
export function Legend({ isHidden = false, ...nativeProps }: LegendProps) { | ||
return <StyledLabel as="legend" isHidden={isHidden} {...nativeProps} /> | ||
} | ||
|
||
export const StyledLabel = styled(Label)<{ | ||
isHidden: boolean | ||
}>` | ||
display: ${p => (p.isHidden ? 'none' : 'table')}; | ||
` |
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 |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { equals, reject } from 'ramda' | ||
import { useCallback, useMemo, useRef } from 'react' | ||
import styled, { css } from 'styled-components' | ||
|
||
import { Fieldset } from '../elements/Fieldset' | ||
import { Legend } from '../elements/Legend' | ||
import { Checkbox } from './Checkbox' | ||
|
||
import type { Option } from '../types' | ||
import type { Promisable } from 'type-fest' | ||
|
||
export type MultiCheckboxProps = { | ||
defaultValue?: string[] | ||
isInline?: boolean | ||
label?: string | ||
name: string | ||
onChange?: (nextValue: string[] | undefined) => Promisable<void> | ||
options: Option[] | ||
} | ||
export function MultiCheckbox({ | ||
defaultValue = [], | ||
isInline = false, | ||
label, | ||
name, | ||
onChange, | ||
options | ||
}: MultiCheckboxProps) { | ||
const checkedOptionValues = useRef<string[]>(defaultValue) | ||
|
||
const key = useMemo(() => `${name}-${JSON.stringify(defaultValue)}`, [defaultValue, name]) | ||
|
||
const handleChange = useCallback( | ||
(nextOptionValue: string, isChecked: boolean) => { | ||
const nextCheckedOptionValues = isChecked | ||
? [...checkedOptionValues.current, nextOptionValue] | ||
: reject(equals(nextOptionValue))(checkedOptionValues.current) | ||
|
||
checkedOptionValues.current = nextCheckedOptionValues | ||
|
||
if (onChange) { | ||
const normalizedNextValue = nextCheckedOptionValues.length ? nextCheckedOptionValues : undefined | ||
|
||
onChange(normalizedNextValue) | ||
} | ||
}, | ||
[onChange] | ||
) | ||
|
||
return ( | ||
<Fieldset key={key}> | ||
{label && <Legend>{label}</Legend>} | ||
|
||
<ChecboxesBox isInline={isInline}> | ||
{options.map((option, index) => ( | ||
<Checkbox | ||
defaultChecked={defaultValue.includes(option.value)} | ||
label={option.label} | ||
name={`${name}${index}`} | ||
onChange={(isChecked: boolean) => handleChange(option.value, isChecked)} | ||
/> | ||
))} | ||
</ChecboxesBox> | ||
</Fieldset> | ||
) | ||
} | ||
|
||
const ChecboxesBox = styled.div<{ | ||
isInline: boolean | ||
}>` | ||
display: flex; | ||
flex-direction: ${p => (p.isInline ? 'row' : 'column')}; | ||
> .rs-checkbox { | ||
> .rs-checkbox-checker { | ||
padding-left: 28px; | ||
padding-top: 2px; | ||
.rs-checkbox-wrapper { | ||
left: 2px; | ||
top: 0 !important; | ||
} | ||
} | ||
} | ||
${p => | ||
p.isInline && | ||
css` | ||
> .rs-checkbox:not(:first-child) { | ||
margin-left: 0.75rem; | ||
} | ||
`} | ||
` |
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { useField } from 'formik' | ||
import { useCallback, useEffect } from 'react' | ||
|
||
import { MultiCheckbox } from '../fields/MultiCheckbox' | ||
|
||
import type { MultiCheckboxProps } from '../fields/MultiCheckbox' | ||
|
||
export type FormikMultiCheckboxProps = Omit<MultiCheckboxProps, 'defaultValue' | 'onChange'> | ||
export function FormikMultiCheckbox({ name, ...originalProps }: FormikMultiCheckboxProps) { | ||
const [, , helpers] = useField(name) | ||
// We don't include `setValues` in `useCallback()` and `useEffect()` dependencies | ||
// both because it is useless and it will trigger infinite hook calls | ||
const { setValue } = helpers | ||
|
||
const handleChange = useCallback((nextValue: string[] | undefined) => { | ||
setValue(nextValue) | ||
|
||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []) | ||
|
||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
useEffect(() => () => setValue(undefined), []) | ||
|
||
return <MultiCheckbox name={name} onChange={handleChange} {...originalProps} /> | ||
} |
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Field } from '../../src' | ||
|
||
import type { FieldProps } from '../../src' | ||
|
||
const args: FieldProps = {} | ||
|
||
export default { | ||
title: 'Elements/Field', | ||
component: Field, | ||
|
||
argTypes: {}, | ||
|
||
args | ||
} | ||
|
||
export const _Field = (props: FieldProps) => ( | ||
<Field {...props}>This is a field for form inputs but it’s basically a div.</Field> | ||
) |
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Fieldset } from '../../src' | ||
|
||
import type { FieldsetProps } from '../../src' | ||
|
||
const args: FieldsetProps = {} | ||
|
||
export default { | ||
title: 'Elements/Fieldset', | ||
component: Fieldset, | ||
|
||
argTypes: {}, | ||
|
||
args | ||
} | ||
|
||
export const _Fieldset = (props: FieldsetProps) => ( | ||
<Fieldset {...props}> | ||
This is am HTML {'<fieldset>'} for form inputs. It should contain a {'<Legend>'} element. | ||
</Fieldset> | ||
) |
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Label } from '../../src' | ||
|
||
import type { LabelProps } from '../../src' | ||
|
||
const args: LabelProps = { | ||
children: 'A form input label' | ||
} | ||
|
||
export default { | ||
title: 'Elements/Label', | ||
component: Label, | ||
|
||
argTypes: {}, | ||
|
||
args | ||
} | ||
|
||
export const _Label = (props: LabelProps) => <Label {...props} /> |
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Legend } from '../../src' | ||
|
||
import type { LegendProps } from '../../src' | ||
|
||
const args: LegendProps = { | ||
children: 'A form fieldset legend', | ||
isHidden: false | ||
} | ||
|
||
export default { | ||
title: 'Elements/Legend', | ||
component: Legend, | ||
|
||
argTypes: {}, | ||
|
||
args | ||
} | ||
|
||
export const _Legend = (props: LegendProps) => <Legend {...props} /> |
Oops, something went wrong.