Skip to content

Commit f8d95d9

Browse files
laraharrowLuke Bowermandbchristopher
authored
update Fields to support description and detail props - update inputText to have width 100% to accommodate the changes (#540)
* first commit * issue with filed props * update Fields to support description and details update inputText to have width of 100% * Tweak for InputText width * ready for review * update to make stylelint happy * update snapshots * restore playground index jsx * Field label widths * ready for review * after review Co-authored-by: Luke Bowerman <[email protected]> Co-authored-by: Daniel Christopher <[email protected]>
1 parent 8fcc108 commit f8d95d9

File tree

13 files changed

+1253
-585
lines changed

13 files changed

+1253
-585
lines changed

packages/components/src/Form/Fields/Field.tsx

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
2525
*/
2626

27-
import { WidthProperty } from 'csstype'
28-
import React, { FunctionComponent } from 'react'
27+
import React, { FunctionComponent, ReactNode } from 'react'
2928
import styled, { css } from 'styled-components'
3029
import { ResponsiveValue, TLengthStyledSystem } from 'styled-system'
3130
import {
@@ -39,22 +38,24 @@ import pick from 'lodash/pick'
3938
import { FlexItem } from '../../Layout/FlexItem'
4039
import { FormControl, FormControlDirections } from '../FormControl/FormControl'
4140
import { Label } from '../Label/Label'
41+
import { Text } from '../../Text/Text'
42+
import { Paragraph } from '../../Text/Paragraph'
4243
import {
4344
ValidationMessage,
4445
ValidationMessageProps,
4546
} from '../ValidationMessage/ValidationMessage'
47+
import { Flex } from '../../Layout'
48+
import { InputText, Select } from '../Inputs'
4649

4750
type ResponsiveSpaceValue = ResponsiveValue<TLengthStyledSystem>
4851

4952
export interface CustomizableFieldAttributesInterface
5053
extends CustomizableAttributes {
5154
labelMargin: SpacingSizes
52-
labelWidth: ResponsiveValue<WidthProperty<TLengthStyledSystem>>
5355
}
5456

5557
export const CustomizableFieldAttributes: CustomizableFieldAttributesInterface = {
5658
labelMargin: 'xsmall',
57-
labelWidth: '20%',
5859
}
5960

6061
export interface FieldProps {
@@ -66,6 +67,14 @@ export interface FieldProps {
6667
* Determines where to place the validation message in relation to the input.
6768
*/
6869
alignValidationMessage?: FormControlDirections
70+
/*
71+
* optional extra description
72+
*/
73+
description?: ReactNode
74+
/**
75+
* notes and details added to the top right corner of the field
76+
*/
77+
detail?: ReactNode
6978
/**
7079
* Id of the input element to match a label to.
7180
*/
@@ -96,17 +105,25 @@ export interface FieldProps {
96105
* Holds the type of validation (error, warning, etc.) and corresponding message.
97106
*/
98107
validationMessage?: ValidationMessageProps
108+
/**
109+
*
110+
* Specify the width of the FieldText if different then 13rem
111+
*/
112+
width?: ResponsiveSpaceValue
99113
}
100114

101115
export const fieldPropKeys = [
102116
'alignLabel',
103117
'alignValidationMessage',
118+
'description',
119+
'detail',
104120
'id',
105121
'label',
106122
'labelFontSize',
107123
'labelFontWeight',
108124
'labelWidth',
109125
'validationMessage',
126+
'width',
110127
]
111128

112129
export const pickFieldProps = (props: FieldProps) =>
@@ -119,8 +136,9 @@ const RequiredStar = styled(props => (
119136
*
120137
</span>
121138
))`
122-
color: ${props => props.theme.colors.semanticColors.danger.darker};
139+
color: ${props => props.theme.colors.palette.red500};
123140
`
141+
124142
const handleHorizontalAlignment = (props: FieldProps) => {
125143
const { alignLabel, labelWidth } = props
126144
const width = labelWidth || CustomizableFieldAttributes.labelWidth
@@ -170,20 +188,30 @@ const getValidationMessageAlignment = (
170188
const FieldComponent: FunctionComponent<FieldProps> = ({
171189
alignValidationMessage,
172190
children,
191+
description,
192+
detail,
173193
id,
174194
label,
175195
labelFontSize,
176196
labelFontWeight,
177197
required,
178198
validationMessage,
199+
width,
179200
...props
180201
}) => {
181202
return (
182203
<FormControl mb="xsmall" {...props}>
183-
<Label htmlFor={id} fontWeight={labelFontWeight} fontSize={labelFontSize}>
184-
{label}
185-
{required && <RequiredStar />}
186-
</Label>
204+
<Flex alignItems="left" justifyContent="space-between" width={width}>
205+
<Label
206+
htmlFor={id}
207+
fontWeight={labelFontWeight}
208+
fontSize={labelFontSize}
209+
>
210+
{label}
211+
{required && <RequiredStar />}
212+
</Label>
213+
{detail && <Text fontSize="xsmall">{detail}</Text>}
214+
</Flex>
187215
<FormControl
188216
alignLabel={getValidationMessageAlignment(alignValidationMessage)}
189217
mb="xsmall"
@@ -196,11 +224,26 @@ const FieldComponent: FunctionComponent<FieldProps> = ({
196224
/>
197225
) : null}
198226
</FormControl>
227+
{description && (
228+
<Paragraph mt="none" fontSize="xsmall">
229+
{description}
230+
</Paragraph>
231+
)}
199232
</FormControl>
200233
)
201234
}
202235

203236
export const Field = styled(FieldComponent)`
237+
width: ${props => props.width};
238+
239+
${InputText} {
240+
width: 100%;
241+
}
242+
243+
${Select} {
244+
width: 100%;
245+
}
246+
204247
${Label} {
205248
${handleHorizontalAlignment}
206249
}

packages/components/src/Form/Fields/FieldCheckbox/FieldCheckbox.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
MIT License
44
55
Copyright (c) 2019 Looker Data Sciences, Inc.
6-
6+
77
Permission is hereby granted, free of charge, to any person obtaining a copy
88
of this software and associated documentation files (the "Software"), to deal
99
in the Software without restriction, including without limitation the rights
1010
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1111
copies of the Software, and to permit persons to whom the Software is
1212
furnished to do so, subject to the following conditions:
13-
13+
1414
The above copyright notice and this permission notice shall be included in all
1515
copies or substantial portions of the Software.
16-
16+
1717
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1818
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1919
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -31,7 +31,9 @@ import { useFormContext } from '../../Form'
3131
import { Checkbox, CheckboxProps } from '../../Inputs/Checkbox/Checkbox'
3232
import { Field, FieldProps, omitFieldProps, pickFieldProps } from '../Field'
3333

34-
export interface FieldCheckboxProps extends FieldProps, CheckboxProps {}
34+
export interface FieldCheckboxProps
35+
extends CheckboxProps,
36+
Omit<FieldProps, 'description' | 'detail'> {}
3537

3638
const FieldCheckboxComponent = forwardRef(
3739
(props: FieldCheckboxProps, ref: Ref<HTMLInputElement>) => {

0 commit comments

Comments
 (0)