diff --git a/packages/li-editor/src/widgets/LayersPanel/LayerAttribute/LayerForm/helper.ts b/packages/li-editor/src/widgets/LayersPanel/LayerAttribute/LayerForm/helper.ts deleted file mode 100644 index e75b485d..00000000 --- a/packages/li-editor/src/widgets/LayersPanel/LayerAttribute/LayerForm/helper.ts +++ /dev/null @@ -1,34 +0,0 @@ -export const dataFormatProcessing = ({ - dataset, - label, - value, -}: { - dataset: Record[]; - label: string; - value: string; -}) => { - const result: Record = {}; - - for (let i = 0; i < dataset.length; i++) { - const item = dataset[i]; - - if (result[item[`${label}`]]) { - result[item[`${label}`]] = [...result[item[`${label}`]], item[`${value}`]]; - } else { - result[item[`${label}`]] = [item[`${value}`]]; - } - } - - const _value = Object.keys(result).map((key) => ({ - [`${label}`]: key, - [`${value}`]: result[key], - })); - - return _value.map((_item) => { - return { - ..._item, - value: _item[`${label}`], - count: _item[`${value}`].length, - }; - }); -}; diff --git a/packages/li-editor/src/widgets/LayersPanel/LayerAttribute/LayerForm/index.tsx b/packages/li-editor/src/widgets/LayersPanel/LayerAttribute/LayerForm/index.tsx index 6e7ee39f..c227d81c 100644 --- a/packages/li-editor/src/widgets/LayersPanel/LayerAttribute/LayerForm/index.tsx +++ b/packages/li-editor/src/widgets/LayersPanel/LayerAttribute/LayerForm/index.tsx @@ -8,7 +8,6 @@ import { max, min, pick } from 'lodash-es'; import React, { useMemo, useState } from 'react'; import { useEditorDatasets, useEditorService, useEditorState } from '../../../../hooks'; import BaseFormSchemaField from '../BaseFormSchemaField'; -import { dataFormatProcessing } from './helper'; import './index.less'; import StyleForm from './StyleForm'; @@ -47,23 +46,11 @@ const LayerForm: React.FC = ({ className, config, onChange }) => return datasetFields.map((item) => { // @ts-ignore const itemValue = dataset.data.map((_item: Record) => _item[item.value]); - - const list = dataFormatProcessing({ - dataset: itemValue.map((_item: string | number) => ({ label: _item, value: _item })), - label: 'label', - value: 'value', - }); + const domain = item.type === 'number' ? [min(itemValue), max(itemValue)] : [...new Set(itemValue)]; return { ...item, - domin: - item.type === 'number' - ? { - min: min(itemValue), - max: max(itemValue), - list, - } - : { list }, + domain, }; }); // @ts-ignore diff --git a/packages/li-p2/src/LayerAttribute/BubbleLayerStyle/helper.ts b/packages/li-p2/src/LayerAttribute/BubbleLayerStyle/helper.ts index b732681f..9b3678fc 100644 --- a/packages/li-p2/src/LayerAttribute/BubbleLayerStyle/helper.ts +++ b/packages/li-p2/src/LayerAttribute/BubbleLayerStyle/helper.ts @@ -8,7 +8,7 @@ export const bubbleLayerStyleFlatToConfig = (style: Record) => { const fillColor = style.fillColorField ? { field: style.fillColorField, - value: typeof style.fillColorScale === 'string' ? style.fillColorRange?.colors : style.fillColorScale?.colors, + value: typeof style.fillColorScale === 'string' ? style.fillColorRange?.colors : style.fillColorScale?.ranges, scale: typeof style.fillColorScale === 'string' ? { type: style.fillColorScale } @@ -79,7 +79,7 @@ export const bubbleLayerStyleConfigToFlat = (styleConfig: BubbleLayerStyleAttrib ? { type: fillColor?.scale?.type ?? '', domain: fillColor?.scale?.domain ?? [], - colors: fillColor?.value, + ranges: fillColor?.value, } : fillColor?.scale?.type : undefined; diff --git a/packages/li-p2/src/LayerAttribute/ChoroplethLayerStyle/helper.ts b/packages/li-p2/src/LayerAttribute/ChoroplethLayerStyle/helper.ts index c48605e8..b6ff07be 100644 --- a/packages/li-p2/src/LayerAttribute/ChoroplethLayerStyle/helper.ts +++ b/packages/li-p2/src/LayerAttribute/ChoroplethLayerStyle/helper.ts @@ -8,7 +8,7 @@ export const choroplethLayerStyleFlatToConfig = (style: Record) => const fillColor = style.fillColorField ? { field: style.fillColorField, - value: typeof style.fillColorScale === 'string' ? style.fillColorRange.colors : style.fillColorScale.colors, + value: typeof style.fillColorScale === 'string' ? style.fillColorRange.colors : style.fillColorScale.ranges, scale: typeof style.fillColorScale === 'string' ? { type: style.fillColorScale } @@ -67,7 +67,7 @@ export const choroplethLayerStyleConfigToFlat = (styleConfig: ChoroplethLayerSty ? { type: fillColor?.scale?.type ?? '', domain: fillColor?.scale?.domain ?? [], - colors: fillColor?.value, + ranges: fillColor?.value, } : fillColor?.scale?.type : undefined; diff --git a/packages/li-p2/src/LayerAttribute/GridLayerStyle/helper.ts b/packages/li-p2/src/LayerAttribute/GridLayerStyle/helper.ts index c1a4bb6b..6fe2507b 100644 --- a/packages/li-p2/src/LayerAttribute/GridLayerStyle/helper.ts +++ b/packages/li-p2/src/LayerAttribute/GridLayerStyle/helper.ts @@ -7,7 +7,7 @@ export const gridLayerStyleFlatToConfig = (style: Record) => { const fillColor = style.fillColorField ? { field: style.fillColorField, - value: typeof style.fillColorScale === 'string' ? style.fillColorRange.colors : style.fillColorScale.colors, + value: typeof style.fillColorScale === 'string' ? style.fillColorRange.colors : style.fillColorScale.ranges, scale: typeof style.fillColorScale === 'string' ? { type: style.fillColorScale } @@ -48,7 +48,7 @@ export const gridLayerStyleConfigToFlat = (styleConfig: GridLayerStyleAttributeV ? { type: color?.scale?.type ?? '', domain: color?.scale?.domain ?? [], - colors: color?.value, + ranges: color?.value, } : color?.scale?.type : undefined; diff --git a/packages/li-p2/src/LayerAttribute/HexbinLayerStyle/helper.ts b/packages/li-p2/src/LayerAttribute/HexbinLayerStyle/helper.ts index c8231349..76441dc7 100644 --- a/packages/li-p2/src/LayerAttribute/HexbinLayerStyle/helper.ts +++ b/packages/li-p2/src/LayerAttribute/HexbinLayerStyle/helper.ts @@ -7,7 +7,7 @@ export const hexbinLayerStyleFlatToConfig = (style: Record) => { const fillColor = style.fillColorField ? { field: style.fillColorField, - value: typeof style.fillColorScale === 'string' ? style.fillColorRange.colors : style.fillColorScale.colors, + value: typeof style.fillColorScale === 'string' ? style.fillColorRange.colors : style.fillColorScale.ranges, scale: typeof style.fillColorScale === 'string' ? { type: style.fillColorScale } @@ -49,7 +49,7 @@ export const hexbinLayerStyleConfigToFlat = (styleConfig: HexbinLayerStyleAttrib ? { type: color?.scale?.type ?? '', domain: color?.scale?.domain ?? [], - colors: color?.value, + ranges: color?.value, } : color?.scale?.type : undefined; diff --git a/packages/li-p2/src/LayerAttribute/LineLayerStyle/helper.ts b/packages/li-p2/src/LayerAttribute/LineLayerStyle/helper.ts index 492e9986..b3324ed5 100644 --- a/packages/li-p2/src/LayerAttribute/LineLayerStyle/helper.ts +++ b/packages/li-p2/src/LayerAttribute/LineLayerStyle/helper.ts @@ -8,7 +8,7 @@ export const lineLayerStyleFlatToConfig = (style: Record) => { const fillColor = style.fillColorField ? { field: style.fillColorField, - value: typeof style.fillColorScale === 'string' ? style.fillColorRange.colors : style.fillColorScale.colors, + value: typeof style.fillColorScale === 'string' ? style.fillColorRange.colors : style.fillColorScale.ranges, scale: typeof style.fillColorScale === 'string' ? { type: style.fillColorScale } @@ -62,7 +62,7 @@ export const lineLayerStyleConfigToFlat = (styleConfig: LineLayerStyleAttributeV ? { type: color?.scale?.type ?? '', domain: color?.scale?.domain ?? [], - colors: color?.value, + ranges: color?.value, } : color?.scale?.type : undefined; diff --git a/packages/li-p2/src/LayerAttribute/common-schema/fill-color-collapse.ts b/packages/li-p2/src/LayerAttribute/common-schema/fill-color-collapse.ts index 0a363dcc..2c858349 100644 --- a/packages/li-p2/src/LayerAttribute/common-schema/fill-color-collapse.ts +++ b/packages/li-p2/src/LayerAttribute/common-schema/fill-color-collapse.ts @@ -40,11 +40,11 @@ export default (options: AttributeSchemaOptions) => { 'x-component': 'ScaleSelector', 'x-component-props': { placeholder: '请选择', - type: + fieldType: '{{ $form.getFieldState("fillColorField",state=> { return state.dataSource.find((item) => item.value === state.value)?.type })}}', - dataset: - '{{ $form.getFieldState("fillColorField",state=> { return state.dataSource.find((item) => item.value === state.value)?.domin })}}', - defaultColors: '{{ $form.getFieldState("fillColorRange",state=> { return state?.value?.colors })}}', + domain: + '{{ $form.getFieldState("fillColorField",state=> { return state.dataSource.find((item) => item.value === state.value)?.domain })}}', + defaultRanges: '{{ $form.getFieldState("fillColorRange",state=> { return state?.value?.colors })}}', }, 'x-decorator-props': {}, 'x-reactions': [ @@ -108,21 +108,23 @@ export default (options: AttributeSchemaOptions) => { { dependencies: ['fillColorScale'], fulfill: { - run: `$form.setFieldState("fillColorRange", (_state) => { - _state.disabled = $form.getFieldState("fillColorScale", (state) => state.value?.type === "threshold"); - - $form.getFieldState('fillColorScale', (state) => { - if (state.value?.domain) { - return (_state.value = { - colors: $form.getFieldState('fillColorScale', (state) => state.value?.colors) - ? $form.getFieldState('fillColorScale', (state) => state.value.colors) - : ['#ffffcc', '#d9f0a3', '#addd8e', '#78c679', '#31a354', '#006837'], - }); - } else { - return; - } - }); - })`, + run: `$form.setFieldState('fillColorRange', (_state) => { + _state.visible = + !$form.getFieldState('fillColorScale', (state) => state.value?.domain) && + $form.getFieldState('fillColorField', (state) => state.value); + }); + `, + }, + }, + { + dependencies: ['fillColorField'], + fulfill: { + run: `$form.setFieldState('fillColorRange', (_state) => { + _state.visible = + !$form.getFieldState('fillColorScale', (state) => state.value?.domain) && + $form.getFieldState('fillColorField', (state) => state.value); + }); + `, }, }, ], diff --git a/packages/li-p2/src/LayerAttribute/components/ScaleSelector/CustomContent/CustomItem/Item/index.tsx b/packages/li-p2/src/LayerAttribute/components/ScaleSelector/CustomContent/CustomItem/Item/index.tsx index 563cfcf9..ed738d0a 100644 --- a/packages/li-p2/src/LayerAttribute/components/ScaleSelector/CustomContent/CustomItem/Item/index.tsx +++ b/packages/li-p2/src/LayerAttribute/components/ScaleSelector/CustomContent/CustomItem/Item/index.tsx @@ -8,7 +8,7 @@ import useStyle from './style'; type ItemProps = { customType: CustomType; value: (string | number | null)[]; - options: { label: string; value: string; count: number }[]; + options: { label: string; value: string }[]; min: number; max: number; position: string | null; diff --git a/packages/li-p2/src/LayerAttribute/components/ScaleSelector/CustomContent/CustomItem/index.tsx b/packages/li-p2/src/LayerAttribute/components/ScaleSelector/CustomContent/CustomItem/index.tsx index bede6e3d..50536aa9 100644 --- a/packages/li-p2/src/LayerAttribute/components/ScaleSelector/CustomContent/CustomItem/index.tsx +++ b/packages/li-p2/src/LayerAttribute/components/ScaleSelector/CustomContent/CustomItem/index.tsx @@ -12,7 +12,7 @@ type RangeItemProps = { customType: CustomType; position: string | null; selectedOption: (string | number | null)[]; - selectOptions: { label: string; value: string; count: number }[]; + selectOptions: { label: string; value: string }[]; min?: number; max?: number; color: string; diff --git a/packages/li-p2/src/LayerAttribute/components/ScaleSelector/CustomContent/index.tsx b/packages/li-p2/src/LayerAttribute/components/ScaleSelector/CustomContent/index.tsx index 4ca013e3..38f479b6 100644 --- a/packages/li-p2/src/LayerAttribute/components/ScaleSelector/CustomContent/index.tsx +++ b/packages/li-p2/src/LayerAttribute/components/ScaleSelector/CustomContent/index.tsx @@ -3,33 +3,41 @@ import { usePrefixCls } from '@formily/antd-v5/esm/__builtins__'; import classnames from 'classnames'; import { isEmpty, uniqueId } from 'lodash-es'; import React, { useEffect, useMemo, useState } from 'react'; -import type { CustomItems, CustomItemType, CustomItemValueType, DatasetType } from '../type'; +import type { CustomItems, CustomItemType, CustomItemValueType } from '../type'; import CustomItem from './CustomItem'; import useStyle from './style'; type CustomContentProps = { fieldType: 'string' | 'number'; - dataset: DatasetType | any; - customRanges: CustomItemType; + domain: string[] | [number, number]; + customRanges?: CustomItemType; onChange: (value: CustomItemType) => void; className?: string; }; const CustomContent = (props: CustomContentProps) => { - const { fieldType = 'string', dataset, customRanges: defaultCustomRanges, onChange, className } = props; + const { fieldType = 'string', domain, customRanges: defaultCustomRanges, onChange, className } = props; const prefixCls = usePrefixCls('formily-color-range-selector__custom-range'); const [wrapSSR, hashId] = useStyle(prefixCls); const [customRanges, setCustomRanges] = useState([]); const selectedOption = useMemo(() => { - if (!customRanges.length) { - return []; + if (fieldType === 'string' && customRanges.length) { + const values = customRanges.map((item) => item.value).flat(); + return values; } - const values = customRanges.map((item) => item.value).flat(); - return values; + return []; }, [customRanges]); + const selectOptions = useMemo(() => { + if (fieldType === 'string') { + return (domain as string[]).map((value: string) => ({ label: value, value })); + } + + return []; + }, [domain]); + useEffect(() => { if (defaultCustomRanges?.list?.length) { const list = defaultCustomRanges.list.map((item: CustomItems) => { @@ -149,8 +157,7 @@ const CustomContent = (props: CustomContentProps) => { return wrapSSR(
{customRanges.map((customItem: CustomItems, index: number) => { - const min = index === 0 ? dataset?.min : customRanges[index - 1].value[1]; - const max = dataset?.max; + const [min, max] = domain as [number, number]; const position = index === 0 ? 'first' : index === customRanges.length - 1 ? 'last' : null; return ( @@ -160,7 +167,7 @@ const CustomContent = (props: CustomContentProps) => { color={customItem.color} value={customItem.value} selectedOption={selectedOption} - selectOptions={dataset.list} + selectOptions={selectOptions} min={min} max={max} position={position} @@ -175,7 +182,7 @@ const CustomContent = (props: CustomContentProps) => {
- 确定 + 应用
, ); diff --git a/packages/li-p2/src/LayerAttribute/components/ScaleSelector/constants.ts b/packages/li-p2/src/LayerAttribute/components/ScaleSelector/constants.ts index defaa009..81018093 100644 --- a/packages/li-p2/src/LayerAttribute/components/ScaleSelector/constants.ts +++ b/packages/li-p2/src/LayerAttribute/components/ScaleSelector/constants.ts @@ -1,8 +1,12 @@ -import type { ColorScaleSelectOptionType } from './index'; +import type { SelectType } from './type'; export const THRESHOLD = 'custom'; -export const DEHAULT_OPTIONS: ColorScaleSelectOptionType[] = [ +export const DEHAULT_OPTIONS: { + label: string; + type: 'number' | 'string' | 'custom'; + value: SelectType; +}[] = [ { label: '等间距', value: 'quantize', diff --git a/packages/li-p2/src/LayerAttribute/components/ScaleSelector/demos/default.tsx b/packages/li-p2/src/LayerAttribute/components/ScaleSelector/demos/default.tsx index 9e563dae..f5eb6d85 100644 --- a/packages/li-p2/src/LayerAttribute/components/ScaleSelector/demos/default.tsx +++ b/packages/li-p2/src/LayerAttribute/components/ScaleSelector/demos/default.tsx @@ -28,31 +28,16 @@ const schema = { type: 'number', title: '颜色划分', default: { - type: 'threshold', - domain: [5, 5.6, 5.7], - colors: ['#63759a', '#e61486', '#f00', '#0f0'], + type: 'threshold', //选择的筛选字段的类型 + domain: [3, 5.666666666666666, 8.333333333333332], + ranges: ['#63759a', '#e61486', '#f00', '#faa'], }, 'x-decorator': 'FormItem', 'x-component': 'ScaleSelector', 'x-component-props': { placeholder: '请选择', - type: 'number', //选择的筛选字段的类型 - dataset: { - min: 5, - max: 7.9, - list: [ - { label: 5, value: 5, count: 17 }, - { label: 5.2, value: 5.2, count: 4 }, - { label: 5.8, value: 5.8, count: 3 }, - { label: 5.6, value: 5.6, count: 2 }, - { label: 5.4, value: 5.4, count: 2 }, - { label: 5.3, value: 5.3, count: 4 }, - { label: 6.1, value: 6.1, count: 1 }, - { label: 5.5, value: 5.5, count: 1 }, - { label: 5.7, value: 5.7, count: 2 }, - { label: 7.9, value: 7.9, count: 1 }, - ], - }, + fieldType: 'number', //选择的筛选字段的类型 + domain: [2, 10], defaultColors: ['#f00', '#ff0', '#00f', '#faa'], }, }, diff --git a/packages/li-p2/src/LayerAttribute/components/ScaleSelector/helper.ts b/packages/li-p2/src/LayerAttribute/components/ScaleSelector/helper.ts index 6890d301..549b70b0 100644 --- a/packages/li-p2/src/LayerAttribute/components/ScaleSelector/helper.ts +++ b/packages/li-p2/src/LayerAttribute/components/ScaleSelector/helper.ts @@ -1,6 +1,6 @@ import { fill, maxBy } from 'lodash-es'; import { THRESHOLD } from './constants'; -import type { CustomItems, CustomItemType, DatasetType } from './type'; +import type { CustomItems, CustomItemType, SelectorValue } from './type'; export const stringToFix = (list: CustomItems[]) => { const colors = list.map((item) => item.color); @@ -46,18 +46,18 @@ export const transformToLayer = (val: CustomItemType) => { } }; -export const transformToScale = (fieldType: 'string' | 'number', val: Record) => { +export const transformToScale = (fieldType: 'string' | 'number', val: SelectorValue) => { if (!val) { return undefined; } - if (typeof val === 'string') { + if (!val.domain) { return val; } - const { domain = [], colors = [] } = val || {}; + const { domain = [], ranges = [] } = val || {}; if (fieldType === 'number') { - const list = colors.map((item: string, index: number) => { + const list = ranges.map((item: string, index: number) => { return { value: [domain[index - 1] ?? null, domain[index] ?? null], color: item, @@ -74,7 +74,7 @@ export const transformToScale = (fieldType: 'string' | 'number', val: Record { return { value: item, - color: colors[index % colors.length], + color: ranges[index % ranges.length], }; }); @@ -99,36 +99,33 @@ export const transformToScale = (fieldType: 'string' | 'number', val: Record { +export const getDefault = (fieldType: 'number' | 'string', domain: [number, number] | string[], colors: string[]) => { if (fieldType === 'number') { - const { min = 0, max = 0 } = dataset; - const _interval = (max - min) / (colors.length - 2); + const [min, max] = domain as [number, number]; + const _interval = (max - min) / colors.length; const _length: number = colors.length - 1 > 0 ? colors.length - 1 : 0; - // @ts-ignore + const _domain = fill(Array(_length), undefined).map((_, index) => { - return min + _interval * index; + return (min + _interval * index + 1).toFixed(2); }); return { type: 'threshold', domain: _domain, - colors: colors, + ranges: colors, }; } else { - const { list = [] } = dataset; const _domain = colors.map((item, index) => { if (index + 1 === colors.length) { - return { color: item, value: list.slice(index).map((item) => item.value) }; + return { color: item, value: domain.slice(index).map((item) => item) }; } - return { color: item, value: [list[index].value] }; + return { color: item, value: [domain[index]] }; }); - const domain = stringToFix(_domain); - return { type: 'cat', - domain, - colors: colors, + domain: stringToFix(_domain), + ranges: colors, }; } }; diff --git a/packages/li-p2/src/LayerAttribute/components/ScaleSelector/index.tsx b/packages/li-p2/src/LayerAttribute/components/ScaleSelector/index.tsx index 9fc545c3..ab4cb6ec 100644 --- a/packages/li-p2/src/LayerAttribute/components/ScaleSelector/index.tsx +++ b/packages/li-p2/src/LayerAttribute/components/ScaleSelector/index.tsx @@ -1,62 +1,58 @@ import { usePrefixCls } from '@formily/antd-v5/esm/__builtins__'; import { connect } from '@formily/react'; -import type { SelectProps } from 'antd'; -import { Select, Switch } from 'antd'; -import type { DefaultOptionType } from 'antd/es/select'; +import { Select } from 'antd'; import cls from 'classnames'; import React, { useEffect, useMemo, useState } from 'react'; import { DEHAULT_OPTIONS, THRESHOLD } from './constants'; import CustomContent from './CustomContent'; import { getDefault, transformToLayer, transformToScale } from './helper'; import useStyle from './style'; -import type { CustomItemType, DatasetType } from './type'; -export interface ColorScaleSelectOptionType extends DefaultOptionType { - type: 'string' | 'number' | 'custom'; - dataset?: DatasetType; -} +import type { CustomItemType, SelectorValue, SelectorValueType, SelectType } from './type'; -export type ScaleSelectorProps = SelectProps & { - type: 'string' | 'number'; - dataset?: DatasetType; - defaultColors?: string[]; +type ScaleSelectorProp = { + fieldType: 'string' | 'number'; + defaultRanges?: string[]; + domain: [number, number] | string[]; + value?: SelectorValue; + /** + * 选择发生改变时 + */ + onChange?: (val: SelectorValue) => void; }; -const Internal = (props: ScaleSelectorProps) => { - const prefixCls = usePrefixCls('formily-scale-selector', props); - const { - type: fieldType, - value, - dataset = { min: 0, max: 100, list: [] }, - defaultColors = ['#f00', '#ff0', '#00f', '#faa'], - } = props; +const Internal = (props: ScaleSelectorProp) => { + const prefixCls = usePrefixCls('formily-scale-selector'); + const { fieldType, value, domain = [], defaultRanges = ['#f00', '#ff0', '#00f', '#faa'], onChange } = props; const [wrapSSR, hashId] = useStyle(prefixCls); - const defaultValue: CustomItemType = useMemo(() => { - return transformToScale(fieldType, value) as CustomItemType; + const defaultValue = useMemo(() => { + if (value) { + return transformToScale(fieldType, value) as CustomItemType; + } }, [value]); const [open, setOpen] = useState(false); - const [type, setType] = useState(); + const _type = (value?.domain ? THRESHOLD : value?.type) as SelectType; + const [type, setType] = useState(_type); const [customOpen, setCustomOpen] = useState(false); useEffect(() => { if (defaultValue) { - const _type = (defaultValue?.type ? defaultValue.type : defaultValue) as string; + const _type = (defaultValue?.type ? defaultValue.type : defaultValue) as SelectType; setType(_type); } }, [defaultValue]); const selectOptions = useMemo(() => { - const options = props.options ?? DEHAULT_OPTIONS; const _type = ['string', 'number'].includes(fieldType) ? fieldType : 'string'; - return options.filter((item) => item.type === _type || item.type === 'custom'); - }, [props.type, props.options]); + return DEHAULT_OPTIONS.filter((item) => item.type === _type || item.type === 'custom'); + }, [fieldType]); useEffect(() => { - if (!props.value?.type || selectOptions.findIndex((item) => item.value === props.value?.type) === -1) { + if (!defaultValue?.type || selectOptions.findIndex((item) => item.value === defaultValue.type) === -1) { if (props.onChange) { - const val = selectOptions[0].value as string; - props.onChange(val, selectOptions); + const val = selectOptions[0].value as SelectorValueType; + props.onChange({ type: val }); } } }, [selectOptions]); @@ -70,32 +66,17 @@ const Internal = (props: ScaleSelectorProps) => { setCustomOpen(false); }; - const onTypeChange = (type: string) => { - if (props.onChange) { - setType(type); - setOpen(false); - props.onChange(type, selectOptions); - } - }; - - const onSwitchChange = (checked: boolean) => { - if (checked) { - setType(THRESHOLD); - setCustomOpen(true); - if (!defaultValue.list) { - const _defaultValue = getDefault(fieldType, dataset, defaultColors); - // @ts-ignore - props?.onChange({ + const onTypeChange = (type: SelectType) => { + setType(type); + if (type === 'custom') { + const _defaultValue = getDefault(fieldType, domain, defaultRanges) as SelectorValue; + if (onChange) + onChange({ ..._defaultValue, }); - } } else { - setCustomOpen(false); - if (props.onChange) { - const val = selectOptions[0].value as string; - setType(val); - props.onChange(val, selectOptions); - } + if (onChange) onChange({ type }); + setOpen(false); } }; @@ -111,32 +92,26 @@ const Internal = (props: ScaleSelectorProps) => { {!customOpen && ( <> {selectOptions.map((item) => { - if (item.value !== THRESHOLD) { - return ( -
onTypeChange(item.value as string)} - > - {item.label} -
- ); - } + return ( +
onTypeChange(item.value)} + > + {item.label} +
+ ); })} )} -
- 自定义 -
- - {customOpen && ( + {type === 'custom' && ( onValueChange(ranges)} /> diff --git a/packages/li-p2/src/LayerAttribute/components/ScaleSelector/type.ts b/packages/li-p2/src/LayerAttribute/components/ScaleSelector/type.ts index d8221b1e..a8ee0075 100644 --- a/packages/li-p2/src/LayerAttribute/components/ScaleSelector/type.ts +++ b/packages/li-p2/src/LayerAttribute/components/ScaleSelector/type.ts @@ -13,8 +13,11 @@ export type CustomItemType = { list: CustomItems[]; }; -export type DatasetType = { - min?: number; - max?: number; - list: { label: string | number; value: string | number; count: number }[]; +export type SelectType = 'custom' | 'quantize' | 'quantile' | 'cat'; + +export type SelectorValueType = 'cat' | 'threshold' | 'quantize' | 'quantile'; +export type SelectorValue = { + type: SelectorValueType; + domain?: (string | number)[]; + ranges?: string[]; };