Skip to content

Commit 9f7ed93

Browse files
authored
v5.2.3 (#612)
* Add storybook case for ButtonGroup * v5.2.3
1 parent f2dfdea commit 9f7ed93

4 files changed

Lines changed: 81 additions & 42 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@netdata/netdata-ui",
3-
"version": "5.2.2",
3+
"version": "5.2.3",
44
"description": "netdata UI kit",
55
"main": "dist/index.js",
66
"module": "dist/es6/index.js",

src/components/button/button.stories.js

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import React, { useState } from "react"
2-
import { Button, IconButton, ButtonGroup } from "."
1+
import React from "react"
2+
import { Button, IconButton } from "."
33
import { iconsList } from "@/components/icon"
44

55
const icons = Object.keys(iconsList)
@@ -17,26 +17,6 @@ export const BaseIconButton = args => (
1717
/>
1818
)
1919

20-
const radioButtonItems = [
21-
{ label: "One", value: 1 },
22-
{ label: "Two", value: 2 },
23-
{ label: "Three", value: 3 },
24-
]
25-
26-
export const RadioButtonGroup = args => {
27-
const [checked, setChecked] = useState(1)
28-
const onChange = value => setChecked(value)
29-
30-
return (
31-
<ButtonGroup
32-
items={radioButtonItems.map(item => ({ ...args, ...item }))}
33-
checked={checked}
34-
buttonProps={args}
35-
onChange={onChange}
36-
/>
37-
)
38-
}
39-
4020
export default {
4121
component: Button,
4222
tags: ["autodocs"],

src/components/button/buttonGroup.js

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { Children, isValidElement, cloneElement } from "react"
1+
import React, { Children, isValidElement, cloneElement, useCallback } from "react"
22
import Flex from "@/components/templates/flex"
33
import { Button } from "./button"
44

@@ -30,24 +30,28 @@ const Content = ({ children }) => {
3030
)
3131
}
3232

33-
const RadioButtons = ({ items, checked, buttonProps = {}, onChange }) => (
34-
<>
35-
{items.map(({ label, value, title }, index) => {
36-
const buttonGroupProps = getButtonGroupProps(index, items.length)
37-
return (
38-
<Button
39-
key={value}
40-
label={label}
41-
onClick={() => onChange(value)}
42-
{...(title ? { title } : {})}
43-
{...(checked != value ? { flavour: "hollow" } : {})}
44-
{...buttonGroupProps}
45-
{...buttonProps}
46-
/>
47-
)
48-
})}
49-
</>
50-
)
33+
const RadioButtons = ({ items, checked, buttonProps = {}, onChange }) => {
34+
return (
35+
<>
36+
{items.map(({ label, value, title }, index) => {
37+
const buttonGroupProps = getButtonGroupProps(index, items.length)
38+
const isChecked = checked === value || (Array.isArray(checked) && checked.includes(value))
39+
40+
return (
41+
<Button
42+
key={value}
43+
label={label}
44+
onClick={() => onChange(value)}
45+
{...(title ? { title } : {})}
46+
{...(!isChecked ? { flavour: "hollow" } : {})}
47+
{...buttonGroupProps}
48+
{...buttonProps}
49+
/>
50+
)
51+
})}
52+
</>
53+
)
54+
}
5155

5256
export const ButtonGroup = ({ items, checked, onChange, children, buttonProps, ...props }) => (
5357
<Flex alignItems="center" {...props}>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import React, { useCallback, useState } from "react"
2+
import { ButtonGroup } from "."
3+
4+
export const RadioButtonGroup = args => {
5+
const [checked, setChecked] = useState(args.checked)
6+
7+
return (
8+
<ButtonGroup
9+
items={args.items.map(item => ({ ...item }))}
10+
checked={checked}
11+
buttonProps={args}
12+
onChange={setChecked}
13+
/>
14+
)
15+
}
16+
17+
export const MultiButtonGroup = args => {
18+
const [checked, setChecked] = useState([args.checked])
19+
20+
const onChange = useCallback(
21+
value => {
22+
setChecked(prev => {
23+
if (prev.includes(value)) return prev.filter(v => v !== value)
24+
return [...prev, value]
25+
})
26+
},
27+
[setChecked]
28+
)
29+
30+
return (
31+
<ButtonGroup
32+
items={args.items.map(item => ({ ...item }))}
33+
checked={checked}
34+
buttonProps={args}
35+
onChange={onChange}
36+
/>
37+
)
38+
}
39+
40+
export default {
41+
component: ButtonGroup,
42+
tags: ["autodocs"],
43+
args: {
44+
items: [
45+
{ label: "One", value: 1 },
46+
{ label: "Two", value: 2 },
47+
{ label: "Three", value: 3 },
48+
],
49+
checked: 1,
50+
buttonProps: {},
51+
},
52+
argTypes: {
53+
checked: { control: "text" },
54+
},
55+
}

0 commit comments

Comments
 (0)