Skip to content

Commit 6d40c44

Browse files
committed
move to variation
1 parent 69ff365 commit 6d40c44

File tree

3 files changed

+77
-30
lines changed

3 files changed

+77
-30
lines changed

projects/packages/forms/src/blocks/field-telephone/edit.js

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
import {
2-
InspectorControls,
3-
useBlockProps,
4-
useInnerBlocksProps,
5-
BlockContextProvider,
6-
} from '@wordpress/block-editor';
7-
import { PanelBody, ToggleControl } from '@wordpress/components';
1+
import { useBlockProps, useInnerBlocksProps, BlockContextProvider } from '@wordpress/block-editor';
82
import { useCallback, useEffect, useMemo, useState } from '@wordpress/element';
93
import { __ } from '@wordpress/i18n';
104
import clsx from 'clsx';
@@ -36,6 +30,9 @@ export default function PhoneFieldEdit( props ) {
3630
'jetpack-field',
3731
'jetpack-field-phone',
3832
'jetpack-field-telephone',
33+
showCountrySelector
34+
? 'jetpack-field-telephone--international'
35+
: 'jetpack-field-telephone--basic',
3936
width ? ` jetpack-field__width-${ width }` : '',
4037
{
4138
'is-selected': isSelected || isInnerBlockSelected,
@@ -54,17 +51,16 @@ export default function PhoneFieldEdit( props ) {
5451
} ) );
5552
}, [] );
5653

57-
const onChangeShowCountrySelector = value => {
58-
setAttributes( {
59-
showCountrySelector: value,
60-
} );
61-
setCountryList( value ? countryPairs : EMPTY_ARRAY );
62-
};
63-
6454
useEffect( () => {
65-
if ( showCountrySelector === undefined || showCountrySelector === true ) {
66-
setAttributes( { showCountrySelector: true, default: defaultCountry || 'US' } );
55+
// Initialize country list based on showCountrySelector attribute
56+
if ( showCountrySelector ) {
6757
setCountryList( countryPairs );
58+
// Set default country if not already set
59+
if ( ! defaultCountry ) {
60+
setAttributes( { default: 'US' } );
61+
}
62+
} else {
63+
setCountryList( EMPTY_ARRAY );
6864
}
6965
}, [ showCountrySelector, setAttributes, countryPairs, defaultCountry ] );
7066

@@ -74,7 +70,9 @@ export default function PhoneFieldEdit( props ) {
7470
[
7571
'jetpack/label',
7672
{
77-
label: __( 'Phone number', 'jetpack-forms' ),
73+
label: showCountrySelector
74+
? __( 'Phone number', 'jetpack-forms' )
75+
: __( 'Phone', 'jetpack-forms' ),
7876
placeholder,
7977
required,
8078
requiredText,
@@ -106,17 +104,6 @@ export default function PhoneFieldEdit( props ) {
106104
<div { ...innerBlocksProps } />
107105
</BlockContextProvider>
108106

109-
<InspectorControls>
110-
<PanelBody title={ __( 'Settings', 'jetpack-forms' ) }>
111-
<ToggleControl
112-
label={ __( 'Show country selector', 'jetpack-forms' ) }
113-
checked={ showCountrySelector || false }
114-
onChange={ onChangeShowCountrySelector }
115-
__nextHasNoMarginBottom={ true }
116-
/>
117-
</PanelBody>
118-
</InspectorControls>
119-
120107
<JetpackFieldControls
121108
clientId={ clientId }
122109
id={ id }

projects/packages/forms/src/blocks/field-telephone/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@ import { getIconColor } from '../shared/util/block-icons';
66
import deprecated from './deprecated';
77
import edit from './edit';
88
import save from './save';
9+
import variations from './variations';
910

1011
const name = 'field-telephone';
1112
export const settings = {
1213
...defaultSettings,
13-
title: __( 'Phone number field', 'jetpack-forms' ),
14+
title: __( 'Phone field', 'jetpack-forms' ),
1415
keywords: [
1516
__( 'Phone', 'jetpack-forms' ),
1617
__( 'Cellular phone', 'jetpack-forms' ),
1718
__( 'Mobile', 'jetpack-forms' ),
1819
],
19-
description: __( 'Collect phone numbers from site visitors.', 'jetpack-forms' ),
20+
description: __( 'Add a phone input field to your form.', 'jetpack-forms' ),
2021
icon: {
2122
foreground: getIconColor(),
2223
src: <Icon icon={ mobile } />,
@@ -43,6 +44,7 @@ export const settings = {
4344
allowedBlocks: [ 'jetpack/label', 'jetpack/phone-input' ],
4445
deprecated,
4546
save,
47+
variations,
4648
example: {
4749
attributes: {
4850
default: 'US',
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { Icon } from '@wordpress/components';
2+
import { __ } from '@wordpress/i18n';
3+
import { mobile } from '@wordpress/icons';
4+
import { getIconColor } from '../shared/util/block-icons';
5+
6+
const variations = [
7+
{
8+
name: 'phone',
9+
title: __( 'Phone', 'jetpack-forms' ),
10+
description: __( 'Simple phone number field.', 'jetpack-forms' ),
11+
icon: {
12+
foreground: getIconColor(),
13+
src: <Icon icon={ mobile } />,
14+
},
15+
attributes: {
16+
showCountrySelector: false,
17+
},
18+
isActive: [ 'showCountrySelector' ],
19+
scope: [ 'inserter', 'transform' ],
20+
isDefault: true,
21+
innerBlocks: [
22+
[
23+
'jetpack/label',
24+
{
25+
label: __( 'Phone', 'jetpack-forms' ),
26+
},
27+
],
28+
[ 'jetpack/phone-input', {} ],
29+
],
30+
},
31+
{
32+
name: 'international-phone',
33+
title: __( 'International phone', 'jetpack-forms' ),
34+
description: __( 'Phone field with country selector.', 'jetpack-forms' ),
35+
icon: {
36+
foreground: getIconColor(),
37+
src: <Icon icon={ mobile } />,
38+
},
39+
attributes: {
40+
showCountrySelector: true,
41+
default: 'US',
42+
},
43+
isActive: [ 'showCountrySelector' ],
44+
scope: [ 'inserter', 'transform' ],
45+
isDefault: false,
46+
innerBlocks: [
47+
[
48+
'jetpack/label',
49+
{
50+
label: __( 'Phone number', 'jetpack-forms' ),
51+
},
52+
],
53+
[ 'jetpack/phone-input', {} ],
54+
],
55+
},
56+
];
57+
58+
export default variations;

0 commit comments

Comments
 (0)