mirror of
https://github.com/maputnik/editor.git
synced 2026-12-04 07:44:22 +00:00
Fixes #950. `icon-padding` is the only property with the `padding` type, which takes one to four numbers. The editor rendered it as a fixed four-box `InputArray`, so: - an unset or single-value padding showed one number and three empty boxes, which is the "extra empty fields" from the last comment on the issue; - `InputArray` only calls `onChange` once every box is filled, so typing one, two or three values never reached the style. Now `padding` shares the `numberArray` case and uses `InputDynamicArray`, the same add/remove list as `hillshade-illumination-direction`. `InputDynamicArray` gets an optional `maxLength`, which hides "Add value" once there are four padding values. Nothing else passes `maxLength`, so the other array fields don't change. An unset `icon-padding` now shows one empty box, like other unset number arrays, instead of the default `2` as a placeholder. | Before | After | |---|---| |  |  | Both screenshots show the `icon-padding` field of a new symbol layer in Chromium. **Performance:** none. It's the component other array properties already use, and only `icon-padding` renders it here. ## Launch Checklist - [x] Briefly describe the changes in this PR. - [x] Link to related issues. - [x] Include before/after visuals or gifs if this PR includes visual changes. - [x] Write tests for all new functionality: two e2e tests in `layers-list.spec.ts`. "should save a single icon-padding value" fails on `main`, and fails when I change the expected `[5]` to `[7]`. "should not offer a fifth icon-padding value" fails without the `maxLength` cap. - [x] Add an entry to `CHANGELOG.md` under the `## main` section. Tested locally: `npm run lint` clean, `npx tsc --noEmit` clean, `npx vitest run` (55 passed), `npx playwright test e2e/layers-list.spec.ts` (44 passed), and the full `npx playwright test` before adding the cap (everything passes; with 4 workers 19 tests in the modal, history and accessibility specs timed out on my machine, and all 19 pass when rerun with `--workers=1`). Assisted-By: Claude Opus 5.5 (Claude Code). It helped write the fix, the tests and this description. I've reviewed and tested the change and can answer questions about it.
167 lines
4.9 KiB
TypeScript
167 lines
4.9 KiB
TypeScript
import React from "react";
|
|
import capitalize from "lodash.capitalize";
|
|
import {MdDelete} from "react-icons/md";
|
|
import { type WithTranslation, withTranslation } from "react-i18next";
|
|
|
|
import { InputString } from "./InputString";
|
|
import { InputNumber } from "./InputNumber";
|
|
import { InputButton } from "./InputButton";
|
|
import { FieldDocLabel } from "./FieldDocLabel";
|
|
import { InputEnum } from "./InputEnum";
|
|
import { InputUrl } from "./InputUrl";
|
|
import { InputColor } from "./InputColor";
|
|
|
|
|
|
export type InputDynamicArrayProps = {
|
|
value?: (string | number | undefined)[]
|
|
type?: "url" | "number" | "enum" | "string" | "color"
|
|
default?: (string | number | undefined)[]
|
|
/** Hide the "Add value" button once the array has this many values */
|
|
maxLength?: number
|
|
onChange?(values: (string | number | undefined)[] | undefined): unknown
|
|
style?: object
|
|
fieldSpec?: {
|
|
values?: any
|
|
}
|
|
"aria-label"?: string
|
|
label: string
|
|
};
|
|
|
|
type InputDynamicArrayInternalProps = InputDynamicArrayProps & WithTranslation;
|
|
|
|
class InputDynamicArrayInternal extends React.Component<InputDynamicArrayInternalProps> {
|
|
changeValue(idx: number, newValue: string | number | undefined) {
|
|
const values = this.values.slice(0);
|
|
values[idx] = newValue;
|
|
if (this.props.onChange) this.props.onChange(values);
|
|
}
|
|
|
|
get values() {
|
|
return this.props.value || this.props.default || [];
|
|
}
|
|
|
|
addValue = () => {
|
|
const values = this.values.slice(0);
|
|
if (this.props.type === "number") {
|
|
values.push(0);
|
|
}
|
|
else if (this.props.type === "url") {
|
|
values.push("");
|
|
}
|
|
else if (this.props.type === "enum") {
|
|
const {fieldSpec} = this.props;
|
|
const defaultValue = Object.keys(fieldSpec!.values)[0];
|
|
values.push(defaultValue);
|
|
} else if (this.props.type === "color") {
|
|
values.push("#000000");
|
|
} else {
|
|
values.push("");
|
|
}
|
|
|
|
if (this.props.onChange) this.props.onChange(values);
|
|
};
|
|
|
|
deleteValue(valueIdx: number) {
|
|
const values = this.values.slice(0);
|
|
values.splice(valueIdx, 1);
|
|
|
|
if (this.props.onChange) this.props.onChange(values.length > 0 ? values : undefined);
|
|
}
|
|
|
|
render() {
|
|
const t = this.props.t;
|
|
const i18nProps = { t, i18n: this.props.i18n, tReady: this.props.tReady };
|
|
const inputs = this.values.map((v, i) => {
|
|
const deleteValueBtn= <DeleteValueInputButton
|
|
onClick={this.deleteValue.bind(this, i)}
|
|
{...i18nProps}
|
|
/>;
|
|
let input;
|
|
if(this.props.type === "url") {
|
|
input = <InputUrl
|
|
value={v as string}
|
|
onChange={this.changeValue.bind(this, i)}
|
|
aria-label={this.props["aria-label"] || this.props.label}
|
|
/>;
|
|
}
|
|
else if (this.props.type === "number") {
|
|
input = <InputNumber
|
|
value={v as number}
|
|
onChange={this.changeValue.bind(this, i)}
|
|
aria-label={this.props["aria-label"] || this.props.label}
|
|
/>;
|
|
}
|
|
else if (this.props.type === "enum") {
|
|
const options = Object.keys(this.props.fieldSpec?.values).map(v => [v, capitalize(v)]);
|
|
input = <InputEnum
|
|
options={options}
|
|
value={v as string}
|
|
onChange={this.changeValue.bind(this, i)}
|
|
aria-label={this.props["aria-label"] || this.props.label}
|
|
/>;
|
|
}
|
|
else if (this.props.type === "color") {
|
|
input = <InputColor
|
|
value={v as string}
|
|
onChange={this.changeValue.bind(this, i)}
|
|
aria-label={this.props["aria-label"] || this.props.label}
|
|
/>;
|
|
}
|
|
else {
|
|
input = <InputString
|
|
value={v as string}
|
|
onChange={this.changeValue.bind(this, i)}
|
|
aria-label={this.props["aria-label"] || this.props.label}
|
|
/>;
|
|
}
|
|
|
|
return <div
|
|
style={this.props.style}
|
|
key={i}
|
|
className="maputnik-array-block"
|
|
>
|
|
<div className="maputnik-array-block-action">
|
|
{deleteValueBtn}
|
|
</div>
|
|
<div className="maputnik-array-block-content">
|
|
{input}
|
|
</div>
|
|
</div>;
|
|
});
|
|
|
|
return (
|
|
<div className="maputnik-array">
|
|
{inputs}
|
|
{(this.props.maxLength === undefined || this.values.length < this.props.maxLength) &&
|
|
<InputButton
|
|
className="maputnik-array-add-value"
|
|
onClick={this.addValue}
|
|
>
|
|
{t("Add value")}
|
|
</InputButton>
|
|
}
|
|
</div>
|
|
);
|
|
}
|
|
}
|
|
|
|
export const InputDynamicArray = withTranslation()(InputDynamicArrayInternal);
|
|
type DeleteValueInputButtonProps = {
|
|
onClick?(...args: unknown[]): unknown
|
|
} & WithTranslation;
|
|
|
|
class DeleteValueInputButton extends React.Component<DeleteValueInputButtonProps> {
|
|
render() {
|
|
const t = this.props.t;
|
|
return <InputButton
|
|
className="maputnik-delete-stop"
|
|
onClick={this.props.onClick}
|
|
title={t("Remove array item")}
|
|
>
|
|
<FieldDocLabel
|
|
label={<MdDelete />}
|
|
/>
|
|
</InputButton>;
|
|
}
|
|
}
|