-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathindex.tsx
52 lines (46 loc) · 1.49 KB
/
index.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
49
50
51
52
import { create, tsx } from '@dojo/framework/core/vdom';
import theme from '../middleware/theme';
import TextInput, { BaseInputProperties, TextInputChildren } from '../text-input';
import * as textInputCss from '../theme/default/text-input.m.css';
import * as numberInputCss from '../theme/default/number-input.m.css';
export interface NumberInputProperties extends BaseInputProperties<{ value: number }> {
/** The min value a number can be */
min?: number;
/** The max value a number can be */
max?: number;
/** The step to increment the number value by */
step?: number;
/** Represents if the input value is valid */
valid?: { valid?: boolean; message?: string } | boolean;
}
const factory = create({ theme })
.properties<NumberInputProperties>()
.children<TextInputChildren | undefined>();
export default factory(function NumberInput({ properties, children, middleware: { theme } }) {
const { initialValue, value, onValue } = properties();
function onValueAdapter(valueAsString: string | undefined) {
if (!onValue) {
return;
}
if (valueAsString === undefined || valueAsString === '') {
onValue();
} else {
onValue(parseFloat(valueAsString));
}
}
return (
<TextInput
{...properties()}
value={value === undefined ? value : `${value}`}
initialValue={initialValue === undefined ? initialValue : `${initialValue}`}
onValue={onValueAdapter}
type="number"
theme={theme.compose(
textInputCss,
numberInputCss
)}
>
{children()[0]}
</TextInput>
);
});