1// Copyright (C) 2023 The Android Open Source Project 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15import m from 'mithril'; 16import {classNames} from '../classnames'; 17 18export interface CheckboxAttrs { 19 // Optional text to show to the right of the checkbox. 20 label?: string; 21 // Whether the label is checked or not, defaults to false. 22 // If omitted, the checkbox will be uncontrolled. 23 checked?: boolean; 24 // Make the checkbox appear greyed out block any interaction with it. No 25 // events will be fired. 26 // Defaults to false. 27 disabled?: boolean; 28 // Extra classes 29 classes?: string|string[]; 30 // Remaining attributes forwarded to the underlying HTML <label>. 31 [htmlAttrs: string]: any; 32} 33 34export class Checkbox implements m.ClassComponent<CheckboxAttrs> { 35 view({attrs}: m.CVnode<CheckboxAttrs>) { 36 const { 37 label, 38 checked, 39 disabled = false, 40 classes: extraClasses, 41 ...htmlAttrs 42 } = attrs; 43 44 const classes = classNames( 45 disabled && 'pf-disabled', 46 extraClasses, 47 ); 48 49 // The default checkbox is removed and an entirely new one created inside 50 // the span element in CSS. 51 return m( 52 'label.pf-checkbox', 53 {class: classes, ...htmlAttrs}, 54 m('input[type=checkbox]', {disabled, checked}), 55 m('span'), 56 label, 57 ); 58 } 59} 60