Socket
Socket
Sign inDemoInstall

lin3s-front-foundation

Package Overview
Dependencies
12
Maintainers
2
Versions
56
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    lin3s-front-foundation

Library that provides a sort of commonly used front-end components in LIN3S projects


Version published
Weekly downloads
37
increased by42.31%
Maintainers
2
Install size
1.10 MB
Created
Weekly downloads
 

Readme

Source

LIN3S Front Foundation

Base CSS and JS files for building views in LIN3S way.

Installation

The recommended and the most suitable way to install is through Yarn:

$ yarn add lin3s-front-foundation

or alternatively through NPM:

$ npm install --save lin3s-front-foundation


Usage - Available features

Async

This package will provide all asynchronous related implementations. For instance, Promise related ones.

Async.cancelablePromise( promise )

This method will wrap a Promise object and provide a cancel() method for canceling the inner Promise. We will access the original promise throught the promise property.

import {Async} from 'lin3s-front-foundation';
 
const aPromise = new Promise((resolve, reject) => {
  // ...
});
 
const aCancelablePromise = Async.cancelablePromise(aPromise);
 
aCancelablePromise.promise.then(resolvedObject => {
  // ...
});
 
aCancelablePromise.cancel();
// aCancelablePromise.promise has been rejected right after calling the cancel() method.

Browser

This package will provide all browser related implementations.

Browser.isIE11()

This method will tell us if the browser is Internet Explorer 11.

import {Browser} from 'lin3s-front-foundation';
 
const isIE11 = Browser.isIE11();

Dom

This package will provide all Dom related implementations.

import {Dom} from 'lin3s-front-foundation';

const currentHtmlLang = Dom.getHtmlLang();

console.log(currentHtmlLang);
Dom.getHtmlLang()

This method will return the html tag's lang attribute.

Dom.loadScript( scriptPath )

This method will load an script by the provided scriptPath and return us a Promise object. This promise will be resolved one the script has been loaded.

import {Dom} from 'lin3s-front-foundation';
 
const scriptPath = 'https://yourdomain.com/script-path.js';
 
const scriptLoadPromise = Dom.loadScript(scriptPath);

scriptLoadPromise.then(() => {
  // Our script has been loaded!
});
Dom.injectScript( scriptCode, domNode = document.body )

This method will inject the specified scriptCode at the passed domNode. domNode will be the document's body by default. Provided scriptCode will be wrapped with an IIFE

import {Dom} from 'lin3s-front-foundation';
 
const 
  mainDomNode = document.querySelector('.main'),
  testScriptA = `console.log('This is the injected script A');`,
  testScriptB = `console.log('This is the injected script B');`;

Dom.injectScript(testScriptA);
Dom.injectScript(testScriptB, mainDomNode);
Dom.waitImagesLoadInDomNode( domNode )

This method will return us a Promise object that will be resolved once all the images contained in the provided domNode have been loaded.

import {Dom} from 'lin3s-front-foundation';
 
const imagesCollection = document.querySelector('.images__collection');
 
const imagesLoadPromise = Dom.waitImagesLoadInDomNode(imagesCollection);
 
imagesLoadPromise.then(() => {
  // All images have been loaded!
});
Dom.scrollToElement( selector, {duration = 500, offset = 0, callback = null} )

This method will scroll to the given element's positions minus offset in the given duration (in milliseconds). A callback can be provided that will trigger once the animation has finished.

import {Dom} from 'lin3s-front-foundation';

const callback = () => console.log('Scroll to element done!');

Dom.scrollToElement('.some-selector', {duration: 2000, offset: 10, callback});

Dom - Utilities / Helpers

Dom.isDomNodeDescendantOfDomNode( needleDomNode, mainDomNode )

This method will return true if the passed needleDomNode y a descendant of the mainDomNode.

Dom.getDomNodeIndex( domNode, selector = null )

This method will return the index of the provided domNode, optinally filtered by a css selector. It is a native alternative to the jQuery's .index() method.

Dom.removeDomNodes( domNodes )

This method will remove the passed domNodes from their parents. It will work with a single node as well. It is a native alternative to the jQuery's .remove() method.

Dom.addSelectorFilteredEventListener( domNode, eventName, selector, event => {} )

This method will add an event listener for the eventName to the passed domNode, filtering the event.target with the defined selector. It is a native alternative to the jQuery's .on(eventName, selector, callback) method when filtering it's targets by a selector.

Dom.dispatchNativeEvent( domNode, eventName )

This method will dispatch a DOMElement native event. It's a native alternative to the jQuery's .trigger(eventName) method.

Dom.scrollToElement( selector, {duration = 500, offset = 0, callback = null} )

This method will help us scrolling to a document node's position. It allows defining a duration (default 500ms), an offset (default 0) and a complete callback (default null).

import {Dom} from 'lin3s-front-foundation';
  
const links = document.querySelectorAll('.menu__list .menu__link');
  
const handleClick = event => Dom.scrollToElement(event.target.hash, {duration: 250, offset: 10});
  
Array.from(links, link => {
  link.addEventListener('click', handleClick, false);
});

Cookies

This package will provide all document.cookie related implementations.

Cookies.read

This method will return the value of a cookie by its name.

import {Cookies} from 'lin3s-front-foundation';

const cookieValue = Cookies.read('some-name');
Cookies.write

This method will write a cookie. Accepts an object with names parameters for name, value, expiration, domain and path.

import {Cookies} from 'lin3s-front-foundation';

Cookies.write({
  name: 'some-name',
  value: 'my value',
  expiration: 3600000, // 1 hour in milliseconds
  domain: 'example.com',
  path: '/'
});

The list of the available parameters, their type and default values are as follows:

ParameterTypeRequiredDefault value
namestringyes-
valuestringno''
expirationint (milliseconds)no*
domainstringnolocation.hostname
pathstringno/

* It will set cookie expiration to 'Session'

Cookies - EventBus events

Cookies.write will publish CookieWrittenEvent through the EventBus. We will subscribe to this event using some exported helper methods:

import {EventBus} from 'lin3s-front-foundation';

EventBus.onCookieWritten(({cookie}) => {
  const myCookieName = cookie.name;
  const myCookieValue = cookie.value;
});


Usage - Available UI components

GMap

This component will provide all the necessary implementations for displaying a Google Map, setting it's markers, setting a clusterer, use the Google Map geocoding feature and displaying the MarkerDetail view.

GMap - Styles

The GMap component comes with some default styles. You must include them in order to correctly render it.

@import './node_modules/lin3s-front-foundation/dist/scss/ui/components/gmap';
@import './node_modules/lin3s-front-foundation/dist/scss/ui/components/gmap-marker-detail';
GMap - Basic setup

In order to setup the GMap component, we will define every required parameter while including the twig template. It will automatically fetch the needed js files and will init the Google Maps map instance.

The list of the available parameters, their type and default values are as follows:

ParameterTypeRequiredDefault value
gmap_api_keystringyes-
gmap_center_latfloatyes-
gmap_center_lngfloatyes-
gmap_initial_zoomintno8
gmap_max_zoomintno12
gmap_marker_default_pathstringyes-
gmap_marker_selected_pathstringyes-
gmap_marker_group_pathstringyes-
gmap_marker_widthintyes-
gmap_marker_heightintyes-
gmap_cluster_enabledintno1 [0,1]
gmap_cluster_text_offset_xintno0
gmap_cluster_text_offset_yintno0
gmap_cluster_text_colorstringno'#ffffff'
gmap_cluster_text_sizeintno18
gmap_stylestringnoview**

** In order to generate the Google Map custom styles, we could use any available tool. For instance the snazzy maps online platform.

The marker images will be required to be served both on .png and .svg formats.

The marker paths must be defined without the file extensions, as the GMap component will get the .png or .svg files based on the client browser.

This is a basic setup example:

{% include '@lin3s_front_foundation/components/gmap.html.twig' with {
    gmap_api_key: 'AIzaSyDQaCE_7C5iAmpwr_y1C1DHbtZsqag74Sk',
    gmap_center_lat: '43.2631394',
    gmap_center_lng: '-2.9275847',
    gmap_initial_zoom: 12,
    gmap_max_zoom: 16,
    gmap_marker_default_path: '/images/gmap/marker-default',
    gmap_marker_selected_path: '/images/gmap/marker-selected',
    gmap_marker_group_path: '/images/gmap/marker-group',
    gmap_marker_width: 40,
    gmap_marker_height: 60,
    gmap_cluster_enabled: 1,
    gmap_cluster_text_size: 14,
    gmap_cluster_text_color: '#222222'
} %}
GMap - JS API

These are the mostly used methods available on the GMap component's instance.

NameParametersReturned valueDescription
setCenterOffsets{
     x: 0,
     y: 0
}
undefined (void)This method will set the instance's center's offset (in pixels)
setMarkersmarkers: [{
     id: 0,
     lat: 43.2631394,
     lng: -2.9275847,
     // your properties...
},
//...
]
undefined (void)This method will display the passed markers on the map. It will generate the clusters automatically.

Each marker object must, at least, have the lat, lng and id properties.
showMarkerDetailViewmarkerId, markerDetailHtmlContentundefined (void)This method will render and display the passed markerDetailHtmlContent centered on the correspondingn marker.
hideMarkerDetailView-undefined (void)This method will hide the currently visible marker detail view.
geocodeAddressaddress: Stringundefined (void)This method will geocode the passed address (Address, town, ZIP code, city...) and center the map on the result's location.

If no result matches the passed address, it will publish an event through the EventBus.
resetMapZoomAndCenterToDefault-undefined (void)This method will reset the instance's center and zoom to the default values.
GMap - EventBus events

Each GMap instance will publish these events through the EventBus. We will subscribe to these events using some exported helper methods:

import {EventBus} from 'lin3s-front-foundation';

const domNode = document.querySelector('.my-map');

EventBus.onGMapInitialized(domNode, gmapInitializedEvent => {
  const gmapInstance = gmapInitializedEvent.gmapInstance;
});

EventBus.onGMapGeocode(domNode, gmapGeocodeEvent => {
  console.log(gmapGeocodeEvent.status);
  console.log(gmapGeocodeEvent.results);
});

EventBus.onGMapMarkerSelected(domNode, gmapMarkerSelectedEvent => {
  const selectedMarker = gmapMarkerSelectedEvent.marker;
});
TypeProperties
GMapInitializedEventgmapInstance: GMap
GMapMarkerSelectedEventgmapInstance: GMap
marker: Your marker object representation
GMapGeocodeEventgmapInstance: GMap
status: response status
results: geociding results
GMap - Advanced features

If we need to work with the initialized GMap component instance, we must subscribe to the GMapInitializedEvent, that will be published through the event-bus once the gmap component has been initialized.

import {EventBus} from 'lin3s-front-foundation';

const domNode = document.querySelector('.my-map');

EventBus.onGMapInitialized(domNode, gmapInitializedEvent => {
  const gmapInstance = gmapInitializedEvent.gmapInstance;
  // whatever...
});

We will set the GMap component's markers calling the setMarkers(markers) method.

If we want to use the built-in geocoding feature, we will call the geocodeAddress(address) of the GMap instance.

The GMap component also comes with methods for displaying/hiding the marker detail view ( showMarkerDetailView(markerId, markerDetailContentHtml) and hideMarkerDetailView()).

This is a complete example of the component's features:

import {EventBus} from 'lin3s-front-foundation';

class GMapTest {
  constructor(domNode) {
    this.domNode = domNode;
  
    EventBus.onGMapInitialized(this.domNode, gmapInitializedEvent => {
      const gmapInstance = gmapInitializedEvent.gmap;
      this.setupMarkers();
      this.init();
    });
  }
  
  setupMarkers() {
    const markers = [{
      id: 0,
      lat: 43.2631394,
      lng: -2.9275847
    }];

    this.gmapInstance.setMarkers(markers);
  }
  
  init() {
    this.filterInput = document.querySelector('.my-input-class'); 
    this.filterInput.addEventListener('input', () => {
      this.gmapInstance.geocodeAddress(this.filterInput.value);
    });
    
    EventBus.onGMapGeocode(this.domNode, gmapGeocodeEvent => {
      console.log(gmapGeocodeEvent.status);
      console.log(gmapGeocodeEvent.results);
    });
    
    EventBus.onGMapMarkerSelected(this.domNode, gmapMarkerSelectedEvent => {
      this.onMarkerSelected(gmapMarkerSelectedEvent.marker);
    });
  }
  
  onMarkerSelected(marker) {
    if (marker === undefined) {
      this.gmapInstance.hideMarkerDetailView();
    } else {
      this.gmapInstance.showMarkerDetailView(
        marker.id,
        `<h3>This is the marker detail's inner html content</h3>
         <p>Marker <b>lat</b>: ${marker.lat}</p>
         <p>Marker <b>lng</b>: ${marker.lng}</p>`
      );
    }
  }
}

onDomReady(() => {
  new GMapTest();
});

FormGroupInput

The component is composed by the FormLabel, FormInput and the FormError atoms.

FormGroupInput - Styles

The FormGroupInput component and it's associated atoms come with some default styles. You must include them in order to correctly render them.

@import './node_modules/lin3s-front-foundation/dist/scss/ui/atoms/form-label';
@import './node_modules/lin3s-front-foundation/dist/scss/ui/atoms/form-error';
@import './node_modules/lin3s-front-foundation/dist/scss/ui/atoms/form-input';
@import './node_modules/lin3s-front-foundation/dist/scss/ui/components/form-group-input';
FormGroupInput - Basic setup

In order to setup the FormGroupInput component, we will define every required parameter while including the twig template.

The list of the available parameters, their type and default values are as follows:

ParameterTypeRequiredDefault value
input_idstringyes
input_namestringnonull
input_valuestringnonull
input_requiredintno0
input_validateintno0
input_validation_patternstringno''
input_validation_typestringno''
input_typestringno'text'
input_placeholderstringyes-
input_label_class_namestringnonull
input_label_modifiersstringnonull
input_label_contenthtmlnonull
input_errorsarray
{
    content: string*,
    modifiers: string
}
nonull
input_class_namestringnonull
input_modifiersstringnonull
input_spinnerintno0

This is a common setup example:

{% include '@lin3s_front_foundation/components/form_group_input.html.twig' with {
    input_id: 'my-form-user-name',
    input_required: 1,
    input_validate: 1,
    input_validation_type: 'phone',
    input_type: 'text',
    input_placeholder: 'Enter some data...',
    input_label_content: 'Your user name',
    input_errors: [{
        content: 'This field is required',
        modifiers: 'form-error--not-filled'
    }, {
        content: 'Entered phone is not a 9 digit valid phone',
        modifiers: 'form-error--not-valid'
    }],
    input_spinner: 1
} %}

FormGroupSelect

This component and it's associated FormSelect atom will build a custom rich select component. The component is composed by the FormSelect, FormLabel, FormInput and the FormError atoms.

FormGroupSelect / FormSelect - Styles

The FormGroupSelect component and it's associated atoms come with some default styles. You must include them in order to correctly render them.

@import './node_modules/lin3s-front-foundation/dist/scss/ui/atoms/form-label';
@import './node_modules/lin3s-front-foundation/dist/scss/ui/atoms/form-error';
@import './node_modules/lin3s-front-foundation/dist/scss/ui/atoms/form-input';
@import './node_modules/lin3s-front-foundation/dist/scss/ui/atoms/form-select';
@import './node_modules/lin3s-front-foundation/dist/scss/ui/components/form-group-select';
FormGroupSelect - Basic setup

In order to setup the FormGroupSelect component, we will define every required parameter while including the twig template.

The list of the available parameters, their type and default values are as follows:

ParameterTypeRequiredDefault valuePurpose
select_idstringyes
select_namestringnonull
select_requiredintno0
select_validateintno0
select_validation_patternstringno''Any valid RegExp pattern
select_validation_typestringno''Built-in validatory validation types ['email', 'phone', 'any']
select_mobile_breakpointintno1024
select_max_height_mobileintno260
select_max_height_desktopintno420
select_is_filterableintno1
select_filter_placeholderstringnonull
select_filter_order_bystringno'value'If you set 'label' as this parameter, the component will order it's items by the 'label' while filtering it's options.
select_label_modifiersstringnonull
select_label_contenthtmlnonull
select_errorsarray
{
    content: string*,
    modifiers: string
}
nonull
select_select_modifiersstringnonull
select_no_selection_labelstringno'--'
select_no_selection_valuestringno'--'
select_optionsarrayyesThese are the component's options. Each option must have, at least, this shape:
{
    value: string*
    label: string*
    selected: int=0
}
select_outside_click_to_close_enabledintno1

This is a full setup example:

{% set my_select_options = [{
    label: Male,
    value: 0,
    selected: 1
}, {
    label: Female,
    value: 1
}] %}

{% include '@lin3s_front_foundation/components/form_group_select.html.twig' with {
    select_id: 'my-select',
    select_required: 1,
    select_validate: 1,
    select_validation_pattern: '^(?!.*--).*$',
    select_mobile_breakpoint: 768,
    select_max_height_mobile: 260,
    select_max_height_desktop: 420,
    select_is_filterable: 1,
    select_filter_placeholder: 'Type to filter...',
    select_filter_order_by: 'label',
    select_label_modifiers: null,
    select_label_content: 'My select\'s label',
    select_errors: [{
        content: 'This field is required',
        modifiers: 'form-error--not-filled'
    }, {
        content: 'You cannot select the default value',
        modifiers: 'form-error--not-valid'
    }],
    select_select_modifiers: null,
    select_no_selection_label: '--',
    select_no_selection_value: '--',
    select_options: my_select_options
} %}
FormSelect - JS API

These are the mostly used methods available on the FormSelect atom's instance.

NameParametersReturned valueDescription
open-undefined (void)This method will open the select
close-undefined (void)This method will close the select
update{
    options,
    publishEvent: true
}
undefined (void)This method will update the FormSelect's options with the provided ones. By default it will publish the FormSelectOptionSelectedEvent. Each option object must, at least, have the label and value properties.
enable-undefined (void)This method will enable the select
disable-undefined (void)This method will disable the select
FormSelect - EventBus events

Each FormSelect instance will publish these events through the EventBus. We will subscribe to these events using some exported helper methods:

import {EventBus} from 'lin3s-front-foundation';

const domNode = document.querySelector('.my-form-select');

EventBus.onFormSelectInitialized(domNode, formSelectInitializedEvent => {
  const formSelectInstance = formSelectInitializedEvent.formSelectInstance;
});

EventBus.onFormSelectOptionSelected(domNode, formSelectOptionSelectedEvent => {
  const selectedValue = formSelectOptionSelectedEvent.optionValue;
});

EventBus.onFormSelectStateChanged(domNode, formSelectStateChangedEvent => {
  const formSelectState = formSelectStateChangedEvent.state;
});
TypeProperties
FormSelectInitializedEventformSelectInstance: FormSelect
FormSelectOptionSelectedEventformSelectInstance: FormSelect
marker: Your marker object representation
FormSelectStateChangedEventformSelectInstance: FormSelect
state: [FormSelect.STATE.OPENED | FormSelect.STATE.CLOSED]

FormGroupTextarea

The component is composed by the FormTextarea, FormLabel and the FormError atoms.

FormGroupTextarea - Styles

The FormGroupTextarea component and it's associated atoms come with some default styles. You must include them in order to correctly render them.

@import './node_modules/lin3s-front-foundation/dist/scss/ui/atoms/form-label';
@import './node_modules/lin3s-front-foundation/dist/scss/ui/atoms/form-error';
@import './node_modules/lin3s-front-foundation/dist/scss/ui/atoms/form-textarea';
@import './node_modules/lin3s-front-foundation/dist/scss/ui/components/form-group-textarea';
FormGroupTextarea - Setup

In order to setup the FormGroupTextarea component, we will define every required parameter while including the twig template.

The list of the available parameters, their type and default values are as follows:

ParameterTypeRequiredDefault valuePurpose
class_namestringnonull
modifiersstringnonull
textarea_idstringnonull
textarea_namestringnonull
textarea_valuestringnonull
textarea_requiredintno0
textarea_validateintno0
textarea_validation_patternstringno''Any valid RegExp pattern
textarea_validation_typestringno''Built-in validatory validation types ['email', 'phone', 'any']
textarea_label_class_namestringno''
textarea_label_modifiersstringno''
textarea_label_contenthtmlnonull
textarea_errorsarray
{
    content: string*,
    modifiers: string
}
nonull
textarea_modifiersstringno''
textarea_placeholderstringno''
textarea_spinnerintno0

This is a full setup example:

{% include '@lin3s_front_foundation/components/form_group_textarea.html.twig' with {
    textarea_id: 'palindrome',
    textarea_required: 1,
    textarea_validate: 1,
    textarea_validation_pattern: '\\b(\\w)?(\\w)\\w?\\2\\1', {# Note that backslashes must be escaped (\ -> \\) #},
    textarea_label_content: '2-5 letter palindrome',
    textarea_errors: [{
        content: 'This field is required',
        modifiers: 'form-error--not-filled'
    }, {
        content: 'Entered text does not include a valid 2-5 letter palindrome',
        modifiers: 'form-error--not-valid'
    }],
    textarea_spinner: 1
} %}

FormGroupCheckbox

The component is composed by the FormCheckbox, FormLabel and the FormError atoms.

FormGroupCheckbox - Styles

The FormGroupCheckbox component and it's associated atoms come with some default styles. You must include them in order to correctly render them.

@import './node_modules/lin3s-front-foundation/dist/scss/ui/atoms/form-label';
@import './node_modules/lin3s-front-foundation/dist/scss/ui/atoms/form-error';
@import './node_modules/lin3s-front-foundation/dist/scss/ui/atoms/form-checkbox';
@import './node_modules/lin3s-front-foundation/dist/scss/ui/components/form-group-checkbox';
FormGroupCheckbox - Setup

In order to setup the FormGroupCheckbox component, we will define every required parameter while including the twig template.

The list of the available parameters, their type and default values are as follows:

ParameterTypeRequiredDefault valuePurpose
class_namestringnonull
modifiersstringnonull
checkbox_idstringnonull
checkbox_namestringnonull
checkbox_requiredintno0
checkbox_validateintno0
checkbox_label_class_namestringno''
checkbox_label_modifiersstringno''
checkbox_label_contenthtmlnonull
checkbox_errorsarray
{
    content: string*,
    modifiers: string
}
nonull
checkbox_modifiersstringno''
checkbox_contentstringyes

This is a full setup example:

{% include '@lin3s_front_foundation/components/form_group_checkbox.html.twig' with {
    checkbox_id: 'palindrome',
    checkbox_required: 1,
    checkbox_validate: 1,
    checkbox_content: 'I hace read and accept the terms and conditions',
    textarea_errors: [{
        content: 'Yout must accept the terms and conditions',
        modifiers: 'form-error--not-filled'
    }]
} %}


Usage - Available UI (React) components

FormGroupSelect (React) component

This React component will build a FormGroupSelect (vanilla) counterpart.

FormGroupSelect (React) - Basic setup

This is a controlled component. For a full initialization example, take a look at the provided example initialization and FormGroupSelect use case.



Usage - Available UI atoms

FormLabel

This atom will render an html <label> with some custom attributes.

The list of the available parameters, their type and default values are as follows:

ParameterTypeRequiredDefault value
label_forstringnonull
label_requiredintno0
label_class_namestringnonull
label_modifiersstringnonull
label_contenthtmlyes-

This is a common setup example:

{% include '@lin3s_front_foundation/atoms/form_label.html.twig' with {
    label_for: 'user-email',
    label_required: 1,
    label_content: 'Email:'
} %}
FormLabel - Customization

In order to customize the atom's appearance, you should define these variables before importing the involved scss file.

$form-label-text-color: #222 !default;
$form-label-text-color-required: #f00 !default;
$form-label-font-family: sans-serif !default;
$form-label-font-size: 16px !default;
$form-label-font-weight: bold !default;
$form-label-line-height: 20px !default;

@import './node_modules/lin3s-front-foundation/dist/scss/ui/atoms/form-label';

FormError

This atom will render a form-input associated error.

The list of the available parameters, their type and default values are as follows:

ParameterTypeRequiredDefault value
error_contentstringyes-
error_class_namestringnonull

This is a common setup example:

{% include '@lin3s_front_foundation/atoms/form_error.html.twig' with {
    error_content: 'This field is required.'
} %}
FormError - Customization

In order to customize the atom's appearance, you should define these variables before importing the involved scss file.

$form-error-background-color: #f2b8c2 !default;
$form-error-text-color: #b20008 !default;
$form-error-border-color: rgba($form-error-text-color, .5) !default;
$form-error-animation: $animation-vertical-node-in !default;
$form-error-font-family: sans-serif !default;
$form-error-font-size: 14px !default;
$form-error-font-weight: normal !default;
$form-error-line-height: 18px !default;

@import './node_modules/lin3s-front-foundation/dist/scss/ui/atoms/form-error';

FormInput

This atom will render a form input.

The list of the available parameters, their type and default values are as follows:

ParameterTypeRequiredDefault value
input_idstringno-
input_namestringnonull
input_valuestringnonull
input_requiredintno0
input_typestringno'text'
input_placeholderstringnonull
input_class_namestringnonull
input_modifiersstringnonull
input_validateintno0
input_validation_patternstringnonull
input_validation_typestringnonull
input_focusableintno1

This is a common setup example:

{% include '@lin3s_front_foundation/atoms/form_input.html.twig' with {
    input_placeholder: 'Enter some data...'
} %}
FormInput - Customization

In order to customize the atom's appearance, you should define these variables before importing the involved scss file.

$form-input-border-color: #d1d1d1 !default;
$form-input-border-color-hover: #0e8fff !default;
$form-input-placeholder-text-color: rgba(#444, .8) !default;
$form-input-font-family: sans-serif !default;
$form-input-font-size: 16px !default;
$form-input-font-size-small: 14px !default;
$form-input-font-weight: normal !default;
$form-input-line-height: 20px !default;
$form-input-line-height-small: 18px !default;

@import './node_modules/lin3s-front-foundation/dist/scss/ui/atoms/form-input';

FormSelect

This atom will render a custom rich form select.

The list of the available parameters, their type and default values are as follows:

ParameterTypeRequiredDefault value
select_idstringno-
select_namestringnonull
select_requiredintno0
select_class_namestringnonull
select_modifiersstringnonull
select_no_selection_labelstringno'--'
select_no_selection_valuestringno'--'
select_mobile_breakpointintno1024
select_max_height_mobileintno260
select_max_height_desktopintno420
select_is_filterableboolno1
select_filter_placeholderstringnonull
select_filter_order_bystring ('label'|'value')no'value'
select_optionsarrayyesThese are the atom's options. Each option must have, at least, this shape:
{
    value: string*
    label: string*
    selected: int=0
}
select_is_filterableintno1
select_outside_click_to_close_enabledintno1
select_validateintno0
select_validation_patternstringnonull
select_validation_typestringnonull

This is a common setup example:

{% set my_select_options = [{
    label: Male,
    value: 0,
    selected: 1
}, {
    label: Female,
    value: 1
}] %}

{% include '@lin3s_front_foundation/atoms/form_select.html.twig' with {
    select_class_name: 'form-select-demo',
    select_id: 'form-select-1',
    select_required: 1,
    select_filter_placeholder: 'Type to filter...',
    select_outside_click_to_close_enabled: 1,
    select_options: my_select_options
} %}
FormSelect - Customization

In order to customize the atom's appearance, you should define these variables before importing the involved scss file.

$form-select-background-color: #fff !default;
$form-select-background-color-disabled: #eee !default;
$form-select-border-color: #d1d1d1 !default;
$form-select-font-family: sans-serif !default;
$form-select-font-size: 16px !default;
$form-select-font-weight: normal !default;
$form-select-line-height: 20px !default;
$form-select-label-text-color: #222 !default;
$form-select-label-text-color-opened: rgba($form-select-label-text-color, .5) !default;
$form-select-options-box-shadow: 0 5px 30px -10px rgba(#222, .25) !default;
$form-select-option-text-color: #222 !default;
$form-select-option-text-color-active: #fff !default;
$form-select-option-background-color-active: #0e8fff !default;
$form-select-option-background-color-hover: #eee !default;
$form-select-option-background-color-hover-and-active: rgba($form-select-option-background-color-active, .8) !default;

@import './node_modules/lin3s-front-foundation/dist/scss/ui/atoms/form-select';

Picture

This atom will render a picture element with different sources depending on browser size and orientation.

The list of the available parameters, their type and default values are as follows:

ParameterTypeRequiredDefault value
picture_class_namestringnonull
picture_image_class_namestringnonull
picture_altstringnonull
picture_src_smallstringyes-
picture_src_mediumstringyes-
picture_src_largestringyes-
picture_src_xlargestringyes-
picture_src_xxlargestringyes-
picture_small_breakpointintno640
picture_medium_breakpointintno1024
picture_large_breakpointintno1200
picture_xlarge_breakpointintno1440
picture_custom_srcsethtmlnonull

This is a common setup example:

{% embed '@lin3s_front_foundation/atoms/picture.html.twig' with {
    picture_class_name: 'my-picture',
    picture_image_class_name: 'my-picture__image',
    picture_alt: 'Some alt text',
    picture_src_small: 'http://mydomain.com/small-image.jpg',
    picture_src_medium: 'http://mydomain.com/medium-image.jpg',
    picture_src_large: 'http://mydomain.com/large-image.jpg',
    picture_src_xlarge: 'http://mydomain.com/xlarge-image.jpg',
    picture_src_xxlarge: 'http://mydomain.com/xxlarge-image.jpg',
    picture_small_breakpoint: 768,
} %}
    {% block custom_srcset %}
        <source srcset="http://mydomain.com/xsmall-image.jpg" media="(max-width: 540px)">
    {% endblock %}
{% endembed %}


Usage - Available macros

The library provides you opinionated macros for rendering the form components with pre-defined parameters.

Atoms - form_inputs

{% macro required(type, id, placeholder, name) %}
{% macro email(id, placeholder, name) %}
{% macro requiredEmail(id, placeholder, name) %}
{% macro phone(id, placeholder, name) %}
{% macro requiredPhone(id, placeholder, name) %}

Components - form_group_checkboxes

{% macro required(id, label, content, errors) %}

Components - form_group_inputs

{% macro required(type, id, placeholder, label, errors, name) %}
{% macro email(id, placeholder, label, errors, name) %}
{% macro requiredEmail(id, placeholder, label, errors, name) %}
{% macro phone(id, placeholder, label, errors, name) %}
{% macro requiredPhone(id, placeholder, label, errors, name) %}

Components - form_group_selects

{% macro required(id, filter_placeholder, label, options, errors) %}
{% macro requiredAndNot(id, filter_placeholder, label, not_valid_value, options, errors) %}

Components - form_group_textareas

{% macro required(id, placeholder, label, errors) %}
{% macro requiredWithPattern(id, placeholder, label, pattern, errors) %}


Validatory

FrontFoundation provides js and scss utility/helper code to work with the validatory librar to make easier our initialization or style customization.

Validatory - initWithEvents & EventBus subscriptions

FrontFoundation library provides an utility method EventBus.validatory.initWithEvents for initializing the validatory library coupled to the lin3s-event-bus library, so our app can be notified when a form, or any of it's element's validation state changes using the exposed EventBus.validatory.onFormStateChanged and EventBus.validatory.onFormElementStateChanged subscriptions.

import {EventBus} from 'lin3s-front-foundation';

EventBus.validatory.initWithEvents({
  formSelector: '#validatory-form',
  formElementSelector: '#validatory-form input, #validatory-form select, #validatory-form textarea'
});

// Event subscriptions through the event-bus

const myForm = document.getElementById('validatory-form');

EventBus.validatory.onFormStateChanged(myForm, stateChangedEvent => {
  // Do what you want with the provided payload object
  console.log(stateChangedEvent.formValidatorInstance);
});

const myFormElement = document.querySelector('#validatory-form .zip-code');

EventBus.validatory.onFormElementStateChanged(myFormElement, stateChangedEvent => {
  // Do what you want with the provided payload object
  console.log(stateChangedEvent.formElementValidatorInstance);
});

Validatory - custom validators and UI customization

In order to build and append a custom validator, we must first prepare the twig markup, then write our custom validator, and add some needed scss.

Note that we are adding some custom validation error messages/markup. (form-error--not-valid-zip-code, form-error--not-valid-no-service). These custom errors will match the custom validator's resolved errorCodes.

{% include '@lin3s_front_foundation/components/form_group_input.html.twig' with {
    input_id: 'zip-code',
    input_required: 1,
    input_validate: 1,
    input_validation_type: 'phone',
    input_placeholder: 'Enter your zip code',
    input_label_content: 'Zip code',
    input_errors: [{
        content: 'This field is required',
        modifiers: 'form-error--not-filled'
    }, {
        content: 'The entered value does not seem ot be a valid zip code',
        modifiers: 'form-error--not-valid-zip-code'
    }, , {
        content: 'Sorry, we are not providing service in the entered zip code's area.',
        modifiers: 'form-error--not-valid-no-service'
    }],
    input_spinner: 1
} %}
import debounce from 'es6-promise-debounce';
import {validatorRegistry, Validator, asyncValidation} from 'validatory';

const
  debouncedValidation = debounce(node => {
    console.log('Asynchronous validation started');

    const validZipCode = /^\d{5}$/.test(node.value); // zip code format validation

    if (!validZipCode) {
      return {valid: false, errorCode: 'zip-code'}; // will match the DOM markup's .form-error--not-valid-zip-code
    }

    return asyncValidation(fetch('https://jsonplaceholder.typicode.com/posts/1'), response => {
      const valid = node.value === '01005';

      return valid ? {valid} : {valid: false, errorCode: 'no-service'}; // will match the DOM markup's .form-error--not-valid-no-service
    });
  }, 500),
  asyncValidator = new Validator({
    supports: node => node.id === 'async',
    isEmpty: node => node.value === '',
    isValid: node => debouncedValidation(node),
  });

validatorRegistry.add(asyncValidator);
@import './../../node_modules/lin3s-front-foundation/dist/scss/_mixins/form-validation';

@include form_group_custom_error('.form-group-input__errors', 'zip-code');
@include form_group_custom_error('.form-group-input__errors', 'no-service');

Keywords

FAQs

Last updated on 13 Jul 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc