generated from gravity-ui/package-example
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(CheckboxGroup): added new input CheckboxGroup (#214)
- Loading branch information
1 parent
c938dc5
commit 075e48b
Showing
44 changed files
with
520 additions
and
26 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
23 changes: 23 additions & 0 deletions
23
src/lib/kit/components/Inputs/CheckboxGroup/CheckboxGroup.scss
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,23 @@ | ||
@import '../../../styles/variables'; | ||
|
||
.#{$ns}checkbox-group { | ||
display: flex; | ||
align-items: center; | ||
height: 28px; | ||
|
||
& > *:not(:last-child) { | ||
margin-right: 6px; | ||
} | ||
|
||
&_vertical { | ||
flex-direction: column; | ||
align-items: flex-start; | ||
margin-top: 8px; | ||
height: auto; | ||
|
||
& > *:not(:last-child) { | ||
margin-right: 0px; | ||
margin-bottom: 6px; | ||
} | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
src/lib/kit/components/Inputs/CheckboxGroup/CheckboxGroup.tsx
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,72 @@ | ||
import React from 'react'; | ||
|
||
import {ArrayInput, FieldArrayValue, transformArrIn, transformArrOut} from '../../../../core'; | ||
import {Checkbox, type CheckboxProps as CheckboxBaseProps} from '@gravity-ui/uikit'; | ||
import {block} from '../../../utils'; | ||
|
||
import './CheckboxGroup.scss'; | ||
|
||
const b = block('checkbox-group'); | ||
|
||
export interface CheckboxGroupProps | ||
extends Omit< | ||
CheckboxBaseProps, | ||
'checked' | 'onChange' | 'onBlur' | 'onFocus' | 'disabled' | 'qa' | 'content' | ||
> {} | ||
|
||
export const CheckboxGroup: ArrayInput<CheckboxGroupProps> = ({name, input, spec, inputProps}) => { | ||
const {value, onBlur, onChange, onFocus} = input; | ||
|
||
const _value: string[] | undefined = React.useMemo( | ||
() => transformArrOut<FieldArrayValue, string[]>(value), | ||
[value], | ||
); | ||
|
||
const options = React.useMemo( | ||
() => | ||
spec.enum?.map((id) => ({ | ||
value: id, | ||
text: spec.description?.[id] || id, | ||
})), | ||
[spec.enum, spec.description], | ||
); | ||
|
||
const handleUpdate = React.useCallback( | ||
(optionValue: string, selected: boolean) => { | ||
let newValue = _value || []; | ||
|
||
if (selected) { | ||
newValue.push(optionValue); | ||
} else { | ||
newValue = newValue.filter((id) => id !== optionValue); | ||
} | ||
|
||
onChange(transformArrIn<string[], FieldArrayValue>(newValue)); | ||
}, | ||
[_value, onChange], | ||
); | ||
|
||
return ( | ||
<div | ||
className={b({vertical: spec.viewSpec.checkboxGroupParams?.placement === 'vertical'})} | ||
data-qa={name} | ||
> | ||
{options?.map(({value: optionValue, text}) => ( | ||
<Checkbox | ||
{...inputProps} | ||
qa={name && `${name}-${optionValue}`} | ||
key={optionValue} | ||
checked={_value?.includes(optionValue)} | ||
onUpdate={(selected: boolean) => handleUpdate(optionValue, selected)} | ||
disabled={ | ||
spec.viewSpec.disabled || | ||
spec.viewSpec.checkboxGroupParams?.disabled?.[optionValue] | ||
} | ||
content={text} | ||
onBlur={onBlur} | ||
onFocus={onFocus} | ||
/> | ||
))} | ||
</div> | ||
); | ||
}; |
Binary file added
BIN
+13.1 KB
...oxGroup.visual.test.tsx-snapshots/CheckboxGroup-default-dark-chromium-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+9.62 KB
...kboxGroup.visual.test.tsx-snapshots/CheckboxGroup-default-dark-webkit-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+12.7 KB
...xGroup.visual.test.tsx-snapshots/CheckboxGroup-default-light-chromium-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+9.3 KB
...boxGroup.visual.test.tsx-snapshots/CheckboxGroup-default-light-webkit-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+15.3 KB
...p.visual.test.tsx-snapshots/CheckboxGroup-default-value-dark-chromium-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+11.5 KB
...oup.visual.test.tsx-snapshots/CheckboxGroup-default-value-dark-webkit-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+14.5 KB
....visual.test.tsx-snapshots/CheckboxGroup-default-value-light-chromium-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+11.1 KB
...up.visual.test.tsx-snapshots/CheckboxGroup-default-value-light-webkit-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+12.9 KB
...xGroup.visual.test.tsx-snapshots/CheckboxGroup-disabled-dark-chromium-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+9.61 KB
...boxGroup.visual.test.tsx-snapshots/CheckboxGroup-disabled-dark-webkit-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+12.4 KB
...Group.visual.test.tsx-snapshots/CheckboxGroup-disabled-light-chromium-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+9.24 KB
...oxGroup.visual.test.tsx-snapshots/CheckboxGroup-disabled-light-webkit-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+20.4 KB
...ual.test.tsx-snapshots/CheckboxGroup-placement-vertical-dark-chromium-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+17.4 KB
...isual.test.tsx-snapshots/CheckboxGroup-placement-vertical-dark-webkit-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+19.9 KB
...al.test.tsx-snapshots/CheckboxGroup-placement-vertical-light-chromium-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+16.7 KB
...sual.test.tsx-snapshots/CheckboxGroup-placement-vertical-light-webkit-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+9.53 KB
...visual.test.tsx-snapshots/CheckboxGroup-view-horizontal-dark-chromium-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+7.98 KB
...p.visual.test.tsx-snapshots/CheckboxGroup-view-horizontal-dark-webkit-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+9.2 KB
...isual.test.tsx-snapshots/CheckboxGroup-view-horizontal-light-chromium-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+7.7 KB
....visual.test.tsx-snapshots/CheckboxGroup-view-horizontal-light-webkit-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+10.1 KB
...p.visual.test.tsx-snapshots/CheckboxGroup-view-vertical-dark-chromium-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+8.62 KB
...oup.visual.test.tsx-snapshots/CheckboxGroup-view-vertical-dark-webkit-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+9.82 KB
....visual.test.tsx-snapshots/CheckboxGroup-view-vertical-light-chromium-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+8.36 KB
...up.visual.test.tsx-snapshots/CheckboxGroup-view-vertical-light-webkit-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
47 changes: 47 additions & 0 deletions
47
src/lib/kit/components/Inputs/CheckboxGroup/__tests__/CheckboxGroup.visual.test.tsx
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,47 @@ | ||
import React from 'react'; | ||
|
||
import {CHECKBOX_GROUP, VALUE} from './helpers'; | ||
|
||
import {test} from '~playwright/core'; | ||
import {DynamicForm} from '~playwright/core/DynamicForm'; | ||
import {DynamicView} from '~playwright/core/DynamicView'; | ||
|
||
test.describe('CheckboxGroup', () => { | ||
test('default', async ({mount, expectScreenshot}) => { | ||
await mount(<DynamicForm spec={CHECKBOX_GROUP.default} />); | ||
|
||
await expectScreenshot(); | ||
}); | ||
|
||
test('default value', async ({mount, expectScreenshot}) => { | ||
await mount(<DynamicForm spec={CHECKBOX_GROUP.defaultValue} />); | ||
|
||
await expectScreenshot(); | ||
}); | ||
|
||
test('placement vertical', async ({mount, expectScreenshot}) => { | ||
await mount(<DynamicForm spec={CHECKBOX_GROUP.placementVertical} />); | ||
|
||
await expectScreenshot(); | ||
}); | ||
|
||
test('disabled', async ({mount, expectScreenshot}) => { | ||
await mount(<DynamicForm spec={CHECKBOX_GROUP.disabled} />); | ||
|
||
await expectScreenshot(); | ||
}); | ||
}); | ||
|
||
test.describe('CheckboxGroup view', () => { | ||
test('vertical', async ({mount, expectScreenshot}) => { | ||
await mount(<DynamicView spec={CHECKBOX_GROUP.placementVertical} value={VALUE.vertical} />); | ||
|
||
await expectScreenshot(); | ||
}); | ||
|
||
test('horizontal', async ({mount, expectScreenshot}) => { | ||
await mount(<DynamicView spec={CHECKBOX_GROUP.default} value={VALUE.horizontal} />); | ||
|
||
await expectScreenshot(); | ||
}); | ||
}); |
102 changes: 102 additions & 0 deletions
102
src/lib/kit/components/Inputs/CheckboxGroup/__tests__/helpers.ts
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,102 @@ | ||
import {ArraySpec, FormValue, SpecTypes} from '../../../../../core'; | ||
|
||
export const CHECKBOX_GROUP: Record<string, ArraySpec> = { | ||
default: { | ||
type: SpecTypes.Array, | ||
enum: ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'], | ||
description: { | ||
monday: 'Mon', | ||
tuesday: 'Tue', | ||
wednesday: 'Wed', | ||
thursday: 'Thu', | ||
friday: 'Fri', | ||
saturday: 'Sat', | ||
sunday: 'Sun', | ||
}, | ||
viewSpec: { | ||
type: 'checkbox_group', | ||
layout: 'row', | ||
layoutTitle: 'Days of the week', | ||
checkboxGroupParams: { | ||
disabled: { | ||
monday: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
defaultValue: { | ||
defaultValue: ['monday', 'wednesday'], | ||
type: SpecTypes.Array, | ||
enum: ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'], | ||
description: { | ||
monday: 'Mon', | ||
tuesday: 'Tue', | ||
wednesday: 'Wed', | ||
thursday: 'Thu', | ||
friday: 'Fri', | ||
saturday: 'Sat', | ||
sunday: 'Sun', | ||
}, | ||
viewSpec: { | ||
type: 'checkbox_group', | ||
layout: 'row', | ||
layoutTitle: 'Days of the week', | ||
layoutDescription: 'Description Days of the week', | ||
checkboxGroupParams: { | ||
disabled: { | ||
monday: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
placementVertical: { | ||
defaultValue: ['monday', 'wednesday'], | ||
type: SpecTypes.Array, | ||
enum: ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'], | ||
description: { | ||
monday: 'Mon', | ||
tuesday: 'Tue', | ||
wednesday: 'Wed', | ||
thursday: 'Thu', | ||
friday: 'Fri', | ||
saturday: 'Sat', | ||
sunday: 'Sun', | ||
}, | ||
viewSpec: { | ||
type: 'checkbox_group', | ||
layout: 'row', | ||
layoutTitle: 'Days of the week', | ||
layoutDescription: 'Description Days of the week', | ||
checkboxGroupParams: { | ||
placement: 'vertical', | ||
disabled: { | ||
monday: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
disabled: { | ||
type: SpecTypes.Array, | ||
enum: ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'], | ||
description: { | ||
monday: 'Mon', | ||
tuesday: 'Tue', | ||
wednesday: 'Wed', | ||
thursday: 'Thu', | ||
friday: 'Fri', | ||
saturday: 'Sat', | ||
sunday: 'Sun', | ||
}, | ||
viewSpec: { | ||
type: 'checkbox_group', | ||
layout: 'row', | ||
layoutTitle: 'Days of the week', | ||
disabled: true, | ||
}, | ||
}, | ||
}; | ||
|
||
export const VALUE: Record<string, FormValue> = { | ||
vertical: ['monday', 'wednesday', 'saturday'], | ||
horizontal: ['monday', 'wednesday', 'saturday'], | ||
}; |
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 @@ | ||
export * from './CheckboxGroup'; |
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
35 changes: 35 additions & 0 deletions
35
src/lib/kit/components/Views/CheckboxGroupView/CheckboxGroupView.scss
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,35 @@ | ||
@import '../../../styles/variables.scss'; | ||
|
||
.#{$ns}checkbox-group-view { | ||
display: flex; | ||
|
||
& > *:not(:last-child) { | ||
margin-right: 6px; | ||
} | ||
|
||
&_vertical { | ||
flex-direction: column; | ||
align-items: flex-start; | ||
|
||
& > *:not(:last-child) { | ||
margin-right: 0px; | ||
margin-bottom: 6px; | ||
} | ||
} | ||
|
||
&__tooltip { | ||
overflow-wrap: break-word; | ||
} | ||
|
||
&__tooltip-container { | ||
max-width: 100%; | ||
overflow: hidden; | ||
text-overflow: ellipsis; | ||
display: block; | ||
margin-bottom: 6px; | ||
|
||
&:last-child { | ||
margin-bottom: 0; | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/lib/kit/components/Views/CheckboxGroupView/CheckboxGroupView.tsx
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,42 @@ | ||
import React from 'react'; | ||
|
||
import {Popover} from '@gravity-ui/uikit'; | ||
import {COMMON_POPOVER_PLACEMENT} from '../../../constants/common'; | ||
import {ArrayView} from '../../../../core'; | ||
import {block} from '../../../utils'; | ||
|
||
import './CheckboxGroupView.scss'; | ||
|
||
const b = block('checkbox-group-view'); | ||
|
||
export const CheckboxGroupView: ArrayView = ({spec, value = []}) => { | ||
const _value = value as string[]; | ||
|
||
const items = React.useMemo( | ||
() => _value.map((item) => spec.description?.[item] || item), | ||
[spec.description, _value], | ||
); | ||
|
||
const verticalPlacement = React.useMemo( | ||
() => spec.viewSpec.checkboxGroupParams?.placement === 'vertical', | ||
[spec.viewSpec.checkboxGroupParams?.placement], | ||
); | ||
|
||
return ( | ||
<div className={b({vertical: verticalPlacement})}> | ||
{items.map((item, idx) => ( | ||
<Popover | ||
placement={COMMON_POPOVER_PLACEMENT} | ||
key={item} | ||
content={item} | ||
className={b('tooltip-container')} | ||
contentClassName={b('tooltip')} | ||
disabled={item.length < 51} | ||
> | ||
{item} | ||
{!verticalPlacement && idx !== items.length - 1 ? ', ' : null} | ||
</Popover> | ||
))} | ||
</div> | ||
); | ||
}; |
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 @@ | ||
export * from './CheckboxGroupView'; |
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
Oops, something went wrong.