forked from carbon-design-system/carbon-components-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: use controlled state with value
- Loading branch information
Showing
2 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* Copyright IBM Corp. 2016, 2018 | ||
* | ||
* This source code is licensed under the Apache-2.0 license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
/** | ||
* @param {Function} propType The original prop type checker. | ||
* @returns {Function} The new prop type checker for `onChange` that makes it required if `value` exists and `readOnly` does not exist. | ||
*/ | ||
export default function requiredIfValueExists(propType) { | ||
return function check(props, propName, componentName, ...rest) { | ||
const { [propName]: onChange, value, readOnly } = props; | ||
const exists = onChange !== undefined; | ||
const valueExists = value !== undefined; | ||
if (__DEV__ && !exists && valueExists && !readOnly) { | ||
return new Error( | ||
`You provided a value prop to \`${componentName}\` without an \`onChange\` handler. ` + | ||
'This will render a read-only field. ' + | ||
'If the field should be mutable use `defaultValue`. Otherwise, set either `onChange` or `readOnly`.' | ||
); | ||
} | ||
return propType(props, propName, componentName, ...rest); | ||
}; | ||
} |