| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | * |
| 7 | * @flow |
| 8 | */ |
| 9 | |
| 10 | import {Children} from 'react'; |
| 11 | |
| 12 | let didWarnSelectedSetOnOption = false; |
| 13 | let didWarnInvalidChild = false; |
| 14 | let didWarnInvalidInnerHTML = false; |
| 15 | |
| 16 | /** |
| 17 | * Implements an <option> host component that warns when `selected` is set. |
| 18 | */ |
| 19 | |
| 20 | export function validateOptionProps(element: Element, props: Object) { |
| 21 | if (__DEV__) { |
| 22 | // If a value is not provided, then the children must be simple. |
| 23 | if (props.value == null) { |
| 24 | if (typeof props.children === 'object' && props.children !== null) { |
| 25 | Children.forEach(props.children, function (child) { |
| 26 | if (child == null) { |
| 27 | return; |
| 28 | } |
| 29 | if ( |
| 30 | typeof child === 'string' || |
| 31 | typeof child === 'number' || |
| 32 | typeof child === 'bigint' |
| 33 | ) { |
| 34 | return; |
| 35 | } |
| 36 | if (!didWarnInvalidChild) { |
| 37 | didWarnInvalidChild = true; |
| 38 | console.error( |
| 39 | 'Cannot infer the option value of complex children. ' + |
| 40 | 'Pass a `value` prop or use a plain string as children to <option>.', |
| 41 | ); |
| 42 | } |
| 43 | }); |
| 44 | } else if (props.dangerouslySetInnerHTML != null) { |
| 45 | if (!didWarnInvalidInnerHTML) { |
| 46 | didWarnInvalidInnerHTML = true; |
| 47 | console.error( |
| 48 | 'Pass a `value` prop if you set dangerouslyInnerHTML so React knows ' + |
| 49 | 'which value should be selected.', |
| 50 | ); |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | // TODO: Remove support for `selected` in <option>. |
| 56 | if (props.selected != null && !didWarnSelectedSetOnOption) { |
| 57 | console.error( |
| 58 | 'Use the `defaultValue` or `value` props on <select> instead of ' + |
| 59 | 'setting `selected` on <option>.', |
| 60 | ); |
| 61 | didWarnSelectedSetOnOption = true; |
| 62 | } |
| 63 | } |
| 64 | } |