-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathRangeField.tsx
48 lines (44 loc) · 1.35 KB
/
RangeField.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import {assignInlineVars} from '@vanilla-extract/dynamic';
import {
DynamicProps,
WithRef,
} from 'solid-headless/dist/types/utils/dynamic-prop';
import {Component} from 'solid-js';
import {omitProps} from 'solid-use/props';
import {styled} from '../../utils';
import {UseTextProps} from '../Text';
import * as styles from './RangeField.css';
type RangeFieldProps = {
value?: number;
onChange?: (value: number) => void;
size?: UseTextProps['size'];
} & WithRef<'input'> &
Omit<DynamicProps<'input'>, 'as' | 'ref' | 'onInput' | 'onChange'>;
export const RangeField: Component<RangeFieldProps> = props => {
function onChange(e: Event): void {
if (props.onChange) {
const target = e.target as HTMLInputElement;
props.onChange(parseInt(target.value, 10));
}
}
const progress = () => {
const value = Number(props.value);
const min = props.min ? Number(props.min) : 0;
const max = props.max ? Number(props.max) : 100;
const percent = (value - min) / (max - min);
return `${percent * 100}%`;
};
return (
<styled.input
class={styles.range}
value={props.value}
onInput={onChange}
onChange={onChange}
type={'range'}
{...omitProps(props, ['class', 'type', 'value', 'onChange'])}
style={assignInlineVars({
[styles.rangeVars.rangeProgress]: progress(),
})}
/>
);
};