@lowdefy/blocks-antd
Version:
Lowdefy Ant Design Blocks
233 lines (229 loc) • 8.73 kB
JavaScript
/*
Copyright 2020-2026 Lowdefy, Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/ import React, { useState } from 'react';
import { get, mergeObjects, serializer, type } from '@lowdefy/helpers';
import { ConfigProvider, Slider } from 'antd';
import { withBlockDefaults } from '@lowdefy/block-utils';
import CheckboxSelector from '../CheckboxSelector/CheckboxSelector.js';
import Label from '../Label/Label.js';
import withTheme from '../withTheme.js';
const includeMarks = (minMax, minMin, step = 1)=>{
const marks = {};
marks[minMin] = {
style: {
marginTop: 2,
fontSize: 12
},
label: /*#__PURE__*/ React.createElement("span", null)
};
// round to fix floating point error
for(let k = minMax[0]; k <= minMax[1]; k = parseFloat((k + step).toPrecision(8))){
marks[k] = {
style: {
marginTop: 2,
fontSize: 12
},
label: /*#__PURE__*/ React.createElement("strong", null, k)
};
}
return marks;
};
const RatingSlider = ({ blockId, classNames = {}, components: { Icon, Link }, events, loading, methods, properties, required, styles = {}, validation, value })=>{
const [check, unCheck] = useState(false);
let propertiesIconMin = serializer.copy(properties.minIcon);
if (type.isString(propertiesIconMin)) {
propertiesIconMin = {
name: propertiesIconMin
};
}
let propertiesIconMax = serializer.copy(properties.maxIcon);
if (type.isString(propertiesIconMax)) {
propertiesIconMax = {
name: propertiesIconMax
};
}
const minMax = [
get(properties, 'max', {
default: 10
}),
properties.min ?? 0
].sort((a, b)=>a - b);
// round to fix floating point error
const minMin = parseFloat((minMax[0] - (properties.step ?? 1)).toPrecision(8));
const validationColor = validation?.status === 'error' ? '#ff4d4f' : validation?.status === 'warning' ? '#faad14' : null;
const sliderTheme = {};
if (properties.color) {
sliderTheme.colorPrimary = properties.color;
}
if (validationColor) {
sliderTheme.railBg = validationColor;
}
const hasSliderTheme = Object.keys(sliderTheme).length > 0;
const slider = /*#__PURE__*/ React.createElement(Slider, {
id: `${blockId}_input`,
className: classNames.element,
components: {
Icon,
Link
},
events: events,
autoFocus: properties.autoFocus,
disabled: properties.disabled || check === true && !properties.disableNotApplicable || loading,
dots: get(properties, 'showDots', {
default: true
}),
tooltip: {
open: value === null || properties.tooltipVisible === 'never' ? false : properties.tooltipVisible === 'always' ? true : undefined,
formatter: (val)=>`${val}`
},
marks: properties.marks ?? (get(properties, 'showMarks', {
default: true
}) ? includeMarks(minMax, minMin, properties.step ?? 1) : undefined),
min: minMin,
max: minMax[1],
range: false,
step: properties.step ?? 1,
style: {
flex: 'auto',
...styles.element
},
onChange: (val)=>{
const v = val === minMin ? null : val;
methods.setValue(v);
methods.triggerEvent({
name: 'onChange',
event: {
value: v
}
});
},
value: value === null ? minMin : value
});
return /*#__PURE__*/ React.createElement(Label, {
blockId: blockId,
classNames: classNames,
components: {
Icon,
Link
},
events: events,
methods: methods,
properties: {
title: properties.title,
size: properties.size,
...properties.label
},
required: required,
styles: styles,
validation: validation,
content: {
content: ()=>/*#__PURE__*/ React.createElement("div", {
style: {
display: 'flex',
flexDirection: 'row',
paddingRight: validation?.status ? 30 : undefined
}
}, !required && !properties.disableNotApplicable && /*#__PURE__*/ React.createElement(CheckboxSelector, {
blockId: `${blockId}_checkbox_selector`,
components: {
Icon,
Link
},
properties: mergeObjects([
{
label: {
disabled: true
},
options: [
{
value: true,
label: properties.notApplicableLabel ?? 'N/A'
}
],
color: properties.color,
disabled: properties.disabled || loading
},
properties.CheckboxInput,
{
style: {
flex: '0 0 1',
paddingTop: 6
}
}
]),
methods: {
...methods,
setValue: (val)=>{
if (val[0] === true) {
unCheck(true);
methods.setValue(properties.notApplicableLabel ?? 'N/A');
} else {
unCheck(false);
}
}
},
value: [
check
]
}), !properties.disableIcons && /*#__PURE__*/ React.createElement(Icon, {
blockId: `${blockId}_iconMin`,
classNames: {
element: classNames.minIcon
},
events: events,
properties: mergeObjects([
{
name: 'AiOutlineFrown',
color: properties.color
},
propertiesIconMin
]),
styles: {
element: {
flex: '0 0 1',
fontSize: '20px',
padding: '6px 6px 0 0',
...styles.minIcon
}
}
}), hasSliderTheme ? /*#__PURE__*/ React.createElement(ConfigProvider, {
theme: {
components: {
Slider: sliderTheme
}
}
}, slider) : slider, !properties.disableIcons && /*#__PURE__*/ React.createElement(Icon, {
blockId: `${blockId}_iconMax`,
classNames: {
element: classNames.maxIcon
},
events: events,
properties: mergeObjects([
{
name: 'AiOutlineSmile',
color: properties.color
},
propertiesIconMax
]),
styles: {
element: {
flex: '0 0 1',
fontSize: '20px',
padding: '6px 0 0 6px',
...styles.maxIcon
}
}
}))
}
});
};
export default withTheme('Slider', withBlockDefaults(RatingSlider));