import React, { useRef } from "react"; import { PiListPlusBold } from "react-icons/pi"; import { TbMathFunction } from "react-icons/tb"; import latest from "@maplibre/maplibre-gl-style-spec/dist/latest.json"; import { type WithTranslation, withTranslation } from "react-i18next"; import { InputButton } from "./InputButton"; import { InputSpec } from "./InputSpec"; import { InputNumber } from "./InputNumber"; import { InputSelect } from "./InputSelect"; import { Block } from "./Block"; import { DeleteStopButton } from "./DeleteStopButton"; import { labelFromFieldName } from "../libs/label-from-field-name"; import { generateUniqueId as docUid } from "../libs/document-uid"; import { sortNumerically } from "../libs/sort-numerically"; import { type MappedLayerErrors } from "../libs/definitions"; /** * We cache a reference for each stop by its index. * * When the stops are reordered the references are also updated (see this.orderStops) this allows React to use the same key for the element and keep keyboard focus. */ function setStopRefs(props: ZoomPropertyInternalProps, state: ZoomPropertyState) { // This is initialised below only if required to improved performance. let newRefs: {[key: number]: string} = {}; if(props.value && (props.value as ZoomWithStops).stops) { (props.value as ZoomWithStops).stops.forEach((_val, idx: number) => { if(Object.prototype.hasOwnProperty.call(!state.refs, idx)) { if(!newRefs) { newRefs = {...state}; } newRefs[idx] = docUid("stop-"); } else { newRefs[idx] = state.refs[idx]; } }); } return newRefs; } type ZoomWithStops = { stops: [number | undefined, number][] base?: number }; type ZoomPropertyInternalProps = { onChange?(...args: unknown[]): unknown onChangeToDataFunction?(...args: unknown[]): unknown onDeleteStop?(...args: unknown[]): unknown onAddStop?(...args: unknown[]): unknown onExpressionClick?(...args: unknown[]): unknown fieldType?: string fieldName: string fieldSpec?: { "property-type"?: string "function-type"?: string } errors?: MappedLayerErrors value?: ZoomWithStops } & WithTranslation; type ZoomPropertyState = { refs: {[key: number]: string} }; const ZoomPropertyInternal: React.FC = ({ errors = {}, ...rest }) => { const props = { errors, ...rest } as ZoomPropertyInternalProps; // The stop refs never reach the rendered output (the row key is derived from // the stop itself), so they live in a ref rather than state: keeping them in // state would mean setting state during render on every pass. const refs = useRef<{[key: number]: string}>({}); refs.current = setStopRefs(props, { refs: refs.current }); // Order the stops altering the refs to reflect their new position. function orderStopsByZoom(stops: ZoomWithStops["stops"]) { const mappedWithRef = stops .map((stop, idx) => { return { ref: refs.current[idx], data: stop }; }) // Sort by zoom .sort((a, b) => sortNumerically(a.data[0]!, b.data[0]!)); // Fetch the new position of the stops const newRefs: {[key:number]: string} = {}; mappedWithRef .forEach((stop, idx) =>{ newRefs[idx] = stop.ref; }); refs.current = newRefs; return mappedWithRef.map((item) => item.data); } function changeZoomStop(changeIdx: number, stopData: number | undefined, value: number) { const stops = (props.value as ZoomWithStops).stops.slice(0); stops[changeIdx] = [stopData, value]; const orderedStops = orderStopsByZoom(stops); const changedValue = { ...props.value as ZoomWithStops, stops: orderedStops }; props.onChange!(props.fieldName, changedValue); } function changeBase(newValue: number | undefined) { const changedValue = { ...props.value, base: newValue }; if (changedValue.base === undefined) { delete changedValue["base"]; } props.onChange!(props.fieldName, changedValue); } const changeDataType = (type: string) => { if (type !== "interpolate" && props.onChangeToDataFunction) { props.onChangeToDataFunction(type); } }; function getDataFunctionTypes(fieldSpec: { "property-type"?: string "function-type"?: string }) { if (fieldSpec["property-type"] === "data-driven") { return ["interpolate", "categorical", "interval", "exponential", "identity"]; } else { return ["interpolate"]; } } const t = props.t; const zoomFields = props.value?.stops.map((stop, idx) => { const zoomLevel = stop[0]; const value = stop[1]; const deleteStopBtn = ; return changeZoomStop(idx, changedStop, value)} min={0} max={22} /> changeZoomStop(idx, zoomLevel, newValue as number)} /> {deleteStopBtn} ; }); // return
return
{labelFromFieldName(props.fieldName)}
changeDataType(propVal)} title={t("Select a type of data scale (default is 'categorical').")} options={getDataFunctionTypes(props.fieldSpec!)} />
changeBase(newValue as number | undefined)} />
{zoomFields}
{t("Stops")}
{t("Zoom")} {t("Output value")}
{t("Add stop")} {t("Convert to expression")}
; }; export const ZoomProperty = withTranslation()(ZoomPropertyInternal);