ValueField
A numeric readout you drag, step, or type into.
Use ValueField for exact entry. Drag or use the arrow keys and wheel to change the value; double-click or press Enter to type one.
import { ValueField, formatters, parsers } from '@soundor/components';
import { View } from '@soundor/react';
import { useState } from 'react';
export default function Demo() {
const [time, setTime] = useState(1500);
return (
<View>
<ValueField
label="Time"
min={1}
max={2000}
value={time}
onChange={setTime}
format={formatters.time}
parse={parsers.number}
width={88}
/>
</View>
);
}value-field.tsx, running in the web runtime.SourceTyping a value
Editing shows the value as text, unit included, with only the number selected.
- A bare number keeps the prefix and unit shown. With
1.50 sshowing, typing2gives2.00 s; withL 30, typing20givesL 20. - Nothing moves. The text starts at the same place in the preview and in the input, so editing starts without a shift, and typing changes only what follows the caret.
- Typed text wins. Type
800 msorR 20, or select everything first, and the value is read as you typed it.formatandparsedecide which units exist:parsers.numberreadsms,s,k,kHz,Hz,dBand%. - A long value is shown whole. If the value is wider than the field, its text is made smaller to fit, down to 7 px; past that it is clipped on one line. While you type the field keeps its size and scrolls the text inside it. See Long values for a width that never shrinks it.
- Invalid text is rejected. The field's border turns red and the value stays as it was. Escape cancels.
Examples
A prefix and a suffix are not props: they are the text around the number in what format returns. The field finds the number in that text and selects only the number when you start typing. A prefix and a suffix stay in the text, so you can change them as well. The first demo above has a suffix, s or ms.
Plain number
format returns the number alone, so there is nothing around it.
import { ValueField } from '@soundor/components';
import { useState } from 'react';
export default function Demo() {
const [voices, setVoices] = useState(8);
// No unit and no prefix: `format` returns the number alone.
return (
<ValueField
label="Voices"
min={1}
max={16}
step={1}
value={voices}
onChange={setVoices}
format={(value) => String(Math.round(value))}
width={64}
/>
);
}value-field-plain.tsx, running in the web runtime.SourceWith a prefix
formatters.pan writes L 30, C or R 30. The letter is a prefix: it stays in the text with only 30 selected, so typing 20 keeps L, and typing R 20 changes the side.
import { ValueField, formatters, parsers } from '@soundor/components';
import { useState } from 'react';
export default function Demo() {
const [pan, setPan] = useState(-0.3);
// `formatters.pan` gives "L 30", "C" or "R 30": the letter is the prefix.
// Only "30" is selected, so typing 20 keeps the L, and typing "R 20" changes
// it: `parsers.pan` reads "L30", "r 12" or "-40".
return (
<ValueField
label="Pan"
min={-1}
max={1}
value={pan}
onChange={setPan}
format={formatters.pan}
parse={parsers.pan}
width={72}
/>
);
}value-field-prefix.tsx, running in the web runtime.SourceWith a prefix and a suffix
Both at once: BW 1.50 oct. Typing 2 gives BW 2.00 oct, and selecting everything lets you retype all of it. A parse function that finds the number in any text accepts 2, 2 oct or BW 2.
import { ValueField } from '@soundor/components';
import { useState } from 'react';
// "BW 1.50 oct": "BW " is the prefix, " oct" the suffix. Only the number is
// selected for typing, and a bare number keeps both.
const format = (octaves: number) => `BW ${octaves.toFixed(2)} oct`;
// Read the number out of whatever was typed, with or without the words.
function parse(text: string) {
const found = /[+\-−]?\d+(?:[.,]\d+)?|[.,]\d+/.exec(text);
if (!found) return null;
const value = Number(found[0].replace(',', '.').replace('−', '-'));
return Number.isFinite(value) ? value : null;
}
export default function Demo() {
const [width, setWidth] = useState(1.5);
return (
<ValueField
label="Bandwidth"
min={0.1}
max={4}
value={width}
onChange={setWidth}
format={format}
parse={parse}
width={104}
/>
);
}value-field-both.tsx, running in the web runtime.SourceLong values
A value that does not fit is made smaller until it does, so it is always readable in full. This is a safety net more than a design: the field should be wide enough for its longest value.
Drag the slider to sweep a frequency from 20 Hz to 20 kHz. The longest text, 20.0 kHz, is 56 px wide in the default type, and the field adds 18 px of padding and border. A width of 76 shows every value at full size, and 56 shrinks the longer ones.
import { Slider, ValueField, formatters, parsers } from '@soundor/components';
import { View } from '@soundor/react';
import { useState } from 'react';
export default function Demo() {
const [hz, setHz] = useState(20_000);
// Sweep the value across its range. The longest text, "20.0 kHz", is 56 px
// wide in the default type: with the field's padding and border, 76 fits
// every value, and 56 does not.
const field = { value: hz, onChange: setHz, min: 20, max: 20_000 };
return (
<View style={{ gap: 16, alignItems: 'center' }}>
<Slider
label="Sweep"
scale="log"
format={formatters.frequency}
width={260}
{...field}
/>
<View style={{ flexDirection: 'row', gap: 16 }}>
<ValueField
label="56 px"
scale="log"
format={formatters.frequency}
parse={parsers.number}
width={56}
{...field}
/>
<ValueField
label="76 px"
scale="log"
format={formatters.frequency}
parse={parsers.number}
width={76}
{...field}
/>
</View>
</View>
);
}value-field-overflow.tsx, running in the web runtime.SourceTo find your own width, take the longest text format can return, usually at the minimum, the maximum and a few values between, and use 7 px per character in the default type, plus 18. For 20.0 kHz that is 8 × 7 + 18 = 74, so 76 leaves a little room. A different type size or font needs a different figure: measure the longest text in the demo and add 18.
Usage
With a parameter
Pass a generated parameter and the control inherits its range, mapping, formatting, parser and reset value. It opens and closes host gestures and records undo history; set history={false} to opt out, or history={{ label: 'Adjust feedback' }} to name the entry.
<ValueField parameter={parameters.time} width={72} />Without a parameter
Pass value and onChange (or defaultValue) with the range you need. A controlled control should update value in response to onChange.
const [time, setTime] = useState(250);
<ValueField
label="Time"
min={1}
max={2000}
value={time}
onChange={setTime}
format={formatters.time}
parse={parsers.number}
/>;Keyboard and accessibility
The field takes the same keys as a Knob while it is focused. Enter opens it for typing, Enter again applies and Esc leaves the value as it was. A double-click opens it for typing too, so the pointer's reset is Alt-click (Option-click on a Mac); from the keyboard, Delete or Backspace resets it. While you type, the field is a text input, so the platform's own editing and selection shortcuts apply. An invalid entry is announced with the hint "Enter a valid value". See Keyboard and accessibility.
Props
See Mixed values for editing multiple items.
ValueField props
Prop
Type
Range props
Prop
Type