1.0.8 • Published 5 years ago

road-dropdown v1.0.8

Weekly downloads
7
License
MIT
Repository
-
Last release
5 years ago

Dropdown with Web Components

Build Status Slack Greenkeeper badge

Dropdown as Web Componment.

How to use:

Install: npm install road-dropdown

Vanilla JS + HTML

Usage with attributes only except for function:

// HTML

<road-dropdown
  label="Dropdown"
  option="option2"
  options='{ "option1": { "label": "Option 1" }, "option2": { "label": "Option 2" } }'
></road-dropdown>
// JavaScript

document
  .querySelector('road-dropdown')
  .addEventListener('onChange', value => console.log(value));

Alternative with properties for object/array:

// HTML

<road-dropdown
  label="Dropdown"
  option="option2"
></road-dropdown>
// JavaScript

document.querySelector('road-dropdown').options = {
  option1: { label: 'Option 1' },
  option2: { label: 'Option 2' },
};

document
  .querySelector('road-dropdown')
  .addEventListener('onChange', value => console.log(value));

React

import React from 'react';

// npm install road-dropdown
import 'road-dropdown';

const Dropdown = ({ label, option, options, onChange }) => {
  const ref = React.createRef();

  React.useEffect(() => {
    const handleChange = customEvent => onChange(customEvent.detail);

    ref.current.addEventListener('onChange', handleChange);

    return () => ref.current.removeEventListener('onChange', handleChange);
  }, []);

  return (
    <road-dropdown
      ref={ref}
      label={label}
      option={option}
      options={JSON.stringify(options)}
      onChange={onChange}
    />
  );
};

export default Dropdown;

React with Hook

Hook to use Web Components in React Components: use-custom-element.

import React from 'react';

// npm install road-dropdown
import 'road-dropdown';

// npm install use-custom-element
import useCustomElement from 'use-custom-element';

const Dropdown = props => {
  const [customElementProps, ref] = useCustomElement(props);

  return <road-dropdown {...customElementProps} ref={ref} />;
};

Contribution

  • git clone git@github.com:rwieruch/web-components-dropdown.git
  • cd web-components-dropdown
  • npm install
  • npm start
  • visit http://localhost:8080
1.0.8

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago