index.js
24 lines
| 1 | import { __ } from '@wordpress/i18n'; |
| 2 | import { TextControl } from '@wordpress/components'; |
| 3 | |
| 4 | /** |
| 5 | * Regular expression matching invalid anchor characters for replacement. |
| 6 | * |
| 7 | * @type {RegExp} |
| 8 | */ |
| 9 | const ANCHOR_REGEX = /[\s#]/g; |
| 10 | |
| 11 | export default ( { anchor, setAttributes } ) => { |
| 12 | return ( |
| 13 | <TextControl |
| 14 | label={ __( 'HTML Anchor', 'generateblocks' ) } |
| 15 | help={ __( 'Anchors lets you link directly to a section on a page.', 'generateblocks' ) } |
| 16 | value={ anchor || '' } |
| 17 | onChange={ ( value ) => { |
| 18 | const anchorValue = value.replace( ANCHOR_REGEX, '-' ); |
| 19 | |
| 20 | setAttributes( { anchor: anchorValue } ); |
| 21 | } } /> |
| 22 | ); |
| 23 | }; |
| 24 |