mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-12-02 12:56:24 -05:00
ui: PathInput, and the media directory stops printing over the value
The five Special Directories fields on the Settings page were unreadable: the media directory printed straight through the value, so Archive Directory showed `a/media/wrolpi/hain_tag)s/%(domain)s`. Cause: `leftSectionWidth='auto'`. Mantine turns that prop into `--input-padding-inline-start`, and `padding-inline-start: auto` is not valid CSS -- it resolves to zero, so the input reserved no room for the section positioned on top of it. It fails only in CSS, only at runtime, and nothing warns. Fixed by expressing the field in the library instead of computing a width. PathInput puts the prefix and the input side by side in a flex row sharing one outline, so there is no width to get wrong and nothing to overlap -- the failure is structurally impossible rather than merely corrected. It also sizes to any prefix, which matters because the media directory is `/media/wrolpi` on a Pi and something else under Docker. Written test-first, and the tests need no mocks at all -- no context, no fetch, no router, just the provider test-utils already supplies. That is the point: Settings.js needs a pile of provider and API mocks, so its inputs were never covered, which is how five broken fields shipped. The tests assert the things that actually matter and can be checked without layout: the prefix is a sibling rather than a child, the value never contains it, onChange reports only what was typed, the prefix is the field's accessible description so a screen-reader user knows what the path is relative to, and disabled reaches the input rather than just the wrapper. Plus a source guard, since jsdom cannot see an overlap: nothing may pass `leftSectionWidth='auto'` or `rightSectionWidth='auto'`. It strips comments first -- PathInput documents the mistake by name, and prose about a mistake is not the mistake.
This commit is contained in:
parent
8ba73e64cd
commit
50cf89b20a
5 changed files with 272 additions and 16 deletions
|
|
@ -1,4 +1,4 @@
|
|||
import React, {forwardRef} from 'react';
|
||||
import React, {forwardRef, useId} from 'react';
|
||||
import {
|
||||
Checkbox as MCheckbox,
|
||||
MultiSelect,
|
||||
|
|
@ -58,6 +58,66 @@ export function Toggle({style, className, ...props}: ToggleProps) {
|
|||
/>
|
||||
}
|
||||
|
||||
export interface PathInputProps
|
||||
extends Omit<React.InputHTMLAttributes<HTMLInputElement>, 'prefix'> {
|
||||
label?: React.ReactNode;
|
||||
description?: React.ReactNode;
|
||||
error?: React.ReactNode;
|
||||
/**
|
||||
* A fixed, non-editable path the value hangs off — the media directory, say. It is
|
||||
* not part of the value, and typing never changes it.
|
||||
*/
|
||||
prefix: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* A path input with a fixed prefix shown to the left of what the user types.
|
||||
*
|
||||
* The prefix is a *sibling* of the input rather than a section layered inside it, which is
|
||||
* the point: Mantine positions `leftSection` absolutely and reserves room for it with
|
||||
* `--input-padding-inline-start`, so a call site that gets that width wrong prints the
|
||||
* prefix straight through the value. Passing `leftSectionWidth='auto'` did exactly that
|
||||
* — `padding-inline-start: auto` is invalid, so it resolved to zero and every one of these
|
||||
* fields rendered its path on top of its own contents.
|
||||
*
|
||||
* As two boxes in a flex row there is no width to compute and nothing to overlap. It also
|
||||
* sizes itself to any prefix, which matters here: the media directory is `/media/wrolpi`
|
||||
* on a Pi and something else entirely under Docker.
|
||||
*/
|
||||
export const PathInput = forwardRef<HTMLInputElement, PathInputProps>((
|
||||
{prefix, label, description, error, required, disabled, className, id, ...props}, ref
|
||||
) => {
|
||||
const generated = useId();
|
||||
const inputId = id ?? `${generated}-input`;
|
||||
const prefixId = `${generated}-prefix`;
|
||||
|
||||
return <div className={['wrolpi-path-input', className].filter(Boolean).join(' ')}>
|
||||
{label && <label className='wrolpi-path-input-label' htmlFor={inputId}>
|
||||
{label}{required && <span aria-hidden='true'> *</span>}
|
||||
</label>}
|
||||
<div className='wrolpi-path-input-control' data-disabled={disabled ? true : undefined}
|
||||
data-error={error ? true : undefined}>
|
||||
{/*
|
||||
Described rather than hidden: a screen reader should hear which directory the
|
||||
typed path is relative to, since that is the whole point of showing it.
|
||||
*/}
|
||||
<span className='wrolpi-path-input-prefix' id={prefixId}>{prefix}</span>
|
||||
<input
|
||||
ref={ref}
|
||||
id={inputId}
|
||||
type='text'
|
||||
required={required}
|
||||
disabled={disabled}
|
||||
aria-describedby={prefixId}
|
||||
{...props}
|
||||
/>
|
||||
</div>
|
||||
{description && <div className='wrolpi-path-input-description'>{description}</div>}
|
||||
{error && <div className='wrolpi-path-input-error'>{error}</div>}
|
||||
</div>
|
||||
});
|
||||
PathInput.displayName = 'PathInput';
|
||||
|
||||
export interface ActionInputProps extends React.ComponentProps<typeof MTextInput> {
|
||||
/** Button (or buttons) attached to the input's trailing edge. */
|
||||
action?: React.ReactNode;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue