1 | ---
|
2 | title: Float Input
|
3 | storybookPath: forms-float-input--default
|
4 | isExperimentalPackage: true
|
5 | ---
|
6 |
|
7 | Float input provides a way for inputting float values.
|
8 |
|
9 | ## Usage
|
10 |
|
11 | ### Field
|
12 |
|
13 | The component must be nested within a [`Field`](/package/field). See
|
14 | [`Field`](/package/field) for more details.
|
15 |
|
16 | ## Examples
|
17 |
|
18 | ### Controlled
|
19 |
|
20 | A `FloatInput` can be either controlled or uncontrolled. To control a
|
21 | `FloatInput` provide a `value`, as well as an `onChange` function to set the new
|
22 | value when the select is updated.
|
23 |
|
24 | ```jsx live
|
25 | const [value, setValue] = React.useState(1000000.101);
|
26 |
|
27 | return (
|
28 | <Stack gap="large">
|
29 | <Field label="Example controlled">
|
30 | <FloatInput value={value} onChange={v => setValue(v)} />
|
31 | </Field>
|
32 | <Text>The current value is: {value}</Text>
|
33 | </Stack>
|
34 | );
|
35 | ```
|
36 |
|
37 | #### Validation
|
38 |
|
39 | The provided controlled value can be of type string or number. Valid numbers are
|
40 | represented as numbers and everything else as a string. This allows for easy
|
41 | passing of floats to other system, whilst also giving a way to check for invalid
|
42 | string values and provide an appropriate validation error.
|
43 |
|
44 | ```jsx live
|
45 | const [value, setValue] = React.useState('Hi there');
|
46 |
|
47 | const isInvalid = typeof value === 'string';
|
48 |
|
49 | return (
|
50 | <Stack gap="large">
|
51 | <Field
|
52 | label="Example controlled validation"
|
53 | tone={isInvalid && 'critical'}
|
54 | message={isInvalid && 'Please provide a valid float input'}
|
55 | >
|
56 | <FloatInput value={value} onChange={v => setValue(v)} />
|
57 | </Field>
|
58 | <Text>The current value is: {value}</Text>
|
59 | <Text>The value type is: {typeof value}</Text>
|
60 | </Stack>
|
61 | );
|
62 | ```
|
63 |
|
64 | ### Uncontrolled
|
65 |
|
66 | A `FloatInput` can also be uncontrolled, managing it's own internal state. To
|
67 | access the value, instead of writing an onChange handler, you would use a ref to
|
68 | get form values from the DOM.
|
69 |
|
70 | ```jsx live
|
71 | const inputRef = React.useRef(null);
|
72 | const [value, setValue] = React.useState('');
|
73 | const showValueHandler = React.useCallback(() => {
|
74 | setValue(inputRef.current?.value);
|
75 | }, [setValue]);
|
76 |
|
77 | return (
|
78 | <Stack gap="large">
|
79 | <Field label="Example uncontrolled">
|
80 | <FloatInput ref={inputRef} />
|
81 | </Field>
|
82 | <Button onClick={showValueHandler}>Show input value</Button>
|
83 | <Text>The input value is: {value}</Text>
|
84 | </Stack>
|
85 | );
|
86 | ```
|
87 |
|
88 | ### Format fraction digits
|
89 |
|
90 | You can also set to what fraction digit you want the displayed value in the
|
91 | `FloatInput` to be.
|
92 |
|
93 | ```jsx live
|
94 | const [value, setValue] = React.useState(10000.101);
|
95 |
|
96 | return (
|
97 | <Stack gap="large">
|
98 | <Field label="Example format fraction digits">
|
99 | <FloatInput
|
100 | fractionDigits={2}
|
101 | value={value}
|
102 | onChange={v => setValue(v)}
|
103 | />
|
104 | </Field>
|
105 | <Text>The current value is: {value}</Text>
|
106 | </Stack>
|
107 | );
|
108 | ```
|
109 |
|
110 | ### Input Adornments
|
111 |
|
112 | Similar to [TextInput](/package/text-input/), you can also add adornments to the
|
113 | `FloatInput` component (at the start or end).
|
114 |
|
115 | ```jsx live
|
116 | const [value, setValue] = React.useState(10000.101);
|
117 |
|
118 | return (
|
119 | <Stack gap="large">
|
120 | <Field label="Example format fraction digits">
|
121 | <FloatInput fractionDigits={2} value={value} onChange={v => setValue(v)}>
|
122 | <InputAdornment placement="end">
|
123 | <Text>kW</Text>
|
124 | </InputAdornment>
|
125 | </FloatInput>
|
126 | </Field>
|
127 | <Text>The current value is: {value}</Text>
|
128 | </Stack>
|
129 | );
|
130 | ```
|
131 |
|
132 | ## Props
|
133 |
|
134 | <PropsTable displayName="FloatInput" />
|
135 |
|
136 | Additional props also include [`TextInput`](/package/text-input) props which are
|
137 | not listed here.
|
138 |
|
139 | [adornment-children]:
|
140 | https://github.com/brighte-labs/spark-web/blob/d4da46200f2d6e5e9291d3c650eaaff7e53f411b/packages/text-input/src/childrenToAdornments.tsx#L12
|