使用法

元のコンポーネントをwithConstrainedTabbingでラップします。

  1. import { useState } from 'react';
  2. import {
  3. withConstrainedTabbing,
  4. TextControl,
  5. Button,
  6. } from '@wordpress/components';
  7. const ConstrainedTabbing = withConstrainedTabbing(
  8. ( { children } ) => children
  9. );
  10. const MyComponentWithConstrainedTabbing = () => {
  11. const [ isConstrainedTabbing, setIsConstrainedTabbing ] = useState( false );
  12. let form = (
  13. <form>
  14. <TextControl label="Input 1" onChange={ () => {} } />
  15. <TextControl label="Input 2" onChange={ () => {} } />
  16. </form>
  17. );
  18. if ( isConstrainedTabbing ) {
  19. form = <ConstrainedTabbing>{ form }</ConstrainedTabbing>;
  20. }
  21. const toggleConstrain = () => {
  22. setIsConstrainedTabbing( ( state ) => ! state );
  23. };
  24. return (
  25. <div>
  26. { form }
  27. <Button variant="secondary" onClick={ toggleConstrain }>
  28. { isConstrainedTabbing ? 'Disable' : 'Enable' } constrain
  29. tabbing
  30. </Button>
  31. </div>
  32. );
  33. }