mirror of
https://github.com/carbon-design-system/carbon-components-svelte.git
synced 2025-09-15 18:31:06 +00:00
parent
9b8559b9b1
commit
4f73b8b71a
9 changed files with 128 additions and 11 deletions
|
@ -0,0 +1,16 @@
|
|||
<script>
|
||||
import Layout from '../../internal/ui/Layout.svelte';
|
||||
import RadioButtonGroup from './RadioButtonGroup.svelte';
|
||||
import RadioButton from '../RadioButton';
|
||||
import { FormGroup } from '../Form';
|
||||
</script>
|
||||
|
||||
<Layout>
|
||||
<FormGroup legendText="Radio Button heading">
|
||||
<RadioButtonGroup {...$$props.group} defaultSelected="default-selected" legend="Group Legend">
|
||||
<RadioButton {...$$props.radio} value="standard" id="radio-1" />
|
||||
<RadioButton {...$$props.radio} value="default-selected" id="radio-2" />
|
||||
<RadioButton {...$$props.radio} value="disabled" id="radio-3" />
|
||||
</RadioButtonGroup>
|
||||
</FormGroup>
|
||||
</Layout>
|
40
src/components/RadioButtonGroup/RadioButtonGroup.stories.js
Normal file
40
src/components/RadioButtonGroup/RadioButtonGroup.stories.js
Normal file
|
@ -0,0 +1,40 @@
|
|||
import { withKnobs, text, select, boolean } from '@storybook/addon-knobs';
|
||||
import Component from './RadioButtonGroup.Story.svelte';
|
||||
|
||||
export default { title: 'RadioButtonGroup', decorators: [withKnobs] };
|
||||
|
||||
const values = {
|
||||
standard: 'standard',
|
||||
'default-selected': 'default-selected',
|
||||
disabled: 'disabled'
|
||||
};
|
||||
|
||||
const orientations = {
|
||||
'Horizontal (horizontal)': 'horizontal',
|
||||
'Vertical (vertical)': 'vertical'
|
||||
};
|
||||
|
||||
const labelPositions = {
|
||||
'Left (left)': 'left',
|
||||
'Right (right)': 'right'
|
||||
};
|
||||
|
||||
export const Default = () => ({
|
||||
Component,
|
||||
props: {
|
||||
group: {
|
||||
name: text('The form control name (name in <RadioButtonGroup>)', 'radio-button-group'),
|
||||
valueSelected: select(
|
||||
'Value of the selected button (valueSelected in <RadioButtonGroup>)',
|
||||
values,
|
||||
'default-selected'
|
||||
),
|
||||
orientation: select('Radio button orientation (orientation)', orientations, 'horizontal'),
|
||||
labelPosition: select('Label position (labelPosition)', labelPositions, 'right')
|
||||
},
|
||||
radio: {
|
||||
disabled: boolean('Disabled (disabled in <RadioButton>)', false),
|
||||
labelText: text('Label text (labelText in <RadioButton>)', 'Radio button label')
|
||||
}
|
||||
}
|
||||
});
|
46
src/components/RadioButtonGroup/RadioButtonGroup.svelte
Normal file
46
src/components/RadioButtonGroup/RadioButtonGroup.svelte
Normal file
|
@ -0,0 +1,46 @@
|
|||
<script>
|
||||
let className = undefined;
|
||||
export { className as class };
|
||||
export let orientation = 'horizontal';
|
||||
export let labelPosition = 'right';
|
||||
export let defaultSelected = undefined;
|
||||
export let disabled = false;
|
||||
export let style = undefined;
|
||||
|
||||
import { createEventDispatcher, setContext } from 'svelte';
|
||||
import { writable } from 'svelte/store';
|
||||
import { cx } from '../../lib';
|
||||
|
||||
const dispatch = createEventDispatcher();
|
||||
const _class = cx(
|
||||
'--radio-button-group',
|
||||
orientation === 'vertical' && `--radio-button-group--${orientation}`,
|
||||
labelPosition && `--radio-button-group--label-${labelPosition}`,
|
||||
className
|
||||
);
|
||||
|
||||
let selected = writable(defaultSelected);
|
||||
|
||||
setContext('RadioButtonGroup', {
|
||||
selected,
|
||||
add: ({ checked, value }) => {
|
||||
if (checked) {
|
||||
selected.set(value);
|
||||
}
|
||||
},
|
||||
update: value => {
|
||||
selected.set(value);
|
||||
}
|
||||
});
|
||||
|
||||
$: {
|
||||
defaultSelected = $selected;
|
||||
dispatch('change', $selected);
|
||||
}
|
||||
</script>
|
||||
|
||||
<div on:click on:mouseover on:mouseenter on:mouseleave class={cx('--form-item')} {style}>
|
||||
<div class={_class} {disabled}>
|
||||
<slot />
|
||||
</div>
|
||||
</div>
|
3
src/components/RadioButtonGroup/index.js
Normal file
3
src/components/RadioButtonGroup/index.js
Normal file
|
@ -0,0 +1,3 @@
|
|||
import RadioButtonGroup from './RadioButtonGroup.svelte';
|
||||
|
||||
export default RadioButtonGroup;
|
Loading…
Add table
Add a link
Reference in a new issue