Skip to content

Commit

Permalink
feat(website): add the ability to define some chart property attribut…
Browse files Browse the repository at this point in the history
…es dynamically depending on the current flavor
  • Loading branch information
plouc committed May 6, 2024
1 parent f16c009 commit 26e19c6
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const PropertyDocumentation = ({
currentFlavor={currentFlavor}
supportedFlavors={property.flavors}
>
<PropertyHeader {...property} context={context} />
<PropertyHeader {...property} currentFlavor={currentFlavor} context={context} />
<Help>{property.help}</Help>
</Control>
)
Expand Down
14 changes: 11 additions & 3 deletions website/src/components/controls/ui/Control.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ import React, { useState, useCallback, PropsWithChildren } from 'react'
import intersection from 'lodash/intersection'
import styled from 'styled-components'
import { MdKeyboardArrowRight, MdKeyboardArrowDown } from 'react-icons/md'
import { Flavor } from '../../../types'
import { Flavor, FlavorAwareChartPropertyAttribute } from '../../../types'
import { PropertyDescription } from './PropertyDescription'
import { PropertyFlavors } from './PropertyFlavors'
import { Cell } from './styled'

interface ControlProps {
id: string
description?: string
description?: FlavorAwareChartPropertyAttribute<string>
flavors: Flavor[]
currentFlavor: Flavor
supportedFlavors?: Flavor[]
}

export const Control = ({
id,
description,
description: _description,
flavors,
currentFlavor,
supportedFlavors,
Expand All @@ -37,6 +37,14 @@ export const Control = ({
}
}

let description: string | undefined = undefined
if (typeof _description === 'string') {
description = _description
} else if (typeof _description === 'object') {
// If an object is provided, it means it depends on the current flavor.
description = _description[currentFlavor]
}

return (
<Container id={id} isPropertySupported={isPropertySupported}>
{description !== undefined && (
Expand Down
16 changes: 14 additions & 2 deletions website/src/components/controls/ui/PropertyHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import isNumber from 'lodash/isNumber'
import isBoolean from 'lodash/isBoolean'
import isFunction from 'lodash/isFunction'
import styled from 'styled-components'
import { ChartProperty } from '../../../types'
import { ChartProperty, Flavor } from '../../../types'
import { ControlContext } from '../types'

const getDefaultValue = (value: any) => {
Expand Down Expand Up @@ -39,6 +39,7 @@ type PropertyHeaderProps = ChartProperty & {
id?: string
name?: string
context?: ControlContext
currentFlavor?: Flavor
}

export const PropertyHeader = ({
Expand All @@ -48,6 +49,7 @@ export const PropertyHeader = ({
required,
defaultValue,
context,
currentFlavor,
}: PropertyHeaderProps) => {
let label: ReactNode = name
if (context) {
Expand All @@ -59,10 +61,20 @@ export const PropertyHeader = ({
)
}

let propertyType: string | undefined = undefined
if (type !== undefined) {
if (typeof type === 'string') {
propertyType = type
} else if (typeof type === 'object' && currentFlavor) {
// If an object is provided, it means it depends on the current flavor.
propertyType = type[currentFlavor]
}
}

return (
<Container>
<Label htmlFor={id}>{label}</Label>
{type !== undefined && <Type>{type}</Type>}
{propertyType && <Type>{propertyType}</Type>}
{required && <Required>required</Required>}
{!required && <Optional>optional</Optional>}
{defaultValue !== undefined && (
Expand Down
6 changes: 4 additions & 2 deletions website/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ export interface ChartMeta {
}[]
}

export type FlavorAwareChartPropertyAttribute<T> = T | Partial<Record<Flavor, T>>

export interface ChartProperty<Settings = any> {
key: string
name?: string
group: string
// type of the property, preferably expressed with TypeScript notation
type: string
type: FlavorAwareChartPropertyAttribute<string>
// will be parsed in Markdown and supports links
help?: string
// will be parsed in Markdown and supports links
description?: string
description?: FlavorAwareChartPropertyAttribute<string>
// assumed to be optional by default
required?: boolean
// default property value as defined for the component,
Expand Down

0 comments on commit 26e19c6

Please sign in to comment.