Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion ui/src/components/BeamlineAttribute/BeamlineAttribute.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ import BeamlineAttributeForm from './BeamlineAttributeForm';

function BeamlineAttribute(props) {
const { attribute, format, precision = 1, suffix, onSave, onCancel } = props;
const { state, value = 0, step = 0.1, msg, readonly = false } = attribute;
const {
state,
value = 0,
step = 0.1,
limits,
msg,
readonly = false,
} = attribute;

const isBusy = state === HW_STATE.BUSY;

Expand Down Expand Up @@ -49,6 +56,7 @@ function BeamlineAttribute(props) {
isBusy={isBusy}
step={step}
precision={precision}
limits={limits}
onSave={onSave}
onCancel={onCancel}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@
}

.input {
width: 8em;
width: 9em;
height: 36px;
appearance: auto !important;
}

.error {
color: var(--bs-form-invalid-color);
line-height: 1;
margin-top: 0.625rem;
margin-bottom: 0;
}
89 changes: 51 additions & 38 deletions ui/src/components/BeamlineAttribute/BeamlineAttributeForm.jsx
Original file line number Diff line number Diff line change
@@ -1,60 +1,73 @@
import { useEffect, useRef } from 'react';
import { useEffect } from 'react';
import { Button, ButtonToolbar, Form } from 'react-bootstrap';
import { useForm } from 'react-hook-form';
import { TiTick, TiTimes } from 'react-icons/ti';

import styles from './BeamlineAttribute.module.css';

function BeamlineAttributeForm(props) {
const { value, isBusy, step, precision, onSave, onCancel } = props;
const inputRef = useRef(null);
const { value, isBusy, step, precision, limits, onSave, onCancel } = props;

const {
register,
setFocus,
setError,
handleSubmit: makeOnSubmit,
formState: { isDirty, errors },
} = useForm({ defaultValues: { value: value.toFixed(precision) } });

useEffect(() => {
if (!isBusy) {
setTimeout(() => {
/* Focus and select text when popover opens and every time a value is applied.
* Timeout ensures this works when opening a popover while another is already opened. */
inputRef.current?.focus({ preventScroll: true });
inputRef.current?.select();
setFocus('value', { shouldSelect: true });
}, 0);
}
}, [isBusy]);

function handleSubmit(evt) {
evt.preventDefault();
const formData = new FormData(evt.target);
const strVal = formData.get('value');
}, [isBusy, setFocus]);

const numVal =
typeof strVal === 'string' ? Number.parseFloat(strVal) : Number.NaN;

if (!Number.isNaN(numVal)) {
onSave(numVal);
async function handleSubmit(data) {
try {
await onSave(data.value);
} catch {
setError('value', { message: 'Unable to set value' });
}
}

const minMaxMsg = `Allowed range: [${limits
.map((v) => v.toFixed(precision))
.join(', ')}]`;

return (
<Form className="d-flex" noValidate onSubmit={handleSubmit}>
<Form.Control
ref={inputRef}
className={styles.input}
name="value"
type="number"
step={step}
defaultValue={value.toFixed(precision)}
disabled={isBusy}
aria-label="Value"
/>
<ButtonToolbar className="ms-1">
{isBusy ? (
<Button variant="danger" size="sm" onClick={() => onCancel()}>
<TiTimes size="1.5em" />
</Button>
) : (
<Button type="submit" variant="success" size="sm">
<TiTick size="1.5em" />
</Button>
)}
</ButtonToolbar>
<Form noValidate onSubmit={makeOnSubmit(handleSubmit)}>
<div className="d-flex">
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a div here so the error message appears underneath the field and button. The relevant changes can be seen by hiding whitespace changes in GitHub.

<Form.Control
{...register('value', {
valueAsNumber: true,
required: 'Please enter a valid number',
min: { value: limits[0], message: minMaxMsg },
max: { value: limits[1], message: minMaxMsg },
disabled: isBusy,
})}
className={styles.input}
type="number"
step={step}
aria-label="Value"
isInvalid={isDirty && !!errors.value}
/>
<ButtonToolbar className="ms-1">
{isBusy ? (
<Button variant="danger" size="sm" onClick={() => onCancel()}>
<TiTimes size="1.5em" />
</Button>
) : (
<Button type="submit" variant="success" size="sm">
<TiTick size="1.5em" />
</Button>
)}
</ButtonToolbar>
</div>
{errors.value && <p className={styles.error}>{errors.value.message}</p>}
</Form>
);
}
Expand Down