Skip to content

Commit

Permalink
feat: add radio input type
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisvxd committed Jun 26, 2023
1 parent 08123b7 commit a7cb3a0
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 15 deletions.
2 changes: 1 addition & 1 deletion apps/demo/app/configs/antd/blocks/Hero.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const Hero: ComponentConfig<HeroProps> = {
type: "text",
},
type: {
type: "select",
type: "radio",
options: [
{
value: "primary",
Expand Down
4 changes: 2 additions & 2 deletions apps/demo/app/configs/antd/blocks/Video.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ export const Video: ComponentConfig<VideoProps> = {
type: "text",
},
autoplay: {
type: "select",
type: "radio",
options: [
{ label: "On", value: "on" },
{ label: "Off", value: "off" },
],
},
loop: {
type: "select",
type: "radio",
options: [
{ label: "On", value: "on" },
{ label: "Off", value: "off" },
Expand Down
4 changes: 2 additions & 2 deletions apps/demo/app/configs/custom/blocks/ButtonGroup/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const ButtonGroup: ComponentConfig<ButtonGroupProps> = {
label: { type: "text" },
href: { type: "text" },
variant: {
type: "select",
type: "radio",
options: [
{ label: "primary", value: "primary" },
{ label: "secondary", value: "secondary" },
Expand All @@ -31,7 +31,7 @@ export const ButtonGroup: ComponentConfig<ButtonGroupProps> = {
},
},
align: {
type: "select",
type: "radio",
options: [
{ label: "left", value: "left" },
{ label: "center", value: "center" },
Expand Down
2 changes: 1 addition & 1 deletion apps/demo/app/configs/custom/blocks/FeatureList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const FeatureList: ComponentConfig<FeatureListProps> = {
},
},
mode: {
type: "select",
type: "radio",
options: [
{ label: "flat", value: "flat" },
{ label: "card", value: "card" },
Expand Down
2 changes: 1 addition & 1 deletion apps/demo/app/configs/custom/blocks/Heading/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const Heading: ComponentConfig<HeadingProps> = {
options: levelOptions,
},
align: {
type: "select",
type: "radio",
options: [
{ label: "Left", value: "left" },
{ label: "Center", value: "center" },
Expand Down
4 changes: 2 additions & 2 deletions apps/demo/app/configs/custom/blocks/Hero/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ export const Hero: ComponentConfig<HeroProps> = {
},
},
align: {
type: "select",
type: "radio",
options: [
{ label: "left", value: "left" },
{ label: "center", value: "center" },
],
},
imageUrl: { type: "text" },
imageMode: {
type: "select",
type: "radio",
options: [
{ label: "inline", value: "inline" },
{ label: "background", value: "background" },
Expand Down
4 changes: 2 additions & 2 deletions apps/demo/app/configs/custom/blocks/Text/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ export const Text: ComponentConfig<TextProps> = {
],
},
align: {
type: "select",
type: "radio",
options: [
{ label: "Left", value: "left" },
{ label: "Center", value: "center" },
{ label: "Right", value: "right" },
],
},
color: {
type: "select",
type: "radio",
options: [
{ label: "Default", value: "default" },
{ label: "Muted", value: "muted" },
Expand Down
4 changes: 2 additions & 2 deletions apps/demo/app/configs/material-ui/blocks/ButtonGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const ButtonGroup: ComponentConfig<ButtonGroupProps> = {
getItemSummary: (item) => item.label || "Button",
groupFields: {
variant: {
type: "select",
type: "radio",
options: [
{ value: "text", label: "text" },
{ value: "outlined", label: "outlined" },
Expand All @@ -36,7 +36,7 @@ export const ButtonGroup: ComponentConfig<ButtonGroupProps> = {
},
},
align: {
type: "select",
type: "radio",
options: [
{ value: "flex-start", label: "left" },
{ value: "center", label: "center" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const TypographyBlock: ComponentConfig<TypographyBlockProps> = {
options: variantOptions,
},
align: {
type: "select",
type: "radio",
options: [
{ label: "Left", value: "left" },
{ label: "Center", value: "center" },
Expand Down
31 changes: 31 additions & 0 deletions packages/core/InputOrGroup/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,37 @@ export const InputOrGroup = ({
);
}

if (field.type === "radio") {
if (!field.options) {
return null;
}

return (
<div className={getClassName("radioGroup")}>
<div className={getClassName("label")}>{field.label || name}</div>

<div className={getClassName("radioGroupItems")}>
{field.options.map((option) => (
<label
key={option.label + option.value}
className={getClassName("radio")}
>
<input
type="radio"
value={option.value}
name={name}
onChange={(e) => onChange(e.currentTarget.value)}
readOnly={readOnly}
defaultChecked={value === option.value}
/>
<div>{option.value}</div>
</label>
))}
</div>
</div>
);
}

return (
<label className={getClassName({ readOnly })}>
<div className={getClassName("label")}>{label || name}</div>
Expand Down
17 changes: 17 additions & 0 deletions packages/core/InputOrGroup/styles.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,20 @@
margin-top: 4px;
overflow: hidden;
}

.Input-radioGroup {
margin-bottom: 8px;
margin-top: 8px;
}

.Input-radioGroupItems {
display: flex;
gap: 8px;
flex-wrap: wrap;
}

.Input-radio {
display: flex;
align-items: center;
gap: 4px;
}
9 changes: 8 additions & 1 deletion packages/core/types/Config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ export type Adaptor<AdaptorParams = {}> = {
export type Field<
Props extends { [key: string]: any } = { [key: string]: any }
> = {
type: "text" | "textarea" | "number" | "select" | "group" | "external";
type:
| "text"
| "textarea"
| "number"
| "select"
| "group"
| "external"
| "radio";
label?: string;
adaptor?: Adaptor;
adaptorParams?: object;
Expand Down

0 comments on commit a7cb3a0

Please sign in to comment.