Skip to content

Commit

Permalink
feat: Add read-only select_multiple field support
Browse files Browse the repository at this point in the history
  • Loading branch information
gmaclennan committed Nov 12, 2019
1 parent b0535b1 commit 0fc88e3
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/ObservationDialog/Field.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @flow
import React from 'react'
import { useIntl } from 'react-intl'
import { SelectOne } from './Select'
import { SelectOne, SelectMultiple } from './Select'
import TextField from './TextField'
import DateField from './DateField'
import DateTimeField from './DateTimeField'
Expand Down Expand Up @@ -50,6 +50,8 @@ const Field = ({ field, value, onChange }: Props) => {
placeholder={placeholder}
/>
)
case 'select_multiple':
return <SelectMultiple value={value} label={label} />
case 'number':
return (
<TextField
Expand Down
10 changes: 9 additions & 1 deletion src/ObservationDialog/ObservationDialog.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@ const getPreset = observation => {
geometry: ['point'],
name: (observation.tags && observation.tags.name) || '',
tags: {},
fields: fields.slice(0, 5),
fields: fields.slice(0, 5).concat([
{
id: 'multi-field',
key: ['multi'],
options: ['one', 'two', 'three'],
type: 'select_multiple'
}
]),
additionalFields: fields.slice(5)
}
}
Expand All @@ -50,6 +57,7 @@ export const openClose = () => {
const [open, setOpen] = React.useState(false)
const obs =
exampleObservations[Math.floor(Math.random() * exampleObservations.length)]
obs.tags.multi = ['one', 'two', 'three']
return (
<>
<Button onClick={() => setOpen(true)}>Open Dialog</Button>
Expand Down
10 changes: 10 additions & 0 deletions src/ObservationDialog/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,16 @@ export const SelectOne = ({
)
}

export const SelectMultiple = ({ value, label }: Props) => {
return (
<TextField
value={Array.isArray(value) ? value.join(', ') : value}
label={label}
disabled
/>
)
}

// for two values, if strings, compare lower case, otherwise strict equal
function lowerCaseEqual(a: any, b: any) {
if (typeof a === 'string' && typeof b === 'string') {
Expand Down
6 changes: 5 additions & 1 deletion src/ObservationDialog/Select.stories.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @flow
import * as React from 'react'

import { SelectOne } from './Select'
import { SelectOne, SelectMultiple } from './Select'

const countries = [
{ label: 'Afghanistan', value: true },
Expand Down Expand Up @@ -72,3 +72,7 @@ export const defaultStory = () => (
defaultStory.story = {
name: 'default'
}

export const selectMultiple = () => (
<SelectMultiple label="Select Country" value={['England', 'Scotland']} />
)

0 comments on commit 0fc88e3

Please sign in to comment.