Skip to content

Commit 4eb8255

Browse files
author
Artur Bien
committed
feat(slider): add slider component
1 parent b04685a commit 4eb8255

File tree

7 files changed

+430
-19
lines changed

7 files changed

+430
-19
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import React from 'react';
2+
import { Panel, Slider, Fieldset } from 'react95-native';
3+
4+
const restrictedValues = [0, 20, 80, 100].map(n => ({
5+
label: `${n.toString()}°C`,
6+
value: n,
7+
}));
8+
9+
const DividerExample = () => {
10+
const [value, setValue] = React.useState(0);
11+
const [withTicksValue, setWithTicksValue] = React.useState(0);
12+
13+
const [restrictedValue, setRestrictedValue] = React.useState(
14+
restrictedValues[0].value,
15+
);
16+
17+
const handleChange = (newValue: number) => {
18+
setValue(newValue);
19+
};
20+
21+
return (
22+
<Panel style={{ flex: 1, padding: 16 }}>
23+
<Fieldset label='Default:' style={{ padding: 16 }}>
24+
<Slider
25+
onChange={handleChange}
26+
onChangeCommitted={v => console.warn('onChangeCommited', v)}
27+
value={value}
28+
/>
29+
</Fieldset>
30+
31+
<Fieldset label='With ticks:' style={{ padding: 16 }}>
32+
<Slider
33+
onChange={newValue => setWithTicksValue(newValue)}
34+
onChangeCommitted={v => console.warn('onChangeCommited', v)}
35+
value={withTicksValue}
36+
marks
37+
step={10}
38+
style={{ width: '80%' }}
39+
/>
40+
</Fieldset>
41+
42+
<Fieldset label='Restricted values:' style={{ padding: 16 }}>
43+
<Slider
44+
onChange={newValue => setRestrictedValue(newValue)}
45+
onChangeCommitted={v => console.warn('onChangeCommited', v)}
46+
value={restrictedValue}
47+
marks={restrictedValues}
48+
step={null}
49+
/>
50+
</Fieldset>
51+
52+
<Fieldset label='Disabled:' style={{ padding: 16 }}>
53+
<Slider
54+
disabled
55+
// value={restrictedValue}
56+
marks={restrictedValues}
57+
step={null}
58+
/>
59+
</Fieldset>
60+
</Panel>
61+
);
62+
};
63+
64+
export default DividerExample;

example/src/examples/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import ScrollPanelExample from './ScrollPanelExample';
1818
import ScrollViewExample from './ScrollViewExample';
1919
import SelectBoxExample from './SelectBoxExample';
2020
import SelectExample from './SelectExample';
21+
import SliderExample from './SliderExample';
2122
import SnackbarExample from './SnackbarExample';
2223
import TabsExample from './TabsExample';
2324
import TextInputExample from './TextInputExample';
@@ -51,6 +52,7 @@ export default [
5152
component: SelectBoxExample,
5253
title: 'SelectBox',
5354
},
55+
{ name: 'SliderExample', component: SliderExample, title: 'Slider' },
5456
{
5557
name: 'TextInputExample',
5658
component: TextInputExample,

src/NumberInput/NumberInput.tsx

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,23 @@ import { View, StyleProp, StyleSheet, ViewStyle } from 'react-native';
33
import useControlledOrUncontrolled from '../common/hooks/useControlledOrUncontrolled';
44
import type { DimensionValue } from '../types';
55
import { blockSizes } from '../common/styles';
6+
import { clamp } from '../common/utils';
67

78
import { TextInput, Button, Text } from '..';
89

9-
type OptionalNumber = number | null;
10-
1110
type Props = {
1211
defaultValue?: number;
1312
disabled?: boolean;
1413
inputWidth?: DimensionValue;
15-
max?: OptionalNumber;
16-
min?: OptionalNumber;
14+
max?: number | null;
15+
min?: number | null;
1716
onChange?: (value: number) => void;
1817
step?: number;
1918
style?: StyleProp<ViewStyle>;
2019
value?: number;
2120
variant?: 'default' | 'flat';
2221
};
2322

24-
// TODO: test clamp function
25-
export function clamp(
26-
value: number,
27-
min: OptionalNumber,
28-
max: OptionalNumber,
29-
): number {
30-
if (max !== null && value > max) {
31-
return max;
32-
}
33-
if (min !== null && value < min) {
34-
return min;
35-
}
36-
return value;
37-
}
38-
3923
// TODO: accessibility etc.
4024
// TODO: allow to center input text horizontally
4125
const NumberInput = ({

0 commit comments

Comments
 (0)