|
| 1 | +import React from "react"; |
| 2 | +import styled from "@emotion/styled"; |
| 3 | +import ReactMarkdown from "react-markdown"; |
| 4 | +import { Checkbox } from "@modulz/design-system"; |
| 5 | +import type { |
| 6 | + Preference, |
| 7 | + TBooleanProperty, |
| 8 | + TNumberProperty, |
| 9 | + TPropertyValueType, |
| 10 | + TStringProperty, |
| 11 | +} from "../core"; |
| 12 | + |
| 13 | +export function PreferenceItem({ |
| 14 | + identifier, |
| 15 | + title, |
| 16 | + properties, |
| 17 | +}: Preference & { |
| 18 | + onChange?: OnPropertyChange; |
| 19 | +}) { |
| 20 | + return ( |
| 21 | + <PreferenceItemContainer data-preference-id={identifier}> |
| 22 | + <h3>{title}</h3> |
| 23 | + <div className="properties"> |
| 24 | + {Object.keys(properties).map((k) => { |
| 25 | + const property = properties[k]; |
| 26 | + switch (property.type) { |
| 27 | + case "boolean": { |
| 28 | + return ( |
| 29 | + <PreferenceCheckboxItem |
| 30 | + key={k} |
| 31 | + {...property} |
| 32 | + onChange={() => {}} |
| 33 | + /> |
| 34 | + ); |
| 35 | + } |
| 36 | + case "number": { |
| 37 | + return ( |
| 38 | + <PreferenceTextFieldItem |
| 39 | + key={k} |
| 40 | + {...property} |
| 41 | + onChange={() => {}} |
| 42 | + /> |
| 43 | + ); |
| 44 | + } |
| 45 | + case "string": { |
| 46 | + if (property.enum) { |
| 47 | + return ( |
| 48 | + <PreferenceSelectItem |
| 49 | + key={k} |
| 50 | + {...property} |
| 51 | + onChange={() => {}} |
| 52 | + /> |
| 53 | + ); |
| 54 | + } |
| 55 | + return ( |
| 56 | + <PreferenceTextFieldItem |
| 57 | + key={k} |
| 58 | + {...property} |
| 59 | + onChange={() => {}} |
| 60 | + /> |
| 61 | + ); |
| 62 | + } |
| 63 | + } |
| 64 | + })} |
| 65 | + </div> |
| 66 | + </PreferenceItemContainer> |
| 67 | + ); |
| 68 | +} |
| 69 | + |
| 70 | +type TOnChange<T> = (value: T) => void; |
| 71 | +type OnPropertyChange = (identifier: string, value: TPropertyValueType) => void; |
| 72 | + |
| 73 | +function PropertyDescription({ |
| 74 | + description, |
| 75 | + markdownDescription, |
| 76 | +}: { |
| 77 | + description: string; |
| 78 | + markdownDescription?: string; |
| 79 | +}) { |
| 80 | + return ( |
| 81 | + <div className="description"> |
| 82 | + {markdownDescription ? ( |
| 83 | + <ReactMarkdown>{markdownDescription}</ReactMarkdown> |
| 84 | + ) : ( |
| 85 | + <p>{description}</p> |
| 86 | + )} |
| 87 | + </div> |
| 88 | + ); |
| 89 | +} |
| 90 | + |
| 91 | +function PreferenceCheckboxItem({ |
| 92 | + ...props |
| 93 | +}: TBooleanProperty & { onChange?: TOnChange<boolean> }) { |
| 94 | + return ( |
| 95 | + <div className="checkbox"> |
| 96 | + <Checkbox /> |
| 97 | + <PropertyDescription {...props} /> |
| 98 | + </div> |
| 99 | + ); |
| 100 | +} |
| 101 | + |
| 102 | +function PreferenceTextFieldItem({ |
| 103 | + ...props |
| 104 | +}: (TStringProperty | TNumberProperty) & { onChange?: TOnChange<string> }) { |
| 105 | + return ( |
| 106 | + <> |
| 107 | + <PropertyDescription {...props} /> |
| 108 | + </> |
| 109 | + ); |
| 110 | +} |
| 111 | + |
| 112 | +function PreferenceSelectItem( |
| 113 | + props: TStringProperty & { onChange?: TOnChange<string> } |
| 114 | +) { |
| 115 | + return <></>; |
| 116 | +} |
| 117 | + |
| 118 | +const PreferenceItemContainer = styled.div` |
| 119 | + /* */ |
| 120 | + display: flex; |
| 121 | +
|
| 122 | + .properties { |
| 123 | + display: flex; |
| 124 | + flex-direction: column; |
| 125 | + } |
| 126 | +
|
| 127 | + .description { |
| 128 | + } |
| 129 | +
|
| 130 | + .checkbox { |
| 131 | + display: flex; |
| 132 | + flex-direction: row; |
| 133 | + gap: 8px; |
| 134 | + align-items: center; |
| 135 | + justify-content: flex-start; |
| 136 | + } |
| 137 | +`; |
0 commit comments