MCPcopy
hub / github.com/facebook/react / updateOptions

Function updateOptions

packages/react-dom-bindings/src/client/ReactDOMSelect.js:63–108  ·  view source on GitHub ↗
(
  node: HTMLSelectElement,
  multiple: boolean,
  propValue: any,
  setDefaultSelected: boolean,
)

Source from the content-addressed store, hash-verified

61}
62
63function updateOptions(
64 node: HTMLSelectElement,
65 multiple: boolean,
66 propValue: any,
67 setDefaultSelected: boolean,
68) {
69 const options: HTMLOptionsCollection = node.options;
70
71 if (multiple) {
72 const selectedValues = (propValue: Array<string>);
73 const selectedValue: {[string]: boolean} = {};
74 for (let i = 0; i < selectedValues.length; i++) {
75 // Prefix to avoid chaos with special keys.
76 selectedValue['$' + selectedValues[i]] = true;
77 }
78 for (let i = 0; i < options.length; i++) {
79 const selected = selectedValue.hasOwnProperty('$' + options[i].value);
80 if (options[i].selected !== selected) {
81 options[i].selected = selected;
82 }
83 if (selected && setDefaultSelected) {
84 options[i].defaultSelected = true;
85 }
86 }
87 } else {
88 // Do not set `select.value` as exact behavior isn't consistent across all
89 // browsers for all cases.
90 const selectedValue = toString(getToStringValue(propValue));
91 let defaultSelected = null;
92 for (let i = 0; i < options.length; i++) {
93 if (options[i].value === selectedValue) {
94 options[i].selected = true;
95 if (setDefaultSelected) {
96 options[i].defaultSelected = true;
97 }
98 return;
99 }
100 if (defaultSelected === null && !options[i].disabled) {
101 defaultSelected = options[i];
102 }
103 }
104 if (defaultSelected !== null) {
105 defaultSelected.selected = true;
106 }
107 }
108}
109
110/**
111 * Implements a <select> host component that allows optionally setting the

Callers 3

initSelectFunction · 0.85
updateSelectFunction · 0.85

Calls 2

toStringFunction · 0.90
getToStringValueFunction · 0.90

Tested by

no test coverage detected