wordpress-gutenberg - RangeControl 组件可以设置为小数之间的范围吗?
问题描述
我正在为 Gutenberg 构建(并边走边学)英雄形象块。我正在关注它的教程,到目前为止它运行良好。但我想使用 RangeControl 组件(在 Gutenberg Editor Inspector Controls 中)来控制英雄叠加层的不透明度。这也有效,但只能设置整数范围(即从 1 到 10)。是否可以改用小数(因此范围为 0.0 到 1.0)?当然我可以使用整数然后将它们转换为小数,但这不是最好的用户体验。
我使用(非常好的)create-guten-block样板,这里是块的代码(注意:我知道我还没有完成 save() 方法!):
块.js
registerBlockType('configit-blocks/hero', {
title: 'Hero Image',
icon: 'format-image',
category: 'common',
attributes: {
textString: {
type: 'array',
source: 'children',
selector: 'h2',
},
fontColor: {
type: 'string',
default: 'black'
},
overlayOpacity: {
type: 'decimalPoint',
default: 0.0
}
},
edit(props) {
const {
setAttributes,
attributes,
className,
focus
} = props;
const { fontColor } = props.attributes;
const { overlayOpacity } = props.attributes;
function onTextChange(changes) {
setAttributes({
textString: changes
});
}
function onTextColorChange(changes) {
setAttributes({
fontColor: changes
})
}
function onOverlayOpacity(changes) {
setAttributes({
overlayOpacity: changes
})
}
return ([
<InspectorControls>
<div>
<strong>Select a font color:</strong>
<ColorPalette
value={fontColor}
onChange={onTextColorChange}
/>
</div>
<div>
<RangeControl
label="Overlay Opacity"
value={ overlayOpacity }
onChange={ onOverlayOpacity }
min={ 0.0 }
max={ 1.0 }
/>
</div>
</InspectorControls>,
<div className={className}
style={{
backgroundImage: `url('http://placehold.it/1440x700')`,
backgroundSize: 'cover',
backgroundPosition: 'center'
}}>
<div className="overlay" />
<RichText
tagName="h2"
className="content"
value={attributes.textString}
onChange={onTextChange}
placeholder="Enter your text here!"
style={{color: fontColor}}
allowReset={false}
/>
</div>
]);
},
save(props) {
const { attributes, className } = props;
const { fontColor } = props.attributes;
return (
<div className={className}
style={{
backgroundImage: "url('http://placehold.it/1440x700')",
backgroundSize: "cover",
backgroundPosition: "center"
}}>
<div className="overlay"/>
<h2 className="content" style={{color: fontColor}}>{attributes.textString}</h2>
</div>
);
}
});
解决方案
是的,将 RangeControl 上的“step”道具设置为您想要的值。(这没有记录,但它有效。)
例子:
<RangeControl
label="Overlay Opacity"
value={ overlayOpacity }
onChange={ onOverlayOpacity }
min={ 0.0 }
max={ 1.0 }
step={ 0.1 }
/>
推荐阅读
- python - 如何对单个列表中的两个元素求和并与整数参数进行比较
- mysql - Presto / MySQL - 检查列值是否为字母数字的正则表达式
- javascript - vue chartjs条形图有时只显示x轴和y轴,条形图和标签不见了
- node.js - 如何在 Vue.js 中解决这个代理问题?
- git - 无法获取 && 合并到我的本地主分支
- python - Python 会抛出一个文件不存在的错误,当它明确存在时
- python - 如何在熊猫中另一列的值之间聚合一列中的值
- android - Android设置菜单图标变小
- c# - 从文本文件中拆分行后如何打印特定字符串(关闭)
- javascript - 使用javascript在刽子手游戏中出现的错误代码