npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@blockly/field-dependent-dropdown

v3.0.15

Published

A Blockly dropdown field that automatically updates its available options depending on the value of another field.

Downloads

560

Readme

@blockly/field-dependent-dropdown Built on Blockly

A Blockly dropdown field where the options depend on the value of a parent field.

Installation

Yarn

yarn add @blockly/field-dependent-dropdown

npm

npm install @blockly/field-dependent-dropdown --save

Usage

This plugin adds a field type FieldDependentDropdown that is an extension of Blockly.FieldDropdown and is registered as 'field_dependent_dropdown' for the JSON API. You can associate it with a parent field that is attached to the same block, along with a mapping from the parent field's possible values to the desired menu options for this child field. Whenever the parent field's value changes, this field will automatically change its own available options to the options that correspond to the new parent value. You can also provide a set of default options that will be used if the parent field's value doesn't match any of the keys in your option mapping.

These changes are recorded properly in the undo history, and the fields can be serialized and later deserialized while preserving their options, values, and validity. You can also create chains of dependent dropdowns that depend on other dependent dropdowns.

Note that the parent field must be attached to the block before the child field, and the child field will attach a validator function to the parent field to intercept changes to its value. If you want to add your own custom field validator to the parent field, you need to use the JavaScript API to define your block and pass your validator to the parent field's constructor. If you try to set the parent's validator later, you'll overwrite the one added by this plugin.

To create a dependent dropdown, you'll need to add this field type to a block definition, and add that block to your toolbox. See below for an example of defining a block that uses this field.

JSON

import * as Blockly from 'blockly';
import '@blockly/field-dependent-dropdown'; // Import with side effects.

Blockly.defineBlocksWithJsonArray([
  {
    'type': 'dependent_dropdown_example',
    'message0': 'Category %1 Animal %2',
    'args0': [
      {
        'type': 'field_dropdown',
        'name': 'ANIMAL_CATEGORY',
        'options': [['Mammal', 'mammal'], ['Bird', 'bird'], ['Cryptid', 'cryptid']]
      },
      {
        'type': 'field_dependent_dropdown',
        'name': 'ANIMAL',
        'parentName': 'ANIMAL_CATEGORY',
        'optionMapping': {
          'mammal': [['Dog', 'dog'], ['Cat', 'cat'], ['Hamster', 'hamster']],
          'bird': [['Parakeet', 'parakeet'], ['Canary', 'canary']]
        }
        'defaultOptions': [['None available', 'noneAvailable']],
      }
    ]
  }
]);

JavaScript

import * as Blockly from 'blockly';
import {FieldDependentDropdown} from '@blockly/field-dependent-dropdown';

Blockly.Blocks['dependent_dropdown_example'] = {
  init: function () {
    const parentFieldName = 'ANIMAL_CATEGORY';
    const childFieldName = 'ANIMAL';
    const parentOptions = [
      ['Mammal', 'mammal'],
      ['Bird', 'bird'],
      ['Cryptid', 'cryptid'],
    ];
    const optionMapping = {
      mammal: [
        ['Dog', 'dog'],
        ['Cat', 'cat'],
        ['Hamster', 'hamster'],
      ],
      bird: [
        ['Parakeet', 'parakeet'],
        ['Canary', 'canary'],
      ],
    };
    const defaultOptions = [['None available', 'noneAvailable']];
    this.appendDummyInput()
      .appendField('Category')
      .appendField(new Blockly.FieldDropdown(parentOptions), parentFieldName)
      .appendField('Animal')
      .appendField(
        new FieldDependentDropdown(
          parentFieldName,
          optionMapping,
          defaultOptions,
        ),
        childFieldName,
      );
  },
};

License

Apache 2.0