Skip to content

Commit 4e4b11a

Browse files
committed
more-slots
1 parent 82092fe commit 4e4b11a

File tree

6 files changed

+106
-3
lines changed

6 files changed

+106
-3
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-declarative-mantine",
3-
"version": "0.0.31",
3+
"version": "0.0.32",
44
"description": "The Mantine ui kit bindings for react-declarative",
55
"private": false,
66
"author": {

src/OneSlotFactory/OneSlotFactory.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ import Date from './components/Date';
1212
import Tree from './components/Tree';
1313
import Time from './components/Time';
1414
import ComboArray from './components/ComboArray';
15+
import Switch from './components/Switch';
16+
import Radio from './components/Radio';
17+
import CheckBox from './components/CheckBox';
1518

1619
interface IOneSlotFactoryProps {
1720
children: React.ReactNode;
@@ -28,6 +31,9 @@ export const defaultSlots = {
2831
Time,
2932
Tree,
3033
YesNo,
34+
Switch,
35+
Radio,
36+
CheckBox,
3137
};
3238

3339
export const OneSlotFactory = ({
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import * as React from 'react';
2+
3+
import { Checkbox as UiCheckBox } from '@mantine/core';
4+
5+
import { ICheckBoxSlot } from 'react-declarative';
6+
7+
export const CheckBox = ({
8+
disabled,
9+
onChange,
10+
title,
11+
value,
12+
}: ICheckBoxSlot) => (
13+
<UiCheckBox
14+
size="md"
15+
disabled={disabled}
16+
checked={Boolean(value)}
17+
onChange={() => onChange(!value)}
18+
label={title}
19+
/>
20+
);
21+
22+
export default CheckBox;
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import * as React from 'react';
2+
import { useCallback, useEffect, useMemo } from 'react';
3+
4+
import { Radio as UiRadio } from '@mantine/core';
5+
6+
import { useOneRadio, useActualValue, IRadioSlot } from 'react-declarative';
7+
8+
export const Radio = ({
9+
disabled,
10+
onChange,
11+
title,
12+
radioValue,
13+
value,
14+
name = '',
15+
}: IRadioSlot) => {
16+
const [radioMap, setRadioMap] = useOneRadio();
17+
const radioMap$ = useActualValue(radioMap);
18+
19+
const checked = useMemo(() => {
20+
return radioMap[name] === radioValue;
21+
}, [radioMap]);
22+
23+
const setValue = useCallback((value: string | null) => setRadioMap((prevRadioMap) => ({
24+
...prevRadioMap,
25+
[name]: value,
26+
})), []);
27+
28+
const handleChange = useCallback((value: string | null) => {
29+
setValue(value);
30+
onChange(value);
31+
}, []);
32+
33+
useEffect(() => {
34+
const { current: radioMap } = radioMap$;
35+
if (value !== radioMap[name]) {
36+
setValue(value);
37+
}
38+
}, [value]);
39+
40+
return (
41+
<UiRadio
42+
size="md"
43+
checked={checked}
44+
disabled={disabled}
45+
onChange={() => handleChange(radioValue || null)}
46+
label={title}
47+
/>
48+
);
49+
};
50+
51+
export default Radio;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import * as React from "react";
2+
3+
import { Switch as UiSwitch } from '@mantine/core';
4+
5+
import { ISwitchSlot } from 'react-declarative';
6+
7+
export const Switch = ({
8+
disabled,
9+
value,
10+
onChange,
11+
title,
12+
}: ISwitchSlot) => {
13+
return (
14+
<UiSwitch
15+
size="md"
16+
checked={Boolean(value)}
17+
disabled={disabled}
18+
onChange={() => onChange(!value)}
19+
label={title}
20+
/>
21+
);
22+
};
23+
24+
export default Switch;

0 commit comments

Comments
 (0)