Skip to content

Commit

Permalink
[form-builder] Support readOnly on datetime fields. Fixes #291 (#294)
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoerge committed Oct 20, 2017
1 parent fe9528d commit c6e35df
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 23 deletions.
56 changes: 33 additions & 23 deletions packages/@sanity/form-builder/src/inputs/DateTime/DateTimeInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'react-datepicker/dist/react-datepicker-cssmodules.css' // eslint-disable
import {uniqueId} from 'lodash'
import React from 'react'
import FormField from 'part:@sanity/components/formfields/default'
import TextInput from 'part:@sanity/components/textinputs/default'
import styles from './styles/DateTimeInput.css'
import PatchEvent, {set, unset} from '../../PatchEvent'

Expand Down Expand Up @@ -33,12 +34,12 @@ type Props = {
title: string,
description: string,
options?: SchemaOptions,
readOnly?: boolean,
},
onChange: PatchEvent => void,
level: number
}


function parseOptions(options: SchemaOptions = {}): ParsedOptions {
return {
dateFormat: options.dateFormat || DEFAULT_DATE_FORMAT,
Expand Down Expand Up @@ -99,7 +100,7 @@ export default class DateInput extends React.Component<Props, State> {
render() {
const {value, type, level} = this.props
const {inputValue} = this.state
const {title, description} = type
const {title, description, readOnly} = type

const momentValue: ?Moment = value ? moment(value) : null

Expand All @@ -109,27 +110,36 @@ export default class DateInput extends React.Component<Props, State> {

return (
<FormField labelFor={this.inputId} label={title} level={level} description={description}>
<div className={styles.root}>
<DatePicker
{...options}
showMonthDropdown
showYearDropdown
todayButton={options.calendarTodayLabel}
selected={momentValue || undefined}
placeholderText={placeholder}
calendarClassName={styles.datepicker}
className={styles.input}
onChange={this.handleChange}
onChangeRaw={this.handleInputChange}
value={inputValue ? inputValue : (momentValue && momentValue.format(getFormat(options)))}
showTimeSelect
disabledKeyboardNavigation
dateFormat={options.dateFormat}
timeFormat={options.timeFormat}
timeIntervals={options.timeStep}
onBlur={this.handleBlur}
/>
</div>
{readOnly
? (
<TextInput
readOnly
value={(momentValue && momentValue.format(getFormat(options)))}
/>
)
: (
<div className={styles.root}>
<DatePicker
{...options}
showMonthDropdown
showYearDropdown
todayButton={options.calendarTodayLabel}
selected={momentValue || undefined}
placeholderText={placeholder}
calendarClassName={styles.datepicker}
className={styles.input}
onChange={this.handleChange}
onChangeRaw={this.handleInputChange}
value={inputValue ? inputValue : (momentValue && momentValue.format(getFormat(options)))}
showTimeSelect
disabledKeyboardNavigation
dateFormat={options.dateFormat}
timeFormat={options.timeFormat}
timeIntervals={options.timeStep}
onBlur={this.handleBlur}
/>
</div>
)}
</FormField>
)
}
Expand Down
6 changes: 6 additions & 0 deletions packages/test-studio/schemas/datetime.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ export default {
timeFormat: 'hh:mm'
}
},
{
name: 'aReadOnlyDateTime',
type: 'datetime',
title: 'A read only datetime',
readOnly: true
},
{
name: 'aDateTimeWithCustomDateAndTimeFormat',
type: 'datetime',
Expand Down

0 comments on commit c6e35df

Please sign in to comment.