@groupeactual/ui-kit 1.0.20 → 1.0.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,18 +1,18 @@
1
1
  import React from 'react';
2
2
  import { RadioGroupProps } from '@mui/material';
3
- interface ItemType<T = string> {
3
+ interface ItemType<T> {
4
4
  label: string;
5
5
  value: T;
6
6
  }
7
- interface Props extends Omit<RadioGroupProps, 'onChange'> {
7
+ interface Props<T> extends Omit<RadioGroupProps, 'onChange'> {
8
8
  label: string;
9
- options: ItemType[];
10
- onChange: (value: string) => void;
9
+ options: ItemType<T>[];
10
+ onChange: (value: T) => void;
11
11
  error?: string;
12
12
  disabled?: boolean;
13
13
  }
14
14
  declare const RadioGroup: {
15
- ({ options, defaultValue, label, onChange, disabled, error }: Props): React.JSX.Element;
15
+ <T>({ options, value, defaultValue, label, onChange, disabled, error }: Props<T>): React.JSX.Element;
16
16
  displayName: string;
17
17
  };
18
18
  export default RadioGroup;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@groupeactual/ui-kit",
3
- "version": "1.0.20",
3
+ "version": "1.0.22",
4
4
  "type": "module",
5
5
  "description": "A simple template for a custom React component library",
6
6
  "devDependencies": {
@@ -42,22 +42,22 @@
42
42
  "@emotion/react": "^11.11.1",
43
43
  "@emotion/styled": "^11.11.0",
44
44
  "@emotion/serialize": "^1.1.2",
45
- "@fortawesome/fontawesome-common-types": "^6.4.0",
46
- "@fortawesome/fontawesome-svg-core": "^6.4.0",
47
- "@fortawesome/pro-regular-svg-icons": "^6.4.0",
48
- "@fortawesome/pro-solid-svg-icons": "^6.4.0",
45
+ "@fortawesome/fontawesome-common-types": "^6.4.2",
46
+ "@fortawesome/fontawesome-svg-core": "^6.4.2",
47
+ "@fortawesome/pro-regular-svg-icons": "git+https://github.com/actualtysoft/pro-regular-svg-icons.git#6.4.2",
48
+ "@fortawesome/pro-solid-svg-icons": "git+https://github.com/actualtysoft/pro-solid-svg-icons.git#6.4.2",
49
49
  "@mui/base": "5.0.0-beta.5",
50
50
  "@mui/material": "^5.13.6",
51
51
  "@mui/system": "^5.13.6",
52
52
  "react": "^18.2.0",
53
53
  "react-dom": "^18.2.0",
54
- "@groupeactual/design-tokens": "1.0.20"
54
+ "@groupeactual/design-tokens": "1.0.22"
55
55
  },
56
56
  "peerDependencies": {
57
- "@fortawesome/fontawesome-common-types": "^6.3.0",
58
- "@fortawesome/fontawesome-svg-core": "^6.3.0",
59
- "@fortawesome/pro-regular-svg-icons": "^6.3.0",
60
- "@fortawesome/pro-solid-svg-icons": "^6.3.0",
57
+ "@fortawesome/fontawesome-common-types": "^6.4.2",
58
+ "@fortawesome/fontawesome-svg-core": "^6.4.2",
59
+ "@fortawesome/pro-regular-svg-icons": "git+https://github.com/actualtysoft/pro-regular-svg-icons.git#6.4.2",
60
+ "@fortawesome/pro-solid-svg-icons": "git+https://github.com/actualtysoft/pro-solid-svg-icons.git#6.4.2",
61
61
  "@mui/base": "^5.0.0-alpha.108",
62
62
  "@mui/material": "^5.12.0",
63
63
  "@mui/system": "^5.12.0",
@@ -1,4 +1,5 @@
1
- import React, { useMemo, useState } from 'react';
1
+ /* eslint-disable no-undefined */
2
+ import React, { useMemo, useState, useEffect } from 'react';
2
3
  import {
3
4
  FormControl,
4
5
  FormControlLabel,
@@ -14,48 +15,58 @@ import {
14
15
 
15
16
  import { RadioStyle } from '@groupeactual/design-tokens';
16
17
 
17
- interface ItemType<T = string> {
18
+ interface ItemType<T> {
18
19
  label: string;
19
20
  value: T;
20
21
  }
21
22
 
22
- interface Props extends Omit<RadioGroupProps, 'onChange'> {
23
+ interface Props<T> extends Omit<RadioGroupProps, 'onChange'> {
23
24
  label: string;
24
- options: ItemType[];
25
- onChange: (value: string) => void;
25
+ options: ItemType<T>[];
26
+ onChange: (value: T) => void;
26
27
  error?: string;
27
28
  disabled?: boolean;
28
29
  }
29
30
 
30
- const RadioGroup = ({
31
+ const RadioGroup = <T,>({
31
32
  options,
33
+ value,
32
34
  defaultValue,
33
35
  label,
34
36
  onChange,
35
37
  disabled = false,
36
38
  error = ''
37
- }: Props) => {
39
+ }: Props<T>) => {
38
40
  const theme = useTheme();
39
41
  const StyledRadioForm = useMemo(
40
42
  () => styled(FormControl)(RadioStyle(theme)),
41
43
  [theme]
42
44
  );
43
45
 
46
+ const isControlled = value !== undefined;
44
47
  const [selectedOption, setSelectedOption] = useState(defaultValue || '');
45
48
 
49
+ useEffect(() => {
50
+ if (!isControlled) {
51
+ setSelectedOption(defaultValue || '');
52
+ }
53
+ }, [defaultValue, isControlled]);
54
+
46
55
  const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
47
- const value = (event.target as HTMLInputElement).value;
48
- setSelectedOption(value);
49
- onChange(value);
56
+ const newValue = (event.target as HTMLInputElement).value as T;
57
+ if (!isControlled) {
58
+ setSelectedOption(newValue);
59
+ }
60
+ onChange(newValue);
50
61
  };
51
62
 
52
63
  const getFormControlClassName = useMemo(() => {
53
64
  const classNames: string[] = [];
54
65
  if (disabled) classNames.push('Mui-disabled');
55
- if (!selectedOption && error) classNames.push('Mui-error');
66
+ if (!selectedOption && !isControlled && error) classNames.push('Mui-error');
56
67
 
57
68
  return classNames.join(' ');
58
- }, [options, disabled, selectedOption, error]);
69
+ }, [disabled, selectedOption, error, isControlled]);
59
70
 
60
71
  return (
61
72
  <StyledRadioForm
@@ -65,8 +76,8 @@ const RadioGroup = ({
65
76
  >
66
77
  <FormLabel>{label}</FormLabel>
67
78
  <RadioGroupMUI
68
- defaultValue={defaultValue}
69
- value={selectedOption}
79
+ defaultValue={isControlled ? undefined : defaultValue}
80
+ value={isControlled ? value : selectedOption}
70
81
  onChange={handleChange}
71
82
  >
72
83
  {options?.slice(0, 7).map((option, index) => (