admin管理员组文章数量:1279182
I tried to use the RangeControl component outside and in the block editor itself. But it looks like there are styles missing, and it does not work properly.
Is there a trick to use it in the block in Gutenberg somehow?
export default function Edit( { attributes, setAttributes } ) {
const blockProps = useBlockProps();
const MyRangeControl = () => {
const [ columns, setColumns ] = useState( 2 );
return(
<RangeControl
label="Rating"
value={ attributes.value }
onChange={ onChangeValue }
min={ 1 }
max={ 10 }
step={ 0.5 }
/>
);
};
return (
<p { ...blockProps }>
<MyRangeControl/>
</p>
);
}
EDIT: It works if I add it like this, but it does not save the value:
export default function Edit( { attributes, setAttributes } ) {
const blockProps = useBlockProps();
const onChangeValue = ( newValue ) => {
setAttributes( { value: newValue} )
}
return (
<p { ...blockProps }>
<RangeControl
label="Rating"
value={ attributes.value }
onChange={ onChangeValue }
min={ 1 }
max={ 10 }
step={ 0.5 }
/>
</p>
);
}
I tried to use the RangeControl component outside and in the block editor itself. But it looks like there are styles missing, and it does not work properly.
Is there a trick to use it in the block in Gutenberg somehow?
export default function Edit( { attributes, setAttributes } ) {
const blockProps = useBlockProps();
const MyRangeControl = () => {
const [ columns, setColumns ] = useState( 2 );
return(
<RangeControl
label="Rating"
value={ attributes.value }
onChange={ onChangeValue }
min={ 1 }
max={ 10 }
step={ 0.5 }
/>
);
};
return (
<p { ...blockProps }>
<MyRangeControl/>
</p>
);
}
EDIT: It works if I add it like this, but it does not save the value:
export default function Edit( { attributes, setAttributes } ) {
const blockProps = useBlockProps();
const onChangeValue = ( newValue ) => {
setAttributes( { value: newValue} )
}
return (
<p { ...blockProps }>
<RangeControl
label="Rating"
value={ attributes.value }
onChange={ onChangeValue }
min={ 1 }
max={ 10 }
step={ 0.5 }
/>
</p>
);
}
Share
Improve this question
edited Nov 18, 2021 at 21:50
Marc
asked Nov 18, 2021 at 21:28
MarcMarc
6979 silver badges28 bronze badges
1 Answer
Reset to default 3This happens because the Component need the correct data type. Use "number" when getting the value. Use "String" as the attributes type.
const onChangeValue = ( newValue ) => {
setAttributes( { value: String(newValue)} )
}
<RangeControl
label="Rating"
value={ Number(attributes.value) }
onChange={ onChangeValue }
min={ 1 }
max={ 10 }
step={ 0.5 }
/>
本文标签: Gutenberg How to use RangeControl in the editor itself and make it work
版权声明:本文标题:Gutenberg: How to use RangeControl in the editor itself and make it work? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741214331a2359750.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论