@workday/canvas-kit-react
Version:
The parent module that contains all Workday Canvas Kit React components
98 lines (97 loc) • 3.93 kB
JavaScript
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RadioGroup = void 0;
const React = __importStar(require("react"));
const Radio_1 = require("./Radio");
const tokens_1 = require("@workday/canvas-kit-react/tokens");
const common_1 = require("@workday/canvas-kit-react/common");
const Container = (0, common_1.styled)('div')({
display: 'inline-block',
boxSizing: 'border-box',
borderRadius: tokens_1.borderRadius.m,
padding: `${tokens_1.space.xxxs} ${tokens_1.space.xs}`,
margin: `0 -${tokens_1.space.xs}`,
'& > div': {
margin: `${tokens_1.space.xxs} ${tokens_1.space.zero}`,
alignItems: 'flex-start',
'> div': {
flex: '0 0 auto',
},
'&:first-of-type': {
marginTop: '6px',
},
'&:last-child': {
marginBottom: tokens_1.space.xxxs,
},
},
}, ({ grow }) => grow && { width: '100%' }, ({ error, theme }) => {
const errorColors = (0, common_1.getErrorColors)(error, theme);
return {
transition: '100ms box-shadow',
boxShadow: errorColors.outer !== errorColors.inner
? `inset 0 0 0 1px ${errorColors.outer}, inset 0 0 0 3px ${errorColors.inner}`
: `inset 0 0 0 2px ${errorColors.inner}`,
};
});
class RadioGroup extends React.Component {
constructor() {
super(...arguments);
this.renderChild = (child, index, value) => {
if (typeof child.type === typeof Radio_1.Radio) {
const childProps = child.props;
const checked = typeof value === 'number' ? index === value : childProps.value === value;
const name = this.props.name ? this.props.name : childProps.name;
return React.cloneElement(child, {
checked,
name,
onChange: this.onRadioChange.bind(this, childProps.onChange, index),
});
}
return child;
};
this.onRadioChange = (existingOnChange, index, event) => {
if (existingOnChange) {
existingOnChange(event);
}
const target = event.currentTarget;
if (target && this.props.onChange) {
if (target.value) {
this.props.onChange(target.value);
}
else {
this.props.onChange(index);
}
}
};
}
render() {
const { value = 0, children, error, onChange, grow, ...elemProps } = this.props;
return (React.createElement(Container, { error: error, grow: grow, ...elemProps }, React.Children.map(children, (child, index) => this.renderChild(child, index, value))));
}
}
RadioGroup.ErrorType = common_1.ErrorType;
exports.RadioGroup = RadioGroup;
RadioGroup.ErrorType = common_1.ErrorType;
;