链接选择的选择

Selection of Linked Selections

我正在使用 React + Redux + Router stack and by this time I have got a simple React component where I am trying to develop a selection of linked selection with this 行为示例。

事实上我的组件可以很容易地使用这个变量:

var VAR_OPTIONS_OLD = [
    {value: 1, name: '1'},
    {value: 2, name: '2'},
    {value: 3, name: '3'},
    {value: 4, name: '4'},
    {value: 5, name: '5'},
]

这个组件在哪里:

<Field
      type={Select}
      options={VAR_OPTIONS_OLD}
      optionValue="value"
      id="someID"
      class="col-12"
      i18n={i18n.someID}
/>

现在我希望我的两个选择都使用这个变量:

var VAR_OPTIONS_NEW = {
    optionA: {
        name: 'Option A',
        suboption: ['Suboption A1', 'Suboption A2', 'Suboption A3']
    },
    optionB: {
        name: 'Option B',
        suboption: ['Suboption B1', 'Suboption B2', 'Suboption B3']
    },
    optionC: {
        name: 'Option C',
        suboption: ['Suboption C1', 'Suboption C2', 'Suboption C3']
    }
}

这样我就可以利用 2 个相互依赖的链接选择。

来源:Var Container Example Gist

在您的查看组件代码:

顶部:

const _ = require('lodash');
const VAR_OPTIONS_NEW = ... // The one you provided.

render():

const firstValue = ... // this should come from your Store (e.g. this.props.firstValue).
const secondValue = ... // this should come from a Store

const firstOptions = _.map(VAR_OPTIONS_NEW, (v, k) => ({ value: k, name: v.name });

let secondOptions = [];
if (VAR_OPTIONS_NEW[firstValue]) {
    secondOptions = VAR_OPTIONS_NEW[firstValue].suboptions.map(
        (name, index) => ({ value: index, name: name })
    );
}
...
<Field
    options={ firstOptions }
    optionsValue={ firstValue }
    onChange={/* is your onChange event connected to Store? */}
    ...
/>
<Field
    options={ secondOptions }
    optionsValue={ secondValue }
    onChange={/* is your onChange event connected to Store? */}
    ...
/>