From 40668bc908999f33ee3783c935041bf9eb11cb60 Mon Sep 17 00:00:00 2001 From: Siddharth Thevaril Date: Thu, 29 Jun 2023 18:44:24 +0530 Subject: [PATCH] add required component --- components/index.js | 1 + components/required/index.js | 47 ++++++++++++++++ components/required/readme.md | 1 + components/required/store.js | 41 ++++++++++++++ .../required-component-example/index.js | 55 +++++++++++++++++++ example/src/index.js | 1 + 6 files changed, 146 insertions(+) create mode 100644 components/required/index.js create mode 100644 components/required/readme.md create mode 100644 components/required/store.js create mode 100644 example/src/blocks/required-component-example/index.js diff --git a/components/index.js b/components/index.js index fa74a066..15d8672e 100644 --- a/components/index.js +++ b/components/index.js @@ -25,3 +25,4 @@ export { PostPrimaryTerm } from './post-primary-term'; export { PostPrimaryCategory } from './post-primary-category'; export { RichTextCharacterLimit, getCharacterCount } from './rich-text-character-limit'; export { CircularProgressBar, Counter } from './counter'; +export { Required } from './required'; diff --git a/components/required/index.js b/components/required/index.js new file mode 100644 index 00000000..9c050f69 --- /dev/null +++ b/components/required/index.js @@ -0,0 +1,47 @@ +import PropTypes from 'prop-types'; +import { useDispatch, useSelect } from '@wordpress/data'; +import { useEffect, useRef } from '@wordpress/element'; +import { v4 as uuidv4 } from 'uuid'; +import { store as editorStore } from '@wordpress/editor'; +import { store as requiredFieldsStore } from './store'; + +const lockKey = 'tenup-required-fields-lock-post' + +export const Required = ({ value, children }) => { + const { current: componentId } = useRef( uuidv4() ); + const { lockPostSaving, unlockPostSaving } = useDispatch( editorStore ); + const { setIsFilled } = useDispatch( requiredFieldsStore ); + + useEffect( () => { + setIsFilled( componentId, !! value ); + }, [ value ] ); + + useDispatch() + + const { shouldLock } = useSelect( ( select ) => { + return { + shouldLock: select( requiredFieldsStore ).shouldLock(), + } + } ); + + useEffect( () => { + if ( shouldLock ) { + lockPostSaving( lockKey ); + } else { + unlockPostSaving( lockKey ); + } + + return () => unlockPostSaving( lockKey ); + }, [ shouldLock ] ); + + return children; +}; + +Required.defaultProps = { + value: '', +}; + +Required.propTypes = { + value: PropTypes.string || PropTypes.bool, + children: PropTypes.node.isRequired, +}; diff --git a/components/required/readme.md b/components/required/readme.md new file mode 100644 index 00000000..b7153564 --- /dev/null +++ b/components/required/readme.md @@ -0,0 +1 @@ +# Required diff --git a/components/required/store.js b/components/required/store.js new file mode 100644 index 00000000..c52acf20 --- /dev/null +++ b/components/required/store.js @@ -0,0 +1,41 @@ +import { createReduxStore, register } from '@wordpress/data'; + +const DEFAULT_STATE = { + fields: {}, +}; + +export const store = createReduxStore( 'required-fields-store', { + reducer( state = DEFAULT_STATE, action ) { + switch ( action.type ) { + case 'SET_FIELD_REQUIRED_STATUS': + return { + ...state, + fields: { + ...state.fields, + [ action.componentId ]: action.status + } + }; + } + + return state; + }, + actions: { + setIsFilled( componentId, status ) { + return { + type: 'SET_FIELD_REQUIRED_STATUS', + componentId, + status, + }; + }, + }, + selectors: { + getFieldRequiredStatuses( state ) { + return state.fields; + }, + shouldLock( state ) { + return Object.keys( state.fields ).filter( field => ! state.fields[ field ] ).length > 0; + } + }, +} ); + +register( store ); diff --git a/example/src/blocks/required-component-example/index.js b/example/src/blocks/required-component-example/index.js new file mode 100644 index 00000000..32a6489d --- /dev/null +++ b/example/src/blocks/required-component-example/index.js @@ -0,0 +1,55 @@ +import { registerBlockType } from '@wordpress/blocks'; +import { useBlockProps } from '@wordpress/block-editor'; +import { TextControl } from '@wordpress/components'; +import { __ } from '@wordpress/i18n'; + +import { Required } from '@10up/block-components'; + +const NAMESPACE = 'example'; + +registerBlockType( `${ NAMESPACE }/required-component-example`, { + apiVersion: 2, + title: __( 'Required Component Example', NAMESPACE ), + icon: 'smiley', + category: 'common', + example: {}, + supports: { + html: false + }, + attributes: { + fname: { + type: 'string', + default: '', + }, + lname: { + type: 'string', + default: '' + } + }, + transforms: {}, + variations: [], + edit: ( { attributes, setAttributes } ) => { + const blockProps = useBlockProps(); + const { fname, lname } = attributes; + + return ( +
+ + setAttributes( { fname: val } ) } + /> + + + setAttributes( { lname: val } ) } + /> + +
+ ) + }, + save: () => null +} ); diff --git a/example/src/index.js b/example/src/index.js index 2034b807..1e0eca63 100644 --- a/example/src/index.js +++ b/example/src/index.js @@ -11,3 +11,4 @@ import './blocks/post-featured-image'; import './blocks/content-item'; import './blocks/hero'; import './blocks/post-meta'; +import './blocks/required-component-example';