@appbuckets/react-ui
Version:
Just Another React UI Framework
90 lines (84 loc) • 2.33 kB
JavaScript
;
var tslib = require('tslib');
var React = require('react');
var reactUiCore = require('@appbuckets/react-ui-core');
require('../BucketTheme/BucketTheme.js');
var BucketContext = require('../BucketTheme/BucketContext.js');
var Checkbox = require('../Checkbox/Checkbox.js');
var Radio_context = require('./Radio.context.js');
function _interopNamespace(e) {
if (e && e.__esModule) return e;
var n = Object.create(null);
if (e) {
Object.keys(e).forEach(function (k) {
if (k !== 'default') {
var d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(
n,
k,
d.get
? d
: {
enumerable: true,
get: function () {
return e[k];
},
}
);
}
});
}
n['default'] = e;
return Object.freeze(n);
}
var React__namespace = /*#__PURE__*/ _interopNamespace(React);
/* --------
* Component Definition
* -------- */
var RadioOption = React__namespace.forwardRef(function (receivedProps, ref) {
var props = BucketContext.useWithDefaultProps('radioOption', receivedProps);
var /** Strict radio props */
value = props.value,
/** All other props passed to Checkbox */
rest = tslib.__rest(props, ['value']);
// ----
// Hook Usage
// ----
var _a = Radio_context.useRadioContext() || {},
currentValue = _a.currentValue,
setValue = _a.setValue;
// ----
// Render the Component
// ----
return Checkbox.create(rest, {
autoGenerateKey: false,
overrideProps: function () {
return {
ref: ref,
checked: !!rest.checked || currentValue === value,
radio: true,
onClick: function (event) {
/** Call user defined click event if exists */
if (typeof rest.onClick === 'function') {
rest.onClick(event, props);
}
/** Set the new value */
if (typeof setValue === 'function') {
setValue(event, value);
}
},
};
},
});
});
RadioOption.displayName = 'RadioOption';
RadioOption.create = reactUiCore.createShorthandFactory(
RadioOption,
function (value) {
return { value: value };
},
function (props) {
return props.value;
}
);
module.exports = RadioOption;