-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
1,228 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
document.addEventListener( 'DOMContentLoaded', () => { | ||
const multiFieldInputs = document.querySelectorAll( '.form-block__element.is-sub-element input[data-max-length]' ); | ||
|
||
for ( const multiFieldInput of multiFieldInputs ) { | ||
multiFieldInput.addEventListener( 'input', onInput ); | ||
multiFieldInput.addEventListener( 'paste', handlePaste ); | ||
} | ||
} ); | ||
|
||
const onInput = ( event ) => addLeadingZero( event.currentTarget ); | ||
|
||
/** | ||
* Add leading zeros to an element. | ||
* | ||
* @see https://stackoverflow.com/a/72864152/3461955 | ||
* | ||
* @param {HTMLElement} element The element to add zeros to | ||
* @param {string} [attribute='data-max-length'] The attribute to check for | ||
*/ | ||
function addLeadingZero( element, attribute = 'data-max-length' ) { | ||
const maxLength = parseInt( element.getAttribute( attribute ) ); | ||
const isNegative = parseInt( element.value ) < 0 | ||
let newValue = ( '0'.repeat( maxLength ) + Math.abs( element.value ).toString() ).slice( -maxLength ); | ||
|
||
if ( isNegative ) { | ||
newValue = '-' + newValue; | ||
} | ||
|
||
element.value = newValue; | ||
} | ||
|
||
/** | ||
* Handle pasting into custom date fields. | ||
* | ||
* @param {Event} event Paste event | ||
*/ | ||
function handlePaste( event ) { | ||
const currentTarget = event.currentTarget; | ||
const isFirstInput = !! currentTarget.closest( '.form-block__element.is-sub-element:first-of-type' ); | ||
|
||
if ( ! isFirstInput ) { | ||
return; | ||
} | ||
|
||
const container = currentTarget.closest( '.form-block__element:not(.is-sub-element)' ); | ||
const inputs = container.querySelectorAll( 'input' ); | ||
const format = getFormat( inputs ); | ||
const paste = ( event.clipboardData || event.originalEvent.clipboardData || window.clipboardData ).getData( 'text' ) || ''; | ||
const matches = paste.match( new RegExp( format ) ); | ||
|
||
if ( matches ) { | ||
event.preventDefault(); | ||
|
||
for ( let i = 0; i < inputs.length; i++ ) { | ||
inputs[ i ].value = matches[ 2 * i + 1 ]; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Get regular expression format from a pasted string. | ||
* | ||
* @param {HTMLCollection} inputs List of inputs | ||
* @returns {string} Regular expression string | ||
*/ | ||
function getFormat( inputs ) { | ||
let isFirst = true; | ||
let format = '^'; | ||
|
||
const escape = ( string, symbol ) => { | ||
let newString; | ||
|
||
for ( let i = 0; i < string.length; i++ ) { | ||
newString = symbol + string.charAt( i ); | ||
} | ||
|
||
return newString; | ||
} | ||
|
||
for ( const input of inputs ) { | ||
if ( ! isFirst ) { | ||
if ( input.previousElementSibling ) { | ||
format += ' ' + input.previousElementSibling.textContent + ' '; | ||
} | ||
} | ||
else { | ||
isFirst = false; | ||
} | ||
|
||
format += '([0-9]{' + input.getAttribute( 'data-validate-length-range' ) + '})'; | ||
format += '('; | ||
|
||
if ( input.nextElementSibling ) { | ||
format += escape( input.nextElementSibling.textContent, '\\' ); | ||
} | ||
} | ||
|
||
format = format.replace( /\($/, '' ); | ||
format += '?)'.repeat( inputs.length ); | ||
|
||
return format.replace( /\)$/, '' ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
.form-block__element { | ||
input:not([type="reset"]):not([type="submit"]), | ||
textarea { | ||
border: 1px solid #949494; | ||
font-family: inherit; | ||
font-size: 1em; | ||
} | ||
|
||
input:not([type="checkbox"]):not([type="radio"]):not([type="reset"]):not([type="submit"]), | ||
textarea { | ||
box-sizing: border-box; | ||
display: block; | ||
padding: calc(.667em + 2px); | ||
width: 100%; | ||
} | ||
|
||
input[type="reset"], | ||
input[type="submit"] { | ||
align-self: flex-start; | ||
} | ||
|
||
select { | ||
-moz-appearance: none; | ||
-webkit-appearance: none; | ||
appearance: none; | ||
background: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='10' height='10' fill='currentColor'><polygon points='0,0 10,0 5,5'/></svg>") no-repeat right calc(.667em + 2px) top 56%; | ||
border: 1px solid #949494; | ||
border-radius: 0; | ||
font-size: 1em; | ||
line-height: 1.6; | ||
padding: calc(.667em + 2px) calc(1.333em + 12px) calc(.667em + 2px) calc(.667em + 2px); | ||
} | ||
|
||
&.is-type-checkbox, | ||
&.is-type-radio { | ||
input { | ||
margin-top: .35em; | ||
} | ||
|
||
.form-block__label { | ||
margin-left: .3em; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.