Skip to content

Commit 628966f

Browse files
committed
Implement RadioItems and condense types
1 parent 88391b4 commit 628966f

File tree

8 files changed

+156
-383
lines changed

8 files changed

+156
-383
lines changed

components/dash-core-components/src/components/RadioItems.react.js

Lines changed: 0 additions & 221 deletions
This file was deleted.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import React, {useMemo} from 'react';
2+
import {RadioItemsProps, PersistedProps, PersistenceTypes} from '../types';
3+
import './css/radioitems.css';
4+
5+
import {sanitizeOptions} from '../utils/optionTypes';
6+
import LoadingElement from '../utils/_LoadingElement';
7+
import {OptionsList} from '../utils/optionRendering';
8+
import {isNil} from 'ramda';
9+
10+
/**
11+
* RadioItems is a component that encapsulates several radio item inputs.
12+
* The values and labels of the RadioItems is specified in the `options`
13+
* property and the seleced item is specified with the `value` property.
14+
* Each radio item is rendered as an input with a surrounding label.
15+
*/
16+
export default function RadioItems({
17+
className,
18+
id,
19+
style,
20+
setProps,
21+
value,
22+
inputStyle = {},
23+
inputClassName = '',
24+
labelStyle = {},
25+
labelClassName = '',
26+
options = [],
27+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
28+
persisted_props = [PersistedProps.value],
29+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
30+
persistence_type = PersistenceTypes.local,
31+
inline = false,
32+
}: RadioItemsProps) {
33+
const sanitizedOptions = useMemo(() => {
34+
return sanitizeOptions(options);
35+
}, [options]);
36+
37+
const stylingProps = {
38+
id,
39+
className: 'dash-radioitems ' + (className ?? ''),
40+
style,
41+
optionClassName: inline ? 'dash-radioitems-inline' : undefined,
42+
inputStyle,
43+
inputClassName,
44+
labelStyle,
45+
labelClassName,
46+
};
47+
48+
return (
49+
<LoadingElement>
50+
{loadingProps => (
51+
<OptionsList
52+
{...loadingProps}
53+
inputType="radio"
54+
options={sanitizedOptions}
55+
selected={isNil(value) ? [] : [value]}
56+
onSelectionChange={selection => {
57+
setProps({value: selection[selection.length - 1]});
58+
}}
59+
{...stylingProps}
60+
/>
61+
)}
62+
</LoadingElement>
63+
);
64+
}
65+
66+
RadioItems.dashPersistence = {
67+
persisted_props: [PersistedProps.value],
68+
persistence_type: PersistenceTypes.local,
69+
};
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
.dash-options-list-option.dash-checklist-inline {
2-
display: inline-block;
2+
display: inline-flex;
33
margin-right: calc(var(--Dash-Spacing) * 4);
44
}

components/dash-core-components/src/components/css/optionslist.css

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
display: flex;
77
align-items: center;
88
user-select: none;
9-
overflow: hidden;
109
}
1110

1211
.dash-options-list-option:not(:has(input[disabled])):hover,
@@ -22,10 +21,8 @@
2221

2322
.dash-options-list-option-text {
2423
white-space: pre;
25-
}
26-
27-
.dash-options-list-option-text > * {
28-
vertical-align: middle;
24+
display: flex;
25+
align-items: center;
2926
}
3027

3128
.dash-options-list-option-checkbox {
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.dash-options-list-option.dash-radioitems-inline {
2+
display: inline-flex;
3+
margin-right: calc(var(--Dash-Spacing) * 4);
4+
}

components/dash-core-components/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import Link from './components/Link.react';
1515
import Loading from './components/Loading.react';
1616
import Location from './components/Location.react';
1717
import Markdown from './components/Markdown.react';
18-
import RadioItems from './components/RadioItems.react';
18+
import RadioItems from './components/RadioItems';
1919
import RangeSlider from './components/RangeSlider';
2020
import Slider from './components/Slider';
2121
import Store from './components/Store.react';

0 commit comments

Comments
 (0)