How to sanitize the WordPress Gutenberg Component ?
-
I try create create Gutenberg element for my site. I use <TextControl> Component for this task.
Now I need undertand how to make sanitize data into value of input of this component. I don’t need that users can write somthing like
<script>alert("OK")</script>
into input. My code look like:
import "./index.scss" import {TextControl, Flex, FlexBlock, FlexItem, Button, Icon, PanelBody, PanelRow, ColorPicker} from '@wordpress/components' import {InspectorControls, BlockControls, AlignmentToolbar, useBlockProps } from '@wordpress/block-editor' import { __ } from '@wordpress/i18n' import { sanitizeText } from '@wordpress/dom'; wp.blocks.registerBlockType( 'gcb/custom-questionnaire', { title: __('Questionnaire block', 'gcb'), description: __('Gives your audience to do a survey of important issues', 'gcb'), icon: 'editor-help', category: 'text', attributes: { question: {type: "string"}, answers: {type: "array", default: [""]}, correctAnswer: {type:"number", default: undefined}, theAlignment: {type:"string", default:"left"} }, example:{ attributes:{ question: __('What are the three basic questions of life?', 'gcb'), correctAnswer: 3, answers: [__('Who am I?', 'gcb'), __('Why am I here?', 'gcb'), __('What happens after I die?', 'gcb')], theAlignment: "center", } }, edit: EditComponent, save: function (props) { return null } }) function EditComponent(props) { const blockProps = useBlockProps({ className:"gcb_adm__box", } ) function updateQuestion(value){ const sanitizedValue = sanitizeText(value, 'text'); props.setAttributes({question: sanitizedValue}) } function deleteAnswer(indexToDelete){ const newAnswers = props.attributes.answers.filter( function (x, index){ return index !== indexToDelete } ); props.setAttributes({ answers:newAnswers}) if( indexToDelete === props.attributes.correctAnswer){ props.setAttributes( {correctAnswer: undefined}) } } function markAsCorrect(index){ props.setAttributes({correctAnswer: index}) } return ( <div {...blockProps} > <BlockControls> <AlignmentToolbar value={props.attributes.theAlignment} onChange={x => props.setAttributes({theAlignment: x})} /> </BlockControls> <InspectorControls> <PanelBody title={__("Supplementing questionnaire data", 'gcb')} initialOpen={true}> <PanelRow> test </PanelRow> </PanelBody> </InspectorControls> <TextControl label={__("Write name of Questionnaire", 'gcb')} value={ props.attributes.question} onChange={updateQuestion} style={{fontSize: "20px"}} /> <p style={{fontSize:"13px", margin: "20px 0 8px 0"}}>{__('Answers:', 'gcb')}</p> {/* SET data for all answers */} {props.attributes.answers.map(function (answer, index){ return ( <Flex> <FlexBlock> <TextControl value={answer} onChange={newValue => { const newAnswers = props.attributes.answers.concat([]); newAnswers[index] = newValue props.setAttributes({ answers: newAnswers}) } } /> </FlexBlock> <FlexItem> <Button onClick={ () => markAsCorrect(index) }> <Icon className={"gcb_icon-star"} icon={ props.attributes.correctAnswer === index ? 'star-filled' : 'star-empty' } /> </Button> </FlexItem> <FlexItem> <Button className={"gcb_delete"} onClick={ () => deleteAnswer(index) }>{__('Delete', 'gcb')}</Button> </FlexItem> </Flex> ) } )} <Button isPrimary className={"gcb_addNew__button"} onClick={ () => { props.setAttributes({answers: props.attributes.answers.concat([""])}) }} >{__('Add another answer', 'gcb')}</Button> </div> ) }
Function updateQuestion does not work correctly. I don’t understand why.
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘How to sanitize the WordPress Gutenberg Component ?’ is closed to new replies.