Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions classes/fields/number.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ public function options() {
'type' => 'boolean',
'excludes-on' => array( static::$type . '_decimals' => 0 ),
),
static::$type . '_keep_leading_zeroes' => array(
'label' => __( 'Keep Leading Zeroes', 'pods' ),
'help' => __( 'By default, Pods will remove leading zeroes from numbers like "00123" which will become "123". When you keep them, the zeroes at the start of the value will be preserved when formatted.', 'pods' ),
'default' => 0,
'type' => 'boolean',
'depends-on' => array(
static::$type . '_format_type' => 'number',
static::$type . '_html5' => false,
),
),
static::$type . '_step' => array(
'label' => __( 'Slider Increment (Step)', 'pods' ),
'depends-on' => array( static::$type . '_format_type' => 'slider' ),
Expand Down Expand Up @@ -147,6 +157,10 @@ public function schema( $options = null ) {

$schema = 'DECIMAL(' . $length . ',' . $decimals . ')';

if ( 1 === (int) pods_v( static::$type . '_keep_leading_zeroes', $options, 0 ) ) {
$schema = 'VARCHAR(' . $length . ')';
}

return $schema;

}
Expand Down Expand Up @@ -232,6 +246,7 @@ public function input( $name, $value = null, $options = null, $pod = null, $id =
// Enforce boolean.
$options[ static::$type . '_html5' ] = filter_var( pods_v( static::$type . '_html5', $options, false ), FILTER_VALIDATE_BOOLEAN );
$options[ static::$type . '_format_soft' ] = filter_var( pods_v( static::$type . '_format_soft', $options, false ), FILTER_VALIDATE_BOOLEAN );
$options[ static::$type . '_keep_leading_zeroes' ] = filter_var( pods_v( static::$type . '_keep_leading_zeroes', $options, false ), FILTER_VALIDATE_BOOLEAN );

// Only format the value for non-HTML5 inputs.
if ( ! $options[ static::$type . '_html5' ] ) {
Expand Down Expand Up @@ -343,13 +358,26 @@ public function pre_save( $value, $id = null, $name = null, $options = null, $fi
return null;
}

$prefix = null;

if (
1 === (int) pods_v( static::$type . '_keep_leading_zeroes', $options, 0 )
&& preg_match( '/^(0+)/', $value, $leading_zeroes )
) {
$prefix = $leading_zeroes[0];
}

$value = number_format( (float) $value, $decimals, '.', '' );

// Optionally remove trailing decimal zero's.
if ( pods_v( static::$type . '_format_soft', $options, false ) ) {
$value = $this->trim_decimals( $value, '.' );
}

if ( null !== $prefix ) {
$value = $prefix . $value;
}

return $value;
}

Expand All @@ -368,6 +396,15 @@ public function format( $value = null, $name = null, $options = null, $pod = nul
$dot = $format_args['dot'];
$decimals = $format_args['decimals'];

$prefix = null;

if (
1 === (int) pods_v( static::$type . '_keep_leading_zeroes', $options, 0 )
&& preg_match( '/^(0+)/', $value, $leading_zeroes )
) {
$prefix = $leading_zeroes[0];
}

if ( 'i18n' === pods_v( static::$type . '_format', $options ) ) {
$value = number_format_i18n( (float) $value, $decimals );
} else {
Expand All @@ -379,6 +416,10 @@ public function format( $value = null, $name = null, $options = null, $pod = nul
$value = $this->trim_decimals( $value, $dot );
}

if ( null !== $prefix ) {
$value = $prefix . $value;
}

return $value;
}

Expand Down
2 changes: 1 addition & 1 deletion ui/js/dfv/pods-dfv.min.asset.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"dependencies":["lodash","moment","react","react-dom","react-jsx-runtime","regenerator-runtime","wp-api-fetch","wp-autop","wp-components","wp-compose","wp-data","wp-element","wp-hooks","wp-i18n","wp-keycodes","wp-plugins","wp-primitives","wp-url"],"version":"cc375f48b888a23fa558"}
{"dependencies":["lodash","moment","react","react-dom","react-jsx-runtime","regenerator-runtime","wp-api-fetch","wp-autop","wp-components","wp-compose","wp-data","wp-element","wp-hooks","wp-i18n","wp-keycodes","wp-plugins","wp-primitives","wp-url"],"version":"34bb2de7dbe3f43d3ee1"}
2 changes: 1 addition & 1 deletion ui/js/dfv/pods-dfv.min.js

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions ui/js/dfv/src/fields/number-field/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const NumberField = ( {
number_format: format,
number_format_soft: softFormat,
number_format_type: type = 'number',
number_keep_leading_zeroes: keepLeadingZeroes = false,
number_html5: html5,
number_max: max,
number_max_length: digitMaxLength,
Expand All @@ -43,7 +44,7 @@ const NumberField = ( {
// a formatted string, so be able to handle either one, but keep
// a formatted version available locally.
const [ formattedValue, setFormattedValue ] = useState(
formatNumberWithPodsFormat( value, format, softFormat )
formatNumberWithPodsFormat( value, format, softFormat, keepLeadingZeroes )
);

useEffect( () => {
Expand Down Expand Up @@ -71,7 +72,7 @@ const NumberField = ( {
setValue( parseFloatWithPodsFormat( event.target.value, 0 <= event.target.value.indexOf( ',' ) ? '9999.99' : '9999,99' ) );
setFormattedValue( formatNumberWithPodsFormat( event.target.value, format, softFormat ) );
} else {
setValue( parseFloatWithPodsFormat( event.target.value, format ) );
setValue( parseFloatWithPodsFormat( event.target.value, format, keepLeadingZeroes ) );
setFormattedValue( event.target.value );
}
};
Expand All @@ -80,7 +81,8 @@ const NumberField = ( {
const newFormattedValue = formatNumberWithPodsFormat(
value,
format,
softFormat
softFormat,
keepLeadingZeroes,
);

setFormattedValue( newFormattedValue );
Expand Down
39 changes: 32 additions & 7 deletions ui/js/dfv/src/helpers/formatNumberWithPodsFormat.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,10 @@ export const getDecimalSeparatorFromPodsFormat = ( format ) => {
export const parseFloatWithPodsFormat = (
newValue,
format,
keepLeadingZeroes = false,
) => {
// Turn empty string to 0.
if ( '' === newValue ) {
if ( '' === newValue || undefined === newValue || null === newValue || false === newValue ) {
return 0;
}

Expand All @@ -82,23 +83,36 @@ export const parseFloatWithPodsFormat = (
return newValue;
}

let prefix = null;

if ( keepLeadingZeroes ) {
prefix = ( newValue.match( /^0+/ ) || [ '' ] )[ 0 ];
}

const thousands = getThousandsSeparatorFromPodsFormat( format );
const dot = getDecimalSeparatorFromPodsFormat( format );

// Remove the thousands separators and change the decimal separator to a period,
// so that parseFloat can handle the rest.
return parseFloat(
let formattedValue = parseFloat(
newValue.split( thousands ).join( '' ).split( dot ).join( '.' ),
);

if ( keepLeadingZeroes && prefix ) {
formattedValue = prefix + formattedValue.toString();
}

return formattedValue;
};

export const formatNumberWithPodsFormat = (
newValue,
format,
trimZeroDecimals = false,
keepLeadingZeroes = false,
) => {
// Skip empty strings or undefined.
if ( '' === newValue || undefined === newValue || null === newValue ) {
if ( '' === newValue || undefined === newValue || null === newValue || false === newValue ) {
return '0';
}

Expand All @@ -112,12 +126,23 @@ export const formatNumberWithPodsFormat = (
? parseFloatWithPodsFormat( newValue, format )
: newValue;

const formattedNumber = isNaN( floatNewValue )
? undefined
: formatNumber( floatNewValue, 'auto', dotSeparator, thousands );
if ( isNaN( floatNewValue ) ) {
return undefined;
}

let formattedNumber = formatNumber( floatNewValue, 'auto', dotSeparator, thousands );
let prefix = null;

if ( keepLeadingZeroes ) {
prefix = ( newValue.match( /^0+/ ) || [ '' ] )[ 0 ];
}

if ( keepLeadingZeroes && prefix ) {
formattedNumber = prefix + formattedNumber.toString();
}

// We may need to trim decimals
if ( ! trimZeroDecimals || undefined === formattedNumber ) {
if ( ! trimZeroDecimals ) {
return formattedNumber;
}

Expand Down
Loading